Skip to content

Commit e3ba97d

Browse files
committed
Use the smart heap in createCustomState and deleteCustomState to prevent fragmentation
Also added allocFromSmartHeap in global.cpp, and made loadSettings in memcard.cpp use it.
1 parent b4f93ef commit e3ba97d

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

ttyd-tools/rel/include/commonfunctions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <gc/OSModule.h>
44
#include <ttyd/seqdrv.h>
55
#include <ttyd/npcdrv.h>
6+
#include <ttyd/memory.h>
67
#include <ttyd/party.h>
78

89
#include <cstdint>
@@ -41,6 +42,7 @@ bool compareStringsSize(const char *str1, const char *str2, size_t size);
4142
bool compareStringToNextMap(const char *str);
4243
void setSeqMapChange(const char *map, const char *bero);
4344
void *clearMemory(void *destination, std::size_t size);
45+
ttyd::memory::SmartAllocationData *allocFromSmartHeap(uint32_t size, uint32_t group);
4446
void *getPartnerPointer();
4547
void *getFollowerPointer();
4648
void removePartnerFromOverworld();

ttyd-tools/rel/include/global.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,10 @@ struct ManageCustomStates
10571057
bool StateWasSelected;
10581058
uint8_t SelectedState;
10591059

1060-
// Do not inline createCustomState nor deleteCustomState
1060+
// Do not inline resizeStateMemory, createCustomState, nor deleteCustomState
1061+
CustomStateStruct *resizeStateMemory(
1062+
CustomStateStruct *state, uint32_t numEntries, int32_t incrementAmount);
1063+
10611064
char *createCustomState();
10621065
uint32_t deleteCustomState(uint32_t stateIndex);
10631066
};

ttyd-tools/rel/source/commonfunctions.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <gc/DEMOPad.h>
44
#include <gc/OSModule.h>
5+
#include <gc/OSCache.h>
56
#include <ttyd/system.h>
67
#include <ttyd/seqdrv.h>
78
#include <ttyd/mariost.h>
@@ -229,6 +230,22 @@ void *clearMemory(void *destination, std::size_t size)
229230
return memset(destination, 0, size);
230231
}
231232

233+
ttyd::memory::SmartAllocationData *allocFromSmartHeap(uint32_t size, uint32_t group)
234+
{
235+
// Allocate the memory
236+
ttyd::memory::SmartAllocationData *SmartData = ttyd::memory::smartAlloc(size, group);
237+
238+
// Set up a temporary local variable to use for getting memory
239+
void *Ptr = SmartData->pMemory;
240+
241+
// Clear the memory, as this is not done automatically
242+
Ptr = clearMemory(Ptr, size);
243+
244+
// Be 100% certain that no memory issues occur
245+
gc::OSCache::DCFlushRange(Ptr, size);
246+
return SmartData;
247+
}
248+
232249
void *getPartnerPointer()
233250
{
234251
ttyd::party::PartySlotId Id = ttyd::mario_party::marioGetPartyId();

ttyd-tools/rel/source/global.cpp

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#include "memcard.h"
33

44
#include <ttyd/msgdrv.h>
5+
#include <ttyd/memory.h>
56
#include <ttyd/mario_pouch.h>
6-
#include <ttyd/mariost.h>
77
#include <ttyd/mario.h>
88
#include <ttyd/mario_motion.h>
99
#include <ttyd/seq_mapchange.h>
@@ -2264,6 +2264,37 @@ void SetCustomText::customTextInit(const char *initialText, uint32_t maxTextSize
22642264
}
22652265
}
22662266

