Skip to content

Commit

Permalink
memory: refactor memory allocators
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Nov 16, 2021
1 parent 295c7c1 commit aed3d26
Show file tree
Hide file tree
Showing 31 changed files with 411 additions and 378 deletions.
226 changes: 114 additions & 112 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -62,144 +62,146 @@ if python3.found()
endif

sources = [
'src/filesystem.c',
'src/global/lib.c',
'src/global/vars_platform.c',
'src/global/vars.c',
'src/main.c',
'src/json.c',
'src/specific/ati.c',
'src/specific/file.c',
'src/specific/clock.c',
'src/specific/output.c',
'src/specific/music.c',
'src/specific/sound.c',
'src/specific/input.c',
'src/specific/shell.c',
'src/specific/hwr.c',
'src/specific/smain.c',
'src/specific/display.c',
'src/specific/frontend.c',
'src/specific/init.c',
'src/inject.c',
'src/log.c',
'src/3dsystem/3d_gen.c',
'src/3dsystem/phd_math.c',
'src/3dsystem/scalespr.c',
'src/3dsystem/3d_gen.c',
'src/args.c',
'src/config.c',
'src/game/option.c',
'src/game/laraswim.c',
'src/game/inventry.c',
'src/game/objects/cabin.c',
'src/game/objects/pickup.c',
'src/game/objects/door.c',
'src/game/objects/puzzle_hole.c',
'src/game/objects/keyhole.c',
'src/game/objects/misc.c',
'src/game/objects/boat.c',
'src/game/objects/bridge.c',
'src/game/objects/trapdoor.c',
'src/game/objects/switch.c',
'src/game/objects/cog.c',
'src/game/objects/scion.c',
'src/game/objects/savegame_crystal.c',
'src/game/objects/gunshot.c',
'src/game/lot.c',
'src/game/camera.c',
'src/game/collide.c',
'src/game/music.c',
'src/game/demo.c',
'src/game/sound.c',
'src/game/text.c',
'src/game/hair.c',
'src/game/sphere.c',
'src/game/larafire.c',
'src/game/pause.c',
'src/game/items.c',
'src/game/control.c',
'src/game/larasurf.c',
'src/game/lara.c',
'src/game/invvars.c',
'src/game/lara1gun.c',
'src/game/lara2gun.c',
'src/game/ai/pod.c',
'src/game/ai/skate_kid.c',
'src/filesystem.c',
'src/game/ai/abortion.c',
'src/game/ai/alligator.c',
'src/game/ai/ape.c',
'src/game/ai/baldy.c',
'src/game/ai/bat.c',
'src/game/ai/bear.c',
'src/game/ai/centaur.c',
'src/game/ai/cowboy.c',
'src/game/ai/crocodile.c',
'src/game/ai/dino.c',
'src/game/ai/evil_lara.c',
'src/game/ai/larson.c',
'src/game/ai/lion.c',
'src/game/ai/mummy.c',
'src/game/ai/mutant.c',
'src/game/ai/natla.c',
'src/game/ai/pierre.c',
'src/game/ai/pod.c',
'src/game/ai/raptor.c',
'src/game/ai/rat.c',
'src/game/ai/bat.c',
'src/game/ai/cowboy.c',
'src/game/ai/skate_kid.c',
'src/game/ai/statue.c',
'src/game/ai/natla.c',
'src/game/ai/mummy.c',
'src/game/ai/mutant.c',
'src/game/ai/wolf.c',
'src/game/ai/bear.c',
'src/game/ai/evil_lara.c',
'src/game/ai/abortion.c',
'src/game/ai/vole.c',
'src/game/ai/ape.c',
'src/game/ai/pierre.c',
'src/game/ai/dino.c',
'src/game/ai/crocodile.c',
'src/game/ai/baldy.c',
'src/game/ai/alligator.c',
'src/game/invfunc.c',
'src/game/savegame.c',
'src/game/overlay.c',
'src/game/effects/twinkle.c',
'src/game/effects/flood.c',
'src/game/ai/wolf.c',
'src/game/box.c',
'src/game/camera.c',
'src/game/cinema.c',
'src/game/collide.c',
'src/game/control.c',
'src/game/demo.c',
'src/game/draw.c',
'src/game/effects/blood.c',
'src/game/effects/body_part.c',
'src/game/effects/bubble.c',
'src/game/effects/chain_block.c',
'src/game/effects/dino_stomp.c',
'src/game/effects/earthquake.c',
'src/game/effects/explosion.c',
'src/game/effects/finish_level.c',
'src/game/effects/flicker.c',
'src/game/effects/turn_180.c',
'src/game/effects/flipmap.c',
'src/game/effects/flood.c',
'src/game/effects/lara_effects.c',
'src/game/effects/waterfall.c',
'src/game/effects/splash.c',
'src/game/effects/explosion.c',
'src/game/effects/ricochet.c',
'src/game/effects/earthquake.c',
'src/game/effects/dino_stomp.c',
'src/game/effects/missile.c',
'src/game/effects/powerup.c',
'src/game/effects/raising_block.c',
'src/game/effects/flipmap.c',
'src/game/effects/ricochet.c',
'src/game/effects/sand.c',
'src/game/effects/finish_level.c',
'src/game/effects/blood.c',
'src/game/effects/chain_block.c',
'src/game/effects/powerup.c',
'src/game/effects/body_part.c',
'src/game/effects/missile.c',
'src/game/effects/bubble.c',
'src/game/effects/splash.c',
'src/game/effects/stairs2slope.c',
'src/game/draw.c',
'src/game/laramisc.c',
'src/game/effects/turn_180.c',
'src/game/effects/twinkle.c',
'src/game/effects/waterfall.c',
'src/game/game.c',
'src/game/requester.c',
'src/game/gamebuf.c',
'src/game/gameflow.c',
'src/game/hair.c',
'src/game/inventry.c',
'src/game/invfunc.c',
'src/game/invvars.c',
'src/game/items.c',
'src/game/lara.c',
'src/game/lara1gun.c',
'src/game/lara2gun.c',
'src/game/larafire.c',
'src/game/laramisc.c',
'src/game/larasurf.c',
'src/game/laraswim.c',
'src/game/lot.c',
'src/game/music.c',
'src/game/objects/boat.c',
'src/game/objects/bridge.c',
'src/game/objects/cabin.c',
'src/game/objects/cog.c',
'src/game/objects/door.c',
'src/game/objects/gunshot.c',
'src/game/objects/keyhole.c',
'src/game/objects/misc.c',
'src/game/objects/pickup.c',
'src/game/objects/puzzle_hole.c',
'src/game/objects/savegame_crystal.c',
'src/game/objects/scion.c',
'src/game/objects/switch.c',
'src/game/objects/trapdoor.c',
'src/game/option.c',
'src/game/overlay.c',
'src/game/pause.c',
'src/game/people.c',
'src/game/requester.c',
'src/game/savegame.c',
'src/game/settings.c',
'src/game/setup.c',
'src/game/cinema.c',
'src/game/traps/teeth_trap.c',
'src/game/traps/spikes.c',
'src/game/traps/falling_block.c',
'src/game/traps/movable_block.c',
'src/game/sound.c',
'src/game/sphere.c',
'src/game/text.c',
'src/game/traps/damocles_sword.c',
'src/game/traps/dart.c',
'src/game/traps/lava.c',
'src/game/traps/thors_hammer.c',
'src/game/traps/midas_touch.c',
'src/game/traps/lightning_emitter.c',
'src/game/traps/rolling_ball.c',
'src/game/traps/flame.c',
'src/game/traps/dart_emitter.c',
'src/game/traps/damocles_sword.c',
'src/game/traps/falling_block.c',
'src/game/traps/falling_ceiling.c',
'src/game/traps/flame.c',
'src/game/traps/lava.c',
'src/game/traps/lightning_emitter.c',
'src/game/traps/midas_touch.c',
'src/game/traps/movable_block.c',
'src/game/traps/pendulum.c',
'src/game/traps/rolling_ball.c',
'src/game/traps/rolling_block.c',
'src/game/box.c',
'src/game/people.c',
'src/inject_util.c',
'src/game/traps/spikes.c',
'src/game/traps/teeth_trap.c',
'src/game/traps/thors_hammer.c',
'src/global/lib.c',
'src/global/vars.c',
'src/global/vars_platform.c',
'src/init.c',
'src/inject.c',
'src/inject_util.c',
'src/json.c',
'src/log.c',
'src/main.c',
'src/memory.c',
'src/specific/ati.c',
'src/specific/clock.c',
'src/specific/display.c',
'src/specific/file.c',
'src/specific/frontend.c',
'src/specific/hwr.c',
'src/specific/init.c',
'src/specific/input.c',
'src/specific/music.c',
'src/specific/output.c',
'src/specific/shell.c',
'src/specific/smain.c',
'src/specific/sound.c',
version_res,
]

