Example #1
0
/* Function: int mempool_clean(MemPool *mempool)
 *
 * Purpose: return all memory to free list
 * Args: mempool - pointer to a MemPool struct
 *
 * Returns: 0 on success, -1 on failure
 */
int mempool_clean(MemPool *mempool)
{
    unsigned int i;
    int ret;

    if (mempool == NULL)
        return -1;

    /* clean used list */
    ret = sf_sdlist_delete(&mempool->used_list);
    if (ret != 0)
        return -1;

    /* clean free list */
    ret = sf_sdlist_delete(&mempool->free_list);
    if (ret != 0)
        return -1;

    /* add everything back to free list */
    for (i = 0; i < mempool->total; i++)
    {
        ret = sf_sdlist_append(&mempool->free_list, &mempool->bucketpool[i],
                               &mempool->listpool[i]);
        if (ret == -1)
            return -1;
    }

    return 0;
}
Example #2
0
/* Function: MemBucket *mempool_alloc(MemPool *mempool);
 * 
 * Purpose: allocate a new object from the mempool
 * Args: mempool - pointer to a MemPool struct
 * 
 * Returns: a pointer to the mempool object on success, NULL on failure
 */ 
MemBucket *mempool_alloc(MemPool *mempool)
{
    SDListItem *li = NULL;
    MemBucket *b;
    
    if(mempool == NULL)
    {
        return NULL;
    }

    if(mempool->free <= 0)
    {
        return NULL;
    }

    /* get one item off the free_list,
       put one item on the usedlist
     */

    li = mempool->free_list.head;

    mempool->free--;
    if((li == NULL) || sf_sdlist_remove(&mempool->free_list, li))
    {
        printf("Failure on sf_sdlist_remove\n");
        return NULL;
    }
        
    
    mempool->used++;

    if(sf_sdlist_append(&mempool->used_list, li->data, li))
    {
        printf("Failure on sf_sdlist_append\n");
        return NULL;
    }

    /* TBD -- make configurable */
    b = li->data;
    bzero(b->data, mempool->obj_size);
    
    return b;
}
Example #3
0
void mempool_free(MemPool *mempool, MemBucket *obj)
{       
    if(sf_sdlist_remove(&mempool->used_list, obj->key))
    {
        printf("failure on remove from used_list");
        return;
    }
    
    mempool->used--;

    /* put the address of the membucket back in the list */
    if(sf_sdlist_append(&mempool->free_list, obj, obj->key))
    {
        printf("failure on add to free_list");
        return;
    }

    mempool->free++;    
    return;
}
Example #4
0
void mempool_free(MemPool *mempool, MemBucket *obj)
{
    if ((mempool == NULL) || (obj == NULL))
        return;

    if(sf_sdlist_remove(&mempool->used_list, obj->key))
    {
#ifdef TEST_MEMPOOL
        printf("failure on remove from used_list");
#endif
        return;
    }

    /* put the address of the membucket back in the list */
    if(sf_sdlist_append(&mempool->free_list, obj, obj->key))
    {
#ifdef TEST_MEMPOOL
        printf("failure on add to free_list");
#endif
        return;
    }

    return;
}
Example #5
0
int mempool_init(MemPool *mempool, PoolCount num_objects, size_t obj_size)
{
    PoolCount i;

    if(mempool == NULL)
        return 1;

    if(num_objects < 1)
        return 1;

    if(obj_size < 1)
        return 1;

    mempool->obj_size = obj_size;

    /* this is the basis pool that represents all the *data pointers
       in the list */
    mempool->datapool = calloc(num_objects, obj_size);
    if(mempool->datapool == NULL)
        return 1;

    mempool->listpool = calloc(num_objects, sizeof(SDListItem));
    if(mempool->listpool == NULL)
    {
        /* well, that sucked, lets clean up */
        ErrorMessage("%s(%d) mempool_init(): listpool is null\n",
                     __FILE__, __LINE__);
        mempool_free_pools(mempool);
        return 1;
    }

    mempool->bucketpool = calloc(num_objects, sizeof(MemBucket));
    if(mempool->bucketpool == NULL)
    {
        ErrorMessage("%s(%d) mempool_init(): bucketpool is null\n",
                     __FILE__, __LINE__);
        mempool_free_pools(mempool);
        return 1;
    }

    /* sets up the 2 memory lists */
    if(sf_sdlist_init(&mempool->used_list, NULL))
    {
        ErrorMessage("%s(%d) mempool_init(): Failed to initialize used list\n",
                     __FILE__, __LINE__);
        mempool_free_pools(mempool);
        return 1;
    }

    if(sf_sdlist_init(&mempool->free_list, NULL))
    {
        ErrorMessage("%s(%d) mempool_init(): Failed to initialize free list\n",
                     __FILE__, __LINE__);
        mempool_free_pools(mempool);
        return 1;
    }


    for(i=0; i<num_objects; i++)
    {
        SDListItem *itemp;
        MemBucket *bp;

        bp = &mempool->bucketpool[i];
        itemp = &mempool->listpool[i];

        /* each bucket knows where it resides in the list */
        bp->key = itemp;

#ifdef TEST_MEMPOOL
        printf("listpool: %p itemp: %p diff: %u\n",
               mempool->listpool, itemp,
               (((char *) itemp) - ((char *) mempool->listpool)));
#endif

        bp->data = ((char *) mempool->datapool) + (i * mempool->obj_size);

#ifdef TEST_MEMPOOL
        printf("datapool: %p bp.data: %p diff: %u\n",
               mempool->datapool,
               mempool->datapool + (i * mempool->obj_size),
               (((char *) bp->data) - ((char *) mempool->datapool)));
#endif

        if(sf_sdlist_append(&mempool->free_list,
                            &mempool->bucketpool[i],
                            &mempool->listpool[i]))
        {
            ErrorMessage("%s(%d) mempool_init(): Failed to add to free list\n",
                         __FILE__, __LINE__);
            mempool_free_pools(mempool);
            return 1;
        }

        mempool->total++;
    }

    return 0;
}
Example #6
0
int mempool_init(MemPool *mempool, PoolCount num_objects, size_t obj_size)
{
    PoolCount i;
    
    if(mempool == NULL)
        return 1;

    if(num_objects < 1)
        return 1;

    if(obj_size < 1)
        return 1;

    mempool->obj_size = obj_size;
    
    /* this is the basis pool that represents all the *data pointers
       in the list */
    mempool->datapool = calloc(num_objects, obj_size);
    
    
    if(mempool->datapool == NULL)
    {
        return 1;
    }

    mempool->listpool = calloc(num_objects, sizeof(SDListItem));

    if(mempool->listpool == NULL)
    {
        /* well, that sucked, lets clean up */
        fprintf(stderr, "mempool: listpool is null\n");
        free(mempool->datapool);
        return 1;
    }

    mempool->bucketpool = calloc(num_objects, sizeof(MemBucket));

    if(mempool->bucketpool == NULL)
    {
        fprintf(stderr, "mempool: bucketpool is null\n");
        free(mempool->datapool);
        free(mempool->listpool);
        return 1;
    }

    /* sets up the 2 memory lists */
    if(sf_sdlist_init(&mempool->used_list, NULL))
    {
        fprintf(stderr, "mempool: used_list failed\n");
        free(mempool->datapool);
        free(mempool->listpool);
        free(mempool->bucketpool);
        return 1;
    }

    if(sf_sdlist_init(&mempool->free_list, NULL))
    {
        fprintf(stderr, "mempool: free_list failed\n");
        free(mempool->datapool);
        free(mempool->listpool);
        free(mempool->bucketpool);
        return 1;
    }


    for(i=0; i<num_objects; i++)
    {
        SDListItem *itemp;
        MemBucket *bp;

        bp = &mempool->bucketpool[i];
        itemp = &mempool->listpool[i];
        
        /* each bucket knows where it resides in the list */
        bp->key = itemp;

#ifdef TEST_MEMPOOL        
        printf("listpool: %p itemp: %p diff: %u\n",
               mempool->listpool, itemp,
               (((char *) itemp) -
                ((char *) mempool->listpool)));
#endif /* TEST_MEMPOOL */
               
        bp->data = ((char *) mempool->datapool) + (i * mempool->obj_size);
        
#ifdef TEST_MEMPOOL        
        printf("datapool: %p bp.data: %p diff: %u\n",
               mempool->datapool,
               mempool->datapool + (i * mempool->obj_size),
               (((char *) bp->data) -
                ((char *) mempool->datapool)));
#endif /* TEST_MEMPOOL */
        

        if(sf_sdlist_append(&mempool->free_list,                           
                            &mempool->bucketpool[i],
                            &mempool->listpool[i]))
        {
            fprintf(stderr, "mempool: free_list_append failed\n");
            free(mempool->datapool);
            free(mempool->listpool);
            free(mempool->bucketpool);
            return 1;
        }

        mempool->free++;
    }

    mempool->used = 0;
    mempool->total = num_objects;
    
    return 0;
}