[Bf-blender-cvs] [c82371f] master: Mempool: simplify memory chunk list building

Campbell Barton noreply at git.blender.org
Tue Apr 8 05:43:03 CEST 2014


Commit: c82371fc06ffb8d2970c985b44167e9a5e0222f1
Author: Campbell Barton
Date:   Tue Apr 8 13:41:57 2014 +1000
https://developer.blender.org/rBc82371fc06ffb8d2970c985b44167e9a5e0222f1

Mempool: simplify memory chunk list building

===================================================================

M	source/blender/blenlib/intern/BLI_mempool.c

===================================================================

diff --git a/source/blender/blenlib/intern/BLI_mempool.c b/source/blender/blenlib/intern/BLI_mempool.c
index 3c158fa..21af8af 100644
--- a/source/blender/blenlib/intern/BLI_mempool.c
+++ b/source/blender/blenlib/intern/BLI_mempool.c
@@ -186,12 +186,9 @@ static BLI_mempool_chunk *mempool_chunk_alloc(BLI_mempool *pool)
  *
  * \param pool  The pool to add the chunk into.
  * \param mpchunk  The new uninitialized chunk (can be malloc'd)
- * \param lasttail  The last element of the previous chunk
  * (used when building free chunks initially)
- * \return The last chunk,
  */
-static BLI_freenode *mempool_chunk_add(BLI_mempool *pool, BLI_mempool_chunk *mpchunk,
-                                       BLI_freenode *lasttail)
+static void mempool_chunk_add(BLI_mempool *pool, BLI_mempool_chunk *mpchunk)
 {
 	const unsigned int esize = pool->esize;
 	BLI_freenode *curnode = CHUNK_DATA(mpchunk);
@@ -209,10 +206,6 @@ static BLI_freenode *mempool_chunk_add(BLI_mempool *pool, BLI_mempool_chunk *mpc
 	mpchunk->next = NULL;
 	pool->chunk_tail = mpchunk;
 
-	if (UNLIKELY(pool->free == NULL)) {
-		pool->free = curnode;
-	}
-
 	/* loop through the allocated data, building the pointer structures */
 	j = pool->pchunk;
 	if (pool->flag & BLI_MEMPOOL_ALLOW_ITER) {
@@ -229,21 +222,15 @@ static BLI_freenode *mempool_chunk_add(BLI_mempool *pool, BLI_mempool_chunk *mpc
 		}
 	}
 
-	/* terminate the list (rewind one)
-	 * will be overwritten if 'curnode' gets passed in again as 'lasttail' */
+	/* terminate the list (rewind one) */
 	curnode = NODE_STEP_PREV(curnode);
-	curnode->next = NULL;
+	/* 'pool->free' may be NULL, in this case its terminating the list */
+	curnode->next = pool->free;
+	pool->free = CHUNK_DATA(mpchunk);
 
 #ifdef USE_TOTALLOC
 	pool->totalloc += pool->pchunk;
 #endif
-
-	/* final pointer in the previously allocated chunk is wrong */
-	if (lasttail) {
-		lasttail->next = CHUNK_DATA(mpchunk);
-	}
-
-	return curnode;
 }
 
 static void mempool_chunk_free(BLI_mempool_chunk *mpchunk)
@@ -269,7 +256,6 @@ BLI_mempool *BLI_mempool_create(unsigned int esize, unsigned int totelem,
                                 unsigned int pchunk, unsigned int flag)
 {
 	BLI_mempool *pool;
-	BLI_freenode *lasttail = NULL;
 	unsigned int i, maxchunks;
 
 	/* allocate the pool structure */
@@ -315,7 +301,7 @@ BLI_mempool *BLI_mempool_create(unsigned int esize, unsigned int totelem,
 		/* allocate the actual chunks */
 		for (i = 0; i < maxchunks; i++) {
 			BLI_mempool_chunk *mpchunk = mempool_chunk_alloc(pool);
-			lasttail = mempool_chunk_add(pool, mpchunk, lasttail);
+			mempool_chunk_add(pool, mpchunk);
 		}
 	}
 
@@ -333,7 +319,7 @@ void *BLI_mempool_alloc(BLI_mempool *pool)
 	if (UNLIKELY(pool->free == NULL)) {
 		/* need to allocate a new chunk */
 		BLI_mempool_chunk *mpchunk = mempool_chunk_alloc(pool);
-		mempool_chunk_add(pool, mpchunk, NULL);
+		mempool_chunk_add(pool, mpchunk);
 	}
 
 	free_pop = pool->free;
@@ -613,7 +599,6 @@ void BLI_mempool_clear_ex(BLI_mempool *pool, const int totelem_reserve)
 	unsigned int maxchunks;
 
 	BLI_mempool_chunk *chunks_temp;
-	BLI_freenode *lasttail = NULL;
 
 #ifdef WITH_MEM_VALGRIND
 	VALGRIND_DESTROY_MEMPOOL(pool);
@@ -654,7 +639,7 @@ void BLI_mempool_clear_ex(BLI_mempool *pool, const int totelem_reserve)
 
 	while ((mpchunk = chunks_temp)) {
 		chunks_temp = mpchunk->next;
-		lasttail = mempool_chunk_add(pool, mpchunk, lasttail);
+		mempool_chunk_add(pool, mpchunk);
 	}
 }




More information about the Bf-blender-cvs mailing list