Some memory allocations have been properly aligned

hopefully without crashes now :p
See: https://git.do.srb2.org/STJr/SRB2/-/merge_requests/2050
This commit is contained in:
alufolie91 committed 2023-07-17 23:14:54 +02:00
1 parent 1c4515f3e3
commit 87d562e83a
5 files changed
+32 -29

No files matched your search

+1 -1
View File
@@ -549,7 +549,7 @@ INT32 CL_CheckFiles(void)
CONS_Debug(DBG_NETPLAY, "searching for '%s' ", fileneeded[i].filename);
// Check in already loaded files
for (j = mainwads+1; wadfiles[j]; j++)
for (j = mainwads+1; j < numwadfiles; j++)
{
nameonly(strcpy(wadfilename, wadfiles[j]->filename));
if (!stricmp(wadfilename, fileneeded[i].filename) &&
+3 -3
View File
@@ -84,7 +84,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;
@@ -145,7 +145,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++;
@@ -164,7 +164,7 @@ static size_t 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);
}
{
+6 -11
View File
@@ -357,12 +357,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
);
}
@@ -372,11 +371,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;
@@ -691,12 +687,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
);
}
+7 -1
View File
@@ -242,7 +242,13 @@ 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 have 0 knowledge about memory alignment but maybe if alignbits is
// something like 32 that would require too much BYTES, so bits amount
// should be divided by 8? Idk, maybe this would break alignment, but at
// least game wouldn't crash with "error allocating 2^32-1 bytes"
INT32 alignbytes = alignbits/8;
I_Assert(alignbytes >= 0 && alignbytes < (INT32)(sizeof(size_t) * 8));
size_t extrabytes = ((size_t)1<<(alignbytes)) - 1;
size_t padsize = 0;
memblock_t *block;
void *ptr;
+15 -13
View File
@@ -76,28 +76,30 @@ void Z_SetUser2(void *ptr, void **newuser, const char *file, INT32 line);
void Z_SetUser2(void *ptr, void **newuser);
#endif
// Z_Free and alloc with alignment
#ifdef ZDEBUG
#define Z_Free(p) Z_Free2(p, __FILE__, __LINE__)
void Z_Free2(void *ptr, const char *file, INT32 line);
#define Z_Malloc(s,t,u) Z_Malloc2(s, t, u, 0, __FILE__, __LINE__)
#define Z_MallocAlign(s,t,u,a) Z_Malloc2(s, t, u, a, __FILE__, __LINE__)
void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
#define Z_Calloc(s,t,u) Z_Calloc2(s, t, u, 0, __FILE__, __LINE__)
#define Z_CallocAlign(s,t,u,a) Z_Calloc2(s, t, u, a, __FILE__, __LINE__)
void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
#define Z_Realloc(p,s,t,u) Z_Realloc2(p, s, t, u, 0, __FILE__, __LINE__)
#define Z_Free(p) Z_Free2(p, __FILE__, __LINE__)
#define Z_MallocAlign(s,t,u,a) Z_Malloc2(s, t, u, a, __FILE__, __LINE__)
#define Z_CallocAlign(s,t,u,a) Z_Calloc2(s, t, u, a, __FILE__, __LINE__)
#define Z_ReallocAlign(p,s,t,u,a) Z_Realloc2(p,s, t, u, a, __FILE__, __LINE__)
void Z_Free2(void *ptr, const char *file, INT32 line);
void *Z_Malloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
void *Z_Calloc2(size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(1);
void *Z_Realloc2(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits, const char *file, INT32 line) FUNCALLOC(2);
#else
void Z_Free(void *ptr);
void *Z_MallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(1);
#define Z_Malloc(s,t,u) Z_MallocAlign(s, t, u, 0)
void *Z_CallocAlign(size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(1);
#define Z_Calloc(s,t,u) Z_CallocAlign(s, t, u, 0)
void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2) ;
#define Z_Realloc(p, s,t,u) Z_ReallocAlign(p, s, t, u, 0)
void *Z_ReallocAlign(void *ptr, size_t size, INT32 tag, void *user, INT32 alignbits) FUNCALLOC(2);
#endif
// 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 *))
size_t Z_TagUsage(INT32 tagnum);
size_t Z_TagsUsage(INT32 lowtag, INT32 hightag);