Skip to content

Commit b623f56

Browse files
committed
loadSettings - Reallocate memory for custom states to avoid fragmentation
1 parent f1440ac commit b623f56

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

ttyd-tools/rel/source/memcard.cpp

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -620,51 +620,66 @@ int32_t loadSettings(int32_t memoryCardSlot)
620620
FrameAdvance.FrameAdvanceButtonCombos.PauseButtonCombo = ButtonCombo;
621621
}
622622

623+
// Get the hit check visualization data
624+
HitCheck.Settings.DrawHits = Settings->HitCheckSettings.DrawHits;
625+
HitCheck.Settings.DrawMisses = Settings->HitCheckSettings.DrawMisses;
626+
627+
// Only get the colors if their alphas are not 0
628+
uint32_t HitsColor = Settings->HitCheckSettings.HitsColor;
629+
if ((HitsColor & 0xFF) != 0)
630+
{
631+
HitCheck.Settings.HitsColor = HitsColor;
632+
}
633+
634+
uint32_t MissesColor = Settings->HitCheckSettings.MissesColor;
635+
if ((MissesColor & 0xFF) != 0)
636+
{
637+
HitCheck.Settings.MissesColor = MissesColor;
638+
}
639+
623640
// Get the custom states
641+
// If there were any previously, then delete them
642+
CustomStateStruct *CustomStates = CustomState.State;
643+
if (CustomStates)
644+
{
645+
delete[] (CustomStates);
646+
CustomState.State = nullptr;
647+
}
648+
624649
uint32_t tempTotalCustomStates = Settings->CustomStateSettings.CustomStateCount;
650+
uint32_t tempTotalCustomStatesSize;
651+
CustomStateStruct *tempState;
652+
625653
if (tempTotalCustomStates > 0)
626654
{
627655
if (tempTotalCustomStates > CUSTOM_STATES_MAX_COUNT)
628656
{
629657
tempTotalCustomStates = CUSTOM_STATES_MAX_COUNT;
630658
}
631659

632-
CustomState.TotalEntries = tempTotalCustomStates;
633-
CustomState.StateWasSelected = false;
634-
635-
CustomStateStruct *tempState = CustomState.State;
636-
if (tempState)
637-
{
638-
delete[] (tempState);
639-
}
640-
641-
tempState = new CustomStateStruct[tempTotalCustomStates];
642-
CustomState.State = tempState;
660+
tempTotalCustomStatesSize = sizeof(CustomStateStruct) * tempTotalCustomStates;
643661

644662
CustomStateStruct *CustomStatesSettings = reinterpret_cast<CustomStateStruct *>(
645663
reinterpret_cast<uint32_t>(Settings) + Settings->CustomStateSettings.OffsetToCustomStates);
646664

647-
memcpy(tempState, CustomStatesSettings, sizeof(CustomStateStruct) * tempTotalCustomStates);
665+
tempState = new CustomStateStruct[tempTotalCustomStates];
666+
memcpy(tempState, CustomStatesSettings, tempTotalCustomStatesSize);
648667
}
649668

650-
// Get the hit check visualization data
651-
HitCheck.Settings.DrawHits = Settings->HitCheckSettings.DrawHits;
652-
HitCheck.Settings.DrawMisses = Settings->HitCheckSettings.DrawMisses;
653-
654-
// Only get the colors if their alphas are not 0
655-
uint32_t HitsColor = Settings->HitCheckSettings.HitsColor;
656-
if ((HitsColor & 0xFF) != 0)
657-
{
658-
HitCheck.Settings.HitsColor = HitsColor;
659-
}
669+
delete[] (MiscData);
660670

661-
uint32_t MissesColor = Settings->HitCheckSettings.MissesColor;
662-
if ((MissesColor & 0xFF) != 0)
671+
// If there are any custom states, reallocate memory for them now to avoid fragmentation
672+
if (tempTotalCustomStates > 0)
663673
{
664-
HitCheck.Settings.MissesColor = MissesColor;
674+
CustomStates = new CustomStateStruct[tempTotalCustomStates];
675+
676+
CustomState.State = CustomStates;
677+
CustomState.TotalEntries = static_cast<uint8_t>(tempTotalCustomStates);
678+
memcpy(CustomStates, tempState, tempTotalCustomStatesSize);
679+
680+
delete[] (tempState);
665681
}
666682

667-
delete[] (MiscData);
668683
return CARD_RESULT_READY;
669684
}
670685

0 commit comments

Comments
 (0)