Skip to content

Commit

Permalink
Merge pull request #1785 from AsparagusEduardo/pret/pr/malloc
Browse files Browse the repository at this point in the history
Removed unnecessary malloc.h macros
  • Loading branch information
GriffinRichards authored Sep 22, 2022
2 parents 36494c1 + 8a68596 commit e74fa7b
Show file tree
Hide file tree
Showing 18 changed files with 49 additions and 52 deletions.
5 changes: 1 addition & 4 deletions gflib/malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
#define GUARD_ALLOC_H

#define HEAP_SIZE 0x1C000
#define malloc Alloc
#define calloc(ct, sz) AllocZeroed((ct) * (sz))
#define free Free

#define FREE_AND_SET_NULL(ptr) \
{ \
free(ptr); \
Free(ptr); \
ptr = NULL; \
}

Expand Down
4 changes: 2 additions & 2 deletions src/battle_pike.c
Original file line number Diff line number Diff line change
Expand Up @@ -1084,7 +1084,7 @@ static u8 GetNextRoomType(void)
}

nextRoomType = roomCandidates[Random() % numRoomCandidates];
free(roomCandidates);
Free(roomCandidates);
if (nextRoomType == PIKE_ROOM_STATUS)
TryInflictRandomStatus();

Expand Down Expand Up @@ -1363,7 +1363,7 @@ static void SetHintedRoom(void)
}

gSaveBlock2Ptr->frontier.pikeHintedRoomType = roomCandidates[Random() % count];
free(roomCandidates);
Free(roomCandidates);
if (gSaveBlock2Ptr->frontier.pikeHintedRoomType == PIKE_ROOM_STATUS && !AtLeastOneHealthyMon())
gSaveBlock2Ptr->frontier.pikeHintedRoomType = PIKE_ROOM_NPC;
if (gSaveBlock2Ptr->frontier.pikeHintedRoomType == PIKE_ROOM_DOUBLE_BATTLE && !AtLeastTwoAliveMons())
Expand Down
8 changes: 4 additions & 4 deletions src/battle_pyramid.c
Original file line number Diff line number Diff line change
Expand Up @@ -1568,7 +1568,7 @@ void GenerateBattlePyramidFloorLayout(u16 *backupMapData, bool8 setPlayerPositio
}
}
RunOnLoadMapScript();
free(floorLayoutOffsets);
Free(floorLayoutOffsets);
}

void LoadBattlePyramidObjectEventTemplates(void)
Expand Down Expand Up @@ -1697,7 +1697,7 @@ static void SetPyramidObjectPositionsUniformly(u8 objType)
} while (!(bits & 4) && TrySetPyramidObjectEventPositionInSquare(objType, floorLayoutOffsets, squareId, objectStartIndex + i));
bits &= 1;
}
free(floorLayoutOffsets);
Free(floorLayoutOffsets);
}

static bool8 SetPyramidObjectPositionsInAndNearSquare(u8 objType, u8 squareId)
Expand Down Expand Up @@ -1760,7 +1760,7 @@ static bool8 SetPyramidObjectPositionsInAndNearSquare(u8 objType, u8 squareId)
r7 &= 1;
}
#ifdef BUGFIX
free(floorLayoutOffsets);
Free(floorLayoutOffsets);
#endif

return (numObjects / 2) > numPlacedObjects;
Expand Down Expand Up @@ -1814,7 +1814,7 @@ static bool8 SetPyramidObjectPositionsNearSquare(u8 objType, u8 squareId)
break;
}
#ifdef BUGFIX
free(floorLayoutOffsets);
Free(floorLayoutOffsets);
#endif

