diff --git a/inc/system/hunk.h b/inc/system/hunk.h index cc15ef473..e0511810b 100644 --- a/inc/system/hunk.h +++ b/inc/system/hunk.h @@ -25,6 +25,9 @@ typedef struct { size_t mapped; } memhunk_t; +// hunk allocation alignment is rounded to cacheline +#define HUNK_ALIGN 64 + void Hunk_Init(void); void Hunk_Begin(memhunk_t *hunk, size_t maxsize); void *Hunk_TryAlloc(memhunk_t *hunk, size_t size); diff --git a/src/common/bsp.c b/src/common/bsp.c index 4bcbf0d5e..cc8ace78e 100644 --- a/src/common/bsp.c +++ b/src/common/bsp.c @@ -1260,7 +1260,7 @@ static void BSP_LoadBspxNormals(bsp_t* bsp, const byte* in, size_t data_size) static size_t BSP_ParseNormalsHeader(bsp_t* bsp, const byte* in, size_t data_size) { - return data_size; + return data_size + HUNK_ALIGN - 1; // extra memory to account for alignment in ALLOC() } static void BSP_ParseDecoupledLM(bsp_t *bsp, const byte *in, size_t filelen) @@ -1331,7 +1331,7 @@ static size_t BSP_ParseExtensionHeader(bsp_t *bsp, lump_t *out, const byte *buf, break; } if (e->parse_header) - extrasize += e->parse_header(bsp, buf + ofs, len); + extrasize += ALIGN(e->parse_header(bsp, buf + ofs, len), HUNK_ALIGN); // to mirror Hunk_TryAlloc() overallocation out[j].fileofs = ofs; out[j].filelen = len; break; @@ -1432,7 +1432,7 @@ int BSP_Load(const char *name, bsp_t **bsp_p) lump_count[i] = count; // round to cacheline - memsize += ALIGN(count * info->memsize, 64); + memsize += ALIGN(count * info->memsize, HUNK_ALIGN); maxpos = max(maxpos, end); } diff --git a/src/unix/hunk.c b/src/unix/hunk.c index c9fb4a02e..831aa1b6b 100644 --- a/src/unix/hunk.c +++ b/src/unix/hunk.c @@ -52,11 +52,11 @@ void *Hunk_TryAlloc(memhunk_t *hunk, size_t size) { void *buf; - Q_assert(size <= SIZE_MAX - 63); + Q_assert(size <= SIZE_MAX - (HUNK_ALIGN - 1)); Q_assert(hunk->cursize <= hunk->maxsize); // round to cacheline - size = ALIGN(size, 64); + size = ALIGN(size, HUNK_ALIGN); if (size > hunk->maxsize - hunk->cursize) return NULL;