Expand Down
5 changes: 3 additions & 2 deletions src/args.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "args.h"

#include "log.h"
#include "memory.h"

#include <windows.h>
#include <shellapi.h>
Expand All @@ -17,11 +18,11 @@ int get_command_line(char ***args, int *arg_count)
return 0;
}

*args = malloc(l_arg_count * sizeof(char **));
*args = Memory_Alloc(l_arg_count * sizeof(char **));
*arg_count = l_arg_count;
for (int i = 0; i < l_arg_count; i++) {
size_t size = wcslen(l_arg_list[i]) + 1;
(*args)[i] = malloc(size);
(*args)[i] = Memory_Alloc(size);
wcstombs((*args)[i], l_arg_list[i], size);
}

Expand Down
11 changes: 3 additions & 8 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
#include "global/vars.h"
#include "json.h"
#include "log.h"
#include "memory.h"

#include <stdlib.h>
#include <string.h>
#include <windows.h>

Expand Down Expand Up @@ -194,12 +194,7 @@ int8_t T1MReadConfig()

size_t cfg_data_size = FileSize(fp);

cfg_data = malloc(cfg_data_size + 1);
if (!cfg_data) {
LOG_ERROR("Failed to allocate memory");
result = T1MReadConfigFromJson("");
goto cleanup;
}
cfg_data = Memory_Alloc(cfg_data_size + 1);
FileRead(cfg_data, 1, cfg_data_size, fp);
cfg_data[cfg_data_size] = '\0';
FileClose(fp);
Expand All @@ -224,7 +219,7 @@ int8_t T1MReadConfig()
FileClose(fp);
}
if (cfg_data) {
free(cfg_data);
Memory_Free(cfg_data);
}
return result;
}
12 changes: 5 additions & 7 deletions src/filesystem.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
#include "filesystem.h"

