Twilight Princess
Decompilation of The Legend of Zelda: Twilight Princess
Loading...
Searching...
No Matches
JASHeapCtrl.h
Go to the documentation of this file.
1#ifndef JASHEAPCTRL_H
2#define JASHEAPCTRL_H
3
7#include <dolphin/os.h>
8#include <dolphin/os.h>
9
10class JASDisposer;
11class JKRHeap;
12class JKRSolidHeap;
13
18class JASHeap {
19public:
20 JASHeap(JASDisposer* param_0 = NULL);
21 void initRootHeap(void*, u32);
22 bool alloc(JASHeap*, u32);
23 bool allocTail(JASHeap*, u32);
24 bool free();
25 u32 getTotalFreeSize() const;
26 u32 getFreeSize() const;
27 void insertChild(JASHeap*, JASHeap*, void*, u32, bool);
31
32 void* getBase() { return mBase; }
33 bool isAllocated() const { return mBase; }
34 u32 getSize() const { return mSize; }
35
37 /* 0x1C */ OSMutex mMutex;
39 /* 0x38 */ u8* mBase;
40 /* 0x3C */ u32 mSize;
41 /* 0x40 */ JASHeap* field_0x40;
42};
43
51 void newMemPool(u32, int);
52 void* alloc(u32);
53 void free(void*, u32);
54
56 return freeMemCount;
57 }
58
60 return totalMemCount;
61 }
62
63 /* 0x00 */ void* field_0x0;
64 /* 0x04 */ u32 freeMemCount;
65 /* 0x08 */ u32 totalMemCount;
66 /* 0x0C */ u32 usedMemCount;
67};
68
70 template <typename A0>
72 struct Lock {
73 Lock(const A0& param_0) { field_0x0 = OSDisableInterrupts(); }
75
77 };
78 };
79
80 template <typename A0>
81 struct ObjectLevelLockable : public OSMutex {
83 OSInitMutex(this);
84 }
85
86 struct Lock {
87 Lock(A0 const& mutex) {
88 mMutex = (A0*)&mutex;
90 }
91
94 }
95
96 A0* mMutex;
97 };
98 };
99
100 template <typename A0>
102 struct Lock {
103 Lock(const A0& param_0) {}
104 };
105 };
106}; // namespace JASThreadingModel
107
112template <typename T>
114public:
115 void newMemPool(int param_0) {
117 JASGenericMemPool::newMemPool(sizeof(T), param_0);
118 }
119
120 void* alloc(u32 n) {
121 JUT_ASSERT(182, n == sizeof(T));
124 }
125
126 void free(void* ptr, u32 n) {
127 JUT_ASSERT(187, n == sizeof(T));
130 }
131
136
141};
142
144
149template<u32 ChunkSize, template<typename> class T>
150class JASMemChunkPool : public T<JASMemChunkPool<ChunkSize, T> >::ObjectLevelLockable {
151 struct MemoryChunk {
153 mNextChunk = nextChunk;
154 mUsedSize = 0;
155 mChunks = 0;
156 }
157
158 bool checkArea(const void* ptr) const {
159 return (u8*)this + 0xc <= (u8*)ptr && (u8*)ptr < (u8*)this + (ChunkSize + 0xc);
160 }
161
163 return mNextChunk;
164 }
165
166 void* alloc(u32 size) {
167 u8* rv = mBuffer + mUsedSize;
168 mUsedSize += size;
169 mChunks++;
170 return rv;
171 }
172
173 void free(void* mem) {
174 mChunks--;
175 }
176
177 bool isEmpty() const {
178 return mChunks == 0;
179 }
180
182 mNextChunk = chunk;
183 }
184
185 u32 getFreeSize() const {
186 return ChunkSize - mUsedSize;
187 }
188
189 void revive() {
190 mUsedSize = 0;
191 }
192
196 u8 mBuffer[ChunkSize];
197 };
198public:
200 field_0x18 = NULL;
201 bool ret = createNewChunk();
202 JUT_ASSERT(320, ret);
203 }
204
206 bool r27 = 0;
207 if (field_0x18 != NULL && field_0x18->isEmpty()) {
209 return true;
210 }
211 MemoryChunk* pMVar4 = field_0x18;
213 if (field_0x18 != NULL) {
214 return true;
215 }
216 JUT_WARN(428, "%s", "Not enough JASSystemHeap");
217 field_0x18 = new (JKRHeap::getSystemHeap(), 0) MemoryChunk(pMVar4);
218 if (field_0x18 != NULL) {
219 return true;
220 }
221 field_0x18 = pMVar4;
222 return false;
223 }
224
225 void* alloc(u32 size) {
226 typename T<JASMemChunkPool<ChunkSize, T> >::Lock lock(*this);
227 u32 freeSize = field_0x18->getFreeSize();
228 if (freeSize < size) {
229 if (ChunkSize < size) {
230 return NULL;
231 }
232 if (createNewChunk() == 0) {
233 return NULL;
234 }
235 }
236 return field_0x18->alloc(size);
237 }
238
239 void free(void* ptr) {
240 typename T<JASMemChunkPool<ChunkSize, T> >::Lock lock(*this);
241 MemoryChunk* chunk = field_0x18;
242 MemoryChunk* prevChunk = NULL;
243 while (chunk != NULL) {
244 if (chunk->checkArea(ptr)) {
245 chunk->free(ptr);
246 bool r26 = false;
247 if (chunk != field_0x18 && chunk->isEmpty()) {
248 MemoryChunk* nextChunk = chunk->getNextChunk();
249 delete chunk;
250 prevChunk->setNextChunk(nextChunk);
251 }
252 return;
253 }
254 prevChunk = chunk;
255 chunk = chunk->getNextChunk();
256 }
257
258 JUT_PANIC(362, "Cannnot free for JASMemChunkPool");
259 }
260
262};
263
264namespace JASKernel {
268 void setupAramHeap(u32, u32);
272
273 extern JASHeap audioAramHeap;
274 extern u32 sAramBase;
275 extern JKRHeap* sSystemHeap;
277};
278
283template <typename T>
285public:
286 static void* operator new(size_t n) {
287#if PLATFORM_GCN
289#endif
290 return memPool_.alloc(n);
291 }
292 static void* operator new(size_t n, void* ptr) {
293 return ptr;
294 }
295 static void operator delete(void* ptr, size_t n) {
296#if PLATFORM_GCN
298#endif
299 memPool_.free(ptr, n);
300 }
301 static void newMemPool(int param_0) {
302#if PLATFORM_GCN
304#endif
305 memPool_.newMemPool(param_0);
306 }
308#if PLATFORM_GCN
310#endif
311 return memPool_.getFreeMemCount();
312 }
314#if PLATFORM_GCN
316#endif
317 return memPool_.getTotalMemCount();
318 }
319
320private:
321 // Fakematch? Is memPool_ both an in-function static and an out-of-function static?
323#if PLATFORM_GCN
325 static JASMemPool<T> memPool_;
326 return memPool_;
327 }
328#endif
329};
330
331#if !PLATFORM_GCN
333#endif
334
339template <typename T>
341public:
342 void newMemPool(int param_0) {
344 JASGenericMemPool::newMemPool(sizeof(T), param_0);
345 }
346
347 void* alloc(size_t count) {
349 return JASGenericMemPool::alloc(count);
350 }
351
352 void free(void* ptr, u32 param_1) {
354 JASGenericMemPool::free(ptr, param_1);
355 }
356};
357
362template <typename T>
364public:
365 static void* operator new(size_t n) {
366#if PLATFORM_GCN
368#endif
369 return memPool_.alloc(n);
370 }
371 static void* operator new(size_t n, void* ptr) {
372 return ptr;
373 }
374 static void operator delete(void* ptr, size_t n) {
375#if PLATFORM_GCN
377#endif
378 memPool_.free(ptr, n);
379 }
380
381 static void newMemPool(int n) {
382#if PLATFORM_GCN
384#endif
385 memPool_.newMemPool(n);
386 }
387
388private:
389 // Fakematch? Is memPool_ both an in-function static and an out-of-function static?
391#if PLATFORM_GCN
396#endif
397};
398
399#if !PLATFORM_GCN
401#endif
402
403extern JKRSolidHeap* JASDram;
404
405#endif /* JASHEAPCTRL_H */
JKRSolidHeap * JASDram
Definition JASHeapCtrl.cpp:244
Definition JASHeapCtrl.h:18
bool isAllocated() const
Definition JASHeapCtrl.h:33
JASDisposer * mDisposer
Definition JASHeapCtrl.h:38
void initRootHeap(void *, u32)
Definition JASHeapCtrl.cpp:19
JASHeap * field_0x40
Definition JASHeapCtrl.h:41
JSUTree< JASHeap > mTree
Definition JASHeapCtrl.h:36
OSMutex mMutex
Definition JASHeapCtrl.h:37
JASHeap * getTailHeap()
Definition JASHeapCtrl.cpp:190
u32 getFreeSize() const
Definition JASHeapCtrl.cpp:148
u32 getTotalFreeSize() const
Definition JASHeapCtrl.cpp:135
u32 mSize
Definition JASHeapCtrl.h:40
bool allocTail(JASHeap *, u32)
Definition JASHeapCtrl.cpp:81
void insertChild(JASHeap *, JASHeap *, void *, u32, bool)
Definition JASHeapCtrl.cpp:168
JASHeap(JASDisposer *param_0=NULL)
Definition JASHeapCtrl.cpp:11
bool alloc(JASHeap *, u32)
Definition JASHeapCtrl.cpp:27
bool free()
Definition JASHeapCtrl.cpp:102
u32 getTailOffset()
Definition JASHeapCtrl.cpp:205
u8 * mBase
Definition JASHeapCtrl.h:39
void * getBase()
Definition JASHeapCtrl.h:32
u32 getSize() const
Definition JASHeapCtrl.h:34
u32 getCurOffset()
Definition JASHeapCtrl.cpp:217
Definition JASHeapCtrl.h:150
void * alloc(u32 size)
Definition JASHeapCtrl.h:225
JASMemChunkPool()
Definition JASHeapCtrl.h:199
MemoryChunk * field_0x18
Definition JASHeapCtrl.h:261
bool createNewChunk()
Definition JASHeapCtrl.h:205
void free(void *ptr)
Definition JASHeapCtrl.h:239
Definition JASHeapCtrl.h:340
void newMemPool(int param_0)
Definition JASHeapCtrl.h:342
void * alloc(size_t count)
Definition JASHeapCtrl.h:347
void free(void *ptr, u32 param_1)
Definition JASHeapCtrl.h:352
Definition JASHeapCtrl.h:113
u32 getTotalMemCount() const
Definition JASHeapCtrl.h:137
void newMemPool(int param_0)
Definition JASHeapCtrl.h:115
void free(void *ptr, u32 n)
Definition JASHeapCtrl.h:126
void * alloc(u32 n)
Definition JASHeapCtrl.h:120
u32 getFreeMemCount() const
Definition JASHeapCtrl.h:132
Definition JASHeapCtrl.h:363
static void newMemPool(int n)
Definition JASHeapCtrl.h:381
static JASMemPool_MultiThreaded< T > & getMemPool()
Definition JASHeapCtrl.h:392
static JASMemPool_MultiThreaded< T > memPool_
Definition JASHeapCtrl.h:390
Definition JASHeapCtrl.h:284
static void newMemPool(int param_0)
Definition JASHeapCtrl.h:301
static u32 getTotalMemCount()
Definition JASHeapCtrl.h:313
static u32 getFreeMemCount()
Definition JASHeapCtrl.h:307
static JASMemPool< T > memPool_
Definition JASHeapCtrl.h:322
static JASMemPool< T > & getMemPool_()
Definition JASHeapCtrl.h:324
Definition JKRHeap.h:23
static JKRHeap * getSystemHeap()
Definition JKRHeap.h:165
Definition JKRSolidHeap.h:10
Definition JSUList.h:188
Definition JASAudioThread.cpp:44
asm BOOL OSRestoreInterrupts(__REGISTER BOOL level)
Definition OSInterrupt.c:106
asm BOOL OSDisableInterrupts(void)
Definition OSInterrupt.c:85
void OSInitMutex(OSMutex *mutex)
Definition OSMutex.c:52
void OSUnlockMutex(OSMutex *mutex)
Definition OSMutex.c:86
void OSLockMutex(OSMutex *mutex)
Definition OSMutex.c:58
unsigned int size
Definition __os.h:106
n
Definition e_fmod.c:115
int BOOL
Definition types.h:33
unsigned long u32
Definition types.h:12
unsigned char u8
Definition types.h:8
Definition JASHeapCtrl.h:143
void setupAramHeap(u32, u32)
Definition JASHeapCtrl.cpp:312
JASMemChunkPool< 1024, JASThreadingModel::ObjectLevelLockable > * sCommandHeap
Definition JASHeapCtrl.cpp:291
u32 getAramFreeSize()
Definition JASHeapCtrl.cpp:325
u32 sAramBase
Definition JASHeapCtrl.cpp:287
JKRHeap * getSystemHeap()
Definition JASHeapCtrl.cpp:302
JASHeap audioAramHeap
Definition JASHeapCtrl.cpp:310
JKRHeap * sSystemHeap
Definition JASHeapCtrl.cpp:289
JASMemChunkPool< 1024, JASThreadingModel::ObjectLevelLockable > * getCommandHeap()
Definition JASHeapCtrl.cpp:306
void setupRootHeap(JKRSolidHeap *, u32)
Definition JASHeapCtrl.cpp:293
JASHeap * getAramHeap()
Definition JASHeapCtrl.cpp:321
u32 getAramSize()
Definition JASHeapCtrl.cpp:329
Definition JASHeapCtrl.h:69
Definition JASWaveArcLoader.h:14
Definition JASHeapCtrl.h:48
~JASGenericMemPool()
Definition JASHeapCtrl.cpp:235
u32 usedMemCount
Definition JASHeapCtrl.h:66
u32 getFreeMemCount() const
Definition JASHeapCtrl.h:55
u32 freeMemCount
Definition JASHeapCtrl.h:64
u32 totalMemCount
Definition JASHeapCtrl.h:65
void * field_0x0
Definition JASHeapCtrl.h:63
void newMemPool(u32, int)
Definition JASHeapCtrl.cpp:251
u32 getTotalMemCount() const
Definition JASHeapCtrl.h:59
void free(void *, u32)
Definition JASHeapCtrl.cpp:277
JASGenericMemPool()
Definition JASHeapCtrl.cpp:228
void * alloc(u32)
Definition JASHeapCtrl.cpp:264
Definition JASHeapCtrl.h:151
void free(void *mem)
Definition JASHeapCtrl.h:173
bool checkArea(const void *ptr) const
Definition JASHeapCtrl.h:158
void revive()
Definition JASHeapCtrl.h:189
u8 mBuffer[ChunkSize]
Definition JASHeapCtrl.h:196
u32 mUsedSize
Definition JASHeapCtrl.h:194
MemoryChunk(MemoryChunk *nextChunk)
Definition JASHeapCtrl.h:152
MemoryChunk * mNextChunk
Definition JASHeapCtrl.h:193
void setNextChunk(MemoryChunk *chunk)
Definition JASHeapCtrl.h:181
u32 getFreeSize() const
Definition JASHeapCtrl.h:185
u32 mChunks
Definition JASHeapCtrl.h:195
bool isEmpty() const
Definition JASHeapCtrl.h:177
void * alloc(u32 size)
Definition JASHeapCtrl.h:166
MemoryChunk * getNextChunk()
Definition JASHeapCtrl.h:162
~Lock()
Definition JASHeapCtrl.h:74
BOOL field_0x0
Definition JASHeapCtrl.h:76
Lock(const A0 &param_0)
Definition JASHeapCtrl.h:73
Definition JASHeapCtrl.h:71
A0 * mMutex
Definition JASHeapCtrl.h:96
Lock(A0 const &mutex)
Definition JASHeapCtrl.h:87
~Lock()
Definition JASHeapCtrl.h:92
Definition JASHeapCtrl.h:81
ObjectLevelLockable()
Definition JASHeapCtrl.h:82
Definition JASHeapCtrl.h:102
Lock(const A0 &param_0)
Definition JASHeapCtrl.h:103
Definition JASHeapCtrl.h:101
Definition OSMutex.h:13