From cac3443d4ed4f3dd1a2dbf86627c0017dedefbb3 Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Mon, 8 Feb 2010 14:59:59 +0000 Subject: Guardedalloc: added MEM_reallocN function to do simple alloc/memcpy/free, not as optimized as a system realloc but I've had to do this often enough manually to justify a utility function. --- intern/guardedalloc/MEM_guardedalloc.h | 7 +++++++ intern/guardedalloc/intern/mallocn.c | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+) (limited to 'intern/guardedalloc') diff --git a/intern/guardedalloc/MEM_guardedalloc.h b/intern/guardedalloc/MEM_guardedalloc.h index e404f174935..2c3aab8eb2b 100644 --- a/intern/guardedalloc/MEM_guardedalloc.h +++ b/intern/guardedalloc/MEM_guardedalloc.h @@ -84,6 +84,13 @@ extern "C" { * newly allocated block. */ void *MEM_dupallocN(void *vmemh); + /** + * Reallocates a block of memory, and returns pointer to the newly + * allocated block, the old one is freed. this is not as optimized + * as a system realloc but just makes a new allocation and copies + * over from existing memory. */ + void *MEM_reallocN(void *vmemh, unsigned int len); + /** * Allocate a block of memory of size len, with tag name str. The * memory is cleared. The name must be static, because only a diff --git a/intern/guardedalloc/intern/mallocn.c b/intern/guardedalloc/intern/mallocn.c index 446daea1c03..6c6836db829 100644 --- a/intern/guardedalloc/intern/mallocn.c +++ b/intern/guardedalloc/intern/mallocn.c @@ -225,6 +225,28 @@ void *MEM_dupallocN(void *vmemh) return newp; } +void *MEM_reallocN(void *vmemh, unsigned int len) +{ + void *newp= NULL; + + if (vmemh) { + MemHead *memh= vmemh; + memh--; + + newp= MEM_mallocN(len, memh->name); + if(newp) { + if(len < memh->len) + memcpy(newp, vmemh, len); + else + memcpy(newp, vmemh, memh->len); + } + + MEM_freeN(vmemh); + } + + return newp; +} + static void make_memhead_header(MemHead *memh, unsigned int len, const char *str) { MemTail *memt; -- cgit v1.2.3