#include "memory.h"

#include <stdio.h>
#include <stdlib.h>

struct MYFILE {
FILE *fp;
};

MYFILE *FileOpen(const char *path, FILE_OPEN_MODE mode)
{
MYFILE *file = malloc(sizeof(MYFILE));
if (!file) {
return NULL;
}
MYFILE *file = Memory_Alloc(sizeof(MYFILE));
switch (mode) {
case FILE_OPEN_WRITE:
file->fp = fopen(path, "wb");
Expand All @@ -25,7 +23,7 @@ MYFILE *FileOpen(const char *path, FILE_OPEN_MODE mode)
break;
}
if (!file->fp) {
free(file);
Memory_Free(file);
return NULL;
}
return file;
Expand Down Expand Up @@ -68,7 +66,7 @@ size_t FileSize(MYFILE *file)
void FileClose(MYFILE *file)
{
fclose(file->fp);
free(file);
Memory_Free(file);
}

int FileDelete(const char *path)
Expand Down
4 changes: 2 additions & 2 deletions src/game/ai/mummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include "game/box.h"
#include "game/collide.h"
#include "game/control.h"
#include "game/gamebuf.h"
#include "game/items.h"
#include "global/vars.h"
#include "specific/init.h"

void SetupMummy(OBJECT_INFO *obj)
{
Expand All @@ -28,7 +28,7 @@ void InitialiseMummy(int16_t item_num)
ITEM_INFO *item = &Items[item_num];
item->touch_bits = 0;
item->mesh_bits = 0xFFFF87FF;
item->data = game_malloc(sizeof(int16_t), GBUF_MUMMY_HEAD_TURN);
item->data = GameBuf_Alloc(sizeof(int16_t), GBUF_CREATURE_DATA);
*(int16_t *)item->data = 0;
}

Expand Down
4 changes: 2 additions & 2 deletions src/game/ai/pod.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#include "game/collide.h"
#include "game/control.h"
#include "game/effects/body_part.h"
#include "game/gamebuf.h"
#include "game/items.h"
#include "game/lot.h"
#include "game/sound.h"
#include "global/vars.h"
#include "specific/init.h"

void SetupPod(OBJECT_INFO *obj)
{
Expand Down Expand Up @@ -69,7 +69,7 @@ void InitialisePod(int16_t item_num)

InitialiseItem(bug_item_num);

item->data = game_malloc(sizeof(int16_t), 0);
item->data = GameBuf_Alloc(sizeof(int16_t), GBUF_CREATURE_DATA);
*(int16_t *)item->data = bug_item_num;

LevelItemCount++;
Expand Down
Loading

0 comments on commit aed3d26

Please sign in to comment.