Skip to content

Commit

Permalink
Dynamic (multi-segment) hunk allocation
Browse files Browse the repository at this point in the history
This makes it possible to play The Immortal Lock without using -heapsize
https://www.slipseer.com/index.php?resources/the-immortal-lock.371/

Implementation details:
- The hunk is now comprised of potentially multiple segments.
  When an allocation request cannot be satisfied, a new (larger)
  segment is appended to the end.
- Hunk memory is now only allocated from the low end,
  high/temp hunk allocations are no longer supported.
- Cached memory is only allocated from the last hunk segment,
  above the low mark.
  • Loading branch information
andrei-drexler committed Jul 14, 2024
1 parent f211636 commit bdb8f7b
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 310 deletions.
44 changes: 1 addition & 43 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1994,14 +1994,7 @@ Allways appends a 0 byte.
*/
#define LOADFILE_ZONE 0
#define LOADFILE_HUNK 1
#define LOADFILE_TEMPHUNK 2
#define LOADFILE_CACHE 3
#define LOADFILE_STACK 4
#define LOADFILE_MALLOC 5

static byte *loadbuf;
static cache_user_t *loadcache;
static int loadsize;
#define LOADFILE_MALLOC 2

byte *COM_LoadFile (const char *path, int usehunk, unsigned int *path_id)
{
Expand All @@ -2025,21 +2018,9 @@ byte *COM_LoadFile (const char *path, int usehunk, unsigned int *path_id)
case LOADFILE_HUNK:
buf = (byte *) Hunk_AllocName (len+1, base);
break;
case LOADFILE_TEMPHUNK:
buf = (byte *) Hunk_TempAlloc (len+1);
break;
case LOADFILE_ZONE:
buf = (byte *) Z_Malloc (len+1);
break;
case LOADFILE_CACHE:
buf = (byte *) Cache_Alloc (loadcache, len+1, base);
break;
case LOADFILE_STACK:
if (len < loadsize)
buf = loadbuf;
else
buf = (byte *) Hunk_TempAlloc (len+1);
break;
case LOADFILE_MALLOC:
buf = (byte *) malloc (len+1);
break;
Expand Down Expand Up @@ -2070,29 +2051,6 @@ byte *COM_LoadZoneFile (const char *path, unsigned int *path_id)
return COM_LoadFile (path, LOADFILE_ZONE, path_id);
}

byte *COM_LoadTempFile (const char *path, unsigned int *path_id)
{
return COM_LoadFile (path, LOADFILE_TEMPHUNK, path_id);
}

void COM_LoadCacheFile (const char *path, struct cache_user_s *cu, unsigned int *path_id)
{
loadcache = cu;
COM_LoadFile (path, LOADFILE_CACHE, path_id);
}

// uses temp hunk if larger than bufsize
byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize, unsigned int *path_id)
{
byte *buf;

loadbuf = (byte *)buffer;
loadsize = bufsize;
buf = COM_LoadFile (path, LOADFILE_STACK, path_id);

return buf;
}

