1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_HeapBase.h
4 
5   Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain proprietary
8   information of Nintendo and/or its licensed developers and are protected by
9   national and international copyright laws. They may not be disclosed to third
10   parties or copied or duplicated in any form, in whole or in part, without the
11   prior written consent of Nintendo.
12 
13   The content herein is highly confidential and should be handled accordingly.
14 
15   $Revision: $
16  *---------------------------------------------------------------------------*/
17 
18 #ifndef NW_UT_HEAP_BASE_H_
19 #define NW_UT_HEAP_BASE_H_
20 
21 #include <nw/ut/ut_LinkList.h>
22 
23 #include <stddef.h>         // for offsetof()
24 
25 namespace nw {
26 namespace ut {
27 
28 // 継承する子クラスをリストで管理するためのクラス
29 //! @details :private
30 class HeapNode
31 {
32   public:
HeapNode()33     HeapNode() {}
~HeapNode()34     virtual ~HeapNode() {}
35 
36   public:
37     LinkListNode mLinkNode;
38 };
39 
40 
41 // 拡張ヒープ、フレームヒープ、ユニットヒープの基底
42 //! @details :private
43 class HeapBase : public HeapNode
44 {
45   public:
46     //
47     //  type
48     //
49 
50     typedef ut::LinkList< HeapBase, offsetof(HeapNode,mLinkNode)> HeapList;
51 
52 
53     //
54     //  constant value
55     //
56 
57     // ヒープからメモリを割り当てるときのデフォルトアライメントサイズ
58     static const int DEFAULT_ALIGNMENT  = 4;
59 
60     // 拡張ヒープ・フレームヒープ・ユニットヒープのシグネチャ
61     static const u32 EXPHEAP_SIGNATURE  = 'EXPH';
62     static const u32 FRMHEAP_SIGNATURE  = 'FRMH';
63     static const u32 UNTHEAP_SIGNATURE  = 'UNTH';
64 
65     // メモリ確保時のオプション
66     static const int OPT_0_CLEAR        = ( 1 << 0 );   // メモリ確保時に、メモリをゼロクリア
67     static const int OPT_DEBUG_FILL     = ( 1 << 1 );   // ヒープ作成時・メモリ確保・解放時にメモリ充填
68     static const int OPT_THREAD_SAFE    = ( 1 << 2 );   // 排他制御の有無
69 
70     // このビットが立っているとエラー出力
71     static const int ERROR_PRINT            = ( 1 << 0 );
72 
73 
74     //
75     //  enum
76     //
77 
78     enum FillType {
79         HEAP_FILL_NOUSE,    // デバッグフィル未使用時
80         HEAP_FILL_ALLOC,    // デバッグフィル確保時
81         HEAP_FILL_FREE,     // デバッグフィル解放時
82         HEAP_FILL_MAX
83     };
84 
85     enum HeapType {
86         HEAP_TYPE_EXP,      // 拡張ヒープ
87         HEAP_TYPE_FRM,      // フレームヒープ
88         HEAP_TYPE_UNIT,     // ユニットヒープ
89         HEAP_TYPE_UNKNOWN   // 未知のヒープ
90     };
91 
92 
93     //
94     //  function
95     //
96 
97     static HeapBase* FindContainHeap( const void* memBlock );   // TODO: static ???
98     static HeapBase* FindParentHeap( const HeapBase* heap );    // TODO: static ???
99 
GetHeapStartAddress()100     void* GetHeapStartAddress() { return this; }
GetHeapEndAddress()101     void* GetHeapEndAddress()   { return mHeapEnd; }
102 
GetTotalSize()103     s32 GetTotalSize()          { return ((s32)(mHeapEnd) - (s32)(this)); }
GetTotalUsableSize()104     s32 GetTotalUsableSize()    { return ((s32)(mHeapEnd) - (s32)(mHeapStart)); }
105 
106 //    void Dump();
107 
108     u32 SetFillValue( FillType type, u32 val );
109     u32 GetFillValue( FillType type );
110 
111     HeapType GetHeapType();
112 
113   protected:
114     static const int MIN_ALIGNMENT = 4;
115 
116     void Initialize( u32 signature, void* start, void* end, u16 optFlag );
117     void Finalize();
118 
GetSignature()119     u32 GetSignature() const { return mSignature; }
GetHeapStart()120     void* GetHeapStart() const { return mHeapStart; }
GetHeapEnd()121     void* GetHeapEnd() const { return mHeapEnd; }
122 
123     void LockHeap();
124     void UnlockHeap();
125 
126     void FillFreeMemory( void* address, u32 size );
127     void FillNoUseMemory( void* address, u32 size );
128     void FillAllocMemory( void* address, u32 size );
129 
130     void*       mHeapStart;     // ヒープ先頭アドレス
131     void*       mHeapEnd;       // ヒープ末尾 (+1) アドレス
132 
133   private:
134     static HeapBase* FindContainHeap( HeapList* pList, const void* memBlock );
135     static HeapList* FindListContainHeap( HeapBase* pHeapBase );
136 
137     u16  GetOptionFlag();
138     void SetOptionFlag( u16 optFlag );
139 
140     u32         mSignature;
141     HeapList    mChildList;
142 #ifdef NW_UT_HEAP_MULTITHREADED
143     OSMutex     mMutex;
144 #endif
145     u32         mAttribute;     // 属性 [8:オプションフラグ]
146 };
147 
148 } // nw::ut
149 } // nw
150 
151 
152 #endif // NW_UT_HEAP_BASE_H_
153 
154