#include "global.h"
#include "pool.h"

void *pool_alloc (struct mempool *pool)
{
struct mempoolblock *tmp = NULLPOOLBLK;

	if (!pool->top || ((pool->index + 1) == pool->bottom->entries))	{
		tmp = callocw (1, ((pool->numentries * pool->size) + sizeof(struct mempoolblock)));
		if (pool->top)	{
			pool->bottom->next = tmp;
			pool->bottom = tmp;
		} else
			pool->top = pool->bottom = tmp;
		pool->index = 0;
		pool->bottom->entries = pool->numentries;
		pool->bottom->base = (char *)(((long)pool->bottom) + sizeof(struct mempoolblock));
	}
	return ((void *)&pool->bottom->base[pool->index++ * pool->size]);
}


void pool_free (struct mempool *pool)
{
struct mempoolblock *temp, *blk;

	for (blk = pool->top; blk; blk = temp)	{
		temp = blk->next;
		free(blk);
	}
	pool->top = pool->bottom = NULLPOOLBLK;
	pool->index = 0;
}
			
