- Timestamp:
- 06/10/06 21:01:32 (3 years ago)
- Location:
- trunk/libapool
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
trunk/libapool/apool.c
r26 r27 3 3 #include "apool.h" 4 4 5 apool * apool_new (size_t blocks, size_t blocksize) { 6 apool *pool = malloc(sizeof(apool)); 5 #define apool_double(pool) apool_expand(pool, pool->blocks) 6 7 void apool_expand (apool *pool, size_t blocks) { 8 size_t blocksize = pool->blocksize; 7 9 struct chunk *chunk = malloc(sizeof(struct chunk)); 8 10 9 chunk->next = NULL;11 chunk->next = pool->chunk; 10 12 pool->chunk = chunk; 11 13 12 14 char *ram = chunk->chunk = calloc(blocks, blocksize); 13 15 16 /* Stack must be able to hold atleast all available items */ 17 free(pool->stack); 18 pool->blocks += blocks; 19 pool->stack = calloc(pool->blocks, sizeof(void *)); 20 pool->pos += (int) blocks; 21 22 /* Use the ram in reverse order */ 23 ram += (blocks - 1) * blocksize; 24 for (size_t i = 0; i < blocks; ram -= blocksize) { 25 pool->stack[i++] = ram; 26 } 27 } 28 29 apool * apool_new (size_t blocks, size_t blocksize) { 30 apool *pool = malloc(sizeof(apool)); 31 14 32 pool->blocksize = blocksize; 15 pool->blocks = blocks; 16 pool->heap = calloc(blocks, sizeof(void *)); 17 pool->pos = (int) blocks; 33 pool->blocks = 0; 34 pool->pos = 0; 35 pool->stack = NULL; /* We have no data, free(null) is ok */ 36 pool->chunk = NULL; /* We must initialize pool->chunk so it's stored */ 18 37 19 for (size_t i = 0; i < blocks; i++) { 20 pool->heap[i] = ram; 21 ram += blocksize; 22 } 38 apool_expand(pool, blocks); 23 39 24 40 return pool; 25 }26 27 /* Double the size of the pool */28 void apool_double (apool *pool) {29 size_t blocks = pool->blocks,30 blocksize = pool->blocksize;31 32 struct chunk *chunk = malloc(sizeof(struct chunk));33 chunk->next = pool->chunk;34 pool->chunk = chunk;35 36 char *ram = chunk->chunk = malloc(blocks * blocksize);37 38 /* Our new position */39 pool->pos = (int) blocks;40 41 /* This may look strange, but we're freeing the array of pointers. I use42 * free and calloc instead of realloc because my stack is empty, I don't43 * care about any data in this. */44 free(pool->heap);45 46 pool->blocks *= 2;47 pool->heap = calloc(pool->blocks, sizeof(void *));48 49 for (size_t i = 0; i < blocks; i++) {50 pool->heap[i] = ram;51 ram += blocksize;52 }53 41 } 54 42 … … 69 57 } 70 58 71 free(pool-> heap);59 free(pool->stack); 72 60 free(pool); 73 61 } … … 78 66 } 79 67 80 return pool-> heap[--pool->pos];68 return pool->stack[--pool->pos]; 81 69 } 82 70 83 71 void apool_free (apool *pool, void * chunk) { 84 pool-> heap[pool->pos++] = chunk;72 pool->stack[pool->pos++] = chunk; 85 73 } -
trunk/libapool/apool.h
r26 r27 10 10 size_t blocksize; 11 11 size_t blocks; 12 void ** heap;12 void **stack; 13 13 struct chunk *chunk; 14 14 } apool; 15 15 16 16 extern apool * apool_new (size_t blocks, size_t blocksize); 17 extern void apool_expand (apool *pool, size_t blocks); 18 extern void apool_dobule (apool *pool); 17 19 extern void apool_destroy (apool *pool); 18 19 /* Double the size of the pool */20 extern void apool_double (apool *pool);21 22 20 extern void * apool_alloc (apool *pool); 23 24 21 extern void apool_free (apool *pool, void * chunk); 25 22