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>
|
|
|
|
|
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"
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
static void Cache_FreeLow (memhunk_t *hunk, int new_low_hunk);
|
|
|
|
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
|
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
|
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
|
|
|
|
2011-03-20 06:50:09 +00:00
|
|
|
static int
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
memblock_t *block;
|
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
|
|
|
}
|
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
|
|
|
|
2021-03-29 10:58:00 +00:00
|
|
|
if (!developer || developer->int_val & SYS_dev)
|
2001-08-28 06:15:58 +00:00
|
|
|
Z_CheckHeap (zone); // DEBUG
|
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
|
|
|
|
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
|
|
|
|
size = (size + 7) & ~7; // align to 8-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
|
|
|
|
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);
|
|
|
|
|
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) {
|
2020-03-31 07:03:54 +00:00
|
|
|
Sys_Printf ("block:%p size:%7i tag:%5x ofs:%x\n",
|
2011-03-08 13:44:56 +00:00
|
|
|
block, z_block_size (block),
|
|
|
|
block->tag, 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");
|
2004-11-14 00:49:00 +00:00
|
|
|
if (block->tag
|
2012-11-19 12:35:27 +00:00
|
|
|
&& (*(int *) ((byte *) block + block->block_size - 4) != ZONEID))
|
2004-11-14 00:49:00 +00:00
|
|
|
Sys_Printf ("ERROR: memory trashed in block\n");
|
|
|
|
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)
|
2001-08-28 23:58:22 +00:00
|
|
|
Sys_Error ("Z_CheckHeap: block size does not touch the next "
|
|
|
|
"block\n");
|
2012-11-19 11:03:21 +00:00
|
|
|
if (block->next->prev != block)
|
2001-08-28 23:58:22 +00:00
|
|
|
Sys_Error ("Z_CheckHeap: next block doesn't have proper back "
|
|
|
|
"link\n");
|
2001-08-28 04:24:40 +00:00
|
|
|
if (!block->tag && !block->next->tag)
|
|
|
|
Sys_Error ("Z_CheckHeap: two consecutive free blocks\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-11-19 11:03:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2001-02-21 19:35:06 +00:00
|
|
|
char name[8];
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
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; ) {
|
2021-07-26 06:43:57 +00:00
|
|
|
if (h->sentinal1 != HUNK_SENTINAL || h->sentinal2 != HUNK_SENTINAL)
|
2001-02-21 19:35:06 +00:00
|
|
|
Sys_Error ("Hunk_Check: trashed sentinal");
|
2021-07-28 06:01:45 +00:00
|
|
|
if (h->size < sizeof (hunkblk_t)
|
|
|
|
|| h->size + (byte *) h > hunk->base + hunk->size)
|
2001-02-21 19:35:06 +00:00
|
|
|
Sys_Error ("Hunk_Check: bad size");
|
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
|
2021-07-26 06:43:57 +00:00
|
|
|
if (h->sentinal1 != HUNK_SENTINAL || h->sentinal2 != HUNK_SENTINAL)
|
2001-02-21 19:35:06 +00:00
|
|
|
Sys_Error ("Hunk_Check: trahsed sentinal");
|
2021-07-28 06:01:45 +00:00
|
|
|
if (h->size < (int) sizeof (hunkblk_t)
|
|
|
|
|| h->size + (byte *) h > hunk->base + hunk->size)
|
2001-02-21 19:35:06 +00:00
|
|
|
Sys_Error ("Hunk_Check: bad size");
|
|
|
|
|
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
|
|
|
|
if (all)
|
2021-07-26 06:43:57 +00:00
|
|
|
Sys_Printf ("%8p :%8zd %8.8s\n", h, h->size, h->name);
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
// print the total
|
|
|
|
if (next == endlow || next == endhigh ||
|
|
|
|
strncmp (h->name, next->name, 8)) {
|
|
|
|
if (!all)
|
2021-02-03 04:21:08 +00:00
|
|
|
Sys_Printf (" :%8i %8.8s (TOTAL)\n", sum, h->name);
|
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 *
|
2021-07-29 02:44:10 +00:00
|
|
|
Hunk_RawAllocName (memhunk_t *hunk, size_t size, const char *name)
|
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
|
|
|
|
Hunk_Check ();
|
|
|
|
#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) {
|
|
|
|
Hunk_HighMark (hunk);
|
|
|
|
Cache_FreeLRU (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) {
|
|
|
|
int mem = hunk->size / (1024 * 1024);
|
2010-12-13 10:11:48 +00:00
|
|
|
mem += 8;
|
|
|
|
mem &= ~7;
|
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;
|
2020-12-20 17:12:51 +00:00
|
|
|
memcpy (h->name, name, 8);
|
|
|
|
h->name[7] = 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
return (void *) (h + 1);
|
|
|
|
}
|
|
|
|
|
2021-07-29 02:44:10 +00:00
|
|
|
VISIBLE void *
|
|
|
|
Hunk_AllocName (memhunk_t *hunk, size_t size, const char *name)
|
|
|
|
{
|
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
void *mem = Hunk_RawAllocName (hunk, size, name);
|
|
|
|
memset (mem, 0, size);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
VISIBLE void *
|
|
|
|
Hunk_RawAlloc (memhunk_t *hunk, size_t size)
|
|
|
|
{
|
|
|
|
return Hunk_RawAllocName (hunk, size, "unknown");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
return Hunk_AllocName (hunk, size, "unknown");
|
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;
|
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
|
|
|
|
|
|
|
|
2021-07-28 06:01:45 +00:00
|
|
|
static cache_system_t *Cache_TryAlloc (memhunk_t *hunk, size_t size,
|
|
|
|
qboolean nobottom);
|
2007-03-30 07:41:17 +00:00
|
|
|
#if 0
|
|
|
|
static void
|
|
|
|
check_cache (void)
|
|
|
|
{
|
|
|
|
cache_system_t *cs;
|
|
|
|
int used = hunk_tempactive ? hunk_tempmark : hunk_high_used;
|
2007-03-28 09:52:01 +00:00
|
|
|
|
2007-03-30 07:41:17 +00:00
|
|
|
for (cs = cache_head.prev; cs != &cache_head; cs = cs->prev)
|
|
|
|
if (cs->prev != &cache_head) {
|
|
|
|
if ((byte *) cs + cs->size != (byte *) cs->prev)
|
|
|
|
Sys_Error ("inconsistent cache %p %p %d %d", cs, cs->prev,
|
|
|
|
(int)cs->size,
|
|
|
|
(int) ((char *)cs->prev - (char *)cs));
|
|
|
|
if (hunk_size - ((byte*)cs - hunk_base) > used)
|
|
|
|
Sys_Error ("cache block out of high hunk");
|
|
|
|
}
|
|
|
|
if (cache_head.prev != &cache_head &&
|
|
|
|
hunk_size - ((byte*) cache_head.prev - hunk_base) != used)
|
|
|
|
Sys_Error ("cache bottom not at bottom of high hunk");
|
|
|
|
}
|
|
|
|
#endif
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_Move (cache_system_t *c)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
memhunk_t *hunk = c->hunk;
|
2001-02-21 19:35:06 +00:00
|
|
|
cache_system_t *new;
|
|
|
|
|
2010-01-13 06:42:26 +00:00
|
|
|
// we are clearing up space at the bottom, so allocate it late
|
2021-07-28 06:01:45 +00:00
|
|
|
new = Cache_TryAlloc (hunk, c->size, true);
|
2001-02-21 19:35:06 +00:00
|
|
|
if (new) {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "cache_move ok\n");
|
2001-02-21 19:35:06 +00:00
|
|
|
|
|
|
|
memcpy (new + 1, c + 1, c->size - sizeof (cache_system_t));
|
|
|
|
new->user = c->user;
|
2021-12-13 02:49:37 +00:00
|
|
|
strncpy (new->name, c->name, sizeof (new->name));
|
2007-03-28 09:52:01 +00:00
|
|
|
Cache_Free (c->user);
|
2001-02-21 19:35:06 +00:00
|
|
|
new->user->data = (void *) (new + 1);
|
|
|
|
} else {
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "cache_move failed\n");
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2007-03-28 09:52:01 +00:00
|
|
|
Cache_Free (c->user); // tough luck...
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Cache_FreeLow
|
|
|
|
|
|
|
|
Throw things out until the hunk can be expanded to the given point
|
|
|
|
*/
|
2007-03-28 09:52:01 +00:00
|
|
|
static void
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_FreeLow (memhunk_t *hunk, int new_low_hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cache_system_t *c;
|
|
|
|
|
|
|
|
while (1) {
|
2021-12-13 02:23:25 +00:00
|
|
|
c = cs_ptr (hunk, hunk->cache_head[0].prev);
|
|
|
|
if (c == hunk->cache_head)
|
2001-02-21 19:35:06 +00:00
|
|
|
return; // nothing in cache at all
|
2021-07-28 06:01:45 +00:00
|
|
|
if ((byte *) c >= hunk->base + new_low_hunk)
|
2001-02-21 19:35:06 +00:00
|
|
|
return; // there is space to grow the hunk
|
2007-03-28 13:09:49 +00:00
|
|
|
Sys_Error ("FIXME: Cache_FreeLow: not enough memory");
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_Move (c); // reclaim the space
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static qboolean
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_FreeLRU (memhunk_t *hunk)
|
2001-08-29 15:29:17 +00:00
|
|
|
{
|
|
|
|
cache_system_t *cs;
|
2002-08-25 04:47:57 +00:00
|
|
|
|
2007-03-30 07:41:17 +00:00
|
|
|
//check_cache ();
|
2021-12-13 02:23:25 +00:00
|
|
|
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)
|
2001-08-29 15:29:17 +00:00
|
|
|
return 0;
|
2007-03-28 09:52:01 +00:00
|
|
|
Cache_Free (cs->user);
|
2001-08-29 15:29:17 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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 *
|
2021-07-28 06:01:45 +00:00
|
|
|
Cache_TryAlloc (memhunk_t *hunk, size_t size, qboolean nobottom)
|
2001-02-21 19:35:06 +00:00
|
|
|
{
|
|
|
|
cache_system_t *cs, *new;
|
|
|
|
|
2007-03-30 07:41:17 +00:00
|
|
|
//check_cache ();
|
2001-08-22 22:03:16 +00:00
|
|
|
// is the cache completely empty?
|
2021-12-13 02:23:25 +00:00
|
|
|
if (!nobottom && hunk->cache_head[0].prev == 0) {
|
2021-07-28 06:01:45 +00:00
|
|
|
new = (cache_system_t *) Hunk_HighAlloc (hunk, size);
|
2007-03-28 13:09:49 +00:00
|
|
|
if (!new)
|
|
|
|
return 0;
|
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);
|
2007-03-30 07:41:17 +00:00
|
|
|
//check_cache ();
|
2001-02-21 19:35:06 +00:00
|
|
|
return new;
|
|
|
|
}
|
2001-08-22 22:03:16 +00:00
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
// search for space in existing cache
|
2021-12-13 02:23:25 +00:00
|
|
|
for (cs = cs_ptr (hunk, hunk->cache_head[0].next);
|
|
|
|
cs != hunk->cache_head;
|
|
|
|
cs = cs_ptr (hunk, cs->next)) {
|
2007-03-28 13:09:49 +00:00
|
|
|
if (cs->user)
|
|
|
|
continue; // block isn't free
|
|
|
|
if (cs->size >= size) {
|
|
|
|
// found a big enough free block. If possible, carve it up for
|
|
|
|
// later reuse, using the upper portion of the block for the
|
|
|
|
// newly allocated block.
|
|
|
|
new = cs;
|
|
|
|
if (size - cs->size >= sizeof (cache_system_t)) {
|
|
|
|
new = (cache_system_t *) ((char *) cs + cs->size - size);
|
2021-12-13 02:49:37 +00:00
|
|
|
new->readlock = 0;
|
|
|
|
new->name[0] = 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
new->size = size;
|
2021-07-28 06:01:45 +00:00
|
|
|
new->hunk = hunk;
|
2007-03-28 13:09:49 +00:00
|
|
|
cs->size -= size;
|
|
|
|
link_cache_system (new, cs);
|
2007-03-30 07:41:17 +00:00
|
|
|
//check_cache ();
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2007-03-28 13:09:49 +00:00
|
|
|
Cache_MakeLRU (new);
|
|
|
|
return new;
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
if (nobottom)
|
|
|
|
return 0;
|
2001-02-21 19:35:06 +00:00
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
// didn't find a free block, so make a new one.
|
2021-07-28 06:01:45 +00:00
|
|
|
new = Hunk_HighAlloc (hunk, size);
|
2007-03-28 13:09:49 +00:00
|
|
|
if (new) {
|
2021-12-13 02:49:37 +00:00
|
|
|
new->readlock = 0;
|
|
|
|
new->name[0] = 0;
|
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
|
|
|
link_cache_system (new, hunk->cache_head);
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_MakeLRU (new);
|
2007-03-30 07:41:17 +00:00
|
|
|
//check_cache ();
|
2001-02-21 19:35:06 +00:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
return 0; // couldn't allocate
|
2001-02-21 19:35:06 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
cs = cs_ptr (hunk, ind);
|
|
|
|
Sys_Printf ("%8d : %.16s\n", (int) cs->size, 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);
|
|
|
|
if (!cs->user->data)
|
2007-03-28 13:09:49 +00:00
|
|
|
Sys_Error ("Cache_Flush: user/system out of sync for "
|
2021-12-13 02:23:25 +00:00
|
|
|
"'%.16s' with %zd size",
|
|
|
|
cs->name, cs->size);
|
|
|
|
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-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_dev, "Cache_Free: freeing '%.16s' %p\n", cs->name, cs);
|
2007-03-28 13:09:49 +00:00
|
|
|
|
2001-02-21 19:35:06 +00:00
|
|
|
Cache_UnlinkLRU (cs);
|
|
|
|
|
2021-12-13 02:23:25 +00:00
|
|
|
memhunk_t *h = cs->hunk;
|
2007-03-28 13:09:49 +00:00
|
|
|
//check_cache ();
|
|
|
|
cs->user = 0;
|
2021-12-13 02:23:25 +00:00
|
|
|
if (!cs_ptr (h, cs->prev)->user) {
|
|
|
|
cs->size += cs_ptr (h, cs->prev)->size;
|
|
|
|
cs_ptr (h, cs_ptr (h, cs->prev)->prev)->next = cs_ind (h, cs);
|
|
|
|
cs->prev = cs_ptr (h, cs->prev)->prev;
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2021-12-13 02:23:25 +00:00
|
|
|
if (!cs_ptr (h, cs->next)->user) {
|
|
|
|
cs = cs_ptr (h, cs->next);
|
|
|
|
cs->size += cs_ptr (h, cs->prev)->size;
|
|
|
|
cs_ptr (h, cs_ptr (h, cs->prev)->prev)->next = cs_ind (h, cs);
|
|
|
|
cs->prev = cs_ptr (h, cs->prev)->prev;
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
2021-12-13 02:23:25 +00:00
|
|
|
if (!cs->next) {
|
|
|
|
cs_ptr (h, cs->next)->prev = cs->prev;
|
|
|
|
cs_ptr (h, cs->prev)->next = cs->next;
|
|
|
|
if (cs->prev) {
|
|
|
|
__auto_type ptr = (byte *) cs_ptr (h, cs->prev);
|
|
|
|
Hunk_FreeToHighMark (h, h->size - (ptr - h->base));
|
|
|
|
} else {
|
|
|
|
Hunk_FreeToHighMark (h, 0);
|
|
|
|
}
|
2007-03-28 13:09:49 +00:00
|
|
|
}
|
|
|
|
//check_cache ();
|
|
|
|
|
|
|
|
c->data = NULL;
|
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) {
|
2021-07-28 06:01:45 +00:00
|
|
|
cs = Cache_TryAlloc (hunk, size, false);
|
2001-02-21 19:35:06 +00:00
|
|
|
if (cs) {
|
|
|
|
strncpy (cs->name, name, sizeof (cs->name) - 1);
|
2021-12-13 02:49:37 +00:00
|
|
|
cs->name[sizeof (cs->name) - 1] = 0;
|
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)
|
|
|
|
{
|
|
|
|
if (!hunk) { hunk = global_hunk; } //FIXME clean up callers
|
|
|
|
Sys_MaskPrintf (SYS_dev, "%4.1f megabyte data cache\n",
|
|
|
|
(hunk->size - hunk->high_used -
|
|
|
|
hunk->low_used) / (float) (1024 * 1024));
|
|
|
|
}
|
|
|
|
|
2007-03-28 13:09:49 +00:00
|
|
|
VISIBLE void
|
|
|
|
Cache_Report (void)
|
2001-08-28 23:26:58 +00:00
|
|
|
{
|
2021-07-28 06:01:45 +00:00
|
|
|
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
|
|
|
}
|