Skip to content

Commit 0024dcb

Browse files
committed
Use fixed array sizes for the heaps
1 parent 4d8a00e commit 0024dcb

File tree

3 files changed

+48
-90
lines changed

3 files changed

+48
-90
lines changed

ttyd-tools/rel/include/global.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ struct WarpByIndexStruct
623623

624624
struct CheckHeaps
625625
{
626-
const char **StandardHeapArray;
627-
const char **SmartHeapArray;
626+
const char *StandardHeapArray[16];
627+
const char *SmartHeapArray[16];
628628
};
629629

630630
extern Menus Menu[27];

ttyd-tools/rel/source/draw.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3861,31 +3861,25 @@ void drawHeapArrayErrors()
38613861
float Scale = 0.6;
38623862

38633863
// Draw the standard heap text
3864-
if (CheckHeap.StandardHeapArray)
3865-
{
3866-
const char **tempHeapArray = CheckHeap.StandardHeapArray;
3867-
uint32_t Counter = 0;
3864+
const char **tempStandardHeapArray = CheckHeap.StandardHeapArray;
3865+
uint32_t Counter = 0;
38683866

3869-
while (tempHeapArray[Counter])
3870-
{
3871-
drawText(tempHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3872-
PosY -= 20;
3873-
Counter++;
3874-
}
3867+
while (tempStandardHeapArray[Counter])
3868+
{
3869+
drawText(tempStandardHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3870+
PosY -= 20;
3871+
Counter++;
38753872
}
38763873

38773874
// Draw the smart heap text
3878-
if (CheckHeap.SmartHeapArray)
3879-
{
3880-
const char **tempHeapArray = CheckHeap.SmartHeapArray;
3881-
uint32_t Counter = 0;
3875+
const char **tempSmartHeapArray = CheckHeap.SmartHeapArray;
3876+
Counter = 0;
38823877

3883-
while (tempHeapArray[Counter])
3884-
{
3885-
drawText(tempHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3886-
PosY -= 20;
3887-
Counter++;
3888-
}
3878+
while (tempSmartHeapArray[Counter])
3879+
{
3880+
drawText(tempSmartHeapArray[Counter], PosX, PosY, Alpha, TextColor, Scale);
3881+
PosY -= 20;
3882+
Counter++;
38893883
}
38903884
}
38913885

ttyd-tools/rel/source/main.cpp

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -382,67 +382,37 @@ uint32_t Mod::pauseArtAttackTimer()
382382
return mPFN_scissor_timer_main_trampoline();
383383
}
384384

385-
void clearHeapArray(const char **&heap)
385+
void clearHeapArray(const char **heap)
386386
{
387-
const char **tempHeapArray = heap;
388-
if (!tempHeapArray)
389-
{
390-
// The array is already cleared
391-
return;
392-
}
393-
394387
uint32_t Counter = 0;
395-
const char *tempHeap = tempHeapArray[Counter];
396-
397-
while (tempHeap)
388+
while (heap[Counter])
398389
{
399390
delete[] heap[Counter];
391+
heap[Counter] = nullptr;
400392
Counter++;
401-
tempHeap = tempHeapArray[Counter];
402393
}
403-
404-
delete[] heap;
405-
heap = nullptr;
406394
}
407395

408-
void addTextToHeapArray(const char **&heap, char *text)
396+
void addTextToHeapArray(const char **heap, char *text)
409397
{
410398
// Get the next available slot in the array
411-
uint32_t FreeSlot = 0;
399+
uint32_t TotalSlots = 16;
400+
int32_t FreeSlot = -1;
412401

413-
if (!heap)
414-
{
415-
heap = new const char *[2]; // Extra slot at the end
416-
clearMemory(heap, 2 * sizeof(const char **));
417-
}
418-
else
402+
for (uint32_t i = 0; i < TotalSlots; i++)
419403
{
420-
const char **tempHeapArray = heap;
421-
const char *tempPtr;
422-
423-
do
424-
{
425-
FreeSlot++;
426-
tempPtr = tempHeapArray[FreeSlot];
427-
}
428-
while (tempPtr);
429-
430-
// Create a new array with a new spot for the next value
431-
uint32_t TotalSlots = FreeSlot + 1;
432-
const char **tempNewHeapArray = new const char *[TotalSlots + 1];
433-
clearMemory(tempNewHeapArray, (TotalSlots + 1) * sizeof(const char **));
434-
435-
// Copy the contents of the old array to the new array
436-
for (uint32_t i = 0; i < FreeSlot; i++)
404+
if (!heap[i])
437405
{
438-
tempNewHeapArray[i] = tempHeapArray[i];
406+
FreeSlot = i;
407+
break;
439408
}
440-
441-
// Delete the old array
442-
delete[] (heap);
443-
444-
// Set the new array as the current array
445-
heap = tempNewHeapArray;
409+
}
410+
411+
// Make sure the new text can be added
412+
if (FreeSlot == -1)
413+
{
414+
// The new text cannot be added
415+
return;
446416
}
447417

448418
// Add the new text at the current free slot
@@ -452,30 +422,24 @@ void addTextToHeapArray(const char **&heap, char *text)
452422
heap[FreeSlot] = NewText;
453423
}
454424

455-
bool checkIfTextAlreadyAdded(const char **&heap, const char *text, uint32_t bytesToCompare)
425+
bool checkIfTextAlreadyAdded(const char **heap, const char *text, uint32_t bytesToCompare)
456426
{
457-
const char **tempHeapArray = heap;
458-
if (tempHeapArray)
427+
uint32_t Counter = 0;
428+
while (heap[Counter])
459429
{
460-
uint32_t Counter = 0;
461-
const char *tempHeapString = tempHeapArray[Counter];
462-
463-
while (tempHeapString)
430+
if (compareStringsSize(heap[Counter], text, bytesToCompare))
464431
{
465-
if (compareStringsSize(tempHeapString, text, bytesToCompare))
466-
{
467-
return true;
468-
}
469-
470-
Counter++;
471-
tempHeapString = tempHeapArray[Counter];
432+
return true;
472433
}
434+
Counter++;
473435
}
474436
return false;
475437
}
476438

477439
void checkHeaps()
478440
{
441+
const char **tempStandardHeapArray = CheckHeap.StandardHeapArray;
442+
const char **tempSmartHeapArray = CheckHeap.SmartHeapArray;
479443
char *tempDisplayBuffer = DisplayBuffer;
480444

481445
// Check the standard heaps
@@ -524,17 +488,17 @@ void checkHeaps()
524488
reinterpret_cast<uint32_t>(currentChunk));
525489

526490
// Only add the current heap once
527-
if (!checkIfTextAlreadyAdded(CheckHeap.StandardHeapArray, tempDisplayBuffer, 16))
491+
if (!checkIfTextAlreadyAdded(tempStandardHeapArray, tempDisplayBuffer, 16))
528492
{
529-
addTextToHeapArray(CheckHeap.StandardHeapArray, tempDisplayBuffer);
493+
addTextToHeapArray(tempStandardHeapArray, tempDisplayBuffer);
530494
}
531495
}
532496
}
533497

534498
if (!ErrorsFound)
535499
{
536500
// No errors were found, so clear the standard heap array
537-
clearHeapArray(CheckHeap.StandardHeapArray);
501+
clearHeapArray(tempStandardHeapArray);
538502
}
539503

540504
// Check the smart heap
@@ -576,19 +540,19 @@ void checkHeaps()
576540
reinterpret_cast<uint32_t>(currentChunk));
577541

578542
// Only add the current heap once
579-
if (!checkIfTextAlreadyAdded(CheckHeap.SmartHeapArray, tempDisplayBuffer, 16))
543+
if (!checkIfTextAlreadyAdded(tempSmartHeapArray, tempDisplayBuffer, 16))
580544
{
581-
addTextToHeapArray(CheckHeap.SmartHeapArray, tempDisplayBuffer);
545+
addTextToHeapArray(tempSmartHeapArray, tempDisplayBuffer);
582546
}
583547
}
584548
else
585549
{
586550
// No errors were found, so clear the smart heap array
587-
clearHeapArray(CheckHeap.SmartHeapArray);
551+
clearHeapArray(tempSmartHeapArray);
588552
}
589553

590554
// Draw any errors that occured
591-
if (CheckHeap.StandardHeapArray || CheckHeap.SmartHeapArray)
555+
if (tempStandardHeapArray[0] || tempSmartHeapArray[0])
592556
{
593557
drawFunctionOnDebugLayer(drawHeapArrayErrors);
594558
}

0 commit comments

Comments
 (0)