From: Arnar Mar Hrafnkelsson Date: Fri, 28 Dec 2001 17:45:41 +0000 (+0000) Subject: Added memory pools to make clean up of temporary buffers simpler. X-Git-Tag: Imager-0.48^2~468 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/8047cbb53abe9b17c4bc65e3d3ba176caffdddc4 Added memory pools to make clean up of temporary buffers simpler. --- diff --git a/imio.h b/imio.h index 66e180dd..5d746042 100644 --- a/imio.h +++ b/imio.h @@ -36,6 +36,20 @@ void* myrealloc(void *p, size_t newsize); #endif /* IMAGER_MALLOC_DEBUG */ + +typedef struct i_mempool { + void **p; + unsigned int alloc; + unsigned int used; +} i_mempool; + +void i_mempool_init(i_mempool *mp); +void i_mempool_extend(i_mempool *mp); +void *i_mempool_alloc(i_mempool *mp, size_t size); +void i_mempool_destroy(i_mempool *mp); + + + #ifdef _MSC_VER #undef min #undef max diff --git a/io.c b/io.c index 6661a3ab..1936ee8a 100644 --- a/io.c +++ b/io.c @@ -260,6 +260,37 @@ myrealloc(void *block, size_t size) { +/* memory pool implementation */ + +void +i_mempool_init(i_mempool *mp) { + mp->alloc = 10; + mp->used = 0; + mp->p = mymalloc(sizeof(void*)*mp->alloc); +} + +void +i_mempool_extend(i_mempool *mp) { + mp->p = myrealloc(mp->p, mp->alloc * 2); + mp->alloc *=2; +} + +void * +i_mempool_alloc(i_mempool *mp, size_t size) { + if (mp->used == mp->alloc) i_mempool_extend(mp); + mp->p[mp->used] = mymalloc(size); + mp->used++; + return mp->p[mp->used-1]; +} + + +void +i_mempool_destroy(i_mempool *mp) { + unsigned int i; + for(i=0; iused; i++) myfree(mp->p[i]); + myfree(mp->p); +} + /* Should these really be here? */