Skip to content

Commit e041480

Browse files
committed
Heap Info - Use a single allocation for the memory usage strings
1 parent a0c02b7 commit e041480

File tree

7 files changed

+39
-68
lines changed

7 files changed

+39
-68
lines changed

ttyd-tools/rel/include/global.h

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -734,26 +734,20 @@ struct OnScreenTimerDisplayFrameCounter
734734
struct HeapInfoDisplay
735735
{
736736
#define MEMORY_USAGE_LINE_BUFFER_SIZE 64
737+
int32_t ArrayCount;
737738
bool *DisplayHeapInfo;
738-
char **MemoryUsageBuffer;
739+
char *MemoryUsageBuffer;
739740
char HeapCorruptionBuffer[512];
740741

741742
HeapInfoDisplay()
742743
{
743-
int32_t NumHeaps = gc::OSAlloc::NumHeaps;
744+
int32_t NumHeaps = gc::OSAlloc::NumHeaps + 1; // Add one for the smart heap
745+
ArrayCount = NumHeaps;
746+
DisplayHeapInfo = new bool[NumHeaps];
744747

745-
DisplayHeapInfo = new bool[NumHeaps + 1]; // Add one for the smart heap
746-
747-
// Add one for the smart heap, subtract 1 for not displaying the free portion of the smart heap
748-
int32_t MemoryUsageArrays = ((NumHeaps + 1) * 2) - 1;
749-
750-
char **tempMemoryUsageBuffer = new char *[MemoryUsageArrays];
751-
MemoryUsageBuffer = tempMemoryUsageBuffer;
752-
753-
for (int32_t i = 0; i < MemoryUsageArrays; i++)
754-
{
755-
tempMemoryUsageBuffer[i] = new char[MEMORY_USAGE_LINE_BUFFER_SIZE];
756-
}
748+
// Subtract 1 for not displaying the free portion of the smart heap
749+
int32_t MemoryUsageArrays = (NumHeaps * 2) - 1;
750+
MemoryUsageBuffer = new char[MemoryUsageArrays * MEMORY_USAGE_LINE_BUFFER_SIZE];
757751
}
758752

759753
~HeapInfoDisplay()
@@ -765,20 +759,6 @@ struct HeapInfoDisplay
765759

766760
if (MemoryUsageBuffer)
767761
{
768-
int32_t NumHeaps = gc::OSAlloc::NumHeaps;
769-
770-
// Add one for the smart heap, subtract 1 for not displaying the free portion of the smart heap
771-
int32_t MemoryUsageArrays = ((NumHeaps + 1) * 2) - 1;
772-
773-
char **tempMemoryUsageBuffer = MemoryUsageBuffer;
774-
for (int32_t i = 0; i < MemoryUsageArrays; i++)
775-
{
776-
if (tempMemoryUsageBuffer[i])
777-
{
778-
delete[] (tempMemoryUsageBuffer[i]);
779-
}
780-
}
781-
782762
delete[] (MemoryUsageBuffer);
783763
}
784764
}

ttyd-tools/rel/include/main.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace mod {
66

77
void fixRoomProblems();
88
void clearHeapCorruptionBuffer();
9-
void clearMemoryUsageBuffers();
9+
void clearMemoryUsageBuffer();
1010

1111
}

ttyd-tools/rel/include/menufunctions.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ double getStickAngle(int32_t stickXYOut[2]); // If the X and Y values are not wa
100100
void getStickAngleString(char *stringOut);
101101
char *getTimeString(char *stringOut, int64_t time);
102102
void updateOnScreenTimerVars();
103-
int32_t getTotalHeaps();
104103

105104
// void getButtonsPressedDynamic(uint8_t *buttonArrayOut, uint32_t currentButtonCombo);
106105
void getButtonsPressed(uint8_t *buttonArrayOut, uint32_t currentButtonCombo);

ttyd-tools/rel/source/draw.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,13 +3434,13 @@ void drawDisplaysMemoryUsageMenu()
34343434

34353435
// Draw each option for displaying memory usage about each heap
34363436
bool *DisplayHeapInfo = HeapInfo.DisplayHeapInfo;
3437-
int32_t TotalHeaps = getTotalHeaps() - 1; // Remove the smart heap from the total
3437+
int32_t NumHeaps = HeapInfo.ArrayCount - 1; // Remove the smart heap from the total
34383438

34393439
int32_t tempCurrentMenuOption = static_cast<int32_t>(MenuVar.CurrentMenuOption);
34403440
char *tempDisplayBuffer = DisplayBuffer;
34413441
const char *String;
34423442

