X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=zone.c;h=2f756d0ea803601a9d7d2ce64368f889516068d3;hb=e465795a9296bcbe43d3d409cfeeaf653a0f5ffe;hp=87e7121248bf6cd0043cdf69156dfffa75dd1a52;hpb=cfee52a1ec9db338098789cae89ae5cf1f7a6fbf;p=xonotic%2Fdarkplaces.git diff --git a/zone.c b/zone.c index 87e71212..2f756d0e 100644 --- a/zone.c +++ b/zone.c @@ -40,15 +40,26 @@ unsigned int sentinel_seed; qboolean mem_bigendian = false; void *mem_mutex = NULL; +// divVerent: enables file backed malloc using mmap to conserve swap space (instead of malloc) +#ifndef FILE_BACKED_MALLOC +# define FILE_BACKED_MALLOC 0 +#endif + // LordHavoc: enables our own low-level allocator (instead of malloc) -#define MEMCLUMPING 0 -#define MEMCLUMPING_FREECLUMPS 0 +#ifndef MEMCLUMPING +# define MEMCLUMPING 0 +#endif +#ifndef MEMCLUMPING_FREECLUMPS +# define MEMCLUMPING_FREECLUMPS 0 +#endif #if MEMCLUMPING // smallest unit we care about is this many bytes #define MEMUNIT 128 // try to do 32MB clumps, but overhead eats into this -#define MEMWANTCLUMPSIZE (1<<27) +#ifndef MEMWANTCLUMPSIZE +# define MEMWANTCLUMPSIZE (1<<27) +#endif // give malloc padding so we can't waste most of a page at the end #define MEMCLUMPSIZE (MEMWANTCLUMPSIZE - MEMWANTCLUMPSIZE/MEMUNIT/32 - 128) #define MEMBITS (MEMCLUMPSIZE / MEMUNIT) @@ -92,6 +103,46 @@ static mempool_t *poolchain = NULL; void Mem_PrintStats(void); void Mem_PrintList(size_t minallocationsize); +#if FILE_BACKED_MALLOC +#include +#include +typedef struct mmap_data_s +{ + size_t len; +} +mmap_data_t; +static void *mmap_malloc(size_t size) +{ + char vabuf[MAX_OSPATH + 1]; + char *tmpdir = getenv("TEMP"); + mmap_data_t *data; + int fd; + size += sizeof(mmap_data_t); // waste block + dpsnprintf(vabuf, sizeof(vabuf), "%s/darkplaces.XXXXXX", tmpdir ? tmpdir : "/tmp"); + fd = mkstemp(vabuf); + if(fd < 0) + return NULL; + ftruncate(fd, size); + data = (unsigned char *) mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_NORESERVE, fd, 0); + close(fd); + unlink(vabuf); + if(!data) + return NULL; + data->len = size; + return (void *) (data + 1); +} +static void mmap_free(void *mem) +{ + mmap_data_t *data; + if(!mem) + return; + data = ((mmap_data_t *) mem) - 1; + munmap(data, data->len); +} +#define malloc mmap_malloc +#define free mmap_free +#endif + #if MEMCLUMPING != 2 // some platforms have a malloc that returns NULL but succeeds later // (Windows growing its swapfile for example) @@ -874,13 +925,13 @@ void Memory_Init (void) u.s = 0x100; mem_bigendian = u.b[0] != 0; - if (Thread_HasThreads()) - mem_mutex = Thread_CreateMutex(); - sentinel_seed = rand(); poolchain = NULL; tempmempool = Mem_AllocPool("Temporary Memory", POOLFLAG_TEMP, NULL); zonemempool = Mem_AllocPool("Zone", 0, NULL); + + if (Thread_HasThreads()) + mem_mutex = Thread_CreateMutex(); } void Memory_Shutdown (void)