Skip to content

Commit d8f5672

Browse files
committed
MEM1 fix
1 parent 29a3b91 commit d8f5672

22 files changed

+223
-115
lines changed

include/Pack/RPSystem/RPSysSystem.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,27 @@ class RPSysSystem {
227227
return mpSystemHeap;
228228
}
229229

230+
/**
231+
* @brief Gets the heap for resource caching
232+
*/
233+
EGG::Heap* getResCacheHeap() const {
234+
return mpResCacheHeap;
235+
}
236+
237+
/**
238+
* @brief Gets the root heap for all scene heaps
239+
*/
240+
EGG::Heap* getRootSceneHeap() const {
241+
return mpRootSceneHeap;
242+
}
243+
244+
/**
245+
* @brief Gets the root heap for all scene dependency heaps
246+
*/
247+
EGG::Heap* getRootSceneDependHeap() const {
248+
return mpRootSceneDependHeap;
249+
}
250+
230251
/**
231252
* @brief Gets the heap which holds the rest of free MEM1 memory
232253
*/

lib/libkiwi/core/kiwiMemStream.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ s32 MemStream::PeekImpl(void* pDst, u32 size) {
160160
* @note String size limited to 0x400 (1024) characters
161161
*/
162162
String MemStream::Read_string() {
163-
static int sTextBufferPos = 0;
163+
int pos = 0;
164164
static char sTextBuffer[1024 / sizeof(char)];
165165

166166
// Form string in work buffer
167-
while (sTextBufferPos < K_LENGTHOF(sTextBuffer)) {
167+
while (pos < K_LENGTHOF(sTextBuffer)) {
168168
char ch = Read_s8();
169-
sTextBuffer[sTextBufferPos++] = ch;
169+
sTextBuffer[pos++] = ch;
170170

171171
// Null terminator
172172
if (ch == '\0') {
@@ -180,7 +180,7 @@ String MemStream::Read_string() {
180180
}
181181

182182
// No matter what happened, null terminator should be at the end
183-
sTextBuffer[sTextBufferPos] = '\0';
183+
sTextBuffer[pos] = '\0';
184184
return String(sTextBuffer);
185185
}
186186

@@ -209,13 +209,13 @@ String MemStream::Peek_string() {
209209
* @note String size limited to 0x200 (512) characters
210210
*/
211211
WString MemStream::Read_wstring() {
212-
static int sTextBufferPos = 0;
212+
int pos = 0;
213213
static wchar_t sTextBuffer[1024 / sizeof(wchar_t)];
214214

215215
// Form string in work buffer
216-
while (sTextBufferPos < K_LENGTHOF(sTextBuffer)) {
216+
while (pos < K_LENGTHOF(sTextBuffer)) {
217217
wchar_t ch = Read_u16();
218-
sTextBuffer[sTextBufferPos++] = ch;
218+
sTextBuffer[pos++] = ch;
219219

220220
// Null terminator
221221
if (ch == L'\0') {
@@ -229,7 +229,7 @@ WString MemStream::Read_wstring() {
229229
}
230230

231231
// No matter what happened, null terminator should be at the end
232-
sTextBuffer[sTextBufferPos] = L'\0';
232+
sTextBuffer[pos] = L'\0';
233233
return WString(sTextBuffer);
234234
}
235235

lib/libkiwi/core/kiwiMemoryMgr.cpp

Lines changed: 58 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void LogHeap(const char* pName, EGG::Heap* pHeap) {
2929
* @param pBlock Target of delete operation
3030
*/
3131
void CheckDoubleFree(const void* pBlock) {
32-
#ifndef NDEBUG
32+
#if !defined(NDEBUG)
3333
// nullptr delete is OK
3434
if (pBlock == nullptr) {
3535
return;
@@ -63,61 +63,32 @@ void CheckDoubleFree(const void* pBlock) {
6363
* @brief Constructor
6464
*/
6565
MemoryMgr::MemoryMgr() {
66-
#if defined(PACK_SPORTS) || defined(PACK_PLAY)
67-
EGG::Heap* pMem1HeapRP = RP_GET_INSTANCE(RPSysSystem)->getSystemHeap();
68-
EGG::Heap* pMem2HeapRP = RP_GET_INSTANCE(RPSysSystem)->getResourceHeap();
69-
#elif defined(PACK_RESORT)
70-
EGG::Heap* pMem1HeapRP = RP_GET_INSTANCE(RPSysSystem)->getRootHeapMem1();
71-
EGG::Heap* pMem2HeapRP = RP_GET_INSTANCE(RPSysSystem)->getResourceHeap();
72-
#endif
73-
74-
K_ASSERT_PTR(pMem1HeapRP);
75-
LogHeap("RPSysSystem:System", pMem1HeapRP);
66+
EGG::Heap* pRootHeapMEM1 = RPSysSystem::getRootHeapMem1();
67+
K_ASSERT_PTR(pRootHeapMEM1);
7668

77-
K_ASSERT_PTR(pMem2HeapRP);
78-
LogHeap("RPSysSystem:Resource", pMem2HeapRP);
69+
EGG::Heap* pRootHeapMEM2 = RPSysSystem::getRootHeapMem2();
70+
K_ASSERT_PTR(pRootHeapMEM2);
7971

80-
mpHeapMEM1 = EGG::ExpHeap::create(HEAP_SIZE, pMem1HeapRP);
72+
mpHeapMEM1 = EGG::ExpHeap::create(HEAP_SIZE_MEM1, pRootHeapMEM1);
8173
K_ASSERT_PTR(mpHeapMEM1);
8274
K_ASSERT(OSIsMEM1Region(mpHeapMEM1));
83-
LogHeap("libkiwi:MEM1", mpHeapMEM1);
8475

85-
mpHeapMEM2 = EGG::ExpHeap::create(HEAP_SIZE, pMem2HeapRP);
76+
mpHeapMEM2 = EGG::ExpHeap::create(HEAP_SIZE_MEM2, pRootHeapMEM2);
8677
K_ASSERT_PTR(mpHeapMEM2);
8778
K_ASSERT(OSIsMEM2Region(mpHeapMEM2));
88-
LogHeap("libkiwi:MEM2", mpHeapMEM2);
79+
80+
Dump();
8981
}
9082

9183
/**
9284
* @brief Destructor
9385
*/
9486
MemoryMgr::~MemoryMgr() {
9587
delete mpHeapMEM1;
96-
delete mpHeapMEM2;
97-
}
98-
99-
/**
100-
* @brief Gets the heap corresponding to the specified memory region
101-
*
102-
* @param memory Target memory region
103-
*/
104-
EGG::Heap* MemoryMgr::GetHeap(EMemory memory) const {
105-
switch (memory) {
106-
case EMemory_MEM1: {
107-
K_ASSERT_PTR(mpHeapMEM1);
108-
return mpHeapMEM1;
109-
}
110-
111-
case EMemory_MEM2: {
112-
K_ASSERT_PTR(mpHeapMEM2);
113-
return mpHeapMEM2;
114-
}
88+
mpHeapMEM1 = nullptr;
11589

116-
default: {
117-
K_UNREACHABLE();
118-
return nullptr;
119-
}
120-
}
90+
delete mpHeapMEM2;
91+
mpHeapMEM2 = nullptr;
12192
}
12293

12394
/**
@@ -148,6 +119,30 @@ void MemoryMgr::Free(void* pBlock) const {
148119
EGG::Heap::free(pBlock, nullptr);
149120
}
150121

122+
/**
123+
* @brief Gets the heap corresponding to the specified memory region
124+
*
125+
* @param memory Target memory region
126+
*/
127+
EGG::Heap* MemoryMgr::GetHeap(EMemory memory) const {
128+
switch (memory) {
129+
case EMemory_MEM1: {
130+
K_ASSERT_PTR(mpHeapMEM1);
131+
return mpHeapMEM1;
132+
}
133+
134+
case EMemory_MEM2: {
135+
K_ASSERT_PTR(mpHeapMEM2);
136+
return mpHeapMEM2;
137+
}
138+
139+
default: {
140+
K_UNREACHABLE();
141+
return nullptr;
142+
}
143+
}
144+
}
145+
151146
/**
152147
* @brief Gets size of available heap memory
153148
*
@@ -190,6 +185,28 @@ bool MemoryMgr::IsHeapMemory(const void* pAddr) const {
190185
return false;
191186
}
192187

188+
/**
189+
* @brief Dumps heap information for debugging purposes
190+
*/
191+
void MemoryMgr::Dump() {
192+
LogHeap("libkiwi:MEM1", mpHeapMEM1);
193+
LogHeap("libkiwi:MEM2", mpHeapMEM2);
194+
195+
LogHeap("EGG:MEM1", RPSysSystem::getRootHeapMem1());
196+
LogHeap("EGG:MEM2", RPSysSystem::getRootHeapMem2());
197+
LogHeap("EGG:System", RPSysSystem::getSystemHeap());
198+
199+
LogHeap("RPSysSystem:System",
200+
RP_GET_INSTANCE(RPSysSystem)->getSystemHeapRP());
201+
LogHeap("RPSysSystem:Resource",
202+
RP_GET_INSTANCE(RPSysSystem)->getResourceHeap());
203+
204+
#if defined(PACK_RESORT)
205+
LogHeap("RPSysSystem:RootScene",
206+
RP_GET_INSTANCE(RPSysSystem)->getRootSceneHeap());
207+
#endif
208+
}
209+
193210
} // namespace kiwi
194211

195212
/**

lib/libkiwi/core/kiwiMemoryMgr.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ class MemoryMgr : public StaticSingleton<MemoryMgr> {
4343
*/
4444
void Free(void* pBlock) const;
4545

46+
/**
47+
* @brief Gets the heap corresponding to the specified memory region
48+
*
49+
* @param memory Target memory region
50+
*/
51+
EGG::Heap* GetHeap(EMemory memory) const;
52+
4653
/**
4754
* @brief Gets size of available heap memory
4855
*
@@ -69,28 +76,31 @@ class MemoryMgr : public StaticSingleton<MemoryMgr> {
6976
* @brief Constructor
7077
*/
7178
MemoryMgr();
79+
7280
/**
7381
* @brief Destructor
7482
*/
7583
~MemoryMgr();
7684

7785
/**
78-
* @brief Gets the heap corresponding to the specified memory region
79-
*
80-
* @param memory Target memory region
86+
* @brief Dumps heap information for debugging purposes
8187
*/
82-
EGG::Heap* GetHeap(EMemory memory) const;
88+
void Dump();
8389

8490
private:
85-
// TODO(kiwi) How to get more MEM1 memory from WS2?
91+
// 1024KB of MEM1 is not available for use in WS2
8692
#if defined(PACK_SPORTS) || defined(PACK_PLAY)
87-
//! Initial size for each heap
88-
static const u32 HEAP_SIZE = OS_MEM_KB_TO_B(1024);
93+
//! Initial heap size in the MEM1 region
94+
static const u32 HEAP_SIZE_MEM1 = OS_MEM_KB_TO_B(1024);
8995
#elif defined(PACK_RESORT)
90-
//! Initial size for each heap
91-
static const u32 HEAP_SIZE = OS_MEM_KB_TO_B(512);
96+
//! Initial heap size in the MEM1 region
97+
static const u32 HEAP_SIZE_MEM1 = OS_MEM_KB_TO_B(512);
9298
#endif
9399

100+
//! Initial heap size in the MEM2 region
101+
static const u32 HEAP_SIZE_MEM2 = OS_MEM_KB_TO_B(1024);
102+
103+
private:
94104
EGG::Heap* mpHeapMEM1; //!< Heap in MEM1 region
95105
EGG::Heap* mpHeapMEM2; //!< Heap in MEM2 region
96106
};

lib/libkiwi/core/kiwiSceneHookMgr.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,9 @@ void SceneHookMgr::DoPause() {
238238
}
239239
}
240240
}
241-
KOKESHI_BY_PACK(KM_CALL(0x8018cec8, SceneHookMgr::DoPause), // Wii Sports
242-
KM_CALL(0x8018b084, SceneHookMgr::DoPause), // Wii Play
243-
KM_CALL(0x8022fc94,
244-
SceneHookMgr::DoPause)); // Wii Sports Resort
241+
KOKESHI_BY_PACK(KM_CALL(0x8018cec8, SceneHookMgr::DoPause), // Wii Sports
242+
KM_CALL(0x8018b084, SceneHookMgr::DoPause), // Wii Play
243+
KM_CALL(0x8022fc94, SceneHookMgr::DoPause)); // Wii Sports Resort
245244

246245
/**
247246
* @brief Runs active hooks and scene function(s) for exiting the pause menu
@@ -264,10 +263,9 @@ void SceneHookMgr::DoUnPause() {
264263
}
265264
}
266265
// 'Continue'
267-
KOKESHI_BY_PACK(KM_CALL(0x8018d118, SceneHookMgr::DoUnPause), // Wii Sports
268-
KM_CALL(0x8018b174, SceneHookMgr::DoUnPause), // Wii Play
269-
KM_CALL(0x8022fc38,
270-
SceneHookMgr::DoUnPause)); // Wii Sports Resort
266+
KOKESHI_BY_PACK(KM_CALL(0x8018d118, SceneHookMgr::DoUnPause), // Wii Sports
267+
KM_CALL(0x8018b174, SceneHookMgr::DoUnPause), // Wii Play
268+
KM_CALL(0x8022fc38, SceneHookMgr::DoUnPause)); // Wii Sports Resort
271269
// 'Start over'
272270
KOKESHI_BY_PACK(KM_CALL(0x8018d14c, SceneHookMgr::DoUnPause), // Wii Sports
273271
KM_CALL(0x8018b1a8, SceneHookMgr::DoUnPause), // Wii Play

lib/libkiwi/debug/kiwiAssert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* @name Internal usage
5555
*/
5656
/**@{*/
57-
#ifndef NDEBUG
57+
#if !defined(NDEBUG)
5858
//! Log a message to the console
5959
#define K_LOG(msg) kiwi_log(msg)
6060
//! Log a message (format string) to the console

lib/libkiwi/debug/kiwiMapFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void MapFile::Close() {
5656
delete *next;
5757
}
5858

59-
delete mpMapBuffer;
59+
delete[] mpMapBuffer;
6060
mpMapBuffer = nullptr;
6161

6262
mIsUnpacked = false;

lib/libkiwi/debug/kiwiNw4rException.cpp

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66
#include <cstdio>
77

88
namespace kiwi {
9+
namespace {
10+
11+
/**
12+
* @brief Prints heap information
13+
*
14+
* @param pName Heap name
15+
* @param pHeap Heap object
16+
*/
17+
void LogHeap(const char* pName, EGG::Heap* pHeap) {
18+
if (pHeap == nullptr) {
19+
Nw4rException::GetInstance().Printf("[%s] nullptr ->\n", pName);
20+
return;
21+
}
22+
23+
Nw4rException::GetInstance().Printf(
24+
"%s: %.2fKB free\n", pName, pHeap,
25+
OS_MEM_B_TO_KB(static_cast<f32>(pHeap->getAllocatableSize())));
26+
}
27+
28+
} // namespace
929

1030
K_DYNAMIC_SINGLETON_IMPL(Nw4rException);
1131

@@ -305,22 +325,22 @@ void Nw4rException::DumpAssert() {
305325
void Nw4rException::PrintHeapInfo() {
306326
Printf("---Heap Info---\n");
307327

308-
Printf("libkiwi (MEM1): %.2f KB free\n",
309-
OS_MEM_B_TO_KB(static_cast<f32>(
310-
MemoryMgr::GetInstance().GetFreeSize(EMemory_MEM1))));
328+
LogHeap("libkiwi (MEM1)", MemoryMgr::GetInstance().GetHeap(EMemory_MEM1));
329+
LogHeap("libkiwi (MEM2)", MemoryMgr::GetInstance().GetHeap(EMemory_MEM2));
311330

312-
Printf("libkiwi (MEM2): %.2f KB free\n",
313-
OS_MEM_B_TO_KB(static_cast<f32>(
314-
MemoryMgr::GetInstance().GetFreeSize(EMemory_MEM2))));
331+
LogHeap("EGG (MEM1)", RPSysSystem::getRootHeapMem1());
332+
LogHeap("EGG (MEM2)", RPSysSystem::getRootHeapMem2());
333+
LogHeap("EGG (System)", RPSysSystem::getSystemHeap());
315334

316-
Printf("RPSysSystem (System): %.2f KB free\n",
317-
OS_MEM_B_TO_KB(static_cast<f32>(
318-
RPSysSystem::getSystemHeap()->getAllocatableSize())));
335+
LogHeap("RPSysSystem (System)",
336+
RP_GET_INSTANCE(RPSysSystem)->getSystemHeapRP());
337+
LogHeap("RPSysSystem (Resource)",
338+
RP_GET_INSTANCE(RPSysSystem)->getResourceHeap());
319339

320-
Printf("RPSysSystem (Resource): %.2f KB free\n",
321-
OS_MEM_B_TO_KB(static_cast<f32>(RP_GET_INSTANCE(RPSysSystem)
322-
->getResourceHeap()
323-
->getAllocatableSize())));
340+
#if defined(PACK_RESORT)
341+
LogHeap("RPSysSystem (RootScene)",
342+
RP_GET_INSTANCE(RPSysSystem)->getRootSceneHeap());
343+
#endif
324344

325345
Printf("\n");
326346
}

lib/libkiwi/k_config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
#define K_TYPEOF(x) __typeof__(x)
6464

6565
// 'decltype'
66-
#ifndef __INTELLISENSE__
66+
#if !defined(__INTELLISENSE__)
6767
#define K_DECLTYPE(x) __decltype__(x)
6868
#else
6969
#define K_DECLTYPE(x) decltype(x)

0 commit comments

Comments
 (0)