include/mempool.h File Reference

Memory pool allocator. More...

#include <types.h>

Include dependency graph for mempool.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mem_pool
 Memory pool. More...

Functions

void mem_pool_init (struct mem_pool *pool, void *start, size_t size, size_t objsize, unsigned int align_order)
 Initialize a memory pool.
void mem_pool_init_physmem (struct mem_pool *mempool, struct physmem_pool *phys_pool, unsigned int nr_objects, size_t objsize, unsigned int align_order)
 Initialize a memory pool using the physmem allocator.
void * mem_pool_alloc (struct mem_pool *pool)
 Allocate an object from a memory pool.
void mem_pool_free (struct mem_pool *pool, const void *obj)
 Free an object previously allocated from a memory pool.


Detailed Description

Memory pool allocator.

A memory pool contains a number of objects of the same size that can be allocated in constant time. This is useful for allocating request structures, etc.

Memory pool objects are managed in Last-In-First-Out (LIFO) order to maximize the chances of allocating a cache-hot object.

See also:
dmapool.h

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file mempool.h.


Function Documentation

void* mem_pool_alloc ( struct mem_pool pool  ) 

Allocate an object from a memory pool.

Parameters:
pool The memory pool from which the object is allocated.
Returns:
A pointer to the newly allocated object, or NULL if the pool is exhausted.

Definition at line 170 of file mempool.c.

References assert, cpu_irq_restore(), cpu_irq_save(), and mem_pool::freelist.

Referenced by usb_req_alloc().

Here is the call graph for this function:

void mem_pool_free ( struct mem_pool pool,
const void *  obj 
)

Free an object previously allocated from a memory pool.

The caller is responsible for making sure obj was originally allocated from pool. If an object is freed into a different pool, the integrity of the pool can no longer be guaranteed.

Note:
The object ceases to exist the moment this function is called, so even though this function casts away the const qualifier internally, it doesn't technically modify the object. Having the const qualifier there allows callers to keep const pointers to objects which are never modified after initialization.
Parameters:
pool The memory pool which the object belongs to.
obj The object to be freed.

Definition at line 202 of file mempool.c.

References assert, cpu_irq_restore(), cpu_irq_save(), and mem_pool::freelist.

Referenced by usb_req_free().

Here is the call graph for this function:

void mem_pool_init ( struct mem_pool pool,
void *  start,
size_t  size,
size_t  objsize,
unsigned int  align_order 
)

Initialize a memory pool.

This function will initialize a memory pool, populating its freelist with objects tightly packed into the specified memory range.

All objects in the pool are guaranteed to be aligned to a multiple of $2^{align\_order}$ bytes. For example, if align_order is 4, each object will start on a 16-byte boundary.

Parameters:
pool The memory pool to be initialized.
start The memory area to be used for object storage.
size The size of the memory area in bytes.
objsize The size of each object in bytes.
align_order log2 of the minimum object alignment in bytes.
Precondition:
objsize must be larger than the size of a pointer.

size must be large enough to hold at least one object.

Definition at line 88 of file mempool.c.

References assert, dbg_printf, mem_pool::freelist, and round_up().

Referenced by mem_pool_init_physmem().

Here is the call graph for this function:

void mem_pool_init_physmem ( struct mem_pool mempool,
struct physmem_pool phys_pool,
unsigned int  nr_objects,
size_t  objsize,
unsigned int  align_order 
)

Initialize a memory pool using the physmem allocator.

This function will grab enough physical memory for nr_objects and use this to initialize mempool.

Parameters:
mempool The memory pool to be initialized.
phys_pool The physical memory pool from which to grab memory
nr_objects The number of objects in the pool
objsize The size of each object in bytes.
align_order log2 of the minimum object alignment in bytes.
Precondition:
nr_objects must be at least one.

There must be enough available memory in phys_pool.

objsize must be larger than the size of a pointer.

See also:
mem_pool_init()

Definition at line 139 of file mempool.c.

References assert, mem_pool_init(), PHYS_MAP_WRBACK, PHYS_MAP_WRBUF, physmem_alloc(), PHYSMEM_ALLOC_ERR, physmem_map(), and round_up().

Referenced by usb_init().

Here is the call graph for this function:


Generated on Tue Sep 15 10:21:18 2009 for libavr32 by  doxygen 1.5.8