return (numObjects / 2) > numPlacedObjects;
Expand Down
10 changes: 5 additions & 5 deletions src/decoration.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ static void HandleDecorationItemsMenuInput(u8 taskId)
DestroyListMenuTask(tMenuTaskId, &sDecorationsScrollOffset, &sDecorationsCursorPos);
RemoveDecorationWindow(WINDOW_DECORATION_CATEGORIES);
RemoveDecorationItemsOtherWindows();
free(sDecorationItemsMenu);
Free(sDecorationItemsMenu);
sSecretBasePC_SelectedDecorationActions[tDecorationMenuCommand][0](taskId);
break;
}
Expand Down Expand Up @@ -1161,7 +1161,7 @@ static void DecorationItemsMenuAction_Cancel(u8 taskId)
RemoveDecorationItemsScrollIndicators();
RemoveDecorationItemsOtherWindows();
DestroyListMenuTask(tMenuTaskId, NULL, NULL);
free(sDecorationItemsMenu);
Free(sDecorationItemsMenu);
ReinitDecorationCategoriesWindow(taskId);
}

Expand Down Expand Up @@ -2060,13 +2060,13 @@ static u8 AddDecorationIconObjectFromIconTable(u16 tilesTag, u16 paletteTag, u8
palette.data = GetDecorationIconPicOrPalette(decor, 1);
palette.tag = paletteTag;
LoadCompressedSpritePalette(&palette);
template = malloc(sizeof(struct SpriteTemplate));
template = Alloc(sizeof(struct SpriteTemplate));
*template = gItemIconSpriteTemplate;
template->tileTag = tilesTag;
template->paletteTag = paletteTag;
spriteId = CreateSprite(template, 0, 0, 0);
FreeItemIconTemporaryBuffers();
free(template);
Free(template);
return spriteId;
}

