# lib/mempool.c File Reference

Memory pool allocator. More...

`#include <assert.h>`
`#include <debug.h>`
`#include <interrupt.h>`
`#include <mempool.h>`
`#include <physmem.h>`
`#include <util.h>`

Include dependency graph for mempool.c:

Go to the source code of this file.

## 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.

This file implements the memory pool interface defined by mempool.h.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All devices
- AppNote:

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

Todo:
  Add a "reset pool" operation which basically frees all objects associated with the pool. This could be useful for error recovery. It also requires fields keeping track of the memory area associated with the pool.

Todo:
  Debugging feature: When freeing an object, verify that the object really belongs to the pool it is being freed to. This requires additional fields keeping track of the memory area associated with the pool.

Todo:
  Debugging feature: When freeing an object, scan the pool to see if the object has been freed before.

Definition in file mempool.c.

## 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 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: