2
2
#include " memcard.h"
3
3
4
4
#include < ttyd/msgdrv.h>
5
+ #include < ttyd/memory.h>
5
6
#include < ttyd/mario_pouch.h>
6
- #include < ttyd/mariost.h>
7
7
#include < ttyd/mario.h>
8
8
#include < ttyd/mario_motion.h>
9
9
#include < ttyd/seq_mapchange.h>
@@ -2264,6 +2264,37 @@ void SetCustomText::customTextInit(const char *initialText, uint32_t maxTextSize
2264
2264
}
2265
2265
}
2266
2266
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
+
2267
2298
char *ManageCustomStates::createCustomState ()
2268
2299
{
2269
2300
uint32_t tempTotalEntries = TotalEntries;
@@ -2290,16 +2321,7 @@ char *ManageCustomStates::createCustomState()
2290
2321
}
2291
2322
else
2292
2323
{
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 );
2303
2325
State = tempState;
2304
2326
2305
2327
tempTotalEntries++;
@@ -2430,16 +2452,7 @@ uint32_t ManageCustomStates::deleteCustomState(uint32_t stateIndex)
2430
2452
memcpy (&tempState[stateIndex], &tempState[stateIndex + 1 ], CopySize);
2431
2453
}
2432
2454
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 );
2443
2456
}
2444
2457
else
2445
2458
{
0 commit comments