Expand Down Expand Up @@ -2105,7 +2105,7 @@ static u8 AddDecorationIconObjectFromObjectEvent(u16 tilesTag, u16 paletteTag, u
template->tileTag = tilesTag;
template->paletteTag = paletteTag;
spriteId = CreateSprite(template, 0, 0, 0);
free(template);
Free(template);
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/diploma.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void CB2_ShowDiploma(void)
ResetPaletteFade();
FreeAllSpritePalettes();
LoadPalette(sDiplomaPalettes, 0, 64);
sDiplomaTilemapPtr = malloc(0x1000);
sDiplomaTilemapPtr = Alloc(0x1000);
InitDiplomaBg();
InitDiplomaWindow();
ResetTempTileDataBuffers();
Expand Down
2 changes: 1 addition & 1 deletion src/easy_chat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ static bool8 InitEasyChatScreenStruct(u8 type, u16 *words, u8 displayedPersonTyp
u8 templateId;
int i;

sEasyChatScreen = malloc(sizeof(*sEasyChatScreen));
sEasyChatScreen = Alloc(sizeof(*sEasyChatScreen));
if (sEasyChatScreen == NULL)
return FALSE;

Expand Down
4 changes: 2 additions & 2 deletions src/event_object_movement.c
Original file line number Diff line number Diff line change
Expand Up @@ -1537,13 +1537,13 @@ u8 CreateObjectGraphicsSprite(u16 graphicsId, void (*callback)(struct Sprite *),
struct Sprite *sprite;
u8 spriteId;

spriteTemplate = malloc(sizeof(struct SpriteTemplate));
spriteTemplate = Alloc(sizeof(struct SpriteTemplate));
CopyObjectGraphicsInfoToSpriteTemplate(graphicsId, callback, spriteTemplate, &subspriteTables);
if (spriteTemplate->paletteTag != TAG_NONE)
LoadObjectEventPalette(spriteTemplate->paletteTag);

spriteId = CreateSprite(spriteTemplate, x, y, subpriority);
free(spriteTemplate);
Free(spriteTemplate);

if (spriteId != MAX_SPRITES && subspriteTables != NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion src/field_region_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static const struct WindowTemplate sFieldRegionMapWindowTemplates[] =
void FieldInitRegionMap(MainCallback callback)
{
SetVBlankCallback(NULL);
sFieldRegionMapHandler = malloc(sizeof(*sFieldRegionMapHandler));
sFieldRegionMapHandler = Alloc(sizeof(*sFieldRegionMapHandler));
sFieldRegionMapHandler->state = 0;
sFieldRegionMapHandler->callback = callback;
SetMainCallback2(MCB2_InitRegionMapRegisters);
Expand Down
12 changes: 6 additions & 6 deletions src/frontier_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ static void Fill1PRecords(struct RankingHall1P *dst, s32 hallFacilityId, s32 lvl
{
s32 i, j;
struct RankingHall1P record1P[HALL_RECORDS_COUNT + 1];
struct PlayerHallRecords *playerHallRecords = calloc(1, sizeof(struct PlayerHallRecords));
struct PlayerHallRecords *playerHallRecords = AllocZeroed(sizeof(struct PlayerHallRecords));
GetPlayerHallRecords(playerHallRecords);

for (i = 0; i < HALL_RECORDS_COUNT; i++)
Expand All @@ -2298,14 +2298,14 @@ static void Fill1PRecords(struct RankingHall1P *dst, s32 hallFacilityId, s32 lvl
record1P[highestId].winStreak = 0;
}

free(playerHallRecords);
Free(playerHallRecords);
}

static void Fill2PRecords(struct RankingHall2P *dst, s32 lvlMode)
{
s32 i, j;
struct RankingHall2P record2P[HALL_RECORDS_COUNT + 1];
struct PlayerHallRecords *playerHallRecords = calloc(1, sizeof(struct PlayerHallRecords));
struct PlayerHallRecords *playerHallRecords = AllocZeroed(sizeof(struct PlayerHallRecords));
GetPlayerHallRecords(playerHallRecords);

for (i = 0; i < HALL_RECORDS_COUNT; i++)
Expand All @@ -2332,7 +2332,7 @@ static void Fill2PRecords(struct RankingHall2P *dst, s32 lvlMode)
record2P[highestId].winStreak = 0;
}

free(playerHallRecords);
Free(playerHallRecords);
}

static void PrintHallRecords(s32 hallFacilityId, s32 lvlMode)
Expand Down Expand Up @@ -2421,7 +2421,7 @@ void ClearRankingHallRecords(void)
void SaveGameFrontier(void)
{
s32 i;
struct Pokemon *monsParty = calloc(PARTY_SIZE, sizeof(struct Pokemon));
struct Pokemon *monsParty = AllocZeroed(sizeof(struct Pokemon) * PARTY_SIZE);

for (i = 0; i < PARTY_SIZE; i++)
monsParty[i] = gPlayerParty[i];
Expand All @@ -2436,7 +2436,7 @@ void SaveGameFrontier(void)
for (i = 0; i < PARTY_SIZE; i++)
gPlayerParty[i] = monsParty[i];

free(monsParty);
Free(monsParty);
}

// Frontier Brain functions.
Expand Down
2 changes: 1 addition & 1 deletion src/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,7 @@ void CB2_LinkError(void)
SetVBlankCallback(VBlankCB_LinkError);
ResetBgsAndClearDma3BusyFlags(0);
InitBgsFromTemplates(0, sLinkErrorBgTemplates, ARRAY_COUNT(sLinkErrorBgTemplates));
sLinkErrorBgTilemapBuffer = tilemapBuffer = malloc(BG_SCREEN_SIZE);
sLinkErrorBgTilemapBuffer = tilemapBuffer = Alloc(BG_SCREEN_SIZE);
SetBgTilemapBuffer(1, tilemapBuffer);
if (InitWindows(sLinkErrorWindowTemplates))
{
Expand Down
4 changes: 2 additions & 2 deletions src/link_rfu_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ void SaveLinkTrainerNames(void)
s32 j;
s32 nextSpace;
s32 connectedTrainerRecordIndices[MAX_RFU_PLAYERS];
struct TrainerNameRecord *newRecords = calloc(ARRAY_COUNT(gSaveBlock1Ptr->trainerNameRecords), sizeof(struct TrainerNameRecord));
struct TrainerNameRecord *newRecords = AllocZeroed(sizeof(gSaveBlock1Ptr->trainerNameRecords));

// Check if we already have a record saved for connected trainers.
for (i = 0; i < GetLinkPlayerCount(); i++)
Expand Down Expand Up @@ -955,7 +955,7 @@ void SaveLinkTrainerNames(void)

// Finalize the new list, and clean up.
memcpy(gSaveBlock1Ptr->trainerNameRecords, newRecords, sizeof(gSaveBlock1Ptr->trainerNameRecords));
free(newRecords);
Free(newRecords);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ void ReadMail(struct Mail *mail, void (*exitCallback)(void), bool8 hasText)
u16 buffer[2];
u16 species;

sMailRead = calloc(1, sizeof(*sMailRead));
sMailRead = AllocZeroed(sizeof(*sMailRead));
sMailRead->language = GAME_LANGUAGE;
sMailRead->international = TRUE;
sMailRead->parserSingle = CopyEasyChatWord;
Expand Down
12 changes: 6 additions & 6 deletions src/record_mixing.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,8 @@ static void Task_RecordMixing_Main(u8 taskId)
switch (tState)
{
case 0: // init
sSentRecord = malloc(sizeof(*sSentRecord));
sReceivedRecords = malloc(sizeof(*sReceivedRecords) * MAX_LINK_PLAYERS);
sSentRecord = Alloc(sizeof(*sSentRecord));
sReceivedRecords = Alloc(sizeof(*sReceivedRecords) * MAX_LINK_PLAYERS);
SetLocalLinkPlayerId(gSpecialVar_0x8005);
VarSet(VAR_TEMP_0, 1);
sReadyToReceive = FALSE;
Expand Down Expand Up @@ -358,8 +358,8 @@ static void Task_RecordMixing_Main(u8 taskId)
case 5: // Wait for the task created by CreateTask_ReestablishCableClubLink
if (!gTasks[tLinkTaskId].isActive)
{
free(sReceivedRecords);
free(sSentRecord);
Free(sReceivedRecords);
Free(sSentRecord);
SetLinkWaitingForScript();
if (gWirelessCommType != 0)
CreateTask(Task_ReturnToFieldRecordMixing, 10);
Expand Down Expand Up @@ -689,7 +689,7 @@ static void ReceiveLilycoveLadyData(LilycoveLady *records, size_t recordSize, u8

if (GetLilycoveLadyId() == 0)
{
lilycoveLady = malloc(sizeof(*lilycoveLady));
lilycoveLady = Alloc(sizeof(*lilycoveLady));
if (lilycoveLady == NULL)
return;

Expand All @@ -705,7 +705,7 @@ static void ReceiveLilycoveLadyData(LilycoveLady *records, size_t recordSize, u8
if (lilycoveLady != NULL)
{
QuizLadyClearQuestionForRecordMix(lilycoveLady);
free(lilycoveLady);
Free(lilycoveLady);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/recorded_battle.c
Original file line number Diff line number Diff line change
Expand Up @@ -475,8 +475,8 @@ bool32 MoveRecordedBattleToSaveData(void)
break;
}

free(battleSave);
free(savSection);
Free(battleSave);
Free(savSection);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion src/region_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ void CB2_OpenFlyMap(void)
SetGpuReg(REG_OFFSET_BG2HOFS, 0);
SetGpuReg(REG_OFFSET_BG3HOFS, 0);
SetGpuReg(REG_OFFSET_BG3VOFS, 0);
sFlyMap = malloc(sizeof(*sFlyMap));
sFlyMap = Alloc(sizeof(*sFlyMap));
if (sFlyMap == NULL)
{
SetMainCallback2(CB2_ReturnToFieldWithOpenMenu);
Expand Down
4 changes: 2 additions & 2 deletions src/secret_base.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ static void Task_ShowSecretBaseRegistryMenu(u8 taskId)
tSelectedRow = 0;
tScrollOffset = 0;
ClearDialogWindowAndFrame(0, FALSE);
sRegistryMenu = calloc(1, sizeof(*sRegistryMenu));
sRegistryMenu = AllocZeroed(sizeof(*sRegistryMenu));
tMainWindowId = AddWindow(&sRegistryWindowTemplates[0]);
BuildRegistryMenuItems(taskId);
FinalizeRegistryMenu(taskId);
Expand Down Expand Up @@ -1006,7 +1006,7 @@ static void HandleRegistryMenuInput(u8 taskId)
ClearWindowTilemap(tMainWindowId);
RemoveWindow(tMainWindowId);
ScheduleBgCopyTilemapToVram(0);
free(sRegistryMenu);
Free(sRegistryMenu);
GoToSecretBasePCRegisterMenu(taskId);
break;
default:
Expand Down
12 changes: 6 additions & 6 deletions src/tv.c
Original file line number Diff line number Diff line change
Expand Up @@ -3450,7 +3450,7 @@ void ReceiveTvShowsData(void *src, u32 size, u8 playersLinkId)
TVShow (*rmBuffer2)[MAX_LINK_PLAYERS][TV_SHOWS_COUNT];
TVShow (*rmBuffer)[MAX_LINK_PLAYERS][TV_SHOWS_COUNT];

rmBuffer2 = malloc(MAX_LINK_PLAYERS * TV_SHOWS_COUNT * sizeof(TVShow));
rmBuffer2 = Alloc(MAX_LINK_PLAYERS * TV_SHOWS_COUNT * sizeof(TVShow));
if (rmBuffer2 != NULL)
{
for (i = 0; i < MAX_LINK_PLAYERS; i++)
Expand Down Expand Up @@ -3488,7 +3488,7 @@ void ReceiveTvShowsData(void *src, u32 size, u8 playersLinkId)
CompactTVShowArray(gSaveBlock1Ptr->tvShows);
DeactivateShowsWithUnseenSpecies();
DeactivateGameCompleteShowsIfNotUnlocked();
free(rmBuffer2);
Free(rmBuffer2);
}
}

Expand Down Expand Up @@ -3835,7 +3835,7 @@ void ReceivePokeNewsData(void *src, u32 size, u8 playersLinkId)
PokeNews (*rmBuffer2)[MAX_LINK_PLAYERS][POKE_NEWS_COUNT];
PokeNews (*rmBuffer)[MAX_LINK_PLAYERS][POKE_NEWS_COUNT];

rmBuffer2 = malloc(MAX_LINK_PLAYERS * POKE_NEWS_COUNT * sizeof(PokeNews));
rmBuffer2 = Alloc(MAX_LINK_PLAYERS * POKE_NEWS_COUNT * sizeof(PokeNews));
if (rmBuffer2 != NULL)
{
for (i = 0; i < MAX_LINK_PLAYERS; i++)
Expand All @@ -3861,7 +3861,7 @@ void ReceivePokeNewsData(void *src, u32 size, u8 playersLinkId)
}
ClearInvalidPokeNews();
ClearPokeNewsIfGameNotComplete();
free(rmBuffer2);
Free(rmBuffer2);
}
}

Expand Down Expand Up @@ -3966,7 +3966,7 @@ static void TranslateShowNames(TVShow *show, u32 language)
int i;
TVShow **shows;

shows = calloc(11, sizeof(TVShow *));
shows = AllocZeroed(sizeof(TVShow *) * 11);
for (i = 0; i < LAST_TVSHOW_IDX; i++)
{
switch (show[i].common.kind)
Expand Down Expand Up @@ -4023,7 +4023,7 @@ static void TranslateShowNames(TVShow *show, u32 language)
break;
}
}
free(shows);
Free(shows);
}

void SanitizeTVShowsForRuby(TVShow *shows)
Expand Down
10 changes: 5 additions & 5 deletions src/union_room.c
Original file line number Diff line number Diff line change
Expand Up @@ -3365,12 +3365,12 @@ static void Task_InitUnionRoom(u8 taskId)
}
break;
case 4:
free(data->spawnPlayer);
free(data->playerList);
free(data->incomingParentList);
free(data->incomingChildList);
Free(data->spawnPlayer);
Free(data->playerList);
Free(data->incomingParentList);
Free(data->incomingChildList);
DestroyTask(data->searchTaskId);
free(sWirelessLinkMain.uRoom);
Free(sWirelessLinkMain.uRoom);
LinkRfu_Shutdown();
DestroyTask(taskId);
break;
Expand Down

0 comments on commit e74fa7b

Please sign in to comment.