BeRTOS
Defines
Pool memory allocator
Embedded optimized general purpose data types

Pool macros. More...

Defines

#define EXTERN_POOL(name)   extern List name
 Extern pool declaration.
#define DECLARE_POOL(name, type, num)   DECLARE_POOL_WITH_STORAGE(name, type, num, List)
 Helper macro to declare a Pool data type.
#define DECLARE_POOL_STATIC(name, type, num)   DECLARE_POOL_WITH_STORAGE(name, type, num, static List)
 Static Pool declaration.
#define pool_init(name, init_func)   (*(name##_init))(init_func)
 Initialize the pool name, calling init_func on each element.
#define pool_alloc(name)   list_remHead(name)
 Allocate an element from the pool.
#define pool_free(name, elem)   ADDHEAD(name, (Node*)elem)
 Recycle an element into the pool.
#define pool_empty(name)   LIST_EMPTY(name)
 Test if the pool is empty.

Detailed Description

Pool macros.

The pool module provides the boilerplate code to create a set of objects of the same type. It provides an interface similar to the heap module, with pool_alloc() and pool_free() functions that allocate and free an element respectively. In contrast with the heap module, you can specify exactly the number of items that you want to be in the pool.

Items in the pool must be a derived class of Node *, which also means that they can be used as-is with list containers, eg. MsgPort.

Example code:

 typedef struct MyType
 {
     Node *n;
     uint16_t *buf;
     // other members here...
 } MyType;

 DECLARE_POOL(mypool, MyType, POOL_SIZE);

 static void elem_init(MyType *e)
 {
     e->buf = NULL;
     // other initializations here
 }

 int main(void)
 {
     pool_init(&mypool, elem_init);

     MyType *foo = pool_alloc(&mypool);
     // do stuff with foo
     pool_free(&mypool, foo);
 }
Author:
Giovanni Bajo <rasky@develer.com>
Luca Ottaviano <lottaviano@develer.com>

Define Documentation

#define DECLARE_POOL (   name,
  type,
  num 
)    DECLARE_POOL_WITH_STORAGE(name, type, num, List)

Helper macro to declare a Pool data type.

Data type inserted into the pool must be a Node * type.

Parameters:
nameVariable name of the pool.
typeData type held by the pool.
numNumber of elements in pool.

Definition at line 117 of file pool.h.

#define DECLARE_POOL_STATIC (   name,
  type,
  num 
)    DECLARE_POOL_WITH_STORAGE(name, type, num, static List)

Static Pool declaration.

See also:
DECLARE_POOL

Definition at line 125 of file pool.h.

#define pool_alloc (   name)    list_remHead(name)

Allocate an element from the pool.

The returned element is of type Node *, it's safe to cast it to the type contained in the pool.

Note:
If the element was recycled with pool_free(), it will not be reset, so don't assume that the element has specific values.
Parameters:
namePointer to pool to alloc from.
Returns:
Element of the type present in the pool.

Definition at line 154 of file pool.h.

#define pool_empty (   name)    LIST_EMPTY(name)

Test if the pool is empty.

Parameters:
namePointer to pool.
Returns:
True if the pool is empty, false otherwise.

Definition at line 173 of file pool.h.

#define pool_free (   name,
  elem 
)    ADDHEAD(name, (Node*)elem)

Recycle an element into the pool.

Note:
Element fields are not reset to its original values, keep that in mind when you alloc nodes.
Parameters:
namePointer to pool where the node should be recycled.
elemElement to be recycled.

Definition at line 165 of file pool.h.

#define pool_init (   name,
  init_func 
)    (*(name##_init))(init_func)

Initialize the pool name, calling init_func on each element.

The init function must have the following prototype:

 void init_func(type *)

where type is the type of objects held in the pool.

Parameters:
namePool to initialize
init_funcInit function to be called on each element

Definition at line 140 of file pool.h.