3443-
for (int32_t i = 0; i < TotalHeaps; i++)
3443+
for (int32_t i = 0; i < NumHeaps; i++)
34443444
{
34453445
// Draw each heap number
34463446
sprintf(tempDisplayBuffer,
@@ -3458,12 +3458,12 @@ void drawDisplaysMemoryUsageMenu()
34583458
}
34593459

34603460
// Draw the smart heap text
3461-
bool CurrentOptionCheck = tempCurrentMenuOption == TotalHeaps;
3461+
bool CurrentOptionCheck = tempCurrentMenuOption == NumHeaps;
34623462
Color = getSelectedTextColor(CurrentOptionCheck);
34633463
drawText("Smart Heap", PosX, PosY, Alpha, Color, Scale);
34643464

34653465
// Draw the bool for the smart heap
3466-
getOnOffTextAndColor(DisplayHeapInfo[TotalHeaps], &String, &Color);
3466+
getOnOffTextAndColor(DisplayHeapInfo[NumHeaps], &String, &Color);
34673467
drawText(String, PosX + 120, PosY, Alpha, Color, Scale);
34683468
}
34693469

@@ -4795,10 +4795,10 @@ void drawMemoryUsage()
47954795
PosY -= 20;
47964796
}
47974797

4798-
char **tempMemoryUsageBuffer = HeapInfo.MemoryUsageBuffer;
4798+
char *tempMemoryUsageBuffer = HeapInfo.MemoryUsageBuffer;
47994799
bool *DisplayHeapInfo = HeapInfo.DisplayHeapInfo;
48004800

4801-
int32_t NumHeaps = getTotalHeaps() - 1; // Remove the smart heap from the total
4801+
int32_t NumHeaps = HeapInfo.ArrayCount - 1; // Remove the smart heap from the total
48024802
uint32_t MemoryUsageCounter = 0;
48034803

48044804
// Draw the text for the main heaps
@@ -4808,15 +4808,17 @@ void drawMemoryUsage()
48084808
if (DisplayHeapInfo[i])
48094809
{
48104810
// Draw the used and free text
4811-
if (tempMemoryUsageBuffer[MemoryUsageCounter][0] != '\0')
4811+
uint32_t MemoryUsageBufferIndex = MemoryUsageCounter * MEMORY_USAGE_LINE_BUFFER_SIZE;
4812+
if (tempMemoryUsageBuffer[MemoryUsageBufferIndex] != '\0')
48124813
{
4813-
drawText(tempMemoryUsageBuffer[MemoryUsageCounter], PosX, PosY, Alpha, Color, Scale);
4814+
drawText(&tempMemoryUsageBuffer[MemoryUsageBufferIndex], PosX, PosY, Alpha, Color, Scale);
48144815
PosY -= 20;
48154816
}
48164817

4817-
if (tempMemoryUsageBuffer[MemoryUsageCounter + 1][0] != '\0')
4818+
MemoryUsageBufferIndex += MEMORY_USAGE_LINE_BUFFER_SIZE;
4819+
if (tempMemoryUsageBuffer[MemoryUsageBufferIndex] != '\0')
48184820
{
4819-
drawText(tempMemoryUsageBuffer[MemoryUsageCounter + 1], PosX, PosY, Alpha, Color, Scale);
4821+
drawText(&tempMemoryUsageBuffer[MemoryUsageBufferIndex], PosX, PosY, Alpha, Color, Scale);
48204822
PosY -= 20;
48214823
}
48224824
}
@@ -4827,14 +4829,15 @@ void drawMemoryUsage()
48274829
if (DisplayHeapInfo[NumHeaps])
48284830
{
48294831
// Draw the used text
4830-
if (tempMemoryUsageBuffer[MemoryUsageCounter][0] != '\0')
4832+
if (tempMemoryUsageBuffer[MemoryUsageCounter] != '\0')
48314833
{
4832-
drawText(tempMemoryUsageBuffer[MemoryUsageCounter], PosX, PosY, Alpha, Color, Scale);
4834+
uint32_t MemoryUsageBufferIndex = MemoryUsageCounter * MEMORY_USAGE_LINE_BUFFER_SIZE;
4835+
drawText(&tempMemoryUsageBuffer[MemoryUsageBufferIndex], PosX, PosY, Alpha, Color, Scale);
48334836
}
48344837
}
48354838

48364839
// Clear each of the memory usage buffers
4837-
clearMemoryUsageBuffers();
4840+
clearMemoryUsageBuffer();
48384841
}
48394842

48404843
void drawNpcNameToPtrError()

ttyd-tools/rel/source/main.cpp

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -467,18 +467,13 @@ void clearHeapCorruptionBuffer()
467467
clearMemory(HeapInfo.HeapCorruptionBuffer, sizeof(HeapInfo.HeapCorruptionBuffer));
468468
}
469469

470-
void clearMemoryUsageBuffers()
470+
void clearMemoryUsageBuffer()
471471
{
472-
int32_t TotalHeaps = gc::OSAlloc::NumHeaps;
472+
int32_t ArrayCount = HeapInfo.ArrayCount;
473473

474-
// Add one for the smart heap, subtract 1 for not displaying the free portion of the smart heap
475-
int32_t MemoryUsageArrays = ((TotalHeaps + 1) * 2) - 1;
476-
477-
char **tempMemoryUsageBuffer = HeapInfo.MemoryUsageBuffer;
478-
for (int32_t i = 0; i < MemoryUsageArrays; i++)
479-
{
480-
clearMemory(tempMemoryUsageBuffer[i], MEMORY_USAGE_LINE_BUFFER_SIZE);
481-
}
474+
// Subtract 1 for not displaying the free portion of the smart heap
475+
int32_t MemoryUsageArrays = (ArrayCount * 2) - 1;
476+
clearMemory(HeapInfo.MemoryUsageBuffer, MemoryUsageArrays * MEMORY_USAGE_LINE_BUFFER_SIZE);
482477
}
483478