// returns malloc'd memory
byte *COM_LoadMallocFile (const char *path, unsigned int *path_id)
{
Expand Down
17 changes: 7 additions & 10 deletions Quake/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ void InsertLinkAfter (link_t *l, link_t *after);

//============================================================================

#define PTR_IN_RANGE(ptr, begin, end) (((uintptr_t)(ptr)-(uintptr_t)(begin)) < ((uintptr_t)(end)-(uintptr_t)(begin)))

//============================================================================

typedef struct vec_header_t {
size_t capacity;
size_t size;
Expand All @@ -169,6 +173,9 @@ void MultiString_Append (char **pvec, const char *str);

//============================================================================

#define BITARRAY_DWORDS(bits) (((bits)+31)/32)
#define BITARRAY_MEM_SIZE(bits) (BITARRAY_DWORDS (bits) * sizeof (uint32_t))

static inline qboolean GetBit (const uint32_t *arr, uint32_t i)
{
return (arr[i/32u] & (1u<<(i%32u))) != 0u;
Expand Down Expand Up @@ -398,20 +405,10 @@ void COM_CloseFile (int h);
// these procedures open a file using COM_FindFile and loads it into a proper
// buffer. the buffer is allocated with a total size of com_filesize + 1. the
// procedures differ by their buffer allocation method.
byte *COM_LoadStackFile (const char *path, void *buffer, int bufsize,
unsigned int *path_id);
// uses the specified stack stack buffer with the specified size
// of bufsize. if bufsize is too short, uses temp hunk. the bufsize
// must include the +1
byte *COM_LoadTempFile (const char *path, unsigned int *path_id);
// allocates the buffer on the temp hunk.
byte *COM_LoadHunkFile (const char *path, unsigned int *path_id);
// allocates the buffer on the hunk.
byte *COM_LoadZoneFile (const char *path, unsigned int *path_id);
// allocates the buffer on the zone.
void COM_LoadCacheFile (const char *path, struct cache_user_s *cu,
unsigned int *path_id);
// uses cache mem for allocating the buffer.
byte *COM_LoadMallocFile (const char *path, unsigned int *path_id);
// allocates the buffer on the system mem (malloc).

Expand Down
3 changes: 2 additions & 1 deletion Quake/gl_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ qpic_t *Draw_TryCachePic (const char *path, unsigned int texflags)
//
// load the pic from disk
//
dat = (qpic_t *)COM_LoadTempFile (path, NULL);
dat = (qpic_t *)COM_LoadMallocFile (path, NULL);
if (!dat)
return NULL;
SwapPic (dat);
Expand Down Expand Up @@ -404,6 +404,7 @@ qpic_t *Draw_TryCachePic (const char *path, unsigned int texflags)
gl.th = (float)dat->height/(float)TexMgr_PadConditional(dat->height); //johnfitz
}

free (dat);
memcpy (pic->pic.data, &gl, sizeof(glpic_t));

return &pic->pic;
Expand Down
16 changes: 6 additions & 10 deletions Quake/gl_model.c
Original file line number Diff line number Diff line change
Expand Up @@ -1784,22 +1784,18 @@ Mod_FindUsedTextures
static void Mod_FindUsedTextures (qmodel_t *mod)
{
msurface_t *s;
int i, count, bit;
int i, count;
int ofs[TEXTYPE_COUNT];
int mark = Hunk_HighMark ();
byte *inuse = (byte *) Hunk_HighAllocName ((mod->numtextures + 7) >> 3, "used textures");

uint32_t *inuse = (uint32_t *) calloc (BITARRAY_DWORDS (mod->numtextures), sizeof (uint32_t));
memset (ofs, 0, sizeof(ofs));
for (i = 0, s = mod->surfaces + mod->firstmodelsurface; i < mod->nummodelsurfaces; i++, s++)
{
texture_t *t = mod->textures[s->texinfo->texnum];
byte *val = &inuse[s->texinfo->texnum >> 3];
if (!t)
continue;
bit = 1 << (s->texinfo->texnum & 7);
if (!(*val & bit))
if (!GetBit (inuse, s->texinfo->texnum))
{
*val |= bit;
SetBit (inuse, s->texinfo->texnum);
ofs[t->type]++;
}
}
Expand All @@ -1817,11 +1813,11 @@ static void Mod_FindUsedTextures (qmodel_t *mod)
for (i = 0; i < mod->numtextures; i++)
{
texture_t *t = mod->textures[i];
if (inuse[i >> 3] & (1 << (i & 7)))
if (GetBit (inuse, i))
mod->usedtextures[ofs[t->type]++] = i;
}

Hunk_FreeToHighMark (mark);
free (inuse);

//Con_Printf("%s: %d/%d textures\n", mod->name, count, mod->numtextures);
}
Expand Down
11 changes: 9 additions & 2 deletions Quake/snd_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ sfxcache_t *S_LoadSound (sfx_t *s)
int len;
float stepscale;
sfxcache_t *sc;
byte stackbuf[1*1024]; // avoid dirtying the cache heap

// see if still in memory
sc = (sfxcache_t *) Cache_Check (&s->cache);
Expand All @@ -113,7 +112,7 @@ sfxcache_t *S_LoadSound (sfx_t *s)

// Con_Printf ("loading %s\n",namebuffer);

data = COM_LoadStackFile(namebuffer, stackbuf, sizeof(stackbuf), NULL);
data = COM_LoadMallocFile (namebuffer, NULL);

if (!data)
{
Expand All @@ -124,12 +123,14 @@ sfxcache_t *S_LoadSound (sfx_t *s)
info = GetWavinfo (s->name, data, com_filesize);
if (info.channels != 1)
{
free (data);
Con_Printf ("%s is a stereo sample\n",s->name);
return NULL;
}

if (info.width != 1 && info.width != 2)
{
free (data);
Con_Printf("%s is not 8 or 16 bit\n", s->name);
return NULL;
}
Expand All @@ -141,13 +142,17 @@ sfxcache_t *S_LoadSound (sfx_t *s)

if (info.samples == 0 || len == 0)
{
free (data);
Con_Printf("%s has zero samples\n", s->name);
return NULL;
}

sc = (sfxcache_t *) Cache_Alloc ( &s->cache, len + sizeof(sfxcache_t), s->name);
if (!sc)
{
free (data);
return NULL;
}

sc->length = info.samples;
sc->loopstart = info.loopstart;
Expand All @@ -157,6 +162,8 @@ sfxcache_t *S_LoadSound (sfx_t *s)

ResampleSfx (s, sc->speed, sc->width, data + info.dataofs);

free (data);

return sc;
}

Expand Down
Loading

0 comments on commit bdb8f7b

Please sign in to comment.