2267+
CustomStateStruct * ManageCustomStates::resizeStateMemory(
2268+
CustomStateStruct *state, uint32_t numEntries, int32_t incrementAmount)
2269+
{
2270+
uint32_t CurrentStatesSize = sizeof(CustomStateStruct) * numEntries;
2271+
2272+
// Allocate temporary memory to hold the states
2273+
// Allocate the memory on the smart heap to avoid fragmentation
2274+
ttyd::memory::SmartAllocationData *SmartData =
2275+
allocFromSmartHeap(CurrentStatesSize,
2276+
ttyd::memory::SmartAllocationGroup::kNone);
2277+
2278+
// Set up a temporary local variable to use for getting the memory
2279+
CustomStateStruct *tempStateSmart = reinterpret_cast<CustomStateStruct *>(SmartData->pMemory);
2280+
2281+
// Copy the states from the old memory to the temporary memory
2282+
memcpy(tempStateSmart, state, CurrentStatesSize);
2283+
2284+
// Free the old memory
2285+
delete[] (state);
2286+
2287+
// Allocate new memory to hold the states
2288+
state = new CustomStateStruct[numEntries + incrementAmount];
2289+
2290+
// Copy the states from the temporary memory to the new memory
2291+
memcpy(state, tempStateSmart, CurrentStatesSize);
2292+
2293+
// Free the temporary memory
2294+
ttyd::memory::smartFree(SmartData);
2295+
return state;
2296+
}
2297+
22672298
char *ManageCustomStates::createCustomState()
22682299
{
22692300
uint32_t tempTotalEntries = TotalEntries;
@@ -2290,16 +2321,7 @@ char *ManageCustomStates::createCustomState()
22902321
}
22912322
else
22922323
{
2293-
// Allocate new memory to hold the new state
2294-
CustomStateStruct *tempStateNew = new CustomStateStruct[tempTotalEntries + 1];
2295-
2296-
// Copy the states from the old memory to the new memory
2297-
memcpy(tempStateNew, tempState, sizeof(CustomStateStruct) * tempTotalEntries);
2298-
2299-
// Free the old memory
2300-
delete[] (tempState);
2301-
2302-
tempState = tempStateNew;
2324+
tempState = resizeStateMemory(tempState, tempTotalEntries, 1);
23032325
State = tempState;
23042326

23052327
tempTotalEntries++;
@@ -2430,16 +2452,7 @@ uint32_t ManageCustomStates::deleteCustomState(uint32_t stateIndex)
24302452
memcpy(&tempState[stateIndex], &tempState[stateIndex + 1], CopySize);
24312453
}
24322454

2433-
// Allocate new memory to hold the states
2434-
CustomStateStruct *tempStateNew = new CustomStateStruct[tempTotalEntries];
2435-
2436-
// Copy the states from the old memory to the new memory
2437-
uint32_t NewSize = sizeof(CustomStateStruct) * tempTotalEntries;
2438-
memcpy(tempStateNew, tempState, NewSize);
2439-
2440-
// Free the old memory
2441-
delete[] (tempState);
2442-
State = tempStateNew;
2455+
State = resizeStateMemory(tempState, tempTotalEntries, 0);
24432456
}
24442457
else
24452458
{

ttyd-tools/rel/source/memcard.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "commonfunctions.h"
44

55
#include <gc/card.h>
6-
#include <gc/OSCache.h>
76
#include <ttyd/cardmgr.h>
87
#include <ttyd/memory.h>
98

@@ -499,15 +498,11 @@ int32_t loadSettings(int32_t memoryCardSlot)
499498
// Set up the memory to be copied from the file
500499
// Allocate the memory on the smart heap to avoid fragmentation
501500
ttyd::memory::SmartAllocationData *SmartData =
502-
ttyd::memory::smartAlloc(FileSizeAdjusted,
501+
allocFromSmartHeap(FileSizeAdjusted,
503502
ttyd::memory::SmartAllocationGroup::kNone);
504503

505-
// Set up a temporary local variable to use for getting memory
504+
// Set up a temporary local variable to use for getting the memory
506505
char *MiscData = reinterpret_cast<char *>(SmartData->pMemory);
507-
clearMemory(MiscData, FileSizeAdjusted);
508-
509-
// Be 100% certain that no memory issues occur, since the allocated memory wasn't cleared automatically
510-
gc::OSCache::DCFlushRange(MiscData, FileSizeAdjusted);
511506

512507
// Get the data from the file
513508
// Must read by the stored size, as the struct size may exceed the size of the file

0 commit comments

Comments
 (0)