Changeset 27

Show
Ignore:
Timestamp:
06/10/06 21:01:32 (2 years ago)
Author:
scott
Message:

Refactored apool memory pool

  • Doubling is now done through apool_expand()
  • Initialization is now done through apool_expand()
  • Added alias apool_double() which maps to apool_expand
Location:
trunk/libapool
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/libapool/apool.c

    r26 r27  
    33#include "apool.h" 
    44 
    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 
     7void apool_expand (apool *pool, size_t blocks) { 
     8    size_t blocksize = pool->blocksize; 
    79    struct chunk *chunk = malloc(sizeof(struct chunk)); 
    810 
    9     chunk->next = NULL; 
     11    chunk->next = pool->chunk; 
    1012    pool->chunk = chunk; 
    1113 
    1214    char *ram = chunk->chunk = calloc(blocks, blocksize); 
    1315 
     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 
     29apool * apool_new (size_t blocks, size_t blocksize) { 
     30    apool *pool = malloc(sizeof(apool)); 
     31 
    1432    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 */ 
    1837 
    19     for (size_t i = 0; i < blocks; i++) { 
    20         pool->heap[i] = ram; 
    21         ram += blocksize; 
    22     } 
     38    apool_expand(pool, blocks); 
    2339 
    2440    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 use 
    42      * free and calloc instead of realloc because my stack is empty, I don't 
    43      * 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     } 
    5341} 
    5442 
     
    6957    } 
    7058 
    71     free(pool->heap); 
     59    free(pool->stack); 
    7260    free(pool); 
    7361} 
     
    7866    } 
    7967 
    80     return pool->heap[--pool->pos]; 
     68    return pool->stack[--pool->pos]; 
    8169} 
    8270 
    8371void apool_free (apool *pool, void * chunk) { 
    84     pool->heap[pool->pos++] = chunk; 
     72    pool->stack[pool->pos++] = chunk; 
    8573} 
  • trunk/libapool/apool.h

    r26 r27  
    1010    size_t blocksize; 
    1111    size_t blocks; 
    12     void **heap; 
     12    void **stack; 
    1313    struct chunk *chunk; 
    1414} apool; 
    1515 
    1616extern apool * apool_new (size_t blocks, size_t blocksize); 
     17extern void apool_expand (apool *pool, size_t blocks); 
     18extern void apool_dobule (apool *pool); 
    1719extern void apool_destroy (apool *pool); 
    18  
    19 /* Double the size of the pool */ 
    20 extern void apool_double (apool *pool); 
    21  
    2220extern void * apool_alloc (apool *pool); 
    23  
    2421extern void apool_free (apool *pool, void * chunk); 
    2522