void * pool_allocate(size_t size) { // get pool struct pool *p = get_pool(size); struct slot *slot; /* locate free slot or expand */ // use next slot if (p->slots != p->cursor) { debug("ALLOCATE", "allocating unused slot %d", p->cursor); slot = pool_create_slot(p, p->cursor++); } // use a freed slot else if (p->freed->size > 0) { slot = (struct slot *) queue_pop(p->freed); debug("ALLOCATE", "reallocating existing slot %d", slot->index); } // need to expand else { debug("ALLOCATE", "no more slots in pool %d, expanding", p->size); pool_expand(p); slot = pool_create_slot(p, p->cursor++); } return (void *) ((char *) slot) + headerlen; }
static void* pool_malloc(size_t sz) { void *res; sz = round_up(sz,8); pthread_mutex_lock(&memory_pool_mutex); pool_expand(sz); res = (void*)((char*)memory_pools->memory + memory_pools->used); memory_pools->used += sz; pthread_mutex_unlock(&memory_pool_mutex); memset(res,0,sz); return res; }