From 7a50a0317eb0668536ce96962bcd1a7910d157bb Mon Sep 17 00:00:00 2001 From: Roman Fomin Date: Sun, 8 Sep 2024 11:47:27 +0700 Subject: [PATCH] more efficient use of zone memory features (#1888) * use PU_RENDERER for allocation of renderer buffers * add Z_StrDup function --- src/i_video.c | 1 + src/r_draw.c | 20 +++----------------- src/r_plane.c | 34 ++++++++++------------------------ src/r_things.c | 17 +++++------------ src/st_stuff.c | 7 +------ src/wi_interlvl.c | 26 ++++++-------------------- src/z_zone.c | 11 +++++++++++ src/z_zone.h | 3 +++ 8 files changed, 40 insertions(+), 79 deletions(-) diff --git a/src/i_video.c b/src/i_video.c index 5d87e8be..3925eb45 100644 --- a/src/i_video.c +++ b/src/i_video.c @@ -1835,6 +1835,7 @@ static void CreateSurfaces(int w, int h) SDL_SetTextureScaleMode(texture, SDL_ScaleModeNearest); + Z_FreeTag(PU_RENDERER); R_InitAnyRes(); ST_InitRes(); diff --git a/src/r_draw.c b/src/r_draw.c index 2dc7b69b..dfc6e3a2 100644 --- a/src/r_draw.c +++ b/src/r_draw.c @@ -763,23 +763,9 @@ void R_ShadeScreen(boolean toggle) void R_InitBufferRes(void) { - if (solidcol) - { - Z_Free(solidcol); - } - if (columnofs) - { - Z_Free(columnofs); - } - if (ylookup) - { - Z_Free(ylookup); - } - - columnofs = Z_Malloc(video.width * sizeof(*columnofs), PU_STATIC, NULL); - ylookup = Z_Malloc(video.height * sizeof(*ylookup), PU_STATIC, NULL); - - solidcol = Z_Calloc(1, video.width * sizeof(*solidcol), PU_STATIC, NULL); + columnofs = Z_Malloc(video.width * sizeof(*columnofs), PU_RENDERER, NULL); + ylookup = Z_Malloc(video.height * sizeof(*ylookup), PU_RENDERER, NULL); + solidcol = Z_Calloc(1, video.width * sizeof(*solidcol), PU_RENDERER, NULL); } // diff --git a/src/r_plane.c b/src/r_plane.c index eee31dfb..2666739b 100644 --- a/src/r_plane.c +++ b/src/r_plane.c @@ -113,33 +113,19 @@ void R_InitPlanes (void) void R_InitPlanesRes(void) { - if (floorclip) Z_Free(floorclip); - if (ceilingclip) Z_Free(ceilingclip); - if (spanstart) Z_Free(spanstart); + floorclip = Z_Calloc(1, video.width * sizeof(*floorclip), PU_RENDERER, NULL); + ceilingclip = Z_Calloc(1, video.width * sizeof(*ceilingclip), PU_RENDERER, NULL); + spanstart = Z_Calloc(1, video.height * sizeof(*spanstart), PU_RENDERER, NULL); - if (cachedheight) Z_Free(cachedheight); - if (cacheddistance) Z_Free(cacheddistance); - if (cachedxstep) Z_Free(cachedxstep); - if (cachedystep) Z_Free(cachedystep); + cachedheight = Z_Calloc(1, video.height * sizeof(*cachedheight), PU_RENDERER, NULL); + cacheddistance = Z_Calloc(1, video.height * sizeof(*cacheddistance), PU_RENDERER, NULL); + cachedxstep = Z_Calloc(1, video.height * sizeof(*cachedxstep), PU_RENDERER, NULL); + cachedystep = Z_Calloc(1, video.height * sizeof(*cachedystep), PU_RENDERER, NULL); - if (yslope) Z_Free(yslope); - if (distscale) Z_Free(distscale); + yslope = Z_Calloc(1, video.height * sizeof(*yslope), PU_RENDERER, NULL); + distscale = Z_Calloc(1, video.width * sizeof(*distscale), PU_RENDERER, NULL); - if (openings) Z_Free(openings); - - floorclip = Z_Calloc(1, video.width * sizeof(*floorclip), PU_STATIC, NULL); - ceilingclip = Z_Calloc(1, video.width * sizeof(*ceilingclip), PU_STATIC, NULL); - spanstart = Z_Calloc(1, video.height * sizeof(*spanstart), PU_STATIC, NULL); - - cachedheight = Z_Calloc(1, video.height * sizeof(*cachedheight), PU_STATIC, NULL); - cacheddistance = Z_Calloc(1, video.height * sizeof(*cacheddistance), PU_STATIC, NULL); - cachedxstep = Z_Calloc(1, video.height * sizeof(*cachedxstep), PU_STATIC, NULL); - cachedystep = Z_Calloc(1, video.height * sizeof(*cachedystep), PU_STATIC, NULL); - - yslope = Z_Calloc(1, video.height * sizeof(*yslope), PU_STATIC, NULL); - distscale = Z_Calloc(1, video.width * sizeof(*distscale), PU_STATIC, NULL); - - openings = Z_Calloc(1, video.width * video.height * sizeof(*openings), PU_STATIC, NULL); + openings = Z_Calloc(1, video.width * video.height * sizeof(*openings), PU_RENDERER, NULL); xtoskyangle = linearsky ? linearskyangle : xtoviewangle; } diff --git a/src/r_things.c b/src/r_things.c index a6dafe5c..140668f5 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -119,19 +119,12 @@ static int maxframe; void R_InitSpritesRes(void) { - if (xtoviewangle) Z_Free(xtoviewangle); - if (linearskyangle) Z_Free(linearskyangle); - if (negonearray) Z_Free(negonearray); - if (screenheightarray) Z_Free(screenheightarray); + xtoviewangle = Z_Calloc(1, (video.width + 1) * sizeof(*xtoviewangle), PU_RENDERER, NULL); + linearskyangle = Z_Calloc(1, (video.width + 1) * sizeof(*linearskyangle), PU_RENDERER, NULL); + negonearray = Z_Calloc(1, video.width * sizeof(*negonearray), PU_RENDERER, NULL); + screenheightarray = Z_Calloc(1, video.width * sizeof(*screenheightarray), PU_RENDERER, NULL); - xtoviewangle = Z_Calloc(1, (video.width + 1) * sizeof(*xtoviewangle), PU_STATIC, NULL); - linearskyangle = Z_Calloc(1, (video.width + 1) * sizeof(*linearskyangle), PU_STATIC, NULL); - negonearray = Z_Calloc(1, video.width * sizeof(*negonearray), PU_STATIC, NULL); - screenheightarray = Z_Calloc(1, video.width * sizeof(*screenheightarray), PU_STATIC, NULL); - - if (clipbot) Z_Free(clipbot); - - clipbot = Z_Calloc(1, 2 * video.width * sizeof(*clipbot), PU_STATIC, NULL); + clipbot = Z_Calloc(1, 2 * video.width * sizeof(*clipbot), PU_RENDERER, NULL); cliptop = clipbot + video.width; } diff --git a/src/st_stuff.c b/src/st_stuff.c index 8ac43eb7..8ad76fa6 100644 --- a/src/st_stuff.c +++ b/src/st_stuff.c @@ -1427,13 +1427,8 @@ void ST_InitRes(void) { int height = V_ScaleY(StatusBarBufferHeight()); - if (st_backing_screen) - { - Z_Free(st_backing_screen); - } - // killough 11/98: allocate enough for hires - st_backing_screen = Z_Malloc(video.pitch * height * sizeof(*st_backing_screen), PU_STATIC, 0); + st_backing_screen = Z_Malloc(video.pitch * height * sizeof(*st_backing_screen), PU_RENDERER, 0); } void ST_Warnings(void) diff --git a/src/wi_interlvl.c b/src/wi_interlvl.c index e5f94fd9..575175ca 100644 --- a/src/wi_interlvl.c +++ b/src/wi_interlvl.c @@ -13,9 +13,6 @@ #include "wi_interlvl.h" -#include -#include - #include "doomtype.h" #include "i_printf.h" #include "w_wad.h" @@ -28,11 +25,6 @@ #include "cjson/cJSON.h" -static char *WI_StringDuplicate(const char *orig) -{ - return strcpy(Z_Malloc(strlen(orig) + 1, PU_LEVEL, NULL), orig); -} - static boolean ParseCondition(cJSON *json, interlevelcond_t *out) { cJSON *condition = cJSON_GetObjectItemCaseSensitive(json, "condition"); @@ -59,7 +51,7 @@ static boolean ParseFrame(cJSON *json, interlevelframe_t *out) { return false; } - out->image_lump = WI_StringDuplicate(image_lump->valuestring); + out->image_lump = Z_StrDup(image_lump->valuestring, PU_LEVEL); cJSON *type = cJSON_GetObjectItemCaseSensitive(json, "type"); if (!cJSON_IsNumber(type)) @@ -160,20 +152,14 @@ static void ParseLevelLayer(cJSON *json, interlevellayer_t *out) interlevel_t *WI_ParseInterlevel(const char *lumpname) { - interlevel_t *out = Z_Calloc(1, sizeof(*out), PU_LEVEL, NULL); - cJSON *json = cJSON_Parse(W_CacheLumpName(lumpname, PU_CACHE)); if (json == NULL) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr != NULL) { - char error_buf[32] = {0}; - memcpy(error_buf, error_ptr, sizeof(error_buf) - 1); - I_Printf(VB_ERROR, "WI_ParseInterlevel: Error before: %s\n", - error_buf); + I_Printf(VB_ERROR, "Error parsing %s", lumpname); } - free(out); cJSON_Delete(json); return NULL; } @@ -181,7 +167,6 @@ interlevel_t *WI_ParseInterlevel(const char *lumpname) cJSON *data = cJSON_GetObjectItemCaseSensitive(json, "data"); if (!cJSON_IsObject(data)) { - free(out); cJSON_Delete(json); return NULL; } @@ -192,13 +177,14 @@ interlevel_t *WI_ParseInterlevel(const char *lumpname) if (!cJSON_IsString(music) || !cJSON_IsString(backgroundimage)) { - free(out); cJSON_Delete(json); return NULL; } - out->music_lump = WI_StringDuplicate(music->valuestring); - out->background_lump = WI_StringDuplicate(backgroundimage->valuestring); + interlevel_t *out = Z_Calloc(1, sizeof(*out), PU_LEVEL, NULL); + + out->music_lump = Z_StrDup(music->valuestring, PU_LEVEL); + out->background_lump = Z_StrDup(backgroundimage->valuestring, PU_LEVEL); cJSON *js_layers = cJSON_GetObjectItemCaseSensitive(data, "layers"); cJSON *js_layer = NULL; diff --git a/src/z_zone.c b/src/z_zone.c index c64fa95a..840abfd9 100644 --- a/src/z_zone.c +++ b/src/z_zone.c @@ -202,6 +202,17 @@ void *Z_Calloc(size_t n1, size_t n2, pu_tag tag, void **user) (n1*=n2) ? memset(Z_Malloc(n1, tag, user), 0, n1) : NULL; } +char *Z_StrDup(const char *orig, pu_tag tag) +{ + size_t size = strlen(orig) + 1; + + char *result = Z_Malloc(size, tag, NULL); + + memcpy(result, orig, size); + + return result; +} + //----------------------------------------------------------------------------- // // $Log: z_zone.c,v $ diff --git a/src/z_zone.h b/src/z_zone.h index 3cdd50d3..648ca304 100644 --- a/src/z_zone.h +++ b/src/z_zone.h @@ -33,6 +33,7 @@ typedef enum { PU_STATIC, PU_LEVEL, + PU_RENDERER, PU_VALLOC, PU_CACHE, /* Must always be last -- killough */ @@ -48,6 +49,8 @@ void Z_ChangeTag(void *ptr, pu_tag tag); void *Z_Calloc(size_t n, size_t n2, pu_tag tag, void **user); void *Z_Realloc(void *p, size_t n, pu_tag tag, void **user); +char *Z_StrDup(const char *orig, pu_tag tag); + #endif //----------------------------------------------------------------------------