From bfbbaf9f56a3622d9748251f09e8cd8d56ab3966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustaf=20Alh=C3=A4ll?= <gustaf@hanicef.me> Date: Fri, 14 Jul 2023 13:25:09 +0200 Subject: [PATCH] Make sure all allocations are properly aligned --- src/d_netfil.c | 2 +- src/lua_hudlib_drawlist.c | 6 +++--- src/r_fps.c | 17 ++++++----------- src/w_wad.c | 2 +- src/z_zone.c | 3 ++- src/z_zone.h | 8 ++++---- 6 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/d_netfil.c b/src/d_netfil.c index e60af2c2c2..3fef756812 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -498,7 +498,7 @@ INT32 CL_CheckFiles(void) CONS_Debug(DBG_NETPLAY, "searching for '%s' ", fileneeded[i].filename); // Check in already loaded files - for (j = mainwads; wadfiles[j]; j++) + for (j = mainwads; j < numwadfiles; j++) { nameonly(strcpy(wadfilename, wadfiles[j]->filename)); if (!stricmp(wadfilename, fileneeded[i].filename) && diff --git a/src/lua_hudlib_drawlist.c b/src/lua_hudlib_drawlist.c index f46f207c10..6f83094ac0 100644 --- a/src/lua_hudlib_drawlist.c +++ b/src/lua_hudlib_drawlist.c @@ -103,7 +103,7 @@ huddrawlist_h LUA_HUD_CreateDrawList(void) { huddrawlist_h drawlist; - drawlist = (huddrawlist_h) Z_CallocAlign(sizeof(struct huddrawlist_s), PU_STATIC, NULL, 64); + drawlist = (huddrawlist_h) Z_Calloc(sizeof(struct huddrawlist_s), PU_STATIC, NULL); drawlist->items = NULL; drawlist->items_capacity = 0; drawlist->items_len = 0; @@ -160,7 +160,7 @@ static size_t AllocateDrawItem(huddrawlist_h list) { if (list->items_capacity == 0) list->items_capacity = 128; else list->items_capacity *= 2; - list->items = (drawitem_t *) Z_ReallocAlign(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL, 64); + list->items = (drawitem_t *) Z_Realloc(list->items, sizeof(struct drawitem_s) * list->items_capacity, PU_STATIC, NULL); } return list->items_len++; @@ -179,7 +179,7 @@ static const char *CopyString(huddrawlist_h list, const char* str) { if (list->strbuf_capacity == 0) list->strbuf_capacity = 256; else list->strbuf_capacity *= 2; - list->strbuf = (char*) Z_ReallocAlign(list->strbuf, sizeof(char) * list->strbuf_capacity, PU_STATIC, NULL, 8); + list->strbuf = (char*) Z_Realloc(list->strbuf, sizeof(char) * list->strbuf_capacity, PU_STATIC, NULL); } const char *result = (const char *) &list->strbuf[list->strbuf_len]; strncpy(&list->strbuf[list->strbuf_len], str, lenstr + 1); diff --git a/src/r_fps.c b/src/r_fps.c index 2d30c9f019..6166f04080 100644 --- a/src/r_fps.c +++ b/src/r_fps.c @@ -369,12 +369,11 @@ static void AddInterpolator(levelinterpolator_t* interpolator) levelinterpolators_size *= 2; } - levelinterpolators = Z_ReallocAlign( + levelinterpolators = Z_Realloc( (void*) levelinterpolators, sizeof(levelinterpolator_t*) * levelinterpolators_size, PU_LEVEL, - NULL, - sizeof(levelinterpolator_t*) * 8 + NULL ); } @@ -384,11 +383,8 @@ static void AddInterpolator(levelinterpolator_t* interpolator) static levelinterpolator_t *CreateInterpolator(levelinterpolator_type_e type, thinker_t *thinker) { - levelinterpolator_t *ret = (levelinterpolator_t*) Z_CallocAlign( - sizeof(levelinterpolator_t), - PU_LEVEL, - NULL, - sizeof(levelinterpolator_t) * 8 + levelinterpolator_t *ret = (levelinterpolator_t*) Z_Calloc( + sizeof(levelinterpolator_t), PU_LEVEL, NULL ); ret->type = type; @@ -703,12 +699,11 @@ void R_AddMobjInterpolator(mobj_t *mobj) interpolated_mobjs_capacity *= 2; } - interpolated_mobjs = Z_ReallocAlign( + interpolated_mobjs = Z_Realloc( interpolated_mobjs, sizeof(mobj_t *) * interpolated_mobjs_capacity, PU_LEVEL, - NULL, - 64 + NULL ); } diff --git a/src/w_wad.c b/src/w_wad.c index 456afef7b9..b13dc8cc1f 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -972,7 +972,7 @@ UINT16 W_InitFile(const char *filename, boolean mainfile, boolean startup) // add the wadfile // CONS_Printf(M_GetText("Added file %s (%u lumps)\n"), filename, numlumps); - wadfiles = Z_Realloc(wadfiles, sizeof(wadfile_t) * (numwadfiles + 1), PU_STATIC, NULL); + wadfiles = Z_Realloc(wadfiles, sizeof(wadfile_t *) * (numwadfiles + 1), PU_STATIC, NULL); wadfiles[numwadfiles] = wadfile; numwadfiles++; // must come BEFORE W_LoadDehackedLumps, so any addfile called by COM_BufInsertText called by Lua doesn't overwrite what we just loaded diff --git a/src/z_zone.c b/src/z_zone.c index 11c4bcb2c7..d23c43b7b5 100644 --- a/src/z_zone.c +++ b/src/z_zone.c @@ -287,7 +287,8 @@ void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) #endif { - size_t extrabytes = (1<<alignbits) - 1; + I_Assert(alignbits >= 0 && alignbits < (INT32)(sizeof(size_t) * 8)); + size_t extrabytes = ((size_t)1<<alignbits) - 1; size_t padsize = 0; memblock_t *block; void *ptr; diff --git a/src/z_zone.h b/src/z_zone.h index c3cd4f0115..f00f577494 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -101,10 +101,10 @@ void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALL void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2); #endif -// Alloc with no alignment -#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, 0) -#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, 0) -#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, 0) +// Alloc with standard alignment +#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, sizeof(void *)) +#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, sizeof(void *)) +#define Z_Realloc(p,s,t,u) Z_ReallocAlign(p, s, t, u, sizeof(void *)) // Free all memory by tag // these don't give line numbers for ZDEBUG currently though -- GitLab