Skip to content

Commit

Permalink
Merge pull request #975 from qw-ctf/hunk-ergonomics
Browse files Browse the repository at this point in the history
MEMORY: Hunk ergonomics.
  • Loading branch information
tcsabina authored Dec 12, 2024
2 parents e7970c4 + 90b4f11 commit 93e6660
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static void Cbuf_Register (cbuf_t *cbuf, int maxsize)
{
assert (!host_initialized);
cbuf->maxsize = maxsize;
cbuf->text_buf = (char *) Hunk_Alloc(maxsize);
cbuf->text_buf = (char *) Hunk_AllocName(maxsize, "cbuf");
cbuf->text_start = cbuf->text_end = (cbuf->maxsize >> 1);
cbuf->wait = false;
cbuf->waitCount = 0;
Expand Down Expand Up @@ -1180,7 +1180,7 @@ void Cmd_AddCommand (char *cmd_name, xcommand_t function)
}
}

cmd = (cmd_function_t *) Hunk_Alloc (sizeof(cmd_function_t));
cmd = (cmd_function_t *) Hunk_AllocName (sizeof(cmd_function_t), "cmd");
cmd->name = cmd_name;
cmd->function = function;
cmd->zmalloced = false;
Expand Down
3 changes: 2 additions & 1 deletion src/gl_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,13 @@ typedef struct vbo_model_vert_s {

typedef struct glpoly_s {
struct glpoly_s *next;
#ifdef RENDERER_OPTION_CLASSIC_OPENGL
struct glpoly_s *chain; //next lightmap poly in chain
struct glpoly_s *fb_chain; //next fb poly in chain
struct glpoly_s *luma_chain; //next luma poly in chain
struct glpoly_s *caustics_chain; //next caustic poly in chain
struct glpoly_s *detail_chain; //next detail poly in chain
struct glpoly_s *drawflat_chain; //next drawflat poly in chain
#endif

unsigned int vbo_start;
int numverts;
Expand Down
2 changes: 1 addition & 1 deletion src/pr2_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ void LoadFields(void)
while (fieldvm_p[num].name) {
num++;
}
f = fields = (field_t *)Hunk_Alloc(sizeof(field_t) * (num + 1));
f = fields = (field_t *)Hunk_AllocName(sizeof(field_t) * (num + 1), "edfields");
while (fieldvm_p->name){
f->name = (stringptr_t)fieldvm_p->name;
f->ofs = fieldvm_p->ofs;
Expand Down
2 changes: 1 addition & 1 deletion src/pr_edict.c
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ char *ED_NewString (char *string)
int i,l;

l = strlen(string) + 1;
nuw = (char *) Hunk_Alloc (l);
nuw = (char *) Hunk_AllocName (l, "edstring");
new_p = nuw;

for (i=0 ; i< l ; i++)
Expand Down
8 changes: 4 additions & 4 deletions src/r_aliasmodel_md3.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static void Mod_MD3LoadSkins(model_t* mod, md3Header_t* header, md3model_t* mode
header->numSkins = found_skins;

// Convert linked list to array so it's moveable & easier to access
surfinf_t* surface_info = Hunk_Alloc(sizeof(surfinf_t) * header->numSkins * header->numSurfaces);
surfinf_t* surface_info = Hunk_AllocName(sizeof(surfinf_t) * header->numSkins * header->numSurfaces, "md3surfinfo");
model->surfinf = (int)((intptr_t)surface_info - (intptr_t)model);
while (skin_list) {
md3_skin_list_entry_t* next = skin_list->next;
Expand Down Expand Up @@ -194,8 +194,8 @@ void Mod_LoadAlias3Model(model_t *mod, void *buffer, int filesize)

numsurfs = LittleLong(((md3Header_t *)buffer)->numSurfaces);

pheader = (md3model_t *)Hunk_Alloc(sizeof(md3model_t));
mem = (md3Header_t *)Hunk_Alloc(filesize);
pheader = (md3model_t *)Hunk_AllocName(sizeof(md3model_t), "md3model");
mem = (md3Header_t *)Hunk_AllocName(filesize, "md3header");
pheader->md3model = (char *)mem - (char *)pheader;
memcpy(mem, buffer, filesize); //casually load the entire thing. As you do.

Expand Down Expand Up @@ -269,7 +269,7 @@ void Mod_LoadAlias3Model(model_t *mod, void *buffer, int filesize)

// swap all the vertices, convert to our format
vert = (md3XyzNormal_t *)((char *)surf + surf->ofsXyzNormals);
output_vert = (ezMd3XyzNormal_t*)Hunk_Alloc(surf->numVerts * surf->numFrames * sizeof(ezMd3XyzNormal_t));
output_vert = (ezMd3XyzNormal_t*)Hunk_AllocName(surf->numVerts * surf->numFrames * sizeof(ezMd3XyzNormal_t), "md3normal");
for (j = 0; j < surf->numVerts * surf->numFrames; j++) {
vert[j].xyz[0] = LittleShort(vert[j].xyz[0]);
vert[j].xyz[1] = LittleShort(vert[j].xyz[1]);
Expand Down
4 changes: 2 additions & 2 deletions src/r_brushmodel_warpsurfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static void R_WarpSurfaceSubdividePolygon(int numverts, float *verts)
return;
}

poly = (glpoly_t *)Hunk_Alloc(sizeof(glpoly_t) + (numverts - 4) * VERTEXSIZE * sizeof(float));
poly = (glpoly_t *)Hunk_AllocName(sizeof(glpoly_t) + (numverts - 4) * VERTEXSIZE * sizeof(float), "subdivpoly");
poly->next = warpface->subdivided;
warpface->subdivided = poly;
poly->numverts = numverts;
Expand Down Expand Up @@ -189,7 +189,7 @@ void R_SkySurfacesBuildPolys(msurface_t *fa)
numverts++;
}

poly = Hunk_Alloc(sizeof(glpoly_t) + (numverts - 4) * VERTEXSIZE * sizeof(float));
poly = Hunk_AllocName(sizeof(glpoly_t) + (numverts - 4) * VERTEXSIZE * sizeof(float), "subdivpoly");
poly->next = NULL;
fa->polys = poly;
poly->numverts = numverts;
Expand Down
2 changes: 1 addition & 1 deletion src/r_lightmaps.c
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ static void R_BuildSurfaceDisplayList(model_t* currentmodel, msurface_t *fa)

// draw texture
if (!fa->polys) { // seems map loaded first time, so light maps loaded first time too
poly = (glpoly_t *)Hunk_Alloc(sizeof(glpoly_t) + (lnumverts - 4) * VERTEXSIZE * sizeof(float));
poly = (glpoly_t *)Hunk_AllocName(sizeof(glpoly_t) + (lnumverts - 4) * VERTEXSIZE * sizeof(float), "lmpoly");
poly->next = fa->polys;
fa->polys = poly;
poly->numverts = lnumverts;
Expand Down
4 changes: 2 additions & 2 deletions src/r_refrag.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ entity_t *r_addent;

void R_Init_EFrags (void)
{
cl.free_efrags = (efrag_t *) Hunk_Alloc (sizeof (efrag_t));
cl.free_efrags = (efrag_t *) Hunk_AllocName (sizeof (efrag_t), "efrag");
memset (cl.free_efrags, 0, sizeof (efrag_t));
}

Expand All @@ -48,7 +48,7 @@ void R_Next_Free_EFrag (void)
// advance the linked list, growing it as needed
if (cl.free_efrags->entnext == NULL)
{
cl.free_efrags->entnext = (efrag_t *) Hunk_Alloc (sizeof (efrag_t));
cl.free_efrags->entnext = (efrag_t *) Hunk_AllocName (sizeof (efrag_t), "efrag");
memset (cl.free_efrags->entnext, 0, sizeof (efrag_t));
}
cl.free_efrags = cl.free_efrags->entnext;
Expand Down
2 changes: 1 addition & 1 deletion src/sv_save.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ void SV_LoadGame_f(void)
}
str[sizeof(str) - 1] = '\0';
length = strlen(str) + 1;
sv.lightstyles[i] = (char *) Hunk_Alloc (length);
sv.lightstyles[i] = (char *) Hunk_AllocName (length, "lightstyle");
strlcpy (sv.lightstyles[i], str, length);
}

Expand Down
6 changes: 3 additions & 3 deletions src/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ void VM_LoadSymbols( vm_t *vm ) {
break;
}
chars = strlen( com_token );
sym = Hunk_Alloc( sizeof( *sym ) + chars);
sym = Hunk_AllocName( sizeof( *sym ) + chars, "qvm-symbols");

Check warning on line 490 in src/vm.c

View workflow job for this annotation

GitHub Actions / ezQuake-windows-x64

'function': conversion from 'size_t' to 'int', possible loss of data [D:\a\ezquake-source\ezquake-source\build-msbuild-x64\ezquake.vcxproj]
*prev = sym;
prev = &sym->next;
sym->next = NULL;
Expand Down Expand Up @@ -737,7 +737,7 @@ static vmHeader_t *VM_LoadQVM( vm_t *vm, qbool alloc ) {

if ( alloc ) {
// allocate zero filled space for initialized and uninitialized data
vm->dataBase = Hunk_Alloc( dataAlloc);
vm->dataBase = Hunk_AllocName( dataAlloc, "qvm");
vm->dataMask = dataLength - 1;
vm->dataAlloc = dataAlloc;
} else {
Expand Down Expand Up @@ -766,7 +766,7 @@ static vmHeader_t *VM_LoadQVM( vm_t *vm, qbool alloc ) {
Con_Printf( "Loading %d jump table targets\n", vm->numJumpTableTargets );

if ( alloc ) {
vm->jumpTableTargets = Hunk_Alloc( header->jtrgLength);
vm->jumpTableTargets = Hunk_AllocName( header->jtrgLength, "qvm-jtt");
} else {
if ( vm->numJumpTableTargets != previousNumJumpTableTargets ) {
VM_Free( vm );
Expand Down
2 changes: 1 addition & 1 deletion src/vm_interpreted.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ qbool VM_PrepareInterpreter2( vm_t *vm, vmHeader_t *header )
{
const char *errMsg;
instruction_t *buf;
buf = ( instruction_t *) Hunk_Alloc( (vm->instructionCount + 8) * sizeof( instruction_t ));
buf = ( instruction_t *) Hunk_AllocName( (vm->instructionCount + 8) * sizeof( instruction_t ), "vm");

errMsg = VM_LoadInstructions( (byte *) header + header->codeOffset, header->codeLength, header->instructionCount, buf );
if ( !errMsg ) {
Expand Down
2 changes: 1 addition & 1 deletion src/wad.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void WAD3_LoadWadFile(const char *filename)
}

lowmark = Hunk_LowMark();
if (!(lumps = Hunk_Alloc(sizeof(lumpinfo_t) * numlumps))) {
if (!(lumps = Hunk_AllocName(sizeof(lumpinfo_t) * numlumps, filename))) {
Com_Printf("WAD3_LoadWadFile: unable to allocate temporary memory for lump table\n");
return;
}
Expand Down
18 changes: 6 additions & 12 deletions src/zone.c
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ void *Hunk_AllocName(int size, const char *name)
size = sizeof(hunk_t) + ((size + 15) & ~15);

if (hunk_size - hunk_low_used - hunk_high_used < size) {
if ((int)developer.value)
{
Hunk_Print(true);
}
#ifdef SERVERONLY
Sys_Error("Hunk_AllocName: Not enough RAM allocated. Try starting using \"-mem 64\" (or more) on the command line.");
#else
Expand All @@ -197,16 +201,6 @@ void *Hunk_AllocName(int size, const char *name)
return (void *)(h + 1);
}

/*
===================
Hunk_Alloc
===================
*/
void *Hunk_Alloc(int size)
{
return Hunk_AllocName(size, "unknown");
}

int Hunk_LowMark(void)
{
return hunk_low_used;
Expand Down Expand Up @@ -589,8 +583,6 @@ void Cache_Init_Commands(void)
Cmd_AddCommand("flush", Cache_Flush);
Cmd_AddCommand("cache_print", Cache_Print);
Cmd_AddCommand("cache_report", Cache_Report);

Cmd_AddCommand("hunk_print", Hunk_Print_f);
}

#ifndef WITH_DP_MEM
Expand Down Expand Up @@ -700,4 +692,6 @@ void Memory_Init(void *buf, int size)
hunk_high_used = 0;

Cache_Init();

Cmd_AddCommand("hunk_print", Hunk_Print_f);
}
1 change: 0 additions & 1 deletion src/zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ startup hunk allocations

void Memory_Init (void *buf, int size);

void *Hunk_Alloc (int size); // returns 0 filled memory
void *Hunk_AllocName (int size, const char *name);

int Hunk_LowMark (void);
Expand Down

0 comments on commit 93e6660

Please sign in to comment.