diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-11-16 23:06:38 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-11-16 23:06:38 +0400 |
commit | 9d05ccf9e84510ca7e3fe140f6e20991a931b8ce (patch) | |
tree | f8f7241b779c9049d853a5e824db2dd150228ebe /source/blender/blenlib | |
parent | 707fcc42a390e91aedd858588c30d04fea43f0e6 (diff) | |
parent | 1af839081b7adcc3ceb13e5e7cc579b309e90915 (diff) |
svn merge -r41899:41926 ^/trunk/blender. also sync mempool with trunk and move BLI_mempool_alloc out of mempools header where it was inlined
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_mempool.h | 87 | ||||
-rw-r--r-- | source/blender/blenlib/intern/BLI_mempool.c | 109 |
2 files changed, 71 insertions, 125 deletions
diff --git a/source/blender/blenlib/BLI_mempool.h b/source/blender/blenlib/BLI_mempool.h index 54b4e5584eb..2f56bc87665 100644 --- a/source/blender/blenlib/BLI_mempool.h +++ b/source/blender/blenlib/BLI_mempool.h @@ -40,36 +40,17 @@ extern "C" #endif struct BLI_mempool; +struct BLI_mempool_chunk; -#include "BLI_listbase.h" -#include "BLI_blenlib.h" -#include <string.h> - -typedef struct BLI_freenode{ - struct BLI_freenode *next; - int freeword; /*used to identify this as a freed node*/ -}BLI_freenode; - -typedef struct BLI_mempool_chunk{ - struct BLI_mempool_chunk *next, *prev; - void *data; -}BLI_mempool_chunk; - -typedef struct BLI_mempool{ - struct ListBase chunks; - int esize, csize, pchunk; /*size of elements and chunks in bytes and number of elements per chunk*/ - BLI_freenode *free; /*free element list. Interleaved into chunk datas.*/ - int totalloc, totused; /*total number of elements allocated in total, and currently in use*/ - int use_sysmalloc, allow_iter; -}BLI_mempool; +typedef struct BLI_mempool BLI_mempool; /*allow_iter allows iteration on this mempool. note: this requires that the first four bytes of the elements never contain the character string 'free'. use with care.*/ -BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, - int use_sysmalloc, int allow_iter); -//void *BLI_mempool_alloc(BLI_mempool *pool); +struct BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, + int use_sysmalloc, int allow_iter); +void *BLI_mempool_alloc(BLI_mempool *pool); void *BLI_mempool_calloc(BLI_mempool *pool); void BLI_mempool_free(BLI_mempool *pool, void *addr); void BLI_mempool_destroy(BLI_mempool *pool); @@ -90,64 +71,6 @@ void BLI_mempool_allow_iter(BLI_mempool *pool); void BLI_mempool_iternew(BLI_mempool *pool, BLI_mempool_iter *iter); void *BLI_mempool_iterstep(BLI_mempool_iter *iter); -/* XXX - copied from BKE_utildefines.h, dont use here because we're in BLI */ -#if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__hppa__) || defined (__BIG_ENDIAN__) - /* Big Endian */ -#define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) ) -#else - /* Little Endian */ -#define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) ) -#endif - -/************ inlined stuff ***********/ -#define FREEWORD MAKE_ID('f', 'r', 'e', 'e') -#include "MEM_guardedalloc.h" - -BM_INLINE void *BLI_mempool_alloc(BLI_mempool *pool) { - void *retval=NULL; - BLI_freenode *curnode=NULL; - char *addr=NULL; - - if (!pool) return NULL; - - pool->totused++; - - if (!(pool->free)) { - int j; - /*need to allocate a new chunk*/ - BLI_mempool_chunk *mpchunk = pool->use_sysmalloc ? (BLI_mempool_chunk*)malloc(sizeof(BLI_mempool_chunk)) : (BLI_mempool_chunk*)MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); - mpchunk->next = mpchunk->prev = NULL; - mpchunk->data = pool->use_sysmalloc ? malloc(pool->csize) : MEM_mallocN(pool->csize, "BLI_Mempool Chunk Data"); - BLI_addtail(&(pool->chunks), mpchunk); - - pool->free = (BLI_freenode*)mpchunk->data; /*start of the list*/ - if (pool->allow_iter) - pool->free->freeword = FREEWORD; - for(addr = (char*)mpchunk->data, j=0; j < pool->pchunk; j++){ - curnode = ((BLI_freenode*)addr); - addr += pool->esize; - curnode->next = (BLI_freenode*)addr; - - if (pool->allow_iter) { - curnode->freeword = FREEWORD; - if (j != pool->pchunk-1) - curnode->next->freeword = FREEWORD; - } - } - curnode->next = NULL; /*terminate the list*/ - - pool->totalloc += pool->pchunk; - } - - retval = pool->free; - if (pool->allow_iter) - pool->free->freeword = 0x7FFFFFFF; - - pool->free = pool->free->next; - //memset(retval, 0, pool->esize); - return retval; -} - #ifdef __cplusplus } #endif diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c index 7dd4b6707c3..80b70dd45b6 100644 --- a/source/blender/blenlib/intern/BLI_mempool.c +++ b/source/blender/blenlib/intern/BLI_mempool.c @@ -29,27 +29,50 @@ * \ingroup bli */ - /* - Simple, fast memory allocator for allocating many elements of the same size. -*/ - -#include "MEM_guardedalloc.h" + * Simple, fast memory allocator for allocating many elements of the same size. + */ #include "BLI_utildefines.h" +#include "BLI_listbase.h" -#include "BLI_blenlib.h" -#include "BLI_linklist.h" -#include "BLI_mempool.h" -#include "BLI_utildefines.h" +#include "BLI_mempool.h" /* own include */ #include "DNA_listBase.h" -#include "DNA_ID.h" -#include <string.h> +#include "MEM_guardedalloc.h" + +#include <string.h> +#include <stdlib.h> -#define BLI_MEMPOOL_INTERN -#include "BLI_mempool.h" +/* note: copied from BKE_utildefines.h, dont use here because we're in BLI */ +#ifdef __BIG_ENDIAN__ +/* Big Endian */ +# define MAKE_ID(a,b,c,d) ( (int)(a)<<24 | (int)(b)<<16 | (c)<<8 | (d) ) +#else +/* Little Endian */ +# define MAKE_ID(a,b,c,d) ( (int)(d)<<24 | (int)(c)<<16 | (b)<<8 | (a) ) +#endif + +#define FREEWORD MAKE_ID('f', 'r', 'e', 'e') + +typedef struct BLI_freenode { + struct BLI_freenode *next; + int freeword; /* used to identify this as a freed node */ +} BLI_freenode; + +typedef struct BLI_mempool_chunk { + struct BLI_mempool_chunk *next, *prev; + void *data; +} BLI_mempool_chunk; + +typedef struct BLI_mempool { + struct ListBase chunks; + int esize, csize, pchunk; /*size of elements and chunks in bytes and number of elements per chunk*/ + BLI_freenode *free; /*free element list. Interleaved into chunk datas.*/ + int totalloc, totused; /*total number of elements allocated in total, and currently in use*/ + int use_sysmalloc, allow_iter; +} BLI_mempool; BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, int use_sysmalloc, int allow_iter) @@ -69,7 +92,7 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, pool = use_sysmalloc ? malloc(sizeof(BLI_mempool)) : MEM_mallocN(sizeof(BLI_mempool), "memory pool"); pool->esize = allow_iter ? MAX2(esize, sizeof(BLI_freenode)) : esize; pool->use_sysmalloc = use_sysmalloc; - pool->pchunk = pchunk; + pool->pchunk = pchunk; pool->csize = esize * pchunk; pool->chunks.first = pool->chunks.last = NULL; pool->totused= 0; @@ -79,7 +102,7 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, if (maxchunks==0) maxchunks = 1; /*allocate the actual chunks*/ - for(i=0; i < maxchunks; i++){ + for (i=0; i < maxchunks; i++) { BLI_mempool_chunk *mpchunk = use_sysmalloc ? malloc(sizeof(BLI_mempool_chunk)) : MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); mpchunk->next = mpchunk->prev = NULL; mpchunk->data = use_sysmalloc ? malloc(pool->csize) : MEM_mallocN(pool->csize, "BLI Mempool Chunk Data"); @@ -92,7 +115,7 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, } /*loop through the allocated data, building the pointer structures*/ - for(addr = mpchunk->data, j=0; j < pool->pchunk; j++){ + for (addr = mpchunk->data, j=0; j < pool->pchunk; j++) { curnode = ((BLI_freenode*)addr); addr += pool->esize; curnode->next = (BLI_freenode*)addr; @@ -114,24 +137,24 @@ BLI_mempool *BLI_mempool_create(int esize, int tote, int pchunk, pool->totalloc += pool->pchunk; } - /*terminate the list*/ curnode->next = NULL; return pool; } -#if 0 + void *BLI_mempool_alloc(BLI_mempool *pool) { void *retval=NULL; - BLI_freenode *curnode=NULL; - char *addr=NULL; - int j; - + if (!pool) return NULL; - + pool->totused++; - if(!(pool->free)){ + if (!(pool->free)) { + BLI_freenode *curnode=NULL; + char *addr; + int j; + /*need to allocate a new chunk*/ BLI_mempool_chunk *mpchunk = pool->use_sysmalloc ? malloc(sizeof(BLI_mempool_chunk)) : MEM_mallocN(sizeof(BLI_mempool_chunk), "BLI_Mempool Chunk"); mpchunk->next = mpchunk->prev = NULL; @@ -165,24 +188,19 @@ void *BLI_mempool_alloc(BLI_mempool *pool) //memset(retval, 0, pool->esize); return retval; } -#endif void *BLI_mempool_calloc(BLI_mempool *pool) { - void *retval=NULL; - retval = BLI_mempool_alloc(pool); - BMEMSET(retval, 0, pool->esize); + void *retval= BLI_mempool_alloc(pool); + memset(retval, 0, pool->esize); return retval; } - -void BLI_mempool_free(BLI_mempool *pool, void *addr) //doesnt protect against double frees, dont be stupid! +/* doesnt protect against double frees, dont be stupid! */ +void BLI_mempool_free(BLI_mempool *pool, void *addr) { BLI_freenode *newhead = addr; - BLI_freenode *curnode=NULL; - char *tmpaddr=NULL; - int i; - + if (pool->allow_iter) newhead->freeword = FREEWORD; newhead->next = pool->free; @@ -192,14 +210,18 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) //doesnt protect against do /*nothing is in use; free all the chunks except the first*/ if (pool->totused == 0) { - BLI_mempool_chunk *mpchunk=NULL, *first; + BLI_freenode *curnode=NULL; + char *tmpaddr=NULL; + int i; + + BLI_mempool_chunk *mpchunk=NULL; + BLI_mempool_chunk *first= pool->chunks.first; - first = pool->chunks.first; BLI_remlink(&pool->chunks, first); - for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { - if(pool->use_sysmalloc) free(mpchunk->data); - else MEM_freeN(mpchunk->data); + for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { + if (pool->use_sysmalloc) free(mpchunk->data); + else MEM_freeN(mpchunk->data); } pool->use_sysmalloc ? BLI_freelist(&(pool->chunks)) : BLI_freelistN(&(pool->chunks)); @@ -208,7 +230,7 @@ void BLI_mempool_free(BLI_mempool *pool, void *addr) //doesnt protect against do pool->totalloc = pool->pchunk; pool->free = first->data; /*start of the list*/ - for(tmpaddr = first->data, i=0; i < pool->pchunk; i++){ + for (tmpaddr = first->data, i=0; i < pool->pchunk; i++) { curnode = ((BLI_freenode*)tmpaddr); tmpaddr += pool->esize; curnode->next = (BLI_freenode*)tmpaddr; @@ -264,15 +286,16 @@ void *BLI_mempool_iterstep(BLI_mempool_iter *iter) void BLI_mempool_destroy(BLI_mempool *pool) { BLI_mempool_chunk *mpchunk=NULL; - if(pool->use_sysmalloc) { - for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { + + if (pool->use_sysmalloc) { + for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { free(mpchunk->data); } BLI_freelist(&(pool->chunks)); free(pool); } else { - for(mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { + for (mpchunk = pool->chunks.first; mpchunk; mpchunk = mpchunk->next) { MEM_freeN(mpchunk->data); } BLI_freelistN(&(pool->chunks)); |