diff --git a/src/dsdhacked.c b/src/dsdhacked.c index 07e841eb..57d87fa7 100644 --- a/src/dsdhacked.c +++ b/src/dsdhacked.c @@ -20,187 +20,192 @@ #include "doomtype.h" #include "info.h" -#include "i_system.h" // I_Realloc +#include "m_array.h" // // States // -state_t* states; +state_t *states = NULL; int num_states; -byte* defined_codeptr_args; -statenum_t* seenstate_tab; -actionf_t* deh_codeptr; +byte *defined_codeptr_args = NULL; +statenum_t *seenstate_tab = NULL; +actionf_t *deh_codeptr = NULL; static void InitStates(void) { - int i; + states = original_states; + num_states = NUMSTATES; - num_states = NUMSTATES; + array_grow(seenstate_tab, num_states); + memset(seenstate_tab, 0, num_states * sizeof(*seenstate_tab)); - states = original_states; + array_grow(deh_codeptr, num_states); + for (int i = 0; i < num_states; i++) + { + deh_codeptr[i] = states[i].action; + } - seenstate_tab = calloc(num_states, sizeof(*seenstate_tab)); - - deh_codeptr = malloc(num_states * sizeof(*deh_codeptr)); - for (i = 0; i < num_states; i++) - deh_codeptr[i] = states[i].action; - - defined_codeptr_args = calloc(num_states, sizeof(*defined_codeptr_args)); + array_grow(defined_codeptr_args, num_states); + memset(defined_codeptr_args, 0, num_states * sizeof(*defined_codeptr_args)); } static void FreeStates(void) { - free(defined_codeptr_args); - free(deh_codeptr); + array_free(defined_codeptr_args); + array_free(deh_codeptr); } void dsdh_EnsureStatesCapacity(int limit) { - int i; - static boolean first_allocation = true; + if (limit < num_states) + { + return; + } - while (limit >= num_states) - { - int old_num_states = num_states; - - num_states *= 2; + const int old_num_states = num_states; + static boolean first_allocation = true; if (first_allocation) { - first_allocation = false; - states = malloc(num_states * sizeof(*states)); - memcpy(states, original_states, old_num_states * sizeof(*states)); + states = NULL; + array_grow(states, old_num_states + limit); + memcpy(states, original_states, old_num_states * sizeof(*states)); + first_allocation = false; } else { - states = I_Realloc(states, num_states * sizeof(*states)); + array_grow(states, limit); } - memset(states + old_num_states, 0, (num_states - old_num_states) * sizeof(*states)); - deh_codeptr = I_Realloc(deh_codeptr, num_states * sizeof(*deh_codeptr)); - memset(deh_codeptr + old_num_states, 0, (num_states - old_num_states) * sizeof(*deh_codeptr)); + num_states = array_capacity(states); + const int size_diff = num_states - old_num_states; + memset(states + old_num_states, 0, size_diff * sizeof(*states)); - defined_codeptr_args = - I_Realloc(defined_codeptr_args, num_states * sizeof(*defined_codeptr_args)); - memset(defined_codeptr_args + old_num_states, 0, - (num_states - old_num_states) * sizeof(*defined_codeptr_args)); + array_grow(deh_codeptr, num_states); + memset(deh_codeptr + old_num_states, 0, size_diff * sizeof(*deh_codeptr)); - seenstate_tab = I_Realloc(seenstate_tab, num_states * sizeof(*seenstate_tab)); - memset(seenstate_tab + old_num_states, 0, - (num_states - old_num_states) * sizeof(*seenstate_tab)); + array_grow(defined_codeptr_args, num_states); + memset(defined_codeptr_args + old_num_states, 0, size_diff * sizeof(*defined_codeptr_args)); - for (i = old_num_states; i < num_states; ++i) + array_grow(seenstate_tab, num_states); + memset(seenstate_tab + old_num_states, 0, size_diff * sizeof(*seenstate_tab)); + + for (int i = old_num_states; i < num_states; ++i) { - states[i].sprite = SPR_TNT1; - states[i].tics = -1; - states[i].nextstate = i; + states[i].sprite = SPR_TNT1; + states[i].tics = -1; + states[i].nextstate = i; } - } } // // Sprites // -char** sprnames; +char **sprnames = NULL; int num_sprites; -static char** deh_spritenames; -static int deh_spritenames_size; -static byte* sprnames_state; +static char **deh_spritenames = NULL; +static byte *sprnames_state = NULL; static void InitSprites(void) { - int i; + sprnames = original_sprnames; + num_sprites = NUMSPRITES; - sprnames = original_sprnames; + array_grow(deh_spritenames, num_sprites); + for (int i = 0; i < num_sprites; i++) + { + deh_spritenames[i] = strdup(sprnames[i]); + } - num_sprites = NUMSPRITES; - - deh_spritenames_size = num_sprites + 1; - deh_spritenames = malloc(deh_spritenames_size * sizeof(*deh_spritenames)); - for (i = 0; i < num_sprites; i++) - deh_spritenames[i] = strdup(sprnames[i]); - deh_spritenames[num_sprites] = NULL; - - sprnames_state = calloc(num_sprites, sizeof(*sprnames_state)); + array_grow(sprnames_state, num_sprites); + memset(sprnames_state, 0, num_sprites * sizeof(*sprnames_state)); } static void EnsureSpritesCapacity(int limit) { - static boolean first_allocation = true; + if (limit < num_sprites) + { + return; + } - while (limit >= num_sprites) - { - int old_num_sprites = num_sprites; - - num_sprites *= 2; + const int old_num_sprites = num_sprites; + static boolean first_allocation = true; if (first_allocation) { - first_allocation = false; - sprnames = malloc(num_sprites * sizeof(*sprnames)); - memcpy(sprnames, original_sprnames, old_num_sprites * sizeof(*sprnames)); + sprnames = NULL; + array_grow(sprnames, old_num_sprites + limit); + memcpy(sprnames, original_sprnames, old_num_sprites * sizeof(*sprnames)); + first_allocation = false; } else { - sprnames = I_Realloc(sprnames, num_sprites * sizeof(*sprnames)); + array_grow(sprnames, limit); } - memset(sprnames + old_num_sprites, 0, (num_sprites - old_num_sprites) * sizeof(*sprnames)); - sprnames_state = I_Realloc(sprnames_state, num_sprites * sizeof(*sprnames_state)); - memset(sprnames_state + old_num_sprites, 0, - (num_sprites - old_num_sprites) * sizeof(*sprnames_state)); - } + num_sprites = array_capacity(sprnames); + const int size_diff = num_sprites - old_num_sprites; + memset(sprnames + old_num_sprites, 0, size_diff * sizeof(*sprnames)); + + array_grow(sprnames_state, num_sprites); + memset(sprnames_state + old_num_sprites, 0, size_diff * sizeof(*sprnames_state)); } static void FreeSprites(void) { - int i; - - for (i = 0; i < deh_spritenames_size; i++) - { - if (deh_spritenames[i]) - free(deh_spritenames[i]); - } - free(deh_spritenames); - free(sprnames_state); + for (int i = 0; i < array_capacity(deh_spritenames); i++) + { + if (deh_spritenames[i]) + { + free(deh_spritenames[i]); + } + } + array_free(deh_spritenames); + array_free(sprnames_state); } int dsdh_GetDehSpriteIndex(const char* key) { - int i; - - for (i = 0; i < num_sprites; ++i) - { - if (sprnames[i] && !strncasecmp(sprnames[i], key, 4) && !sprnames_state[i]) + for (int i = 0; i < num_sprites; ++i) { - sprnames_state[i] = true; // sprite has been edited - return i; + if (sprnames[i] && !strncasecmp(sprnames[i], key, 4) && !sprnames_state[i]) + { + sprnames_state[i] = true; // sprite has been edited + return i; + } } - } - return -1; + return -1; } int dsdh_GetOriginalSpriteIndex(const char* key) { - int i; - const char* c; + int i; + const char* c; - for (i = 0; deh_spritenames[i]; ++i) - if (!strncasecmp(deh_spritenames[i], key, 4)) - return i; + for (i = 0; i < array_capacity(deh_spritenames); ++i) + { + if (deh_spritenames[i] && !strncasecmp(deh_spritenames[i], key, 4)) + { + return i; + } + } - // is it a number? - for (c = key; *c; c++) - if (!isdigit(*c)) - return -1; + // is it a number? + for (c = key; *c; c++) + { + if (!isdigit(*c)) + { + return -1; + } + } - i = atoi(key); - EnsureSpritesCapacity(i); + i = atoi(key); + EnsureSpritesCapacity(i); - return i; + return i; } // @@ -208,160 +213,159 @@ int dsdh_GetOriginalSpriteIndex(const char* key) // #include "sounds.h" -sfxinfo_t* S_sfx; +sfxinfo_t *S_sfx = NULL; int num_sfx; -static char** deh_soundnames; -static int deh_soundnames_size; -static byte* sfx_state; +static char **deh_soundnames = NULL; +static byte *sfx_state = NULL; static void InitSFX(void) { - int i; + S_sfx = original_S_sfx; + num_sfx = NUMSFX; - S_sfx = original_S_sfx; + array_grow(deh_soundnames, num_sfx); + for (int i = 1; i < num_sfx; i++) + { + deh_soundnames[i] = S_sfx[i].name ? strdup(S_sfx[i].name) : NULL; + } - num_sfx = NUMSFX; - - deh_soundnames_size = num_sfx + 1; - deh_soundnames = malloc(deh_soundnames_size * sizeof(*deh_soundnames)); - for (i = 1; i < num_sfx; i++) - if (S_sfx[i].name != NULL) - deh_soundnames[i] = strdup(S_sfx[i].name); - else - deh_soundnames[i] = NULL; - deh_soundnames[0] = NULL; - deh_soundnames[num_sfx] = NULL; - - sfx_state = calloc(num_sfx, sizeof(*sfx_state)); + array_grow(sfx_state, num_sfx); + memset(sfx_state, 0, num_sfx * sizeof(*sfx_state)); } static void FreeSFX(void) { - int i; - - for (i = 1; i < deh_soundnames_size; i++) - { - if (deh_soundnames[i]) - free(deh_soundnames[i]); - } - free(deh_soundnames); - free(sfx_state); + for (int i = 1; i < array_capacity(deh_soundnames); i++) + { + if (deh_soundnames[i]) + { + free(deh_soundnames[i]); + } + } + array_free(deh_soundnames); + array_free(sfx_state); } void dsdh_EnsureSFXCapacity(int limit) { - int i; - static int first_allocation = true; + if (limit < num_sfx) + { + return; + } - while (limit >= num_sfx) - { - int old_num_sfx = num_sfx; - - num_sfx *= 2; + const int old_num_sfx = num_sfx; + static int first_allocation = true; if (first_allocation) { - first_allocation = false; - S_sfx = malloc(num_sfx * sizeof(*S_sfx)); - memcpy(S_sfx, original_S_sfx, old_num_sfx * sizeof(*S_sfx)); + S_sfx = NULL; + array_grow(S_sfx, old_num_sfx + limit); + memcpy(S_sfx, original_S_sfx, old_num_sfx * sizeof(*S_sfx)); + first_allocation = false; } else { - S_sfx = I_Realloc(S_sfx, num_sfx * sizeof(*S_sfx)); + array_grow(S_sfx, limit); } - memset(S_sfx + old_num_sfx, 0, (num_sfx - old_num_sfx) * sizeof(*S_sfx)); - sfx_state = I_Realloc(sfx_state, num_sfx * sizeof(*sfx_state)); - memset(sfx_state + old_num_sfx, 0, - (num_sfx - old_num_sfx) * sizeof(*sfx_state)); + num_sfx = array_capacity(S_sfx); + const int size_diff = num_sfx - old_num_sfx; + memset(S_sfx + old_num_sfx, 0, size_diff * sizeof(*S_sfx)); - for (i = old_num_sfx; i < num_sfx; ++i) + array_grow(sfx_state, num_sfx); + memset(sfx_state + old_num_sfx, 0, size_diff * sizeof(*sfx_state)); + + for (int i = old_num_sfx; i < num_sfx; ++i) { - S_sfx[i].priority = 127; - S_sfx[i].pitch = -1; - S_sfx[i].volume = -1; - S_sfx[i].lumpnum = -1; + S_sfx[i].priority = 127; + S_sfx[i].pitch = -1; + S_sfx[i].volume = -1; + S_sfx[i].lumpnum = -1; } - } } int dsdh_GetDehSFXIndex(const char* key, size_t length) { - int i; - - for (i = 1; i < num_sfx; ++i) - { - if (S_sfx[i].name && - strlen(S_sfx[i].name) == length && - !strncasecmp(S_sfx[i].name, key, length) && - !sfx_state[i]) + for (int i = 1; i < num_sfx; ++i) { - sfx_state[i] = true; // sfx has been edited - return i; + if (S_sfx[i].name && + strlen(S_sfx[i].name) == length && + !strncasecmp(S_sfx[i].name, key, length) && + !sfx_state[i]) + { + sfx_state[i] = true; // sfx has been edited + return i; + } } - } - return -1; + return -1; } int dsdh_GetOriginalSFXIndex(const char* key) { - int i; - const char* c; + int i; + const char* c; - for (i = 1; deh_soundnames[i]; ++i) - if (!strncasecmp(deh_soundnames[i], key, 6)) - return i; + for (i = 1; i < array_capacity(deh_soundnames); ++i) + { + if (deh_soundnames[i] && !strncasecmp(deh_soundnames[i], key, 6)) + { + return i; + } + } - // is it a number? - for (c = key; *c; c++) - if (!isdigit(*c)) - return -1; + // is it a number? + for (c = key; *c; c++) + { + if (!isdigit(*c)) + { + return -1; + } + } - i = atoi(key); - dsdh_EnsureSFXCapacity(i); + i = atoi(key); + dsdh_EnsureSFXCapacity(i); - return i; + return i; } // // Music // -musicinfo_t *S_music; +musicinfo_t *S_music = NULL; int num_music; -static byte *music_state; +static byte *music_state = NULL; static void InitMusic(void) { - S_music = original_S_music; - num_music = NUMMUSIC; + S_music = original_S_music; + num_music = NUMMUSIC; - music_state = calloc(num_music, sizeof(*music_state)); + array_grow(music_state, num_music); + memset(music_state, 0, num_music * sizeof(*music_state)); } int dsdh_GetDehMusicIndex(const char* key, int length) { - int i; - - for (i = 1; i < num_music; ++i) - { - if (S_music[i].name && - strlen(S_music[i].name) == length && - !strncasecmp(S_music[i].name, key, length) && - !music_state[i]) + for (int i = 1; i < num_music; ++i) { - music_state[i] = true; // music has been edited - return i; + if (S_music[i].name && + strlen(S_music[i].name) == length && + !strncasecmp(S_music[i].name, key, length) && + !music_state[i]) + { + music_state[i] = true; // music has been edited + return i; + } } - } - return -1; + return -1; } static void FreeMusic(void) { - free(music_state); + array_free(music_state); } // @@ -369,65 +373,65 @@ static void FreeMusic(void) // #include "p_map.h" // MELEERANGE -mobjinfo_t* mobjinfo; +mobjinfo_t* mobjinfo = NULL; int num_mobj_types; static void InitMobjInfo(void) { - num_mobj_types = NUMMOBJTYPES; - - mobjinfo = original_mobjinfo; + mobjinfo = original_mobjinfo; + num_mobj_types = NUMMOBJTYPES; } void dsdh_EnsureMobjInfoCapacity(int limit) { - int i; - static boolean first_allocation = true; + if (limit < num_mobj_types) + { + return; + } - while (limit >= num_mobj_types) - { - int old_num_mobj_types = num_mobj_types; - - num_mobj_types *= 2; + const int old_num_mobj_types = num_mobj_types; + static boolean first_allocation = true; if (first_allocation) { - first_allocation = false; - mobjinfo = malloc(num_mobj_types * sizeof(*mobjinfo)); - memcpy(mobjinfo, original_mobjinfo, old_num_mobj_types * sizeof(*mobjinfo)); + mobjinfo = NULL; + array_grow(mobjinfo, old_num_mobj_types + limit); + memcpy(mobjinfo, original_mobjinfo, old_num_mobj_types * sizeof(*mobjinfo)); + first_allocation = false; } else { - mobjinfo = I_Realloc(mobjinfo, num_mobj_types * sizeof(*mobjinfo)); + array_grow(mobjinfo, limit); } - memset(mobjinfo + old_num_mobj_types, 0, - (num_mobj_types - old_num_mobj_types) * sizeof(*mobjinfo)); - for (i = old_num_mobj_types; i < num_mobj_types; ++i) + num_mobj_types = array_capacity(mobjinfo); + memset(mobjinfo + old_num_mobj_types, 0, + (num_mobj_types - old_num_mobj_types) * sizeof(*mobjinfo)); + + for (int i = old_num_mobj_types; i < num_mobj_types; ++i) { - mobjinfo[i].droppeditem = MT_NULL; - mobjinfo[i].infighting_group = IG_DEFAULT; - mobjinfo[i].projectile_group = PG_DEFAULT; - mobjinfo[i].splash_group = SG_DEFAULT; - mobjinfo[i].altspeed = NO_ALTSPEED; - mobjinfo[i].meleerange = MELEERANGE; + mobjinfo[i].droppeditem = MT_NULL; + mobjinfo[i].infighting_group = IG_DEFAULT; + mobjinfo[i].projectile_group = PG_DEFAULT; + mobjinfo[i].splash_group = SG_DEFAULT; + mobjinfo[i].altspeed = NO_ALTSPEED; + mobjinfo[i].meleerange = MELEERANGE; } - } } void dsdh_InitTables(void) { - InitStates(); - InitSprites(); - InitSFX(); - InitMusic(); - InitMobjInfo(); + InitStates(); + InitSprites(); + InitSFX(); + InitMusic(); + InitMobjInfo(); } void dsdh_FreeTables(void) { - FreeStates(); - FreeSprites(); - FreeSFX(); - FreeMusic(); + FreeStates(); + FreeSprites(); + FreeSFX(); + FreeMusic(); } diff --git a/src/m_array.c b/src/m_array.c index d32e3533..b6d700fd 100644 --- a/src/m_array.c +++ b/src/m_array.c @@ -27,16 +27,21 @@ struct buffer_s #define ARR_PTR(v) \ ((struct buffer_s *)((char *)(v) - offsetof(struct buffer_s, buffer))) -size_t array_size(void *v) +unsigned int array_size(void *v) { - return v ? ARR_PTR((v))->size : 0; + return v ? ARR_PTR(v)->size : 0; +} + +unsigned int array_capacity(void *v) +{ + return v ? ARR_PTR(v)->capacity : 0; } void array_clear(void *v) { if (v) { - ARR_PTR((v))->size = 0; + ARR_PTR(v)->size = 0; } } diff --git a/src/m_array.h b/src/m_array.h index 8df78778..8617eb01 100644 --- a/src/m_array.h +++ b/src/m_array.h @@ -21,9 +21,10 @@ void *M_ArrayIncreaseCapacity(void *v, size_t esize, size_t n); void *M_ArrayIncreaseSize(void *v, size_t esize); -size_t array_size(void *v); -void array_clear(void *v); -void array_free(void *v); +unsigned int array_size(void *v); +unsigned int array_capacity(void *v); +void array_clear(void *v); +void array_free(void *v); #define array_grow(v, n) \ ((v) = M_ArrayIncreaseCapacity((v), sizeof(*(v)), n))