mirror of
https://gitlab.com/dosowisko.net/libsuperderpy.git
synced 2025-02-01 02:56:43 +01:00
garbage collector for util functions returning strings that are usually used as function params
because why not :D
This commit is contained in:
parent
0e159a40c1
commit
6bf5fc493b
5 changed files with 37 additions and 4 deletions
|
@ -169,3 +169,24 @@ SYMBOL_INTERNAL void GamestateProgress(struct Game *game) {
|
|||
game->_priv.cur_gamestate.t = al_get_time();
|
||||
}
|
||||
}
|
||||
|
||||
SYMBOL_INTERNAL void* AddGarbage(struct Game *game, void* data) {
|
||||
if (!game->_priv.garbage) {
|
||||
game->_priv.garbage = malloc(sizeof(struct libsuperderpy_list));
|
||||
game->_priv.garbage->data = data;
|
||||
game->_priv.garbage->next = NULL;
|
||||
} else {
|
||||
struct libsuperderpy_list *garbage = malloc(sizeof(struct libsuperderpy_list));
|
||||
garbage->next = game->_priv.garbage;
|
||||
garbage->data = data;
|
||||
game->_priv.garbage = garbage;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
SYMBOL_INTERNAL void ClearGarbage(struct Game *game) {
|
||||
while (game->_priv.garbage) {
|
||||
free(game->_priv.garbage->data);
|
||||
game->_priv.garbage = game->_priv.garbage->next;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,5 +48,7 @@ void Console_Load(struct Game *game);
|
|||
void Console_Unload(struct Game *game);
|
||||
void SetupViewport(struct Game *game);
|
||||
void GamestateProgress(struct Game *game);
|
||||
void* AddGarbage(struct Game *game, void* data);
|
||||
void ClearGarbage(struct Game *game);
|
||||
|
||||
#endif
|
||||
|
|
|
@ -66,6 +66,8 @@ SYMBOL_EXPORT struct Game* libsuperderpy_init(int argc, char** argv, const char*
|
|||
game->_priv.font_bsod = NULL;
|
||||
game->_priv.console = NULL;
|
||||
|
||||
game->_priv.garbage = NULL;
|
||||
|
||||
game->config.fullscreen = atoi(GetConfigOptionDefault(game, "SuperDerpy", "fullscreen", "1"));
|
||||
game->config.music = atoi(GetConfigOptionDefault(game, "SuperDerpy", "music", "10"));
|
||||
game->config.voice = atoi(GetConfigOptionDefault(game, "SuperDerpy", "voice", "10"));
|
||||
|
@ -404,6 +406,7 @@ SYMBOL_EXPORT int libsuperderpy_run(struct Game *game) {
|
|||
EventGamestates(game, &ev);
|
||||
}
|
||||
}
|
||||
ClearGarbage(game);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -39,6 +39,11 @@
|
|||
|
||||
struct Gamestate;
|
||||
|
||||
struct libsuperderpy_list {
|
||||
void *data;
|
||||
struct libsuperderpy_list *next;
|
||||
};
|
||||
|
||||
/*! \brief Main struct of the game. */
|
||||
struct Game {
|
||||
ALLEGRO_DISPLAY *display; /*!< Main Allegro display. */
|
||||
|
@ -104,6 +109,8 @@ struct Game {
|
|||
int loaded, toLoad;
|
||||
} cur_gamestate;
|
||||
|
||||
struct libsuperderpy_list *garbage;
|
||||
|
||||
} _priv; /*!< Private resources. Do not use in gamestates! */
|
||||
|
||||
bool shuttingdown; /*!< If true then shut down of the game is pending. */
|
||||
|
|
|
@ -217,7 +217,7 @@ SYMBOL_EXPORT char* GetGameName(struct Game *game, char* format) {
|
|||
// FIXME: that's not how you program in C!
|
||||
char *result = malloc(sizeof(char)*255);
|
||||
snprintf(result, 255, format, game->name);
|
||||
return result;
|
||||
return AddGarbage(game, result);
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT char* GetDataFilePath(struct Game *game, char* filename) {
|
||||
|
@ -229,7 +229,7 @@ SYMBOL_EXPORT char* GetDataFilePath(struct Game *game, char* filename) {
|
|||
char *result = 0;
|
||||
|
||||
if (al_filename_exists(filename)) {
|
||||
return strdup(filename);
|
||||
return AddGarbage(game, strdup(filename));
|
||||
}
|
||||
|
||||
{
|
||||
|
@ -237,7 +237,7 @@ SYMBOL_EXPORT char* GetDataFilePath(struct Game *game, char* filename) {
|
|||
strcat(origfn, filename);
|
||||
|
||||
if (al_filename_exists(origfn)) {
|
||||
return strdup(origfn);
|
||||
return AddGarbage(game, strdup(origfn));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -254,7 +254,7 @@ SYMBOL_EXPORT char* GetDataFilePath(struct Game *game, char* filename) {
|
|||
FatalError(game, true, "Could not find data file: %s!", filename);
|
||||
exit(1);
|
||||
}
|
||||
return result;
|
||||
return AddGarbage(game, result);
|
||||
}
|
||||
|
||||
SYMBOL_EXPORT void PrintConsole(struct Game *game, char* format, ...) {
|
||||
|
|
Loading…
Reference in a new issue