484479
void addTextToHeapCorruptionBuffer(char *text)
@@ -615,7 +610,7 @@ void handleStandardHeapChunkResults(void *addressWithError,
615610
Chunks);
616611

617612
// Add the text to the memory usage buffer
618-
strncpy(HeapInfo.MemoryUsageBuffer[memoryUsageBufferIndex],
613+
strncpy(&HeapInfo.MemoryUsageBuffer[memoryUsageBufferIndex * MEMORY_USAGE_LINE_BUFFER_SIZE],
619614
tempDisplayBuffer, MEMORY_USAGE_LINE_BUFFER_SIZE - 1);
620615
}
621616
}
@@ -676,7 +671,7 @@ void handleSmartHeapChunkResults(void *addressWithError,
676671
Chunks);
677672

678673
// Add the text to the memory usage buffer
679-
strncpy(HeapInfo.MemoryUsageBuffer[memoryUsageBufferIndex],
674+
strncpy(&HeapInfo.MemoryUsageBuffer[memoryUsageBufferIndex * MEMORY_USAGE_LINE_BUFFER_SIZE],
680675
tempDisplayBuffer, MEMORY_USAGE_LINE_BUFFER_SIZE - 1);
681676
}
682677
}
@@ -685,16 +680,16 @@ void checkHeaps()
685680
{
686681
// Clear the heap buffers to be safe
687682
clearHeapCorruptionBuffer();
688-
clearMemoryUsageBuffers();
683+
clearMemoryUsageBuffer();
689684

690685
void *AddressWithError;
691686
uint32_t MemoryUsageCounter = 0;
692687

693688
// Check the standard heaps
694689
gc::OSAlloc::HeapInfo *HeapArray = gc::OSAlloc::HeapArray;
695-
int32_t TotalHeaps = gc::OSAlloc::NumHeaps;
690+
int32_t NumHeaps = gc::OSAlloc::NumHeaps;
696691

697-
for (int32_t i = 0; i < TotalHeaps; i++)
692+
for (int32_t i = 0; i < NumHeaps; i++)
698693
{
699694
const gc::OSAlloc::HeapInfo *heap = &HeapArray[i];
700695

@@ -735,12 +730,12 @@ void checkHeaps()
735730
// Draw the memory usage details
736731
// Make sure at least one heap is being drawn
737732
bool *DisplayHeapInfo = HeapInfo.DisplayHeapInfo;
738-
for (int32_t i = 0; i <= TotalHeaps; i++)
733+
for (int32_t i = 0; i <= NumHeaps; i++)
739734
{
740735
if (DisplayHeapInfo[i])
741736
{
742737
drawFunctionOnDebugLayerWithOrder(drawMemoryUsage, 5.f);
743-
return;
738+
break;
744739
}
745740
}
746741
}

ttyd-tools/rel/source/menu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2608,7 +2608,7 @@ void menuCheckButton()
26082608
case A:
26092609
{
26102610
// Make sure the selected option is valid
2611-
if (tempCurrentMenuOption >= static_cast<uint32_t>(getTotalHeaps()))
2611+
if (tempCurrentMenuOption >= static_cast<uint32_t>(HeapInfo.ArrayCount))
26122612
{
26132613
break;
26142614
}

ttyd-tools/rel/source/menufunctions.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4195,12 +4195,6 @@ void updateOnScreenTimerVars()
41954195
OnScreenTimer.PreviousFrameTime = CurrentFrameTime;
41964196
}
41974197

4198-
int32_t getTotalHeaps()
4199-
{
4200-
int32_t TotalHeaps = gc::OSAlloc::NumHeaps;
4201-
return TotalHeaps + 1; // Add one to include the smart heap at the end
4202-
}
4203-
42044198
/*void getButtonsPressedDynamic(uint8_t *buttonArrayOut, uint32_t currentButtonCombo)
42054199
{
42064200
uint32_t Counter = 0;
@@ -4981,7 +4975,7 @@ void adjustBattlesStatusSelection(uint32_t button)
49814975

49824976
void adjustDisplaysMemoryUsageSelection(uint32_t button)
49834977
{
4984-
uint32_t TotalMenuOptions = static_cast<uint32_t>(getTotalHeaps());
4978+
uint32_t TotalMenuOptions = static_cast<uint32_t>(HeapInfo.ArrayCount);
49854979
uint32_t MaxOptionsPerRow = 1;
49864980
uint32_t MaxOptionsPerPage = TotalMenuOptions;
49874981
uint8_t tempPage[1];

0 commit comments

Comments
 (0)