Twilight Princess
Decompilation of The Legend of Zelda: Twilight Princess
Loading...
Searching...
No Matches
JKRHeap.h
Go to the documentation of this file.
1#ifndef JKRHEAP_H
2#define JKRHEAP_H
3
6
7class JKRHeap;
8typedef void (*JKRErrorHandler)(void*, u32, int);
9
10extern bool data_804508B0;
11
16class JKRHeap : public JKRDisposer {
17public:
18 class TState {
19 public:
20 /* 0x00 */ u32 mUsedSize;
21 /* 0x04 */ u32 mCheckCode;
22 /* 0x08 */ u32 mBuf;
23 /* 0x0C */ u32 field_0xc;
24 /* 0x10 */ JKRHeap* mHeap;
25 /* 0x14 */ u32 mId;
26
27 public:
28 u32 getUsedSize() const { return mUsedSize; }
29 u32 getCheckCode() const { return mCheckCode; }
30 JKRHeap* getHeap() const { return mHeap; }
31 u32 getId() const { return mId; }
32 };
33
34public:
35 JKRHeap(void* data, u32 size, JKRHeap* parent, bool errorFlag);
36 virtual ~JKRHeap();
37
40 void destroy();
41
42 void* alloc(u32 size, int alignment);
43 void free(void* ptr);
44 void freeAll();
45 void freeTail();
46 s32 resize(void* ptr, u32 size);
47 s32 getSize(void* ptr);
49 void* getMaxFreeBlock();
51 s32 changeGroupID(u8 newGroupId);
53
54 JKRHeap* find(void* ptr) const;
55 JKRHeap* findAllHeap(void* ptr) const;
56
58 bool dispose(void* ptr, u32 size);
59 void dispose(void* begin, void* end);
60 void dispose();
61
62 bool setErrorFlag(bool errorFlag);
63 bool isSubHeap(JKRHeap* heap) const;
64
65 /* vt[03] */ virtual void callAllDisposer();
66 /* vt[04] */ virtual u32 getHeapType() = 0;
67 /* vt[05] */ virtual bool check() = 0;
68 /* vt[06] */ virtual bool dump_sort();
69 /* vt[07] */ virtual bool dump() = 0;
70 /* vt[08] */ virtual void do_destroy() = 0;
71 /* vt[09] */ virtual void* do_alloc(u32 size, int alignment) = 0;
72 /* vt[10] */ virtual void do_free(void* ptr) = 0;
73 /* vt[11] */ virtual void do_freeAll() = 0;
74 /* vt[12] */ virtual void do_freeTail() = 0;
75 /* vt[13] */ virtual void do_fillFreeArea() = 0;
76 /* vt[14] */ virtual s32 do_resize(void* ptr, u32 size) = 0;
77 /* vt[15] */ virtual s32 do_getSize(void* ptr) = 0;
78 /* vt[16] */ virtual s32 do_getFreeSize() = 0;
79 /* vt[17] */ virtual void* do_getMaxFreeBlock() = 0;
80 /* vt[18] */ virtual s32 do_getTotalFreeSize() = 0;
81 /* vt[19] */ virtual s32 do_changeGroupID(u8 newGroupID);
82 /* vt[20] */ virtual u8 do_getCurrentGroupId();
83 /* vt[21] */ virtual void state_register(JKRHeap::TState* p, u32 id) const;
84 /* vt[22] */ virtual bool state_compare(JKRHeap::TState const& r1, JKRHeap::TState const& r2) const;
85 /* vt[23] */ virtual void state_dump(JKRHeap::TState const& p) const;
86
88 bool getDebugFill() const { return mDebugFill; }
89 void* getStartAddr() const { return (void*)mStart; }
90 void* getEndAddr() const { return (void*)mEnd; }
91 u32 getSize() const { return mSize; }
92 bool getErrorFlag() const { return mErrorFlag; }
93 void callErrorHandler(JKRHeap* heap, u32 size, int alignment) {
94 if (mErrorHandler) {
95 (*mErrorHandler)(heap, size, alignment);
96 }
97 }
98
99 JKRHeap* getParent() const {
101 return parent->getObject();
102 }
103
107 void lock() { OSLockMutex(&mMutex); }
109 u32 getHeapSize() { return mSize; }
110
111protected:
112 /* 0x00 */ // vtable
113 /* 0x04 */ // JKRDisposer
114 /* 0x18 */ OSMutex mMutex;
115 /* 0x30 */ u8* mStart;
116 /* 0x34 */ u8* mEnd;
117 /* 0x38 */ u32 mSize;
118 /* 0x3C */ bool mDebugFill;
119 /* 0x3D */ bool mCheckMemoryFilled;
120 /* 0x3E */ u8 mAllocationMode; // EAllocMode?
121 /* 0x3F */ u8 mGroupId;
124 /* 0x68 */ bool mErrorFlag;
125 /* 0x69 */ bool mInitFlag;
126 /* 0x6A */ u8 padding_0x6a[2];
127
128public:
129 static bool initArena(char** memory, u32* size, int maxHeaps);
130 static void* alloc(u32 size, int alignment, JKRHeap* heap);
131 static void free(void* ptr, JKRHeap* heap);
132 static s32 resize(void* ptr, u32 size, JKRHeap* heap);
133 static s32 getSize(void* ptr, JKRHeap* heap);
134 static JKRHeap* findFromRoot(void* ptr);
135
136 static void copyMemory(void* dst, void* src, u32 size);
137 static void fillMemory(void* dst, u32 size, u8 value); // NOTE: never used
138 static bool checkMemoryFilled(void* src, u32 size, u8 value);
139
141
143 static void* getCodeStart(void) { return mCodeStart; }
144 static void* getCodeEnd(void) { return mCodeEnd; }
145 static void* getUserRamStart(void) { return mUserRamStart; }
146 static void* getUserRamEnd(void) { return mUserRamEnd; }
147 static u32 getMemorySize(void) { return mMemorySize; }
148 static JKRHeap* getRootHeap() { return sRootHeap; }
149#ifdef DEBUG
150 static JKRHeap* getRootHeap2() { return sRootHeap2; }
151#endif
152
153 static JKRHeap* getSystemHeap() { return sSystemHeap; }
154 static JKRHeap* getCurrentHeap() { return sCurrentHeap; }
155 static void setSystemHeap(JKRHeap* heap) { sSystemHeap = heap; }
156 static void setCurrentHeap(JKRHeap* heap) { sCurrentHeap = heap; }
157
158 static void setState_u32ID_(TState* state, u32 id) { state->mId = id; }
159 static void setState_uUsedSize_(TState* state, u32 usedSize) { state->mUsedSize = usedSize; }
160 static void setState_u32CheckCode_(TState* state, u32 checkCode) {
161 state->mCheckCode = checkCode;
162 }
163 static void* getState_buf_(TState* state) { return &state->mBuf; }
164 static void* getState_(TState* state) { return getState_buf_(state); }
165
166 static void* mCodeStart;
167 static void* mCodeEnd;
168 static void* mUserRamStart;
169 static void* mUserRamEnd;
171
173#ifdef DEBUG
175#endif
176
179
181};
182
183void* operator new(size_t size);
184void* operator new(size_t size, int alignment);
185void* operator new(size_t size, JKRHeap* heap, int alignment);
186
187void* operator new[](size_t size);
188void* operator new[](size_t size, int alignment);
189void* operator new[](size_t size, JKRHeap* heap, int alignment);
190
191void operator delete(void* ptr);
192void operator delete[](void* ptr);
193
194inline void* operator new(size_t size, void* ptr) {
195 return ptr;
196}
197
198void JKRDefaultMemoryErrorRoutine(void* heap, u32 size, int alignment);
199
200inline void* JKRAllocFromHeap(JKRHeap* heap, u32 size, int alignment) {
201 return JKRHeap::alloc(size, alignment, heap);
202}
203
204inline void* JKRAllocFromSysHeap(u32 size, int alignment) {
205 JKRHeap* systemHeap = JKRHeap::getSystemHeap();
206 return systemHeap->alloc(size, alignment);
207}
208
209inline void JKRFreeToHeap(JKRHeap* heap, void* ptr) {
210 JKRHeap::free(ptr, heap);
211}
212
213inline void JKRFreeToSysHeap(void* ptr) {
214 JKRHeap* systemHeap = JKRHeap::getSystemHeap();
215 systemHeap->free(ptr);
216}
217
218inline void JKRFree(void* ptr) {
219 JKRHeap::free(ptr, NULL);
220}
221
223 return JKRHeap::getSystemHeap();
224}
225
228}
229
231 return heap->becomeCurrentHeap();
232}
233
234inline u32 JKRGetMemBlockSize(JKRHeap* heap, void* block) {
235 return JKRHeap::getSize(block, heap);
236}
237
238inline void* JKRAlloc(u32 size, int alignment) {
239 return JKRHeap::alloc(size, alignment, NULL);
240}
241
242inline s32 JKRResizeMemBlock(JKRHeap* heap, void* ptr, u32 size) {
243 return JKRHeap::resize(ptr, size, heap);
244}
245
247 return JKRHeap::getRootHeap();
248}
249
250#endif /* JKRHEAP_H */
void JKRFreeToHeap(JKRHeap *heap, void *ptr)
Definition JKRHeap.h:209
s32 JKRResizeMemBlock(JKRHeap *heap, void *ptr, u32 size)
Definition JKRHeap.h:242
JKRHeap * JKRGetCurrentHeap()
Definition JKRHeap.h:226
JKRHeap * JKRGetSystemHeap()
Definition JKRHeap.h:222
void(* JKRErrorHandler)(void *, u32, int)
Definition JKRHeap.h:8
bool data_804508B0
u32 JKRGetMemBlockSize(JKRHeap *heap, void *block)
Definition JKRHeap.h:234
JKRHeap * JKRGetRootHeap()
Definition JKRHeap.h:246
void * JKRAllocFromSysHeap(u32 size, int alignment)
Definition JKRHeap.h:204
JKRHeap * JKRSetCurrentHeap(JKRHeap *heap)
Definition JKRHeap.h:230
void * JKRAlloc(u32 size, int alignment)
Definition JKRHeap.h:238
void JKRFreeToSysHeap(void *ptr)
Definition JKRHeap.h:213
void * JKRAllocFromHeap(JKRHeap *heap, u32 size, int alignment)
Definition JKRHeap.h:200
void JKRFree(void *ptr)
Definition JKRHeap.h:218
void JKRDefaultMemoryErrorRoutine(void *heap, u32 size, int alignment)
Definition JKRHeap.cpp:390
void OSUnlockMutex(OSMutex *mutex)
Definition OSMutex.c:88
void OSLockMutex(OSMutex *mutex)
Definition OSMutex.c:62
T cLib_calcTimer(T *value)
Definition c_lib.h:74
static s32 r2
Definition c_math.cpp:176
static s32 r1
Definition c_math.cpp:173
Definition JKRDisposer.h:12
Definition JKRHeap.h:18
JKRHeap * getHeap() const
Definition JKRHeap.h:30
u32 getId() const
Definition JKRHeap.h:31
u32 getUsedSize() const
Definition JKRHeap.h:28
JKRHeap * mHeap
Definition JKRHeap.h:24
u32 mUsedSize
Definition JKRHeap.h:20
u32 getCheckCode() const
Definition JKRHeap.h:29
u32 mId
Definition JKRHeap.h:25
u32 mBuf
Definition JKRHeap.h:22
u32 field_0xc
Definition JKRHeap.h:23
u32 mCheckCode
Definition JKRHeap.h:21
Definition JKRHeap.h:16
virtual void state_dump(JKRHeap::TState const &p) const
Definition JKRHeap.cpp:496
void lock()
Definition JKRHeap.h:107
OSMutex mMutex
Definition JKRHeap.h:114
virtual bool dump_sort()
Definition m_Do_main.cpp:556
static JKRHeap * getCurrentHeap()
Definition JKRHeap.h:154
virtual void state_register(JKRHeap::TState *p, u32 id) const
Definition JKRHeap.cpp:483
static void * getUserRamEnd(void)
Definition JKRHeap.h:146
void destroy()
Definition JKRHeap.cpp:138
bool mErrorFlag
Definition JKRHeap.h:124
static void * mCodeEnd
Definition JKRHeap.h:167
JKRHeap * findAllHeap(void *ptr) const
Definition JKRHeap.cpp:307
virtual void * do_getMaxFreeBlock()=0
virtual bool dump()=0
void * getEndAddr() const
Definition JKRHeap.h:90
virtual s32 do_getTotalFreeSize()=0
static JKRHeap * sCurrentHeap
Definition JKRHeap.h:178
void * getStartAddr() const
Definition JKRHeap.h:89
void dispose()
Definition JKRHeap.cpp:368
virtual s32 do_resize(void *ptr, u32 size)=0
bool isSubHeap(JKRHeap *heap) const
Definition JKRHeap.cpp:419
static void * getCodeStart(void)
Definition JKRHeap.h:143
static void * getState_buf_(TState *state)
Definition JKRHeap.h:163
static void setState_uUsedSize_(TState *state, u32 usedSize)
Definition JKRHeap.h:159
static JKRHeap * getSystemHeap()
Definition JKRHeap.h:153
void * alloc(u32 size, int alignment)
Definition JKRHeap.cpp:156
static JKRErrorHandler mErrorHandler
Definition JKRHeap.h:180
u8 mAllocationMode
Definition JKRHeap.h:120
static void setState_u32CheckCode_(TState *state, u32 checkCode)
Definition JKRHeap.h:160
JKRHeap * becomeCurrentHeap()
Definition JKRHeap.cpp:131
virtual void callAllDisposer()
Definition JKRHeap.cpp:183
u8 mGroupId
Definition JKRHeap.h:121
bool mDebugFill
Definition JKRHeap.h:118
virtual s32 do_getFreeSize()=0
static void setDefaultDebugFill(bool status)
Definition JKRHeap.h:142
virtual s32 do_changeGroupID(u8 newGroupID)
Definition JKRHeap.cpp:503
bool getDebugFill() const
Definition JKRHeap.h:88
s32 resize(void *ptr, u32 size)
Definition JKRHeap.cpp:219
static void * getState_(TState *state)
Definition JKRHeap.h:164
virtual void * do_alloc(u32 size, int alignment)=0
void * getMaxFreeBlock()
Definition JKRHeap.cpp:248
static void * getUserRamStart(void)
Definition JKRHeap.h:145
u8 padding_0x6a[2]
Definition JKRHeap.h:126
static void setSystemHeap(JKRHeap *heap)
Definition JKRHeap.h:155
s32 getTotalFreeSize()
Definition JKRHeap.cpp:253
void unlock()
Definition JKRHeap.h:108
JKRHeap * find(void *ptr) const
Definition JKRHeap.cpp:287
static JKRHeap * getRootHeap2()
Definition JKRHeap.h:150
virtual void do_fillFreeArea()=0
virtual u32 getHeapType()=0
void removeDisposer(JKRDisposer *disposer)
Definition JKRHeap.h:106
bool mInitFlag
Definition JKRHeap.h:125
virtual u8 do_getCurrentGroupId()
Definition JKRHeap.cpp:508
void freeTail()
Definition JKRHeap.cpp:200
virtual void do_freeAll()=0
static u32 mMemorySize
Definition JKRHeap.h:170
virtual bool check()=0
virtual void do_free(void *ptr)=0
u32 getHeapSize()
Definition JKRHeap.h:109
static u32 getMemorySize(void)
Definition JKRHeap.h:147
static void * mUserRamStart
Definition JKRHeap.h:168
JSUList< JKRDisposer > mDisposerList
Definition JKRHeap.h:123
virtual ~JKRHeap()
Definition JKRHeap.cpp:63
static void * getCodeEnd(void)
Definition JKRHeap.h:144
void callErrorHandler(JKRHeap *heap, u32 size, int alignment)
Definition JKRHeap.h:93
static bool initArena(char **memory, u32 *size, int maxHeaps)
Definition JKRHeap.cpp:93
static bool checkMemoryFilled(void *src, u32 size, u8 value)
void appendDisposer(JKRDisposer *disposer)
Definition JKRHeap.h:105
void freeAll()
Definition JKRHeap.cpp:192
void dispose_subroutine(u32 start, u32 end)
Definition JKRHeap.cpp:329
static JKRHeap * getRootHeap()
Definition JKRHeap.h:148
u32 getSize() const
Definition JKRHeap.h:91
static JKRErrorHandler setErrorHandler(JKRErrorHandler errorHandler)
Definition JKRHeap.cpp:407
virtual bool state_compare(JKRHeap::TState const &r1, JKRHeap::TState const &r2) const
Definition JKRHeap.cpp:490
u8 * mStart
Definition JKRHeap.h:115
bool mCheckMemoryFilled
Definition JKRHeap.h:119
static JKRHeap * findFromRoot(void *ptr)
Definition JKRHeap.cpp:274
JKRHeap * getParent() const
Definition JKRHeap.h:99
static void setState_u32ID_(TState *state, u32 id)
Definition JKRHeap.h:158
static void copyMemory(void *dst, void *src, u32 size)
Definition JKRHeap.cpp:376
s32 getFreeSize()
Definition JKRHeap.cpp:243
u32 mSize
Definition JKRHeap.h:117
JSUTree< JKRHeap > mChildTree
Definition JKRHeap.h:122
bool getErrorFlag() const
Definition JKRHeap.h:92
void setDebugFill(bool debugFill)
Definition JKRHeap.h:87
JKRHeap * becomeSystemHeap()
Definition JKRHeap.cpp:124
JSUTree< JKRHeap > & getHeapTree()
Definition JKRHeap.h:104
void free(void *ptr)
Definition JKRHeap.cpp:175
static JKRHeap * sRootHeap2
Definition JKRHeap.h:174
s32 changeGroupID(u8 newGroupId)
Definition JKRHeap.cpp:258
static void fillMemory(void *dst, u32 size, u8 value)
virtual void do_destroy()=0
u8 * mEnd
Definition JKRHeap.h:116
static JKRHeap * sRootHeap
Definition JKRHeap.h:172
static void * mUserRamEnd
Definition JKRHeap.h:169
bool setErrorFlag(bool errorFlag)
Definition JKRHeap.cpp:399
static void * mCodeStart
Definition JKRHeap.h:166
static void setCurrentHeap(JKRHeap *heap)
Definition JKRHeap.h:156
u32 getMaxAllocatableSize(int alignment)
Definition JKRHeap.cpp:267
virtual void do_freeTail()=0
virtual s32 do_getSize(void *ptr)=0
static JKRHeap * sSystemHeap
Definition JKRHeap.h:177
Definition JSUList.h:96
bool remove(JSULink< T > *link)
Definition JSUList.h:111
bool append(JSULink< T > *link)
Definition JSUList.h:103
Definition JSUList.h:186
T * getObject() const
Definition JSUList.h:209
JSUTree< T > * getParent() const
Definition JSUList.h:211
static void end(b_oh_class *i_this)
Definition d_a_b_oh.cpp:527
static SECTION_RODATA u8 const id[8]
Definition d_a_npc_bans.cpp:1309
static u16 data[4]
Definition d_vibration.cpp:114
p
Definition e_acos.c:98
static u8 block[48]
Definition fstload.c:39
static s32 status
Definition fstload.c:6
@ start
Definition strtoul.c:8
Definition OSMutex.h:10
unsigned long u32
Definition types.h:10
signed long s32
Definition types.h:6
unsigned char u8
Definition types.h:8