You can use them for efficient allocation of large amount of small fixed-sized memory blocks and to free them all at once. If you need more features, see more complex memory pools.

Definitions


struct eltpool {
  struct eltpool_chunk *first_chunk;
  struct eltpool_free *first_free;
  uint elt_size;
  uint chunk_size;
  uint elts_per_chunk;
  uint num_allocated;           // Just for debugging
  uint num_chunks;
};

Memory pool of fixed-sized elements. You should use this one as an opaque handle only, the insides are internal.

Basic manipulation


struct eltpool *ep_new(uint elt_size, uint elts_per_chunk);

Create a new memory pool for elements of elt_size bytes. The pool will allocate chunks of at least elts_per_chunk elements. Higher numbers lead to better allocation times but also to bigger unused memory blocks. Call ep_delete() to free all pool’s resources.

Element pools can be treated as resources, see res_eltpool().


void ep_delete(struct eltpool *pool);

Release a memory pool created by ep_new() including all elements allocated from that pool.


u64 ep_total_size(struct eltpool *pool);

Return the total number of bytes allocated by a given memory pool including all internals.

Allocation routines


static inline void *ep_alloc(struct eltpool *pool);

Allocate a new element on a given memory pool. The results is always aligned to a multiple of the element’s size.


static inline void ep_free(struct eltpool *pool, void *p);

Release an element previously allocated by ep_alloc(). Note thet the memory is not really freed (until mp_delete()), but it can be reused by future ep_alloc()'s.