X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=zone.c;h=2c72c113cfb50dd6c30e0a86eaef8665c279c24b;hb=15401d0a14c409b7dd75e88c603048c797fa2232;hp=85a3b2199776b654539afc10499e9b08889fc7ff;hpb=46f08c84d7e41aafaac412fad5d26ee7b6405762;p=xonotic%2Fdarkplaces.git diff --git a/zone.c b/zone.c index 85a3b219..2c72c113 100644 --- a/zone.c +++ b/zone.c @@ -111,16 +111,13 @@ typedef struct mmap_data_s size_t len; } mmap_data_t; -#define MMAP_PAGE_SIZE max(sizeof(mmap_data_t), (size_t)sysconf(_SC_PAGE_SIZE)) static void *mmap_malloc(size_t size) { char vabuf[MAX_OSPATH + 1]; char *tmpdir = getenv("TEMP"); - unsigned char *data; + mmap_data_t *data; int fd; - size += MMAP_PAGE_SIZE; // waste block - size += MMAP_PAGE_SIZE - 1; // also, waste up to this amount for management info - size -= (size % MMAP_PAGE_SIZE); // round down + size += sizeof(mmap_data_t); // waste block dpsnprintf(vabuf, sizeof(vabuf), "%s/darkplaces.XXXXXX", tmpdir ? tmpdir : "/tmp"); fd = mkstemp(vabuf); if(fd < 0) @@ -131,16 +128,16 @@ static void *mmap_malloc(size_t size) unlink(vabuf); if(!data) return NULL; - ((mmap_data_t *) data)->len = size; - return (void *) (data + MMAP_PAGE_SIZE); + data->len = size; + return (void *) (data + 1); } static void mmap_free(void *mem) { - unsigned char *data; + mmap_data_t *data; if(!mem) return; - data = (unsigned char *) mem - MMAP_PAGE_SIZE; - munmap(data, ((mmap_data_t *) data)->len); + data = ((mmap_data_t *) mem) - 1; + munmap(data, data->len); } #define malloc mmap_malloc #define free mmap_free @@ -385,7 +382,12 @@ void *_Mem_Alloc(mempool_t *pool, void *olddata, size_t size, size_t alignment, return NULL; } if (pool == NULL) - Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline); + { + if(olddata) + pool = ((memheader_t *)((unsigned char *) olddata - sizeof(memheader_t)))->pool; + else + Sys_Error("Mem_Alloc: pool == NULL (alloc at %s:%i)", filename, fileline); + } if (mem_mutex) Thread_LockMutex(mem_mutex); if (developer_memory.integer) @@ -695,44 +697,6 @@ void Mem_ExpandableArray_FreeArray(memexpandablearray_t *l) memset(l, 0, sizeof(*l)); } -// VorteX: hacked Mem_ExpandableArray_AllocRecord, it does allocate record at certain index -void *Mem_ExpandableArray_AllocRecordAtIndex(memexpandablearray_t *l, size_t index) -{ - size_t j; - if (index >= l->numarrays) - { - if (l->numarrays == l->maxarrays) - { - memexpandablearray_array_t *oldarrays = l->arrays; - l->maxarrays = max(l->maxarrays * 2, 128); - l->arrays = (memexpandablearray_array_t*) Mem_Alloc(l->mempool, l->maxarrays * sizeof(*l->arrays)); - if (oldarrays) - { - memcpy(l->arrays, oldarrays, l->numarrays * sizeof(*l->arrays)); - Mem_Free(oldarrays); - } - } - l->arrays[index].numflaggedrecords = 0; - l->arrays[index].data = (unsigned char *) Mem_Alloc(l->mempool, (l->recordsize + 1) * l->numrecordsperarray); - l->arrays[index].allocflags = l->arrays[index].data + l->recordsize * l->numrecordsperarray; - l->numarrays++; - } - if (l->arrays[index].numflaggedrecords < l->numrecordsperarray) - { - for (j = 0;j < l->numrecordsperarray;j++) - { - if (!l->arrays[index].allocflags[j]) - { - l->arrays[index].allocflags[j] = true; - l->arrays[index].numflaggedrecords++; - memset(l->arrays[index].data + l->recordsize * j, 0, l->recordsize); - return (void *)(l->arrays[index].data + l->recordsize * j); - } - } - } - return NULL; -} - void *Mem_ExpandableArray_AllocRecord(memexpandablearray_t *l) { size_t i, j;