mirror of
https://github.com/libretro/libretro-tyrquake.git
synced 2025-02-21 08:21:00 +00:00
[PATCH] Hunk_TempAllocExtend
This patch implements a new memory allocation feature, which allows us to use the "Temp" hunk as an expandable blob of storage space. The first user of this will be some "string tree" functions, which build up an RB tree of strings which will be used for sorting file lists. We could just use Z_Malloc, but the zone isn't well suited to this type of allocation (potentially hundreds of small struct + string allocations) and is probably too small. Signed-off-by: Tyrann <tyrann@disenchant.net>
This commit is contained in:
parent
87523609fa
commit
dd63643adf
@ -564,6 +564,45 @@ Hunk_TempAlloc(int size)
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* =====================
|
||||
* Hunk_TempAllocExtend
|
||||
*
|
||||
* Extend the existing temp hunk allocation.
|
||||
* Size is the number of extra bytes required
|
||||
* =====================
|
||||
*/
|
||||
void *
|
||||
Hunk_TempAllocExtend(int size)
|
||||
{
|
||||
hunk_t *old, *new;
|
||||
|
||||
if (!hunk_tempactive)
|
||||
Sys_Error("%s: temp hunk not active");
|
||||
|
||||
old = (hunk_t *)(hunk_base + hunk_size - hunk_high_used);
|
||||
|
||||
if (old->sentinal != HUNK_SENTINAL)
|
||||
Sys_Error("%s: old sentinal trashed\n", __func__);
|
||||
if (strncmp(old->name, "temp", 8))
|
||||
Sys_Error("%s: old hunk name trashed\n", __func__);
|
||||
|
||||
size = (size + 15) & ~15;
|
||||
if (hunk_size - hunk_low_used - hunk_high_used < size) {
|
||||
Con_Printf("%s: failed on %i bytes\n", __func__, size);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
hunk_high_used += size;
|
||||
Cache_FreeHigh(hunk_high_used);
|
||||
|
||||
new = (hunk_t *)(hunk_base + hunk_size - hunk_high_used);
|
||||
memmove(new, old, sizeof(hunk_t));
|
||||
new->size += size;
|
||||
|
||||
return (void *)(new + 1);
|
||||
}
|
||||
|
||||
/*
|
||||
* ===========================================================================
|
||||
*
|
||||
|
@ -107,6 +107,7 @@ int Hunk_HighMark(void);
|
||||
void Hunk_FreeToHighMark(int mark);
|
||||
|
||||
void *Hunk_TempAlloc(int size);
|
||||
void *Hunk_TempAllocExtend(int size);
|
||||
|
||||
void Hunk_Check(void);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user