2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
zone.c
|
|
|
|
|
|
|
|
(description)
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
2001-09-05 18:23:38 +00:00
|
|
|
#include <stdarg.h>
|
2001-02-21 19:35:06 +00:00
|
|
|
#include <stdlib.h>
|
2022-06-06 04:25:43 +00:00
|
|
|
#include <ctype.h>
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/cmd.h"
|
2001-08-28 06:15:58 +00:00
|
|
|
#include "QF/cvar.h"
|
2011-12-24 01:04:33 +00:00
|
|
|
#include "QF/mathlib.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/qargs.h"
|
|
|
|
#include "QF/sys.h"
|
2012-02-06 03:48:22 +00:00
|
|
|
#include "QF/va.h"
|
2001-03-27 20:33:07 +00:00
|
|
|
#include "QF/zone.h"
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2002-05-29 20:58:53 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
static void Cache_FreeLow (memhunk_t *hunk, size_t new_low_hunk);
|
|
|
|
static void Cache_FreeHigh (memhunk_t *hunk, size_t new_high_hunk);
|
2021-07-28 06:01:45 +00:00
|
|
|
static void Cache_Profile_r (memhunk_t *hunk);
|
|
|
|
static qboolean Cache_FreeLRU (memhunk_t *hunk);
|
2007-03-28 09:52:01 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
#define ZONEID 0x1d4a11
|
|
|
|
#define HUNK_SENTINAL 0x1df001ed
|
|
|
|
|
|
|
|
#define MINFRAGMENT 64
|
2021-02-03 04:19:19 +00:00
|
|
|
#define HUNK_ALIGN 64
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
/*
|
2001-08-28 04:24:40 +00:00
|
|
|
ZONE MEMORY ALLOCATION
|
|
|
|
|
|
|
|
There is never any space between memblocks, and there will never be two
|
|
|
|
contiguous free memblocks.
|
|
|
|
|
|
|
|
The rover can be left pointing at a non-empty block
|
|
|
|
|
2010-01-13 06:42:26 +00:00
|
|
|
The zone calls are pretty much used only for small strings and structures,
|
2001-02-21 19:35:06 +00:00
|
|
|
all big things are allocated on the hunk.
|
|
|
|
*/
|
|
|
|
|
2021-07-26 06:43:57 +00:00
|
|
|
typedef struct memblock_s {
|
|
|
|
size_t block_size; // including the header and possibly tiny fragments
|
|
|
|
struct memblock_s *next;
|
|
|
|
struct memblock_s *prev;
|
|
|
|
size_t size; // requested size
|
2022-06-04 16:08:45 +00:00
|
|
|
byte pad[64 - 3 * 4 - 4 * sizeof (size_t)];
|
2001-08-28 04:24:40 +00:00
|
|
|
int tag; // a tag of 0 is a free block
|
2011-03-08 13:44:56 +00:00
|
|
|
int id; // should be ZONEID
|
2022-06-04 16:08:45 +00:00
|
|
|
int retain; // reference counter (optional usage)
|
2021-12-13 02:23:25 +00:00
|
|
|
} __attribute__((aligned (64))) memblock_t;
|
2001-08-28 04:24:40 +00:00
|
|
|
|
2021-07-26 06:43:57 +00:00
|
|
|
struct memzone_s {
|
|
|
|
size_t size; // total bytes malloced, including header
|
|
|
|
size_t used; // ammount used, including header
|
|
|
|
size_t offset;
|
|
|
|
size_t ele_size;
|
2012-02-06 03:48:22 +00:00
|
|
|
void (*error) (void *, const char *);
|
|
|
|
void *data;
|
|
|
|
memblock_t *rover;
|
2021-12-13 02:23:25 +00:00
|
|
|
memblock_t blocklist; // start / end cap for linked list
|
|
|
|
} __attribute__((aligned (64)));
|
2001-08-28 23:58:22 +00:00
|
|
|
|
2022-06-05 07:48:25 +00:00
|
|
|
static size_t
|
2011-03-20 06:50:09 +00:00
|
|
|
z_block_size (memblock_t *block)
|
|
|
|
{
|
2012-11-19 12:35:27 +00:00
|
|
|
return block->block_size - sizeof (memblock_t) - 4;
|
2011-03-20 06:50:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
z_offset (memzone_t *zone, memblock_t *block)
|
|
|
|
{
|
|
|
|
int offset = ((byte *) (block + 1) - (byte *) zone);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2011-03-20 06:50:09 +00:00
|
|
|
return offset / zone->ele_size + zone->offset;
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:08:45 +00:00
|
|
|
static void
|
|
|
|
z_error (memzone_t *zone, const char *msg)
|
|
|
|
{
|
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, msg);
|
|
|
|
Sys_Error ("%s", msg);
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2021-07-26 06:43:57 +00:00
|
|
|
Z_ClearZone (memzone_t *zone, size_t size, size_t zone_offset, size_t ele_size)
|
2001-08-28 04:24:40 +00:00
|
|
|
{
|
2022-06-04 16:08:45 +00:00
|
|
|
memblock_t *block
|
|
|
|
= __builtin_choose_expr (__builtin_offsetof (memblock_t, retain) == 60,
|
|
|
|
0, (void) 0);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
// set the entire zone to one free block
|
|
|
|
|
2004-11-14 00:49:00 +00:00
|
|
|
block = (memblock_t *) (zone + 1);
|
|
|
|
zone->blocklist.next = block;
|
|
|
|
zone->blocklist.prev = block;
|
2001-08-28 04:24:40 +00:00
|
|
|
zone->blocklist.tag = 1; // in use block
|
|
|
|
zone->blocklist.id = 0;
|
2012-11-19 12:35:27 +00:00
|
|
|
zone->blocklist.block_size = 0;
|
2012-11-19 12:50:42 +00:00
|
|
|
zone->blocklist.size = 0;
|
2011-03-08 13:44:56 +00:00
|
|
|
zone->offset = zone_offset;
|
|
|
|
zone->ele_size = ele_size;
|
2001-08-28 04:24:40 +00:00
|
|
|
zone->rover = block;
|
2004-11-14 00:49:00 +00:00
|
|
|
zone->size = size;
|
|
|
|
zone->used = sizeof (memzone_t);
|
2012-02-06 03:48:22 +00:00
|
|
|
zone->error = 0;
|
|
|
|
zone->data = 0;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
block->prev = block->next = &zone->blocklist;
|
|
|
|
block->tag = 0; // free block
|
|
|
|
block->id = ZONEID;
|
2012-11-19 12:50:42 +00:00
|
|
|
//block->id2 = ZONEID;
|
2012-11-19 12:35:27 +00:00
|
|
|
block->block_size = size - sizeof (memzone_t);
|
2012-11-19 12:50:42 +00:00
|
|
|
block->size = 0;
|
2001-08-28 04:24:40 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-08-28 23:58:22 +00:00
|
|
|
Z_Free (memzone_t *zone, void *ptr)
|
2001-08-28 04:24:40 +00:00
|
|
|
{
|
|
|
|
memblock_t *block, *other;
|
2011-03-08 13:44:56 +00:00
|
|
|
|
2012-02-06 03:48:22 +00:00
|
|
|
if (!ptr) {
|
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, "Z_Free: NULL pointer");
|
2001-08-28 04:24:40 +00:00
|
|
|
Sys_Error ("Z_Free: NULL pointer");
|
2012-02-06 03:48:22 +00:00
|
|
|
}
|
2001-08-28 04:24:40 +00:00
|
|
|
|
2002-08-25 04:47:57 +00:00
|
|
|
block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
2011-03-20 06:50:09 +00:00
|
|
|
if (((byte *) block < (byte *) zone)
|
2012-02-06 03:48:22 +00:00
|
|
|
|| (((byte *) block) >= (byte *) zone + zone->size)) {
|
|
|
|
const char *msg;
|
|
|
|
msg = nva ("Z_Free: freed a pointer outside of the zone: %x",
|
2011-03-20 06:50:09 +00:00
|
|
|
z_offset (zone, block));
|
2012-02-06 03:48:22 +00:00
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, msg);
|
|
|
|
Sys_Error ("%s", msg);
|
|
|
|
}
|
2012-11-19 12:50:42 +00:00
|
|
|
if (block->id != ZONEID/* || block->id2 != ZONEID*/) {
|
2012-02-06 03:48:22 +00:00
|
|
|
const char *msg;
|
|
|
|
msg = nva ("bad pointer %x", z_offset (zone, block));
|
|
|
|
Sys_Printf ("%s\n", msg);
|
2011-03-20 06:50:09 +00:00
|
|
|
Z_Print (zone);
|
|
|
|
fflush (stdout);
|
2012-02-06 03:48:22 +00:00
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, msg);
|
2001-08-28 04:24:40 +00:00
|
|
|
Sys_Error ("Z_Free: freed a pointer without ZONEID");
|
2011-03-20 06:50:09 +00:00
|
|
|
}
|
2012-02-06 03:48:22 +00:00
|
|
|
if (block->tag == 0) {
|
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, "Z_Free: freed a freed pointer");
|
2001-08-28 04:24:40 +00:00
|
|
|
Sys_Error ("Z_Free: freed a freed pointer");
|
2012-02-06 03:48:22 +00:00
|
|
|
}
|
2022-06-04 16:08:45 +00:00
|
|
|
if (block->retain) {
|
|
|
|
const char *msg = nva ("Z_Free: freed a retained pointer: %d",
|
|
|
|
block->retain);
|
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, msg);
|
|
|
|
Sys_Error ("%s", msg);
|
|
|
|
}
|
2001-08-28 04:24:40 +00:00
|
|
|
|
|
|
|
block->tag = 0; // mark as free
|
2012-11-19 12:50:42 +00:00
|
|
|
block->size = 0;
|
2012-11-19 12:35:27 +00:00
|
|
|
zone->used -= block->block_size;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
other = block->prev;
|
2001-08-28 05:21:30 +00:00
|
|
|
if (!other->tag) {
|
|
|
|
// merge with previous free block
|
2012-11-19 12:35:27 +00:00
|
|
|
other->block_size += block->block_size;
|
2001-08-28 04:24:40 +00:00
|
|
|
other->next = block->next;
|
|
|
|
other->next->prev = other;
|
2001-08-28 05:21:30 +00:00
|
|
|
if (block == zone->rover)
|
|
|
|
zone->rover = other;
|
2001-08-28 04:24:40 +00:00
|
|
|
block = other;
|
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
other = block->next;
|
2001-08-28 05:21:30 +00:00
|
|
|
if (!other->tag) {
|
|
|
|
// merge the next free block onto the end
|
2012-11-19 12:35:27 +00:00
|
|
|
block->block_size += other->block_size;
|
2001-08-28 04:24:40 +00:00
|
|
|
block->next = other->next;
|
|
|
|
block->next->prev = block;
|
2001-08-28 05:21:30 +00:00
|
|
|
if (other == zone->rover)
|
|
|
|
zone->rover = block;
|
2001-08-28 04:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2021-07-26 06:43:57 +00:00
|
|
|
Z_Malloc (memzone_t *zone, size_t size)
|
2001-08-28 04:24:40 +00:00
|
|
|
{
|
|
|
|
void *buf;
|
2001-08-28 06:15:58 +00:00
|
|
|
|
2001-08-28 05:21:30 +00:00
|
|
|
buf = Z_TagMalloc (zone, size, 1);
|
2012-02-06 03:48:22 +00:00
|
|
|
if (!buf) {
|
|
|
|
const char *msg;
|
2021-07-26 06:43:57 +00:00
|
|
|
msg = nva ("Z_Malloc: failed on allocation of %zd bytes", size);
|
2012-02-06 03:48:22 +00:00
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, msg);
|
|
|
|
Sys_Error ("%s", msg);
|
|
|
|
}
|
2001-08-28 04:24:40 +00:00
|
|
|
memset (buf, 0, size);
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2002-08-25 04:47:57 +00:00
|
|
|
void *
|
2021-07-26 06:43:57 +00:00
|
|
|
Z_TagMalloc (memzone_t *zone, size_t size, int tag)
|
2001-08-28 04:24:40 +00:00
|
|
|
{
|
2012-11-19 12:50:42 +00:00
|
|
|
int extra;
|
|
|
|
int requested_size = size;
|
|
|
|
memblock_t *start, *rover, *new, *base;
|
2001-08-28 04:24:40 +00:00
|
|
|
|
2022-06-03 03:04:27 +00:00
|
|
|
if (developer & SYS_zone)
|
2022-02-01 02:56:45 +00:00
|
|
|
Z_CheckHeap (zone); // DEBUG
|
|
|
|
|
2012-02-06 03:48:22 +00:00
|
|
|
if (!tag) {
|
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, "Z_TagMalloc: tried to use a 0 tag");
|
2001-08-28 04:24:40 +00:00
|
|
|
Sys_Error ("Z_TagMalloc: tried to use a 0 tag");
|
2012-02-06 03:48:22 +00:00
|
|
|
}
|
2001-08-28 04:24:40 +00:00
|
|
|
|
|
|
|
// scan through the block list looking for the first free block
|
|
|
|
// of sufficient size
|
2002-08-25 04:47:57 +00:00
|
|
|
size += sizeof (memblock_t); // account for size of block header
|
|
|
|
size += 4; // space for memory trash tester
|
2022-06-04 16:17:32 +00:00
|
|
|
size = (size + 63) & ~63; // align to 64-byte boundary
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 05:21:30 +00:00
|
|
|
base = rover = zone->rover;
|
2001-08-28 04:24:40 +00:00
|
|
|
start = base->prev;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 05:21:30 +00:00
|
|
|
do {
|
2002-08-25 04:47:57 +00:00
|
|
|
if (rover == start) // scaned all the way around the list
|
2001-08-28 04:24:40 +00:00
|
|
|
return NULL;
|
|
|
|
if (rover->tag)
|
|
|
|
base = rover = rover->next;
|
|
|
|
else
|
|
|
|
rover = rover->next;
|
2012-11-19 12:35:27 +00:00
|
|
|
} while (base->tag || base->block_size < size);
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
// found a block big enough
|
2012-11-19 12:35:27 +00:00
|
|
|
extra = base->block_size - size;
|
2001-08-28 05:21:30 +00:00
|
|
|
if (extra > MINFRAGMENT) {
|
|
|
|
// there will be a free fragment after the allocated block
|
2002-08-25 04:47:57 +00:00
|
|
|
new = (memblock_t *) ((byte *) base + size);
|
2012-11-19 12:35:27 +00:00
|
|
|
new->block_size = extra;
|
2001-08-28 04:24:40 +00:00
|
|
|
new->tag = 0; // free block
|
|
|
|
new->prev = base;
|
|
|
|
new->id = ZONEID;
|
2012-11-19 12:50:42 +00:00
|
|
|
//new->id2 = ZONEID;
|
2001-08-28 04:24:40 +00:00
|
|
|
new->next = base->next;
|
|
|
|
new->next->prev = new;
|
|
|
|
base->next = new;
|
2012-11-19 12:35:27 +00:00
|
|
|
base->block_size = size;
|
2001-08-28 04:24:40 +00:00
|
|
|
}
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2022-06-04 16:08:45 +00:00
|
|
|
base->retain = 0; // use is optional, but must be 0 to free
|
2001-08-28 04:24:40 +00:00
|
|
|
base->tag = tag; // no longer a free block
|
2012-11-19 12:50:42 +00:00
|
|
|
base->size = requested_size;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 05:21:30 +00:00
|
|
|
zone->rover = base->next; // next allocation will start looking here
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
base->id = ZONEID;
|
2012-11-19 12:50:42 +00:00
|
|
|
//base->id2 = ZONEID;
|
2001-08-28 04:24:40 +00:00
|
|
|
|
2012-11-19 12:35:27 +00:00
|
|
|
zone->used += base->block_size;
|
2004-11-14 00:49:00 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
// marker for memory trash testing
|
2012-11-19 12:35:27 +00:00
|
|
|
*(int *) ((byte *) base + base->block_size - 4) = ZONEID;
|
2001-08-28 04:24:40 +00:00
|
|
|
|
2004-11-14 00:49:00 +00:00
|
|
|
return (void *) (base + 1);
|
2001-08-28 04:24:40 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2021-07-26 06:43:57 +00:00
|
|
|
Z_Realloc (memzone_t *zone, void *ptr, size_t size)
|
2002-05-29 20:58:53 +00:00
|
|
|
{
|
2021-07-26 06:43:57 +00:00
|
|
|
size_t old_size;
|
2002-08-25 04:47:57 +00:00
|
|
|
memblock_t *block;
|
2002-05-29 20:58:53 +00:00
|
|
|
void *old_ptr;
|
2012-05-21 23:23:22 +00:00
|
|
|
|
2002-05-29 20:58:53 +00:00
|
|
|
if (!ptr)
|
|
|
|
return Z_Malloc (zone, size);
|
|
|
|
|
2022-06-03 03:04:27 +00:00
|
|
|
if (developer & SYS_zone)
|
2022-02-01 02:56:45 +00:00
|
|
|
Z_CheckHeap (zone); // DEBUG
|
|
|
|
|
2002-08-25 04:47:57 +00:00
|
|
|
block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
2012-11-19 12:50:42 +00:00
|
|
|
if (block->id != ZONEID/* || block->id2 != ZONEID*/) {
|
2012-02-06 03:48:22 +00:00
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data,
|
|
|
|
"Z_Realloc: realloced a pointer without ZONEID");
|
2002-05-29 20:58:53 +00:00
|
|
|
Sys_Error ("Z_Realloc: realloced a pointer without ZONEID");
|
2012-02-06 03:48:22 +00:00
|
|
|
}
|
|
|
|
if (block->tag == 0) {
|
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, "Z_Realloc: realloced a freed pointer");
|
2002-05-29 20:58:53 +00:00
|
|
|
Sys_Error ("Z_Realloc: realloced a freed pointer");
|
2012-02-06 03:48:22 +00:00
|
|
|
}
|
2002-05-29 20:58:53 +00:00
|
|
|
|
2012-11-19 12:35:27 +00:00
|
|
|
old_size = block->block_size;
|
2010-11-21 01:06:56 +00:00
|
|
|
old_size -= sizeof (memblock_t); // account for size of block header
|
|
|
|
old_size -= 4; // space for memory trash tester
|
2002-05-29 20:58:53 +00:00
|
|
|
old_ptr = ptr;
|
|
|
|
|
|
|
|
Z_Free (zone, ptr);
|
|
|
|
ptr = Z_TagMalloc (zone, size, 1);
|
2012-02-06 03:48:22 +00:00
|
|
|
if (!ptr) {
|
|
|
|
const char *msg;
|
2021-07-26 06:43:57 +00:00
|
|
|
msg = nva ("Z_Realloc: failed on allocation of %zd bytes", size);
|
2012-02-06 03:48:22 +00:00
|
|
|
if (zone->error)
|
|
|
|
zone->error (zone->data, msg);
|
|
|
|
Sys_Error ("%s", msg);
|
|
|
|
}
|
2002-05-29 20:58:53 +00:00
|
|
|
|
|
|
|
if (ptr != old_ptr)
|
|
|
|
memmove (ptr, old_ptr, min (old_size, size));
|
2011-12-19 00:45:46 +00:00
|
|
|
if (old_size < size)
|
|
|
|
memset ((byte *)ptr + old_size, 0, size - old_size);
|
2002-05-29 20:58:53 +00:00
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
2004-11-14 00:49:00 +00:00
|
|
|
|
|
|
|
void
|
2001-08-28 23:58:22 +00:00
|
|
|
Z_Print (memzone_t *zone)
|
2001-08-28 04:24:40 +00:00
|
|
|
{
|
|
|
|
memblock_t *block;
|
2005-01-02 14:23:20 +00:00
|
|
|
|
2021-07-26 06:43:57 +00:00
|
|
|
Sys_Printf ("zone size: %zd location: %p used: %zd\n",
|
2004-11-14 00:49:00 +00:00
|
|
|
zone->size, zone, zone->used);
|
2005-01-02 14:23:20 +00:00
|
|
|
|
|
|
|
for (block = zone->blocklist.next ; ; block = block->next) {
|
2022-06-05 07:48:25 +00:00
|
|
|
Sys_Printf ("block:%p size:%8zd tag:%5x ret: %5d ofs:%x\n",
|
2011-03-08 13:44:56 +00:00
|
|
|
block, z_block_size (block),
|
2022-06-04 16:08:45 +00:00
|
|
|
block->tag, block->retain, z_offset (zone, block));
|
2005-01-02 14:23:20 +00:00
|
|
|
|
2001-08-28 04:24:40 +00:00
|
|
|
if (block->next == &zone->blocklist)
|
2012-05-21 23:23:22 +00:00
|
|
|
break; // all blocks have been hit
|
2012-11-19 12:50:42 +00:00
|
|
|
if (block->id != ZONEID/* || block->id2 != ZONEID*/)
|
2011-03-08 13:44:56 +00:00
|
|
|
Sys_Printf ("ERROR: block ids incorrect\n");
|
2012-11-19 12:35:27 +00:00
|
|
|
if ((byte *) block + block->block_size != (byte *) block->next)
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("ERROR: block size does not touch the next block\n");
|
2012-11-19 12:35:27 +00:00
|
|
|
if (block->next->prev != block)
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("ERROR: next block doesn't have proper back link\n");
|
2001-08-28 04:24:40 +00:00
|
|
|
if (!block->tag && !block->next->tag)
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("ERROR: two consecutive free blocks\n");
|
2022-02-11 16:57:03 +00:00
|
|
|
int id = *(int *) ((byte *) block + block->block_size - 4);
|
|
|
|
if (block->tag && (id != ZONEID))
|
|
|
|
Sys_Printf ("ERROR: memory trashed in block %x != %x\n",
|
|
|
|
id, ZONEID);
|
2004-11-14 00:49:00 +00:00
|
|
|
fflush (stdout);
|
2001-08-28 04:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-14 00:49:00 +00:00
|
|
|
|
2001-08-28 23:58:22 +00:00
|
|
|
void
|
|
|
|
Z_CheckHeap (memzone_t *zone)
|
2001-08-28 04:24:40 +00:00
|
|
|
{
|
|
|
|
memblock_t *block;
|
2005-01-02 14:23:20 +00:00
|
|
|
|
|
|
|
for (block = zone->blocklist.next ; ; block = block->next) {
|
2001-08-28 05:21:30 +00:00
|
|
|
if (block->next == &zone->blocklist)
|
2012-05-21 23:23:22 +00:00
|
|
|
break; // all blocks have been hit
|
2012-11-19 12:35:27 +00:00
|
|
|
if ((byte *) block + block->block_size != (byte *) block->next)
|
2022-02-11 16:57:03 +00:00
|
|
|
z_error (zone,
|
|
|
|
"Z_CheckHeap: block size does not touch the next block");
|
2012-11-19 11:03:21 +00:00
|
|
|
if (block->next->prev != block)
|
2022-02-11 16:57:03 +00:00
|
|
|
z_error (zone,
|
|
|
|
"Z_CheckHeap: next block doesn't have proper back link");
|
2001-08-28 04:24:40 +00:00
|
|
|
if (!block->tag && !block->next->tag)
|
2022-02-11 16:57:03 +00:00
|
|
|
z_error (zone, "Z_CheckHeap: two consecutive free blocks");
|
|
|
|
if (block->id != ZONEID/* || block->id2 != ZONEID*/)
|
|
|
|
z_error (zone, "ERROR: block ids incorrect");
|
|
|
|
if ((byte *) block + block->block_size != (byte *) block->next)
|
|
|
|
z_error (zone, "ERROR: block size does not touch the next block");
|
|
|
|
if (block->next->prev != block)
|
|
|
|
z_error (zone, "ERROR: next block doesn't have proper back link");
|
|
|
|
if (!block->tag && !block->next->tag)
|
|
|
|
z_error (zone, "ERROR: two consecutive free blocks");
|
|
|
|
if (block->tag
|
|
|
|
&& (*(int *) ((byte *) block + block->block_size - 4) != ZONEID))
|
|
|
|
z_error (zone, "ERROR: memory trashed in block");
|
2001-08-28 04:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-06 03:48:22 +00:00
|
|
|
VISIBLE void
|
|
|
|
Z_SetError (memzone_t *zone, void (*err) (void *, const char *), void *data)
|
|
|
|
{
|
|
|
|
zone->error = err;
|
|
|
|
zone->data = data;
|
|
|
|
}
|
|
|
|
|
2021-12-27 11:12:03 +00:00
|
|
|
VISIBLE void
|
2021-07-26 06:43:57 +00:00
|
|
|
Z_CheckPointer (const memzone_t *zone, const void *ptr, size_t size)
|
2012-11-19 11:03:21 +00:00
|
|
|
{
|
|
|
|
const memblock_t *block;
|
|
|
|
const char *block_mem;
|
|
|
|
const char *check = (char *) ptr;
|
|
|
|
|
|
|
|
for (block = zone->blocklist.next ; ; block = block->next) {
|
|
|
|
if (block->next == &zone->blocklist)
|
|
|
|
break; // all blocks have been hit
|
|
|
|
if (check < (const char *) block
|
2012-11-19 12:35:27 +00:00
|
|
|
|| check >= (const char *) block + block->block_size)
|
2012-11-19 11:03:21 +00:00
|
|
|
continue;
|
|
|
|
// a block that overlaps with the memory region has been found
|
|
|
|
if (!block->tag)
|
|
|
|
zone->error (zone->data, "invalid access to unallocated memory");
|
|
|
|
block_mem = (char *) &block[1];
|
2012-11-19 12:50:42 +00:00
|
|
|
if (check < block_mem || check + size > block_mem + block->size)
|
2012-11-19 11:03:21 +00:00
|
|
|
zone->error (zone->data, "invalid access to allocated memory");
|
|
|
|
return; // access ok
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 16:08:45 +00:00
|
|
|
VISIBLE int
|
|
|
|
Z_IncRetainCount (memzone_t *zone, void *ptr)
|
|
|
|
{
|
|
|
|
memblock_t *block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
|
|
|
if (!++block->retain) {
|
|
|
|
z_error (zone, "inc retain count wrapped to 0");
|
|
|
|
}
|
|
|
|
return block->retain;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
Z_DecRetainCount (memzone_t *zone, void *ptr)
|
|
|
|
{
|
|
|
|
memblock_t *block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
|
|
|
if (--block->retain == -1) {
|
|
|
|
z_error (zone, "dec retain count wrapped past 0");
|
|
|
|
}
|
|
|
|
return block->retain;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE int
|
|
|
|
Z_GetRetainCount (memzone_t *zone, void *ptr)
|
|
|
|
{
|
|
|
|
memblock_t *block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
|
|
|
return block->retain;
|
|
|
|
}
|
|
|
|
|
2022-06-05 07:48:25 +00:00
|
|
|
VISIBLE int
|
|
|
|
Z_GetTag (memzone_t *zone, void *ptr)
|
|
|
|
{
|
|
|
|
memblock_t *block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
|
|
|
return block->tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void
|
|
|
|
Z_SetTag (memzone_t *zone, void *ptr, int tag)
|
|
|
|
{
|
|
|
|
if (!tag) {
|
|
|
|
z_error (zone, "Attept to set tag to 0");
|
|
|
|
}
|
|
|
|
memblock_t *block = (memblock_t *) ((byte *) ptr - sizeof (memblock_t));
|
|
|
|
block->tag = tag;
|
|
|
|
}
|
2021-12-27 11:12:03 +00:00
|
|
|
VISIBLE void
|
|
|
|
Z_MemInfo (const memzone_t *zone, size_t *used, size_t *size)
|
|
|
|
{
|
|
|
|
*used = zone->used;
|
|
|
|
*size = zone->size;
|
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
//============================================================================
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
typedef struct cache_system_s cache_system_t;
|
|
|
|
struct cache_system_s {
|
2021-12-13 02:23:25 +00:00
|
|
|
uint32_t prev;
|
|
|
|
uint32_t next;
|
|
|
|
uint32_t lru_prev;
|
|
|
|
uint32_t lru_next;
|
2021-07-28 06:01:45 +00:00
|
|
|
struct memhunk_s *hunk;
|
|
|
|
size_t size; // including this header
|
|
|
|
cache_user_t *user;
|
2021-12-13 02:23:25 +00:00
|
|
|
char name[16];
|
|
|
|
int readlock;
|
|
|
|
} __attribute__((aligned (64)));
|
2021-07-28 06:01:45 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
typedef struct {
|
2021-07-26 06:43:57 +00:00
|
|
|
int sentinal1;
|
|
|
|
int sentinal2;
|
2021-07-28 06:01:45 +00:00
|
|
|
size_t size; // including sizeof(hunkblk_t), -1 = not allocated
|
2021-12-13 07:33:09 +00:00
|
|
|
char name[16];
|
2021-07-28 06:01:45 +00:00
|
|
|
} __attribute__((aligned (64))) hunkblk_t;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
struct memhunk_s {
|
|
|
|
byte *base;
|
|
|
|
size_t size;
|
|
|
|
size_t low_used;
|
|
|
|
size_t high_used;
|
|
|
|
size_t tempmark;
|
|
|
|
qboolean tempactive;
|
2021-12-13 02:23:25 +00:00
|
|
|
cache_system_t cache_head[1];
|
2021-07-28 06:01:45 +00:00
|
|
|
} __attribute__((aligned (64)));
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-12-13 02:23:25 +00:00
|
|
|
static cache_system_t *
|
|
|
|
cs_ptr (memhunk_t *hunk, uint32_t cs_ind)
|
|
|
|
{
|
|
|
|
return &hunk->cache_head[cs_ind];
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
|
|
|
cs_ind (memhunk_t *hunk, cache_system_t *cs_ptr)
|
|
|
|
{
|
|
|
|
return cs_ptr - hunk->cache_head;
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
static memhunk_t *global_hunk;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
static int
|
|
|
|
hunk_check (memhunk_t *hunk, hunkblk_t *h, int err)
|
|
|
|
{
|
|
|
|
const char *msg = 0;
|
|
|
|
if (h->sentinal1 != HUNK_SENTINAL || h->sentinal2 != HUNK_SENTINAL) {
|
|
|
|
msg = "Hunk_Check: trashed sentinel";
|
|
|
|
}
|
|
|
|
if (!msg && (h->size < sizeof (hunkblk_t)
|
|
|
|
|| h->size + (byte *) h > hunk->base + hunk->size)) {
|
|
|
|
msg = "Hunk_Check: bad size";
|
|
|
|
}
|
|
|
|
if (!msg) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte *buf = (byte *) h;
|
|
|
|
int len = sizeof (*h);
|
|
|
|
int pos = 0, llen, i;
|
|
|
|
|
|
|
|
fflush (stdout);
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
while (pos < len) {
|
|
|
|
llen = (len - pos < 16 ? len - pos : 16);
|
|
|
|
fprintf (stderr, "%08x: ", pos);
|
|
|
|
for (i = 0; i < llen; i++)
|
|
|
|
fprintf (stderr, "%02x ", buf[pos + i]);
|
|
|
|
for (i = 0; i < 16 - llen; i++)
|
|
|
|
fprintf (stderr, " ");
|
|
|
|
fprintf (stderr, " | ");
|
|
|
|
|
|
|
|
for (i = 0; i < llen; i++)
|
|
|
|
fprintf (stderr, "%c", isprint (buf[pos + i]) ? buf[pos + i] : '.');
|
|
|
|
for (i = 0; i < 16 - llen; i++)
|
|
|
|
fprintf (stderr, " ");
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
pos += llen;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
Sys_Error ("%p: %zd: %s", h, (byte *) h - (byte *) hunk, msg);
|
|
|
|
}
|
|
|
|
fprintf (stderr, "%p: %zd: %s", h, (byte *) h - (byte *) hunk, msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
Hunk_Check
|
|
|
|
|
|
|
|
Run consistancy and sentinal trahing checks
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_Check (memhunk_t *hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
hunkblk_t *h;
|
|
|
|
byte *hunk_end = hunk->base + hunk->low_used;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
for (h = (hunkblk_t *) hunk->base; (byte *) h < hunk_end; ) {
|
2022-06-06 04:25:43 +00:00
|
|
|
hunk_check (hunk, h, 1);
|
2021-07-28 06:01:45 +00:00
|
|
|
h = (hunkblk_t *) ((byte *) h + h->size);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Hunk_Print
|
|
|
|
|
|
|
|
If "all" is specified, every single allocation is printed.
|
|
|
|
Otherwise, allocations with the same name will be totaled up before
|
|
|
|
printing.
|
|
|
|
*/
|
2021-02-03 04:21:08 +00:00
|
|
|
|
|
|
|
VISIBLE void
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_Print (memhunk_t *hunk, qboolean all)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
hunkblk_t *h, *next, *endlow, *starthigh, *endhigh;
|
2001-08-28 23:58:22 +00:00
|
|
|
int count, sum, totalblocks;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
count = 0;
|
|
|
|
sum = 0;
|
|
|
|
totalblocks = 0;
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
h = (hunkblk_t *) hunk->base;
|
|
|
|
endlow = (hunkblk_t *) (hunk->base + hunk->low_used);
|
|
|
|
starthigh = (hunkblk_t *) (hunk->base + hunk->size - hunk->high_used);
|
|
|
|
endhigh = (hunkblk_t *) (hunk->base + hunk->size);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
Sys_Printf (" :%8zd total hunk size\n", hunk->size);
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("-------------------------\n");
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// skip to the high hunk if done with low hunk
|
|
|
|
if (h == endlow) {
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("-------------------------\n");
|
2021-07-26 06:43:57 +00:00
|
|
|
Sys_Printf (" :%8zd REMAINING\n",
|
2021-07-28 06:01:45 +00:00
|
|
|
hunk->size - hunk->low_used - hunk->high_used);
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("-------------------------\n");
|
2001-02-21 19:35:06 +00:00
|
|
|
h = starthigh;
|
|
|
|
}
|
|
|
|
// if totally done, break
|
|
|
|
if (h == endhigh)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// run consistancy checks
|
2022-06-06 04:25:43 +00:00
|
|
|
if (!hunk_check (hunk, h, 0)) {
|
|
|
|
break;
|
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
next = (hunkblk_t *) ((byte *) h + h->size);
|
2001-02-21 19:35:06 +00:00
|
|
|
count++;
|
|
|
|
totalblocks++;
|
|
|
|
sum += h->size;
|
|
|
|
|
|
|
|
// print the single block
|
2021-12-13 07:33:09 +00:00
|
|
|
if (all) {
|
|
|
|
const int sz = sizeof (h->name);
|
2022-06-06 04:33:54 +00:00
|
|
|
Sys_Printf ("%8p :%8zd %*.*s\n", h, h->size, sz, sz,
|
|
|
|
h->name[0] ? h->name : "unknown");
|
2021-12-13 07:33:09 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
// print the total
|
|
|
|
if (next == endlow || next == endhigh ||
|
2021-12-13 07:33:09 +00:00
|
|
|
strncmp (h->name, next->name, sizeof (h->name))) {
|
|
|
|
if (!all) {
|
|
|
|
const int sz = sizeof (h->name);
|
|
|
|
Sys_Printf (" :%8i %*.*s (TOTAL)\n",
|
2022-06-06 04:33:54 +00:00
|
|
|
sum, sz, sz, h->name[0] ? h->name : "unknown");
|
2021-12-13 07:33:09 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
count = 0;
|
|
|
|
sum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
h = next;
|
|
|
|
}
|
|
|
|
|
2001-09-21 04:22:46 +00:00
|
|
|
Sys_Printf ("-------------------------\n");
|
|
|
|
Sys_Printf ("%8i total blocks\n", totalblocks);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2021-02-03 04:21:08 +00:00
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
static void
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToHighMark (memhunk_t *hunk, size_t mark)
|
2007-03-28 13:09:49 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
if (hunk->tempactive) {
|
|
|
|
hunk->tempactive = false;
|
|
|
|
Hunk_FreeToHighMark (hunk, hunk->tempmark);
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2021-07-29 02:44:10 +00:00
|
|
|
if (mark == hunk->high_used)
|
|
|
|
return;
|
2021-07-28 06:01:45 +00:00
|
|
|
if (mark > hunk->high_used)
|
2021-07-26 06:43:57 +00:00
|
|
|
Sys_Error ("Hunk_FreeToHighMark: bad mark %zd", mark);
|
2021-07-28 06:01:45 +00:00
|
|
|
memset (hunk->base + hunk->size - hunk->high_used, 0,
|
|
|
|
hunk->high_used - mark);
|
|
|
|
hunk->high_used = mark;
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_HighMark (memhunk_t *hunk)
|
2007-03-28 13:09:49 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
if (hunk->tempactive) {
|
|
|
|
hunk->tempactive = false;
|
|
|
|
Hunk_FreeToHighMark (hunk, hunk->tempmark);
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
return hunk->high_used;
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2022-06-06 04:33:54 +00:00
|
|
|
Hunk_RawAlloc (memhunk_t *hunk, size_t size)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
hunkblk_t *h;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
#ifdef PARANOID
|
[zone] Add failing test cases
The tests fail as they exercise how the cache *SHOULD* work rather than
how it does now.
The tests do currently pass for the pending work I've done on the cache
system, but while working on it, I remembered why I reworked cache
allocation...
The essential problem is that sounds are loaded into the cache, which is
fine for synchronous output targets, but has proven to be a minefield
for asynchronous output targets (JACK, ALSA).
The reason for the minefield is the hunk takes priority over the cache,
and is free to move cache blocks around, and *even dispose of them
entirely* in order to satisfy memory allocations from either end of the
hunk. Doing this in an entirely single-threaded process (as DOS Quake
was) is perfectly safe, as the users of the cache just reload the
pointer each time, and bail if it's null (meaning the block has been
freed), or even cause the data to be reloaded if possible (I'm a little
fuzzy on the details for that as I didn't write that code). However, in
multi-threaded code, especially real-time (JACK, possibly ALSA), it's a
recipe for disaster. The 4cab5b90e6379 commit was a (mostly) successful
attempt to mitigate the problem by allocating the cache blocks from the
high-hunk (thus minimizing any movement caused by low-hunk allocations),
it resulted in cache allocates and regular high-hunk allocations somehow
getting intertwined: while investigating just how much memory ad_tears
needs (somewhere between 192MB and 256MB), I got "trashed sentinel"
errors and upon investigation, I found what looks very suspiciously like
audio data written across a hunk control block.
I've decided that the cache allocation *algorithm* should be reverted to
how it was originally designed by Id (details will remain "modern"), but
while working on the tests, I remembered why I had done the changes in
the first place (above story). Thus the work on reverting the cache
allocation can't go in until I get sound memory management independent
of the cache. The tests are going in now so I have a constant reminder :)
2022-06-03 03:17:34 +00:00
|
|
|
Hunk_Check (hunk);
|
2001-02-21 19:35:06 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
size = sizeof (hunkblk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
if (hunk->size - hunk->low_used - hunk->high_used < size) {
|
2022-06-06 04:33:54 +00:00
|
|
|
Hunk_HighMark (hunk); // force free of temp hunk
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2021-07-28 06:01:45 +00:00
|
|
|
if (hunk->size - hunk->low_used - hunk->high_used < size) {
|
2022-05-31 11:48:50 +00:00
|
|
|
int mem = (hunk->size + sizeof (memhunk_t)) / (1024 * 1024);
|
2010-12-13 10:11:48 +00:00
|
|
|
mem += 8;
|
|
|
|
mem &= ~7;
|
2022-06-06 04:33:54 +00:00
|
|
|
Hunk_Print (hunk, 1);
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_Profile_r (hunk);
|
2001-02-21 19:35:06 +00:00
|
|
|
Sys_Error
|
2010-12-13 10:11:48 +00:00
|
|
|
("Not enough RAM allocated. Try starting using \"-mem %d\" on "
|
2021-07-26 06:43:57 +00:00
|
|
|
"the %s command line. (%zd - %zd - %zd < %zd)", mem,
|
2021-07-28 06:01:45 +00:00
|
|
|
PACKAGE_NAME, hunk->size, hunk->low_used, hunk->high_used, size);
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
h = (hunkblk_t *) (hunk->base + hunk->low_used);
|
|
|
|
hunk->low_used += size;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_FreeLow (hunk, hunk->low_used);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
h->size = size;
|
2021-07-26 06:43:57 +00:00
|
|
|
h->sentinal1 = HUNK_SENTINAL;
|
|
|
|
h->sentinal2 = HUNK_SENTINAL;
|
2022-06-06 04:33:54 +00:00
|
|
|
h->name[0] = 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
return (void *) (h + 1);
|
|
|
|
}
|
|
|
|
|
2021-07-29 02:44:10 +00:00
|
|
|
VISIBLE void *
|
2022-06-06 04:33:54 +00:00
|
|
|
Hunk_RawAllocName (memhunk_t *hunk, size_t size, const char *name)
|
2021-07-29 02:44:10 +00:00
|
|
|
{
|
2022-06-06 04:33:54 +00:00
|
|
|
void *mem = Hunk_RawAlloc (hunk, size);
|
|
|
|
hunkblk_t *h = ((hunkblk_t *) mem) - 1;
|
|
|
|
memccpy (h->name, name, 0, sizeof (h->name));
|
2021-07-29 02:44:10 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void *
|
2022-06-06 04:33:54 +00:00
|
|
|
Hunk_AllocName (memhunk_t *hunk, size_t size, const char *name)
|
2021-07-29 02:44:10 +00:00
|
|
|
{
|
2022-06-06 04:33:54 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
void *mem = Hunk_RawAllocName (hunk, size, name);
|
|
|
|
memset (mem, 0, size);
|
|
|
|
return mem;
|
2021-07-29 02:44:10 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_Alloc (memhunk_t *hunk, size_t size)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
2022-06-06 04:33:54 +00:00
|
|
|
void *mem = Hunk_RawAlloc (hunk, size);
|
|
|
|
memset (mem, 0, size);
|
|
|
|
return mem;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 06:43:57 +00:00
|
|
|
VISIBLE size_t
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_LowMark (memhunk_t *hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
return hunk->low_used;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-29 02:44:10 +00:00
|
|
|
VISIBLE void
|
|
|
|
Hunk_RawFreeToLowMark (memhunk_t *hunk, size_t mark)
|
|
|
|
{
|
|
|
|
if (mark == hunk->low_used)
|
|
|
|
return;
|
|
|
|
if (mark > hunk->low_used)
|
|
|
|
Sys_Error ("Hunk_FreeToLowMark: bad mark %zd", mark);
|
|
|
|
hunk->low_used = mark;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToLowMark (memhunk_t *hunk, size_t mark)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
2021-07-29 02:44:10 +00:00
|
|
|
if (mark == hunk->low_used)
|
|
|
|
return;
|
2021-07-28 06:01:45 +00:00
|
|
|
if (mark > hunk->low_used)
|
2021-07-26 06:43:57 +00:00
|
|
|
Sys_Error ("Hunk_FreeToLowMark: bad mark %zd", mark);
|
2021-07-28 06:01:45 +00:00
|
|
|
memset (hunk->base + mark, 0, hunk->low_used - mark);
|
|
|
|
hunk->low_used = mark;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
static void *
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_HighAlloc (memhunk_t *hunk, size_t size)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
hunkblk_t *h;
|
2021-02-03 04:21:08 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
if (hunk->tempactive) {
|
|
|
|
Hunk_FreeToHighMark (hunk, hunk->tempmark);
|
|
|
|
hunk->tempactive = false;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
#ifdef PARANOID
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_Check (hunk);
|
2001-02-21 19:35:06 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
size = sizeof (hunkblk_t) + ((size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1));
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
if (hunk->size - hunk->low_used - hunk->high_used < size) {
|
2021-07-26 06:43:57 +00:00
|
|
|
Sys_Printf ("Hunk_HighAlloc: failed on %zd bytes\n", size);
|
2001-02-21 19:35:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
hunk->high_used += size;
|
2022-06-06 04:25:43 +00:00
|
|
|
Cache_FreeHigh (hunk, hunk->high_used);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
h = (void *) (hunk->base + hunk->size - hunk->high_used);
|
2021-07-26 06:43:57 +00:00
|
|
|
h->sentinal1 = HUNK_SENTINAL;
|
|
|
|
h->sentinal2 = HUNK_SENTINAL;
|
2021-02-03 04:21:08 +00:00
|
|
|
h->size = size;
|
|
|
|
h->name[0] = 0;
|
|
|
|
return h + 1;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Hunk_TempAlloc
|
|
|
|
|
|
|
|
Return space from the top of the hunk
|
|
|
|
*/
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_TempAlloc (memhunk_t *hunk, size_t size)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
2001-02-21 19:35:06 +00:00
|
|
|
void *buf;
|
|
|
|
|
2021-02-03 04:19:19 +00:00
|
|
|
size = (size + HUNK_ALIGN - 1) & ~(HUNK_ALIGN - 1);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
if (hunk->tempactive) {
|
|
|
|
size_t temp_free = hunk->high_used - hunk->tempmark;
|
|
|
|
if (temp_free >= size + (int) sizeof (hunkblk_t)) {
|
|
|
|
byte *temp_block = hunk->base + hunk->size - hunk->high_used;
|
|
|
|
return (hunkblk_t *) temp_block + 1;
|
2001-12-03 08:46:56 +00:00
|
|
|
}
|
2021-07-28 06:01:45 +00:00
|
|
|
Hunk_FreeToHighMark (hunk, hunk->tempmark);
|
|
|
|
hunk->tempactive = false;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
hunk->tempmark = Hunk_HighMark (hunk);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
buf = Hunk_HighAlloc (hunk, size);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
hunk->tempactive = true;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-07-29 06:27:48 +00:00
|
|
|
VISIBLE int
|
|
|
|
Hunk_PointerIsValid (memhunk_t *hunk, void *ptr)
|
|
|
|
{
|
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
|
|
|
|
size_t offset = (byte *) ptr - hunk->base;
|
|
|
|
if (offset >= hunk->size) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (offset < hunk->low_used) {
|
|
|
|
// the pointer is somewhere in the lower space of the hunk
|
|
|
|
// FIXME better checking?
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (offset >= hunk->size - hunk->high_used + sizeof (hunkblk_t)) {
|
|
|
|
// the pointer is somewhere in the upper space of the hunk
|
|
|
|
// FIXME better checking?
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// the pointer is somewhere in between the two marks, so it has probably
|
|
|
|
// been freed
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-08-28 23:58:22 +00:00
|
|
|
/* CACHE MEMORY */
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2003-08-11 06:05:07 +00:00
|
|
|
static inline void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_UnlinkLRU (cache_system_t * cs)
|
|
|
|
{
|
2021-12-13 02:23:25 +00:00
|
|
|
memhunk_t *hunk = cs->hunk;
|
|
|
|
cs_ptr (hunk, cs->lru_next)->lru_prev = cs->lru_prev;
|
|
|
|
cs_ptr (hunk, cs->lru_prev)->lru_next = cs->lru_next;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-12-13 02:23:25 +00:00
|
|
|
cs->lru_prev = cs->lru_next = 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2011-05-04 00:31:18 +00:00
|
|
|
static void
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_MakeLRU (cache_system_t * cs)
|
|
|
|
{
|
2021-12-13 02:23:25 +00:00
|
|
|
memhunk_t *hunk = cs->hunk;
|
|
|
|
__auto_type nx = cs_ptr (hunk, hunk->cache_head[0].lru_next);
|
|
|
|
nx->lru_prev = cs_ind (hunk, cs);
|
|
|
|
cs->lru_next = cs_ind (hunk, nx);
|
|
|
|
cs->lru_prev = 0;
|
|
|
|
hunk->cache_head[0].lru_next = cs_ind (hunk, cs);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
static void
|
|
|
|
link_cache_system (cache_system_t *new, cache_system_t *cs)
|
|
|
|
{
|
2021-12-13 02:23:25 +00:00
|
|
|
memhunk_t *hunk = cs->hunk;
|
|
|
|
new->next = cs_ind (hunk, cs);
|
2007-03-28 13:09:49 +00:00
|
|
|
new->prev = cs->prev;
|
2021-12-13 02:23:25 +00:00
|
|
|
cs_ptr (hunk, cs->prev)->next = cs_ind (hunk, new);
|
|
|
|
cs->prev = cs_ind (hunk, new);
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
/*
|
|
|
|
Cache_TryAlloc
|
|
|
|
|
|
|
|
Looks for a free block of memory between the high and low hunk marks
|
|
|
|
Size should already include the header and padding
|
|
|
|
*/
|
2002-11-05 19:12:51 +00:00
|
|
|
static cache_system_t *
|
2022-06-06 04:25:43 +00:00
|
|
|
Cache_TryAlloc (memhunk_t *hunk, size_t size,
|
|
|
|
size_t low_space, size_t high_space)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cache_system_t *cs, *new;
|
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
low_space = max (low_space, hunk->low_used);
|
|
|
|
high_space = max (high_space, hunk->high_used);
|
|
|
|
|
|
|
|
if (hunk->cache_head[0].prev == 0) {
|
|
|
|
// The cache is completely empty, so just check for space
|
|
|
|
if (hunk->size - high_space < low_space + size) {
|
2007-03-28 13:09:49 +00:00
|
|
|
return 0;
|
2022-06-06 04:25:43 +00:00
|
|
|
}
|
|
|
|
// cache memory comes from the free region of the hunk. Should either
|
|
|
|
// end of the hunk need to grow, interfering cache blocks will be
|
|
|
|
// either moved or freed if there is nowhere to move the block.
|
|
|
|
new = (cache_system_t *) (hunk->base + hunk->low_used);
|
2001-02-21 19:35:06 +00:00
|
|
|
new->size = size;
|
2021-07-28 06:01:45 +00:00
|
|
|
new->hunk = hunk;
|
2021-12-13 02:23:25 +00:00
|
|
|
hunk->cache_head[0].prev = cs_ind (hunk, new);
|
|
|
|
hunk->cache_head[0].next = cs_ind (hunk, new);
|
|
|
|
new->prev = new->next = 0;
|
2021-12-13 02:49:37 +00:00
|
|
|
new->readlock = 0;
|
|
|
|
new->name[0] = 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_MakeLRU (new);
|
|
|
|
return new;
|
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
new = (cache_system_t *) (hunk->base + low_space);
|
|
|
|
uint32_t csi = hunk->cache_head[0].next;
|
|
|
|
do {
|
|
|
|
cs = cs_ptr (hunk, csi);
|
|
|
|
if ((byte *) cs >= (byte *) new + size) {
|
|
|
|
|
|
|
|
new->size = size;
|
|
|
|
new->hunk = hunk;
|
|
|
|
|
|
|
|
link_cache_system (new, cs);
|
|
|
|
|
|
|
|
new->readlock = 0;
|
|
|
|
new->name[0] = 0;
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
Cache_MakeLRU (new);
|
|
|
|
return new;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2022-06-06 04:25:43 +00:00
|
|
|
|
|
|
|
// try next block. If it is a hole, then the resulting cs will be
|
|
|
|
// greater than new (though possibly not sufficiently so), but if
|
|
|
|
// it's not a hole, then they'll be the same and the difference in
|
|
|
|
// the test above will be 0
|
|
|
|
new = (cache_system_t *) ((byte *) cs + cs->size);
|
|
|
|
csi = cs->next;
|
|
|
|
} while (csi);
|
|
|
|
|
|
|
|
// came to the end of the cache. try to allocate from between the cache
|
|
|
|
// and the high hunk
|
|
|
|
if ((byte *) new < hunk->base + low_space) {
|
|
|
|
new = (cache_system_t *) (hunk->base + low_space);
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2022-06-06 04:25:43 +00:00
|
|
|
if (hunk->base + hunk->size - high_space >= (byte *) new + size) {
|
|
|
|
new->size = size;
|
|
|
|
new->hunk = hunk;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
link_cache_system (new, hunk->cache_head);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-12-13 02:49:37 +00:00
|
|
|
new->readlock = 0;
|
|
|
|
new->name[0] = 0;
|
2022-06-06 04:25:43 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_MakeLRU (new);
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
return 0; // couldn't allocate
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
static void
|
|
|
|
Cache_Move (cache_system_t *c, size_t new_low_hunk, size_t new_high_hunk)
|
|
|
|
{
|
|
|
|
memhunk_t *hunk = c->hunk;
|
|
|
|
cache_system_t *new;
|
|
|
|
|
|
|
|
new = Cache_TryAlloc (hunk, c->size, new_low_hunk, new_high_hunk);
|
|
|
|
if (new) {
|
|
|
|
Sys_MaskPrintf (SYS_cache, "cache_move ok\n");
|
|
|
|
|
|
|
|
memcpy (new + 1, c + 1, c->size - sizeof (cache_system_t));
|
|
|
|
new->user = c->user;
|
|
|
|
memccpy (new->name, c->name, 0, sizeof (new->name));
|
|
|
|
Cache_Free (c->user);
|
|
|
|
new->user->data = (void *) (new + 1);
|
|
|
|
} else {
|
|
|
|
Sys_MaskPrintf (SYS_cache, "cache_move failed\n");
|
|
|
|
|
|
|
|
Cache_Free (c->user); // tough luck...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cache_FreeLow
|
|
|
|
|
|
|
|
Throw things out until the hunk can be expanded to the given point
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
Cache_FreeLow (memhunk_t *hunk, size_t new_low_hunk)
|
|
|
|
{
|
|
|
|
cache_system_t *c;
|
|
|
|
uint32_t ci;
|
|
|
|
|
|
|
|
// if next is 0, then there is nothing in the cache
|
|
|
|
while ((ci = hunk->cache_head[0].next)) {
|
|
|
|
c = cs_ptr (hunk, ci);
|
|
|
|
if ((byte *) c >= hunk->base + new_low_hunk) {
|
|
|
|
// there is space to grow the hunk
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// reclaim the space (the block will be moved or freed)
|
|
|
|
Cache_Move (c, new_low_hunk, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Cache_FreeHigh (memhunk_t *hunk, size_t new_high_hunk)
|
|
|
|
{
|
|
|
|
cache_system_t *c;
|
|
|
|
uint32_t ci;
|
|
|
|
while ((ci = hunk->cache_head[0].prev)) {
|
|
|
|
c = cs_ptr (hunk, ci);
|
|
|
|
if ((byte *) c + c->size <= hunk->base + hunk->size - new_high_hunk) {
|
|
|
|
// there is space to grow the hunk
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// reclaim the space (the block will be moved or freed)
|
|
|
|
Cache_Move (c, 0, new_high_hunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static qboolean
|
|
|
|
Cache_FreeLRU (memhunk_t *hunk)
|
|
|
|
{
|
|
|
|
cache_system_t *cs;
|
|
|
|
|
|
|
|
for (cs = cs_ptr (hunk, hunk->cache_head[0].lru_prev);
|
|
|
|
cs != hunk->cache_head && cs->readlock;
|
|
|
|
cs = cs_ptr (hunk, cs->lru_prev)) {
|
|
|
|
}
|
|
|
|
if (cs == hunk->cache_head)
|
|
|
|
return 0;
|
|
|
|
Cache_Free (cs->user);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
static void
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_Profile_r (memhunk_t *hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2007-03-28 13:09:49 +00:00
|
|
|
unsigned int i;
|
|
|
|
unsigned int items[31] = {0}, sizes[31] = {0};
|
|
|
|
int count = 0, total = 0;
|
2021-12-13 02:23:25 +00:00
|
|
|
cache_system_t *cs;
|
2007-03-28 13:09:49 +00:00
|
|
|
|
2021-12-13 02:23:25 +00:00
|
|
|
for (uint32_t ind = hunk->cache_head[0].next; ind; ind = cs->next) {
|
|
|
|
cs = cs_ptr (hunk, ind);
|
|
|
|
for (i = 0; (cs->size >> (i + 1)) && i < 30; i++) {
|
|
|
|
}
|
2007-03-28 13:09:49 +00:00
|
|
|
items[i]++;
|
|
|
|
sizes[i] += cs->size;
|
|
|
|
total += cs->size;
|
|
|
|
count++;
|
2021-12-13 02:23:25 +00:00
|
|
|
ind = cs->next;
|
2001-11-25 03:16:15 +00:00
|
|
|
}
|
2007-03-28 13:09:49 +00:00
|
|
|
Sys_Printf ("Cache Profile:\n");
|
|
|
|
Sys_Printf ("%8s %8s %8s %8s %8s\n",
|
|
|
|
"count", "min", "max", "average", "percent");
|
|
|
|
for (i = 0; i < 31; i++) {
|
|
|
|
if (!items[i])
|
|
|
|
continue;
|
|
|
|
Sys_Printf ("%8d %8d %8d %8d %7d%%\n",
|
|
|
|
items[i], 1 << i, (1 << (i + 1)) - 1,
|
|
|
|
sizes[i] / items[i],
|
|
|
|
(sizes[i] * 100) / total);
|
|
|
|
}
|
|
|
|
Sys_Printf ("Total allocations: %d in %d allocations, average of"
|
2012-01-23 07:46:52 +00:00
|
|
|
" %d per allocation\n", total, count,
|
|
|
|
count ? total / count : -1);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_Profile (void)
|
|
|
|
{
|
|
|
|
Cache_Profile_r (global_hunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Cache_Print_r (memhunk_t *hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-12-13 02:23:25 +00:00
|
|
|
cache_system_t *cs;
|
|
|
|
for (uint32_t ind = hunk->cache_head[0].next; ind; ind = cs->next) {
|
2021-12-13 07:33:09 +00:00
|
|
|
const int sz = sizeof (cs->name);
|
2021-12-13 02:23:25 +00:00
|
|
|
cs = cs_ptr (hunk, ind);
|
2021-12-13 07:33:09 +00:00
|
|
|
Sys_Printf ("%8zd : %.*s\n", cs->size, sz, cs->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_Print (void)
|
|
|
|
{
|
|
|
|
Cache_Print_r (global_hunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
init_cache (memhunk_t *hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-12-13 02:23:25 +00:00
|
|
|
hunk->cache_head[0].hunk = hunk;
|
|
|
|
hunk->cache_head[0].size = 0;
|
|
|
|
hunk->cache_head[0].next = hunk->cache_head[0].prev = 0;
|
|
|
|
hunk->cache_head[0].lru_next = hunk->cache_head[0].lru_prev = 0;
|
|
|
|
hunk->cache_head[0].user = (cache_user_t *) 1; // make it look allocated
|
|
|
|
hunk->cache_head[0].readlock = 1; // don't try to free or move it
|
2021-07-28 06:01:45 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
static void
|
|
|
|
Cache_Init (void)
|
|
|
|
{
|
2002-08-25 04:47:57 +00:00
|
|
|
Cmd_AddCommand ("cache_flush", Cache_Flush, "Clears the current game "
|
|
|
|
"cache");
|
2001-08-28 23:58:22 +00:00
|
|
|
Cmd_AddCommand ("cache_profile", Cache_Profile, "Prints a profile of "
|
|
|
|
"the current cache");
|
2002-08-25 04:47:57 +00:00
|
|
|
Cmd_AddCommand ("cache_print", Cache_Print, "Prints out items in the "
|
|
|
|
"cache");
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-03-28 13:09:49 +00:00
|
|
|
Cache_Flush
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
Throw everything out, so new data will be demand cached
|
2001-02-21 19:35:06 +00:00
|
|
|
*/
|
2021-07-28 06:01:45 +00:00
|
|
|
static void
|
|
|
|
Cache_Flush_r (memhunk_t *hunk)
|
2007-03-28 13:09:49 +00:00
|
|
|
{
|
2007-03-30 07:41:17 +00:00
|
|
|
// cache_head.prev is guaranteed to not be free because it's the bottom
|
|
|
|
// one and Cache_Free actually properly releases it
|
2021-12-13 02:23:25 +00:00
|
|
|
while (hunk->cache_head[0].prev) {
|
|
|
|
__auto_type cs = cs_ptr (hunk, hunk->cache_head[0].prev);
|
2021-12-13 07:33:09 +00:00
|
|
|
if (!cs->user->data) {
|
|
|
|
const int sz = sizeof (cs->name);
|
2007-03-28 13:09:49 +00:00
|
|
|
Sys_Error ("Cache_Flush: user/system out of sync for "
|
2021-12-13 07:33:09 +00:00
|
|
|
"'%.*s' with %zd size",
|
|
|
|
sz, cs->name, cs->size);
|
|
|
|
}
|
2021-12-13 02:23:25 +00:00
|
|
|
Cache_Free (cs->user); // reclaim the space
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
VISIBLE void
|
|
|
|
Cache_Flush (void)
|
|
|
|
{
|
|
|
|
// cache_head.prev is guaranteed to not be free because it's the bottom
|
|
|
|
Cache_Flush_r (global_hunk);
|
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
VISIBLE void *
|
|
|
|
Cache_Check (cache_user_t *c)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cache_system_t *cs;
|
|
|
|
|
|
|
|
if (!c->data)
|
2007-03-28 13:09:49 +00:00
|
|
|
return NULL;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
cs = ((cache_system_t *) c->data) - 1;
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
// move to head of LRU
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_UnlinkLRU (cs);
|
2007-03-28 13:09:49 +00:00
|
|
|
Cache_MakeLRU (cs);
|
2001-08-28 23:26:58 +00:00
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
return c->data;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
/*
|
|
|
|
Cache_Free
|
|
|
|
|
|
|
|
Frees the memory and removes it from the LRU list
|
|
|
|
*/
|
|
|
|
VISIBLE void
|
|
|
|
Cache_Free (cache_user_t *c)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cache_system_t *cs;
|
|
|
|
|
|
|
|
if (!c->data)
|
2007-03-28 13:09:49 +00:00
|
|
|
Sys_Error ("Cache_Free: not allocated");
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
cs = ((cache_system_t *) c->data) - 1;
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
if (cs->readlock)
|
|
|
|
Sys_Error ("Cache_Free: attempt to free locked block");
|
|
|
|
|
2021-12-13 07:33:09 +00:00
|
|
|
const int sz = sizeof (cs->name);
|
2022-06-03 03:04:27 +00:00
|
|
|
Sys_MaskPrintf (SYS_cache, "Cache_Free: freeing '%.*s' %p\n",
|
2021-12-13 07:33:09 +00:00
|
|
|
sz, cs->name, cs);
|
2007-03-28 13:09:49 +00:00
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
memhunk_t *hunk = cs->hunk;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2022-06-06 04:25:43 +00:00
|
|
|
cs_ptr (hunk, cs->prev)->next = cs->next;
|
|
|
|
cs_ptr (hunk, cs->next)->prev = cs->prev;
|
|
|
|
cs->next = cs->prev = 0;
|
2007-03-28 13:09:49 +00:00
|
|
|
|
|
|
|
c->data = NULL;
|
2022-06-06 04:25:43 +00:00
|
|
|
|
|
|
|
Cache_UnlinkLRU (cs);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
static void *
|
|
|
|
Cache_Alloc_r (memhunk_t *hunk, cache_user_t *c, size_t size, const char *name)
|
2001-08-30 11:37:50 +00:00
|
|
|
{
|
|
|
|
cache_system_t *cs;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
if (c->data)
|
2021-07-28 06:01:45 +00:00
|
|
|
Sys_Error ("Cache_Alloc_r: already allocated");
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
if (size <= 0)
|
2021-07-28 06:01:45 +00:00
|
|
|
Sys_Error ("Cache_Alloc_r: size %zd", size);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2021-02-03 04:19:19 +00:00
|
|
|
size = (size + sizeof (cache_system_t) + HUNK_ALIGN - 1) & ~(HUNK_ALIGN-1);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2012-05-21 23:23:22 +00:00
|
|
|
// find memory for it
|
2001-02-21 19:35:06 +00:00
|
|
|
while (1) {
|
2022-06-06 04:25:43 +00:00
|
|
|
cs = Cache_TryAlloc (hunk, size, 0, 0);
|
2001-02-21 19:35:06 +00:00
|
|
|
if (cs) {
|
2021-12-15 07:40:51 +00:00
|
|
|
memccpy (cs->name, name, 0, sizeof (cs->name));
|
2001-02-21 19:35:06 +00:00
|
|
|
c->data = (void *) (cs + 1);
|
|
|
|
cs->user = c;
|
|
|
|
break;
|
|
|
|
}
|
2001-08-29 15:29:17 +00:00
|
|
|
// free the least recently used cachedat
|
2021-07-28 06:01:45 +00:00
|
|
|
if (!Cache_FreeLRU (hunk))
|
2001-02-21 19:35:06 +00:00
|
|
|
Sys_Error ("Cache_Alloc: out of memory");
|
|
|
|
}
|
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
return Cache_Check (c);
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
VISIBLE void *
|
|
|
|
Cache_Alloc (cache_user_t *c, size_t size, const char *name)
|
|
|
|
{
|
|
|
|
return Cache_Alloc_r (global_hunk, c, size, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Cache_Report_r (memhunk_t *hunk)
|
|
|
|
{
|
2022-06-03 03:04:27 +00:00
|
|
|
Sys_Printf ("%4.1f megabyte data cache\n",
|
|
|
|
(hunk->size - hunk->high_used -
|
|
|
|
hunk->low_used) / (float) (1024 * 1024));
|
2021-07-28 06:01:45 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
VISIBLE void
|
|
|
|
Cache_Report (void)
|
2001-08-28 23:26:58 +00:00
|
|
|
{
|
2022-06-03 03:04:27 +00:00
|
|
|
if (developer & SYS_cache) {
|
|
|
|
Cache_Report_r (global_hunk);
|
|
|
|
}
|
2001-08-28 23:26:58 +00:00
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-08-31 12:22:45 +00:00
|
|
|
Cache_Add (cache_user_t *c, void *object, cache_loader_t loader)
|
2001-08-30 11:37:50 +00:00
|
|
|
{
|
2001-08-31 12:22:45 +00:00
|
|
|
if (c->data || c->object || c->loader)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Cache_Add: cache item already exists!");
|
2001-08-30 11:37:50 +00:00
|
|
|
|
2001-08-31 12:22:45 +00:00
|
|
|
c->object = object;
|
2001-08-30 11:37:50 +00:00
|
|
|
c->loader = loader;
|
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
// c->loader (c, Cache_Alloc); // for debugging
|
2001-08-30 11:37:50 +00:00
|
|
|
}
|
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
VISIBLE void
|
2001-08-30 11:37:50 +00:00
|
|
|
Cache_Remove (cache_user_t *c)
|
|
|
|
{
|
2001-08-31 12:22:45 +00:00
|
|
|
if (!c->object || !c->loader)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Cache_Remove: already removed!");
|
2001-08-30 11:37:50 +00:00
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
if (Cache_Check (c))
|
|
|
|
Cache_Free (c);
|
2001-08-30 11:37:50 +00:00
|
|
|
|
2001-08-31 12:22:45 +00:00
|
|
|
c->object = 0;
|
2001-08-30 11:37:50 +00:00
|
|
|
c->loader = 0;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2001-08-31 03:48:26 +00:00
|
|
|
Cache_TryGet (cache_user_t *c)
|
2001-08-30 11:37:50 +00:00
|
|
|
{
|
|
|
|
void *mem;
|
2002-08-25 04:47:57 +00:00
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
mem = Cache_Check (c);
|
2001-08-30 11:37:50 +00:00
|
|
|
if (!mem) {
|
2007-03-28 09:52:01 +00:00
|
|
|
c->loader (c->object, Cache_Alloc);
|
|
|
|
mem = Cache_Check (c);
|
2001-08-30 11:37:50 +00:00
|
|
|
}
|
2001-08-31 03:48:26 +00:00
|
|
|
if (mem)
|
|
|
|
(((cache_system_t *)c->data) - 1)->readlock++;
|
|
|
|
|
|
|
|
return mem;
|
|
|
|
}
|
2001-08-30 11:37:50 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void *
|
2001-08-31 03:48:26 +00:00
|
|
|
Cache_Get (cache_user_t *c)
|
|
|
|
{
|
|
|
|
void *mem = Cache_TryGet (c);
|
2002-08-25 04:47:57 +00:00
|
|
|
|
2001-08-30 11:37:50 +00:00
|
|
|
if (!mem)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Cache_Get: couldn't get cache!");
|
2001-08-30 11:37:50 +00:00
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-08-30 11:37:50 +00:00
|
|
|
Cache_Release (cache_user_t *c)
|
|
|
|
{
|
|
|
|
int *readlock;
|
2002-08-25 04:47:57 +00:00
|
|
|
|
2001-08-30 11:37:50 +00:00
|
|
|
readlock = &(((cache_system_t *)c->data) - 1)->readlock;
|
|
|
|
|
|
|
|
if (!*readlock)
|
2002-05-14 06:12:29 +00:00
|
|
|
Sys_Error ("Cache_Release: already released!");
|
2001-08-30 11:37:50 +00:00
|
|
|
|
|
|
|
(*readlock)--;
|
|
|
|
|
|
|
|
// if (!*readlock)
|
2007-03-28 09:52:01 +00:00
|
|
|
// Cache_Free (c); // for debugging
|
2001-08-30 11:37:50 +00:00
|
|
|
}
|
|
|
|
|
2007-03-23 14:22:59 +00:00
|
|
|
VISIBLE int
|
2007-03-23 12:36:55 +00:00
|
|
|
Cache_ReadLock (cache_user_t *c)
|
|
|
|
{
|
|
|
|
return (((cache_system_t *)c->data) - 1)->readlock;
|
|
|
|
}
|
|
|
|
|
2001-08-29 09:14:18 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
//============================================================================
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
VISIBLE memhunk_t *
|
|
|
|
Hunk_Init (void *buf, size_t size)
|
|
|
|
{
|
|
|
|
memhunk_t *hunk = buf;
|
|
|
|
hunk->base = (byte *) (hunk + 1);
|
|
|
|
hunk->size = size - sizeof (memhunk_t);
|
|
|
|
hunk->low_used = 0;
|
|
|
|
hunk->high_used = 0;
|
|
|
|
|
|
|
|
init_cache (hunk);
|
|
|
|
return hunk;
|
|
|
|
}
|
|
|
|
|
2021-12-13 00:07:43 +00:00
|
|
|
VISIBLE memhunk_t *
|
2021-07-26 06:43:57 +00:00
|
|
|
Memory_Init (void *buf, size_t size)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
global_hunk = Hunk_Init (buf, size);
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_Init ();
|
2021-12-13 00:07:43 +00:00
|
|
|
return global_hunk;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|