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