fteqw/engine/common/zone.c

757 lines
14 KiB
C
Raw Normal View History

/*
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 the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// Z_zone.c
#include "quakedef.h"
#ifdef _WIN32
#include "winquake.h"
#endif
#define NOZONE
#ifdef _DEBUG
//#define MEMDEBUG 8192 //Debugging adds sentinels (the number is the size - I have the ram)
#endif
//must be multiple of 4.
//#define TEMPDEBUG 4
//#define ZONEDEBUG 4
//#define HUNKDEBUG 4
//these need to be defined because it makes some bits of code simpler
#ifndef HUNKDEBUG
#define HUNKDEBUG 0
#endif
#ifndef ZONEDEBUG
#define ZONEDEBUG 0
#endif
#ifndef TEMPDEBUG
#define TEMPDEBUG 0
#endif
#if ZONEDEBUG>0 || HUNKDEBUG>0 || TEMPDEBUG>0
qbyte sentinalkey;
#endif
#define TAGLESS 1
int zmemtotal;
int zmemdelta;
typedef struct memheader_s {
int size;
int tag;
} memheader_t;
typedef struct zone_s {
struct zone_s *next;
struct zone_s *pvdn; // down if first, previous if not
memheader_t mh;
} zone_t;
zone_t *zone_head;
#ifdef MULTITHREAD
void *zonelock;
#endif
#if 0
static void Z_DumpTree(void)
{
zone_t *zone;
zone_t *nextlist;
zone_t *t;
zone_t *prev;
zone = zone_head;
while(zone)
{
nextlist = zone->pvdn;
fprintf(stderr, " +-+ %016x (tag: %08x)\n", zone, zone->mh.tag);
prev = zone;
t = zone->next;
while(t)
{
if (t->pvdn != prev)
fprintf(stderr, "Previous link failure\n");
prev = t;
t = t->next;
}
while(zone)
{
fprintf(stderr, " +-- %016x\n", zone);
zone = zone->next;
}
zone = nextlist;
}
}
#endif
void *VARGS Z_TagMalloc(int size, int tag)
{
zone_t *zone;
zone = (zone_t *)malloc(size + sizeof(zone_t));
if (!zone)
Sys_Error("Z_Malloc: Failed on allocation of %i bytes", size);
Q_memset(zone, 0, size + sizeof(zone_t));
zone->mh.tag = tag;
zone->mh.size = size;
#ifdef MULTITHREAD
if (zonelock)
Sys_LockMutex(zonelock);
#endif
#if 0
fprintf(stderr, "Before alloc:\n");
Z_DumpTree();
fprintf(stderr, "\n");
#endif
if (zone_head == NULL)
zone_head = zone;
else
{
zone_t *s = zone_head;
while (s && s->mh.tag != tag)
s = s->pvdn;
if (s)
{ // tag match
zone->next = s->next;
if (s->next)
s->next->pvdn = zone;
zone->pvdn = s;
s->next = zone;
}
else
{
zone->pvdn = zone_head;
// if (s->next)
// s->next->pvdn = zone;
zone_head = zone;
}
}
#if 0
fprintf(stderr, "After alloc:\n");
Z_DumpTree();
fprintf(stderr, "\n");
#endif
#ifdef MULTITHREAD
if (zonelock)
Sys_UnlockMutex(zonelock);
#endif
return (void *)(zone + 1);
}
#ifdef USE_MSVCRT_DEBUG
void *ZF_MallocNamed(int size, char *file, int line)
{
return _calloc_dbg(size, 1, _NORMAL_BLOCK, file, line);
}
void *Z_MallocNamed(int size, char *file, int line)
{
void *mem = ZF_MallocNamed(size, file, line);
if (!mem)
Sys_Error("Z_Malloc: Failed on allocation of %i bytes", size);
return mem;
}
#else
void *ZF_Malloc(int size)
{
#ifdef ANDROID
void *ret = NULL;
//android is linux, but not gnu and thus not posix...
ret = memalign(max(sizeof(float)*4, sizeof(void*)), size);
if (ret)
memset(ret, 0, size);
return ret;
#elif defined(__linux__)
void *ret = NULL;
if (!posix_memalign(&ret, max(sizeof(float)*4, sizeof(void*)), size))
memset(ret, 0, size);
return ret;
#else
return calloc(size, 1);
#endif
}
void *Z_Malloc(int size)
{
void *mem = ZF_Malloc(size);
if (!mem)
Sys_Error("Z_Malloc: Failed on allocation of %i bytes", size);
return mem;
}
#endif
void Z_StrCat(char **ptr, const char *append)
playdemo accepts https urls now. will start playing before the file has finished downloading, to avoid unnecessary delays. reworked network addresses to separate address family and connection type. this should make banning people more reliable, as well as simplifying a whole load of logic (no need to check for ipv4 AND ipv6). tcpconnect will keep trying to connect even if the connection wasn't instant, instead of giving up instantly. rewrote tcp connections quite a bit. sv_port_tcp now handles qtv+qizmo+http+ws+rtcbroker+tls equivalents. qtv_streamport is now a legacy cvar and now acts equivalently to sv_port_tcp (but still separate). rewrote screenshot and video capture code to use strides. this solves image-is-upside down issues with vulkan. ignore alt key in browser port. oh no! no more red text! oh no! no more alt-being-wrongly-down-and-being-unable-to-type-anything-without-forcing-alt-released! reworked audio decoder interface. now has clearly defined success/unavailable/end-of-file results. this should solve a whole load of issues with audio streaming. fixed various openal audio streaming issues too. openal also got some workarounds for emscripten's poor emulation. fixed ogg decoder to retain sync properly if seeked. updated menu_media a bit. now reads vorbis comments/id3v1 tags to get proper track names. also saves the playlist so you don't have to manually repopulate the list so it might actually be usable now (after how many years?) r_stains now defaults to 0, and is no longer enabled by presets. use decals if you want that sort of thing. added fs_noreexec cvar, so configs will not be reexeced on gamedir change. this also means defaults won't be reapplied, etc. added 'nvvk' renderer on windows, using nvidia's vulkan-inside-opengl gl extension. mostly just to see how much slower it is. fixed up the ftp server quite a lot. more complete, more compliant, and should do ipv6 properly to-boot. file transfers also threaded. fixed potential crash inside runclientphys. experimental sv_antilag=3 setting. totally untested. the aim is to avoid missing due to lagged knockbacks. may be expensive for the server. browser port's websockets support fixed. experimental support for webrtc ('works for me', requires a broker server). updated avplug(renamed to ffmpeg so people know what it is) to use ffmpeg 3.2.4 properly, with its new encoder api. should be much more robust... also added experimental audio decoder for game music etc (currently doesn't resample, so playback rates are screwed, disabled by cvar). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@5097 fc73d0e0-1445-4013-8a0c-d673dee63da5
2017-05-10 02:08:58 +00:00
{
size_t oldlen = *ptr?strlen(*ptr):0;
size_t newlen = strlen(append);
char *newptr = BZ_Malloc(oldlen+newlen+1);
memcpy(newptr, *ptr, oldlen);
memcpy(newptr+oldlen, append, newlen);
newptr[oldlen+newlen] = 0;
BZ_Free(*ptr);
*ptr = newptr;
}
void VARGS Z_TagFree(void *mem)
{
zone_t *zone = ((zone_t *)mem) - 1;
#if 0
fprintf(stderr, "Before free:\n");
Z_DumpTree();
fprintf(stderr, "\n");
#endif
#ifdef MULTITHREAD
if (zonelock)
Sys_LockMutex(zonelock);
#endif
if (zone->next)
zone->next->pvdn = zone->pvdn;
if (zone->pvdn && zone->pvdn->mh.tag == zone->mh.tag)
zone->pvdn->next = zone->next;
else
{ // zone is first entry in a tag list
zone_t *s = zone_head;
if (zone != s)
{ // traverse and update down list
while (s->pvdn != zone)
s = s->pvdn;
if (zone->next)
s->pvdn = zone->next;
else
s->pvdn = zone->pvdn;
}
}
if (zone == zone_head)
{ // freeing head node so update head pointer
if (zone->next) // move to next, pvdn should be maintained properly
zone_head = zone->next;
else // no more entries with this tag so move head down
zone_head = zone->pvdn;
}
#if 0
fprintf(stderr, "After free:\n");
Z_DumpTree();
fprintf(stderr, "\n");
#endif
#ifdef MULTITHREAD
if (zonelock)
Sys_UnlockMutex(zonelock);
#endif
free(zone);
}
void VARGS Z_Free(void *mem)
{
free(mem);
}
void VARGS Z_FreeTags(int tag)
{
zone_t *taglist;
zone_t *t;
#ifdef MULTITHREAD
if (zonelock)
Sys_LockMutex(zonelock);
#endif
if (zone_head)
{
if (zone_head->mh.tag == tag)
{ // just pull off the head
taglist = zone_head;
zone_head = zone_head->pvdn;
}
else
{ // search for tag list and isolate it
zone_t *z;
z = zone_head;
while (z->pvdn != NULL && z->pvdn->mh.tag != tag)
z = z->pvdn;
if (z->pvdn == NULL)
taglist = NULL;
else
{
taglist = z->pvdn;
z->pvdn = z->pvdn->pvdn;
}
}
}
else
taglist = NULL;
#ifdef MULTITHREAD
if (zonelock)
Sys_UnlockMutex(zonelock);
#endif
// actually free list
while (taglist != NULL)
{
t = taglist->next;
free(taglist);
taglist = t;
}
}
fix colormod added frag message filter, and dedicated frag tracker. added 'windowed consoles' for social-type stuff without depending upon csqc mods for it. added in_deviceids command which allows listing/renumbering device ids. slider widgets now support inverted ranges, so gamma selection isn't so weird. fix top/bottom colour selection bug. software banding feature is now part of the 'software' preset (now that it supports mipmaps). support for loading .maps, and editing their brushes etc (with appropriate qc mod). 'map mymap.map' to use. expect problems with missing wads and replacement textures overriding them and messing up texture scales. snd_inactive is now default. fix threading issue with wavs, no more error from 0-sample-but-otherwise-valid wavs. added -makeinstaller option to embed a manifest inside the exe (and icon). the resulting program will insist on installing the game if its run from outside a valid basedir. framegroup support for q1mdl. textures are now loaded on multiple worker threads, for reduced load times. moo har har. netgraph shows packet+byte rates too. added r_lightstylescale, pretty similar to contrast, but doesn't impose any framerate cost, but may have overbrighting issues. r_softwarebanding now works on q2bsp too. fixed crepuscular lights. gzip transfer encoding is performed while downloading, instead of inducing stalls. FINALLY fix ezquake download compat issue (dimman found the issue). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4851 fc73d0e0-1445-4013-8a0c-d673dee63da5
2015-04-14 23:12:17 +00:00
//close enough
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
#ifdef USE_MSVCRT_DEBUG
qboolean ZF_ReallocElementsNamed(void **ptr, size_t *elements, size_t newelements, size_t elementsize, const char *file, int line)
#else
fix colormod added frag message filter, and dedicated frag tracker. added 'windowed consoles' for social-type stuff without depending upon csqc mods for it. added in_deviceids command which allows listing/renumbering device ids. slider widgets now support inverted ranges, so gamma selection isn't so weird. fix top/bottom colour selection bug. software banding feature is now part of the 'software' preset (now that it supports mipmaps). support for loading .maps, and editing their brushes etc (with appropriate qc mod). 'map mymap.map' to use. expect problems with missing wads and replacement textures overriding them and messing up texture scales. snd_inactive is now default. fix threading issue with wavs, no more error from 0-sample-but-otherwise-valid wavs. added -makeinstaller option to embed a manifest inside the exe (and icon). the resulting program will insist on installing the game if its run from outside a valid basedir. framegroup support for q1mdl. textures are now loaded on multiple worker threads, for reduced load times. moo har har. netgraph shows packet+byte rates too. added r_lightstylescale, pretty similar to contrast, but doesn't impose any framerate cost, but may have overbrighting issues. r_softwarebanding now works on q2bsp too. fixed crepuscular lights. gzip transfer encoding is performed while downloading, instead of inducing stalls. FINALLY fix ezquake download compat issue (dimman found the issue). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4851 fc73d0e0-1445-4013-8a0c-d673dee63da5
2015-04-14 23:12:17 +00:00
qboolean ZF_ReallocElements(void **ptr, size_t *elements, size_t newelements, size_t elementsize)
#endif
fix colormod added frag message filter, and dedicated frag tracker. added 'windowed consoles' for social-type stuff without depending upon csqc mods for it. added in_deviceids command which allows listing/renumbering device ids. slider widgets now support inverted ranges, so gamma selection isn't so weird. fix top/bottom colour selection bug. software banding feature is now part of the 'software' preset (now that it supports mipmaps). support for loading .maps, and editing their brushes etc (with appropriate qc mod). 'map mymap.map' to use. expect problems with missing wads and replacement textures overriding them and messing up texture scales. snd_inactive is now default. fix threading issue with wavs, no more error from 0-sample-but-otherwise-valid wavs. added -makeinstaller option to embed a manifest inside the exe (and icon). the resulting program will insist on installing the game if its run from outside a valid basedir. framegroup support for q1mdl. textures are now loaded on multiple worker threads, for reduced load times. moo har har. netgraph shows packet+byte rates too. added r_lightstylescale, pretty similar to contrast, but doesn't impose any framerate cost, but may have overbrighting issues. r_softwarebanding now works on q2bsp too. fixed crepuscular lights. gzip transfer encoding is performed while downloading, instead of inducing stalls. FINALLY fix ezquake download compat issue (dimman found the issue). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4851 fc73d0e0-1445-4013-8a0c-d673dee63da5
2015-04-14 23:12:17 +00:00
{
void *n;
size_t oldsize;
size_t newsize;
//protect against malicious overflows
if (newelements > SIZE_MAX / elementsize)
return false;
oldsize = *elements * elementsize;
newsize = newelements * elementsize;
#ifdef USE_MSVCRT_DEBUG
n = BZ_ReallocNamed(*ptr, newsize, file, line);
#else
fix colormod added frag message filter, and dedicated frag tracker. added 'windowed consoles' for social-type stuff without depending upon csqc mods for it. added in_deviceids command which allows listing/renumbering device ids. slider widgets now support inverted ranges, so gamma selection isn't so weird. fix top/bottom colour selection bug. software banding feature is now part of the 'software' preset (now that it supports mipmaps). support for loading .maps, and editing their brushes etc (with appropriate qc mod). 'map mymap.map' to use. expect problems with missing wads and replacement textures overriding them and messing up texture scales. snd_inactive is now default. fix threading issue with wavs, no more error from 0-sample-but-otherwise-valid wavs. added -makeinstaller option to embed a manifest inside the exe (and icon). the resulting program will insist on installing the game if its run from outside a valid basedir. framegroup support for q1mdl. textures are now loaded on multiple worker threads, for reduced load times. moo har har. netgraph shows packet+byte rates too. added r_lightstylescale, pretty similar to contrast, but doesn't impose any framerate cost, but may have overbrighting issues. r_softwarebanding now works on q2bsp too. fixed crepuscular lights. gzip transfer encoding is performed while downloading, instead of inducing stalls. FINALLY fix ezquake download compat issue (dimman found the issue). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4851 fc73d0e0-1445-4013-8a0c-d673dee63da5
2015-04-14 23:12:17 +00:00
n = BZ_Realloc(*ptr, newsize);
#endif
fix colormod added frag message filter, and dedicated frag tracker. added 'windowed consoles' for social-type stuff without depending upon csqc mods for it. added in_deviceids command which allows listing/renumbering device ids. slider widgets now support inverted ranges, so gamma selection isn't so weird. fix top/bottom colour selection bug. software banding feature is now part of the 'software' preset (now that it supports mipmaps). support for loading .maps, and editing their brushes etc (with appropriate qc mod). 'map mymap.map' to use. expect problems with missing wads and replacement textures overriding them and messing up texture scales. snd_inactive is now default. fix threading issue with wavs, no more error from 0-sample-but-otherwise-valid wavs. added -makeinstaller option to embed a manifest inside the exe (and icon). the resulting program will insist on installing the game if its run from outside a valid basedir. framegroup support for q1mdl. textures are now loaded on multiple worker threads, for reduced load times. moo har har. netgraph shows packet+byte rates too. added r_lightstylescale, pretty similar to contrast, but doesn't impose any framerate cost, but may have overbrighting issues. r_softwarebanding now works on q2bsp too. fixed crepuscular lights. gzip transfer encoding is performed while downloading, instead of inducing stalls. FINALLY fix ezquake download compat issue (dimman found the issue). git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4851 fc73d0e0-1445-4013-8a0c-d673dee63da5
2015-04-14 23:12:17 +00:00
if (!n)
return false;
if (newsize > oldsize)
memset((char*)n+oldsize, 0, newsize - oldsize);
*elements = newelements;
*ptr = n;
return true;
}
/*
void *Z_Realloc(void *data, int newsize)
{
memheader_t *memref;
if (!data)
return Z_Malloc(newsize);
memref = ((memheader_t *)data) - 1;
if (memref[0].tag != TAGLESS)
{ // allocate a new block and copy since we need to maintain the lists
zone_t *zone = ((zone_t *)data) - 1;
int size = zone->mh.size;
if (size != newsize)
{
void *newdata = Z_Malloc(newsize);
if (size > newsize)
size = newsize;
memcpy(newdata, data, size);
Z_Free(data);
data = newdata;
}
}
else
{
int oldsize = memref[0].size;
memref = realloc(memref, newsize + sizeof(memheader_t));
memref->size = newsize;
if (newsize > oldsize)
memset((qbyte *)memref + sizeof(memheader_t) + oldsize, 0, newsize - oldsize);
data = ((memheader_t *)memref) + 1;
}
return data;
}
*/
#ifdef USE_MSVCRT_DEBUG
void *BZF_MallocNamed(int size, const char *file, int line) //BZ_MallocNamed but allowed to fail - like straight malloc.
{
void *mem;
mem = _malloc_dbg(size, _NORMAL_BLOCK, file, line);
if (mem)
{
zmemdelta += size;
zmemtotal += size;
}
return mem;
}
#else
void *BZF_Malloc(int size) //BZ_Malloc but allowed to fail - like straight malloc.
{
void *mem;
mem = malloc(size);
if (mem)
{
zmemdelta += size;
zmemtotal += size;
}
return mem;
}
#endif
#ifdef USE_MSVCRT_DEBUG
void *BZ_MallocNamed(int size, const char *file, int line) //BZ_MallocNamed but allowed to fail - like straight malloc.
{
void *mem = BZF_MallocNamed(size, file, line);
if (!mem)
Sys_Error("BZ_Malloc: Failed on allocation of %i bytes", size);
return mem;
}
#else
rewrote ban code, merging bans+nonbans+cuffs+mute+cripple+deaf+lagged+vip. added timeouts. new penalties have no dedicated command. use the addip command for it. maplist command now generates links. implemented skin objects for q3. added a csqc builtin for it. also supports compositing skins. playing demos inside zips/pk3s/paks should now work. bumped default rate cvar. added cl_transfer to attempt to connect to a new server without disconnecting first. rewrote fog command. alpha and mindist arguments are now supported. fog change also happens over a short time period. added new args to the showpic console command. can now create clickable items for touchscreen/absmouse users. fixed menus to properly support right-aligned text. this finally fixes variable-width fonts. rewrote console tab completion suggestions display. now clickable links. strings obtained from qc are now marked as const. this has required quite a few added consts all over the place. probably crappy attempt at adding joypad support to the sdl port. no idea if it works. changed key bind event code. buttons now track which event they should trigger when released, instead of being the same one the whole time. this allows +forward etc clickable buttons on screen. Also simplified modifier keys - they no longer trigger random events when pressing the modifier key itself. Right modifiers can now be bound separately from left modifiers. Right will use left's binding if not otherwise bound. Bind assumes left if there's no prefix. multiplayer->setup->network menu no longer crashes. added rgb colours to the translation view (but not to the colour-changing keys). added modelviewer command to view models. added menu_mods menu to switch mods in a more friendly way. will be shown by default if multiple manifests exist in the binarydir. clamped classic tracer density. scrag particles no longer look quite so buggy. added ifdefs to facilitate a potential winrt port. the engine should now have no extra dependencies, but still needs system code+audio drivers to be written. if it can't set a renderer, it'll now try to use *every* renderer until it finds one that works. added experimental mapcluster server mode (that console command). New maps will be started up as required. rewrote skeletal blending code a bit. added cylinder geomtypes. fix cfg_save writing to the wrong path bug. VFS_CLOSE now returns a boolean. false means there was some sort of fatal error (either crc when reading was bad, or the write got corrupted or something). Typically ignorable, depends how robust you want to be. win32 tls code now supports running as a server. added connect tls://address support, as well as equivalent sv_addport support. exposed basic model loading api to plugins. d3d11 backend now optionally supports tessellation hlsl. no suitable hlsl provided by default. !!tess to enable. attempted to add gamma ramp support for d3d11. added support for shader blobs to speed up load times. r_shaderblobs 1 to enable. almost vital for d3d11. added vid_srgb cvar. shadowless lights are no longer disabled if shadows are not supported. attempt to add support for touchscreens in win7/8. Wrote gimmicky lua support, using lua instead of ssqc. define VM_LUA to enable. updated saved game code. can again load saved games from vanilla-like engines. changed scale clamping. 0.0001 should no longer appear as 1. changed default mintic from 0.03 to 0.013 to match vanilla qw. I don't know why it was at 0.03. probably a typo. git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4623 fc73d0e0-1445-4013-8a0c-d673dee63da5
2014-03-30 08:55:06 +00:00
void *BZ_Malloc(int size) //Doesn't clear. The expectation is a large file, rather than sensitive data structures.
{
void *mem = BZF_Malloc(size);
if (!mem)
Sys_Error("BZ_Malloc: Failed on allocation of %i bytes", size);
return mem;
}
#endif
#ifdef USE_MSVCRT_DEBUG
void *BZF_ReallocNamed(void *data, int newsize, const char *file, int line)
{
return _realloc_dbg(data, newsize, _NORMAL_BLOCK, file, line);
}
void *BZ_ReallocNamed(void *data, int newsize, const char *file, int line)
{
void *mem = BZF_ReallocNamed(data, newsize, file, line);
if (!mem)
Sys_Error("BZ_Realloc: Failed on reallocation of %i bytes", newsize);
return mem;
}
#else
void *BZF_Realloc(void *data, int newsize)
{
return realloc(data, newsize);
}
void *BZ_Realloc(void *data, int newsize)
{
void *mem = BZF_Realloc(data, newsize);
if (!mem)
Sys_Error("BZ_Realloc: Failed on reallocation of %i bytes", newsize);
return mem;
}
#endif
void BZ_Free(void *data)
{
free(data);
}
typedef struct zonegroupblock_s
{
union
{
struct zonegroupblock_s *next;
vec4_t align16;
};
} zonegroupblock_t;
#ifdef USE_MSVCRT_DEBUG
#undef ZG_Malloc
void *QDECL ZG_Malloc(zonegroup_t *ctx, int size){return ZG_MallocNamed(ctx, size, "ZG_Malloc", size);}
void *ZG_MallocNamed(zonegroup_t *ctx, int size, char *file, int line)
#else
void *QDECL ZG_Malloc(zonegroup_t *ctx, int size)
#endif
{
zonegroupblock_t *newm;
size += sizeof(zonegroupblock_t); //well, at least the memory will be pointer aligned...
#ifdef USE_MSVCRT_DEBUG
newm = Z_MallocNamed(size, file, line);
#else
newm = Z_Malloc(size);
#endif
newm->next = ctx->first;
ctx->first = newm;
ctx->bytes += size;
return(void*)(newm+1);
}
void ZG_FreeGroup(zonegroup_t *ctx)
{
zonegroupblock_t *old;
while(ctx->first)
{
old = ctx->first;
ctx->first = old->next;
BZ_Free(old);
}
ctx->bytes = 0;
}
//============================================================================
/*
=================
Hunk_TempAlloc
Return space from the top of the hunk
clears old temp.
=================
*/
typedef struct hnktemps_s {
struct hnktemps_s *next;
#if TEMPDEBUG>0
int len;
#endif
} hnktemps_t;
hnktemps_t *hnktemps;
void Hunk_TempFree(void)
{
hnktemps_t *nt;
COM_AssertMainThread("Hunk_TempFree");
while (hnktemps)
{
#if TEMPDEBUG>0
int i;
qbyte *buf;
buf = (qbyte *)(hnktemps+1);
for (i = 0; i < TEMPDEBUG; i++)
{
if (buf[i] != sentinalkey)
Sys_Error ("Hunk_Check: corrupt sentinal");
}
buf+=TEMPDEBUG;
//app data
buf += hnktemps->len;
for (i = 0; i < TEMPDEBUG; i++)
{
if (buf[i] != sentinalkey)
Sys_Error ("Hunk_Check: corrupt sentinal");
}
#endif
nt = hnktemps->next;
free(hnktemps);
hnktemps = nt;
}
}
//allocates without clearing previous temp.
//safer than my hack that fuh moaned about...
void *Hunk_TempAllocMore (int size)
{
void *buf;
#if TEMPDEBUG>0
hnktemps_t *nt;
COM_AssertMainThread("Hunk_TempAllocMore");
nt = (hnktemps_t*)malloc(size + sizeof(hnktemps_t) + TEMPDEBUG*2);
if (!nt)
return NULL;
nt->next = hnktemps;
nt->len = size;
hnktemps = nt;
buf = (void *)(nt+1);
memset(buf, sentinalkey, TEMPDEBUG);
buf = (char *)buf + TEMPDEBUG;
memset(buf, 0, size);
memset((char *)buf + size, sentinalkey, TEMPDEBUG);
return buf;
#else
hnktemps_t *nt;
COM_AssertMainThread("Hunk_TempAllocMore");
nt = (hnktemps_t*)malloc(size + sizeof(hnktemps_t));
if (!nt)
return NULL;
nt->next = hnktemps;
hnktemps = nt;
buf = (void *)(nt+1);
memset(buf, 0, size);
return buf;
#endif
}
void *Hunk_TempAlloc (int size)
{
Hunk_TempFree();
return Hunk_TempAllocMore(size);
}
/*
===============================================================================
CACHE MEMORY
===============================================================================
*/
void Cache_Flush(void)
{
//this generically named function is hyjacked to flush models and sounds, as well as ragdolls etc
#ifdef HAVE_CLIENT
S_Purge(false);
#endif
#if defined(HAVE_CLIENT) || defined(HAVE_SERVER)
#ifdef RAGDOLL
rag_flushdolls(true);
#endif
Mod_Purge(MP_FLUSH);
#endif
#ifdef HAVE_CLIENT
Reworked client support for DPP5+. less code now, its much more graceful. added waterfog command. waterfog overrides regular fog only when the view is in water. fixed 64bit printf format specifiers. should work better on winxp64. fixed some spec angle weirdness. fixed viewsize 99.99 weirdness with ezhud. fixed extra offset on the console (exhibited in 64bit builds, but not limited to). fixed .avi playback, can now actually display frames again. reimplemented line sparks. fixed r_editlights_save flipping the light's pitch. fixed issue with oggs failing to load. fixed condump to cope with unicode properly. made sv_bigcoords default except in quake. hexen2 kinda needs it for bsp angle precision. fixed nq server to not stall weirdly on map changes. fixed qwprogs svc_cdtrack not bugging out with nq clients on the server. fixed restart command to load the last map run by the server, instead of start.bsp (when idle) optimised d3d9 renderer a little. now uses less draw calls, especially with complex scenes. seems to get higher framerates than opengl now. fixed d3d9 renderer to not bug out quite so much when run fullscreen (shader subsystem is now correctly initialised). fixed a couple of bugs from font change. also now supports utf-8 in a few more places. r_editlights_reload no longer generates rtlights inside the void. this resolves a few glitches (but should also help framerates a little). fixed so corona-only lights won't generate shadowmaps and waste lots of time. removed lots of #defines from qclib. I should never have made them in the first place, but I was lazy. obviously there's more left that I cba to remove yet. fixed nested calls with variant-vectors. this fixes csaddon's light editor. fixed qcc hc calling conventions using redundant stores. disabled keywords can still be used by using __keyword instead. fixed ftegccgui grep feature. fixed motionless-dog qcc bug. tweaked qcc warnings a little. -Wall is now a viable setting. you should be able to fix all those warnings. fixed qw svc_intermission + dpp5+ clients bug. fixed annoying spam about disconnecting in hexen2. rewrote status command a little to cope with ipv6 addresses more gracefully fixed significant stall when hibernating/debugging a server with a player sitting on it. fixed truelightning. fixed rocketlight overriding pflags. fixed torches vanishing on vid_restart. fixed issue with decal scaling. fixed findentityfield builtin. fixed fteqcc issue with ptr+1 fixed use of arrays inside class functions. fixed/implemented fteqcc emulation of pointer opcodes. added __inout keyword to fteqcc, so that it doesn't feel so horrendous. fixed sizeof(*foo) fixed *struct = struct; fixed recursive structs. fixed fteqcc warning report. fixed sdl2 controller support, hopefully. attempted to implement xinput, including per-player audio playback. slightly fixed relaxed attitude to mouse focus when running fullscreen. fixed weird warnings/errors with 'ent.arrayhead' terms. now generates sane errors. implemented bindmaps (for csqc). fixed crashing bug with eprint builtin. implemented subset of music_playlist_* functionality. significant changes to music playback. fixed some more dpcsqc compat. fixed binds menu. now displays and accepts modifiers. fixed issues with huge lightmaps. fixed protocol determinism with dp clients connecting to fte servers. the initial getchallenge request now inhibits vanilla nq connection requests. implemented support for 'dupe' userinfo key, allowing clients to request client->server packet duplication. should probably queue them tbh. implemented sv_saveentfile command. fixed resume after breaking inside a stepped-over function. fixed erroneous footer after debugging. (I wonder just how many things I broke with these fixes) git-svn-id: https://svn.code.sf.net/p/fteqw/code/trunk@4946 fc73d0e0-1445-4013-8a0c-d673dee63da5
2015-07-26 10:56:18 +00:00
Image_Purge();
#endif
}
static void Hunk_Print_f (void)
{
Con_Printf("Z Delta: %iKB\n", zmemdelta/1024); zmemdelta = 0;
Con_Printf("Z Total: %iKB\n", zmemtotal/1024);
//note: Zone memory isn't tracked reliably. we don't track the mem that is freed, so it'll just climb and climb
//we don't track reallocs either.
#if 0
{
zone_t *zone;
int zoneused = 0;
int zoneblocks = 0;
for(zone = zone_head; zone; zone=zone->next)
{
zoneused += zone->size + sizeof(zone_t);
zoneblocks++;
}
Con_Printf("Zone: %i containing %iKB\n", zoneblocks, zoneused/1024);
}
#endif
#ifdef USE_MSVCRT_DEBUG
{
static struct _CrtMemState savedstate;
static qboolean statesaved;
_CrtMemDumpAllObjectsSince(statesaved?&savedstate:NULL);
_CrtMemCheckpoint(&savedstate);
statesaved = true;
}
#endif
}
void Cache_Init(void)
{
Cmd_AddCommand ("flush", Cache_Flush);
Cmd_AddCommand ("hunkprint", Hunk_Print_f);
#if 0
Cmd_AddCommand ("zoneprint", Zone_Print_f);
#endif
#ifdef NAMEDMALLOCS
Cmd_AddCommand ("zonegroups", Zone_Groups_f);
#endif
}
//============================================================================
/*
========================
Memory_Init
========================
*/
void Memory_Init (void)
{
#if 0 //ndef NOZONE
int p;
int zonesize = DYNAMIC_SIZE;
#endif
#if ZONEDEBUG>0 || HUNKDEBUG>0 || TEMPDEBUG>0||CACHEDEBUG>0
srand(time(0));
sentinalkey = rand() & 0xff;
#endif
Cache_Init ();
#ifdef MULTITHREAD
if (!zonelock)
zonelock = Sys_CreateMutex(); // this can fail!
#endif
#if 0 //ndef NOZONE
p = COM_CheckParm ("-zone");
if (p)
{
if (p < com_argc-1)
zonesize = Q_atoi (com_argv[p+1]) * 1024;
else
Sys_Error ("Memory_Init: you must specify a size in KB after -zone");
}
mainzone = Hunk_AllocName ( zonesize, "zone" );
Z_ClearZone (mainzone, zonesize);
#endif
}
void Memory_DeInit(void)
{
Hunk_TempFree();
Cache_Flush();
#ifdef MULTITHREAD
if (zonelock)
{
Sys_DestroyMutex(zonelock);
zonelock = NULL;
}
#endif
}