FD.io VPP  v19.04.1-1-ge4a0f9f
Vector Packet Processing
pool.h File Reference

Fixed length block allocator. More...

+ Include dependency graph for pool.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pool_header_t
 

Macros

#define pool_aligned_header_bytes   vec_aligned_header_bytes (sizeof (pool_header_t), sizeof (void *))
 Align pool header so that pointers are naturally aligned. More...
 
#define pool_init_fixed(pool, max_elts)
 initialize a fixed-size, preallocated pool More...
 
#define pool_validate_index(v, i)
 
#define pool_len(p)   vec_len(p)
 Number of elements in pool vector. More...
 
#define pool_bytes(P)   (vec_bytes (P) + pool_header_bytes (P))
 Memory usage of pool. More...
 
#define pool_get_aligned(P, E, A)   _pool_get_aligned_internal(P,E,A,0)
 Allocate an object E from a pool P with alignment A. More...
 
#define pool_get_aligned_zero(P, E, A)   _pool_get_aligned_internal(P,E,A,1)
 Allocate an object E from a pool P with alignment A and zero it. More...
 
#define pool_get(P, E)   pool_get_aligned(P,E,0)
 Allocate an object E from a pool P (unspecified alignment). More...
 
#define pool_get_zero(P, E)   pool_get_aligned_zero(P,E,0)
 Allocate an object E from a pool P and zero it. More...
 
#define pool_get_aligned_will_expand(P, YESNO, A)
 See if pool_get will expand the pool or not. More...
 
#define pool_get_will_expand(P, YESNO)   pool_get_aligned_will_expand(P,YESNO,0)
 Tell the caller if pool get will expand the pool. More...
 
#define pool_is_free(P, E)
 Use free bitmap to query whether given element is free. More...
 
#define pool_is_free_index(P, I)   pool_is_free((P),(P)+(I))
 Use free bitmap to query whether given index is free. More...
 
#define pool_put(P, E)
 Free an object E in pool P. More...
 
#define pool_put_index(p, i)
 Free pool element with given index. More...
 
#define pool_alloc_aligned(P, N, A)
 Allocate N more free elements to pool (general version). More...
 
#define pool_alloc(P, N)   pool_alloc_aligned(P,N,0)
 Allocate N more free elements to pool (unspecified alignment). More...
 
#define pool_dup_aligned(P, A)
 Return copy of pool with alignment. More...
 
#define pool_dup(P)   pool_dup_aligned(P,0)
 Return copy of pool without alignment. More...
 
#define pool_free(p)   (p) = _pool_free(p)
 Free a pool. More...
 
#define pool_foreach_region(LO, HI, POOL, BODY)
 Optimized iteration through pool. More...
 
#define pool_foreach(VAR, POOL, BODY)
 Iterate through pool. More...
 
#define pool_elt_at_index(p, i)
 Returns pointer to element at given index. More...
 
#define pool_next_index(P, I)
 Return next occupied pool index after i, useful for safe iteration. More...
 
#define pool_foreach_index(i, v, body)
 Iterate pool by index. More...
 
#define pool_flush(VAR, POOL, BODY)
 Remove all elements from a pool in a safe way. More...
 

Functions

static pool_header_tpool_header (void *v)
 Get pool header from user pool pointer. More...
 
void fpool_free (void *)
 
static void pool_validate (void *v)
 Validate a pool. More...
 
static void pool_header_validate_index (void *v, uword index)
 
static uword pool_elts (void *v)
 Number of active elements in a pool. More...
 
static uword pool_header_bytes (void *v)
 Memory usage of pool header. More...
 
static uword pool_free_elts (void *v)
 Queries whether pool has at least N_FREE free elements. More...
 

Detailed Description

Fixed length block allocator.

Pools are built from clib vectors and bitmaps. Use pools when repeatedly allocating and freeing fixed-size data. Pools are fast, and avoid memory fragmentation.

Definition in file pool.h.

Macro Definition Documentation

#define pool_aligned_header_bytes   vec_aligned_header_bytes (sizeof (pool_header_t), sizeof (void *))

Align pool header so that pointers are naturally aligned.

Definition at line 72 of file pool.h.

#define pool_alloc (   P,
 
)    pool_alloc_aligned(P,N,0)

Allocate N more free elements to pool (unspecified alignment).

Definition at line 341 of file pool.h.

#define pool_alloc_aligned (   P,
  N,
  A 
)
Value:
do { \
pool_header_t * _p; \
\
if ((P)) \
{ \
_p = pool_header (P); \
if (_p->max_elts) \
{ \
clib_warning ("Can't expand fixed-size pool"); \
} \
} \
\
(P) = _vec_resize ((P), 0, (vec_len (P) + (N)) * sizeof (P[0]), \
(A)); \
_p = pool_header (P); \
vec_resize (_p->free_indices, (N)); \
_vec_len (_p->free_indices) -= (N); \
} while (0)
void os_out_of_memory(void)
Definition: unix-misc.c:219
#define P(fmt,...)
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:242
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
Definition: pool.h:72
#define clib_warning(format, args...)
Definition: error.h:59
#define A(x)
Definition: main.c:1024
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Allocate N more free elements to pool (general version).

Definition at line 318 of file pool.h.

#define pool_bytes (   P)    (vec_bytes (P) + pool_header_bytes (P))

Memory usage of pool.

Definition at line 161 of file pool.h.

#define pool_dup (   P)    pool_dup_aligned(P,0)

Return copy of pool without alignment.

Parameters
Ppool to copy
Returns
copy of pool

Definition at line 379 of file pool.h.

#define pool_dup_aligned (   P,
  A 
)
Value:
({ \
typeof (P) _pool_var (new) = 0; \
pool_header_t * _pool_var (ph), * _pool_var (new_ph); \
u32 _pool_var (n) = pool_len (P); \
if ((P)) \
{ \
_pool_var (new) = _vec_resize (_pool_var (new), _pool_var (n), \
_pool_var (n) * sizeof ((P)[0]), \
clib_memcpy_fast (_pool_var (new), (P), \
_pool_var (n) * sizeof ((P)[0])); \
_pool_var (ph) = pool_header (P); \
_pool_var (new_ph) = pool_header (_pool_var (new)); \
_pool_var (new_ph)->free_bitmap = \
clib_bitmap_dup (_pool_var (ph)->free_bitmap); \
_pool_var (new_ph)->free_indices = \
vec_dup (_pool_var (ph)->free_indices); \
_pool_var (new_ph)->max_elts = _pool_var (ph)->max_elts; \
} \
_pool_var (new); \
})
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
#define pool_len(p)
Number of elements in pool vector.
Definition: pool.h:140
#define clib_bitmap_dup(v)
Duplicate a bitmap.
Definition: bitmap.h:87
#define P(fmt,...)
unsigned int u32
Definition: types.h:88
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:375
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
Definition: pool.h:72
#define A(x)
Definition: main.c:1024

Return copy of pool with alignment.

Parameters
Ppool to copy
Aalignment (may be zero)
Returns
copy of pool

Definition at line 350 of file pool.h.

#define pool_elt_at_index (   p,
  i 
)
Value:
({ \
typeof (p) _e = (p) + (i); \
ASSERT (! pool_is_free (p, _e)); \
_e; \
})
int i
#define pool_is_free(P, E)
Use free bitmap to query whether given element is free.
Definition: pool.h:275
#define ASSERT(truth)

Returns pointer to element at given index.

ASSERTs that the supplied index is valid. Even though one can write correct code of the form

1 p = pool_base + index;

use of pool_elt_at_index is strongly suggested.

Definition at line 514 of file pool.h.

#define pool_flush (   VAR,
  POOL,
  BODY 
)
Value:
{ \
uword *_pool_var(ii), *_pool_var(dv) = NULL; \
pool_foreach((VAR), (POOL), \
({ \
vec_add1(_pool_var(dv), (VAR) - (POOL)); \
})); \
vec_foreach(_pool_var(ii), _pool_var(dv)) \
{ \
(VAR) = pool_elt_at_index((POOL), *_pool_var(ii)); \
do { BODY; } while (0); \
pool_put((POOL), (VAR)); \
} \
vec_free(_pool_var(dv)); \
}
#define NULL
Definition: clib.h:58
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
#define pool_foreach(VAR, POOL, BODY)
Iterate through pool.
Definition: pool.h:493
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:514
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286
#define vec_free(V)
Free vector's memory (no header).
Definition: vec.h:341
u64 uword
Definition: types.h:112
#define vec_foreach(var, vec)
Vector iterator.

Remove all elements from a pool in a safe way.

Parameters
VAReach element in the pool
POOLThe pool to flush
BODYThe actions to perform on each element before it is returned to the pool. i.e. before it is 'freed'

Definition at line 553 of file pool.h.

#define pool_foreach (   VAR,
  POOL,
  BODY 
)
Value:
do { \
uword _pool_foreach_lo, _pool_foreach_hi; \
pool_foreach_region (_pool_foreach_lo, _pool_foreach_hi, (POOL), \
({ \
for ((VAR) = (POOL) + _pool_foreach_lo; \
(VAR) < (POOL) + _pool_foreach_hi; \
(VAR)++) \
do { BODY; } while (0); \
})); \
} while (0)
for(i=1;i<=collision_buckets;i++)
#define pool_foreach_region(LO, HI, POOL, BODY)
Optimized iteration through pool.
Definition: pool.h:419
u64 uword
Definition: types.h:112

Iterate through pool.

Parameters
VARA variable of same type as pool vector to be used as an iterator.
POOLThe pool to iterate across.
BODYThe operation to perform, typically a code block. See the example below.

This macro will call BODY with each active pool element.

It is a bad idea to allocate or free pool element from within pool_foreach. Build a vector of indices and dispose of them later. Or call pool_flush.

Example
proc_t *procs; // a pool of processes.
proc_t *proc; // pointer to one process; used as the iterator.
pool_foreach (proc, procs, ({
if (proc->state != PROC_STATE_RUNNING)
continue;
// check a running proc in some way
...
}));
Warning
Because pool_foreach is a macro, syntax errors can be difficult to find inside BODY, let alone actual code bugs. One can temporarily split a complex pool_foreach into a trivial pool_foreach which builds a vector of active indices, and a vec_foreach() (or plain for-loop) to walk the active index vector.

Definition at line 493 of file pool.h.

#define pool_foreach_index (   i,
  v,
  body 
)
Value:
for ((i) = 0; (i) < vec_len (v); (i)++) \
{ \
if (! pool_is_free_index ((v), (i))) \
do { body; } while (0); \
}
int i
#define pool_is_free_index(P, I)
Use free bitmap to query whether given index is free.
Definition: pool.h:283
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)

Iterate pool by index.

Definition at line 538 of file pool.h.

#define pool_foreach_region (   LO,
  HI,
  POOL,
  BODY 
)
Value:
do { \
uword _pool_var (i), _pool_var (lo), _pool_var (hi), _pool_var (len); \
uword _pool_var (bl), * _pool_var (b); \
pool_header_t * _pool_var (p); \
\
_pool_var (p) = pool_header (POOL); \
_pool_var (b) = (POOL) ? _pool_var (p)->free_bitmap : 0; \
_pool_var (bl) = vec_len (_pool_var (b)); \
_pool_var (len) = vec_len (POOL); \
_pool_var (lo) = 0; \
for (_pool_var (i) = 0; \
_pool_var (i) <= _pool_var (bl); \
_pool_var (i)++) \
{ \
uword _pool_var (m), _pool_var (f); \
_pool_var (m) = (_pool_var (i) < _pool_var (bl) \
? _pool_var (b) [_pool_var (i)] \
: 1); \
while (_pool_var (m) != 0) \
{ \
_pool_var (f) = first_set (_pool_var (m)); \
_pool_var (hi) = (_pool_var (i) * BITS (_pool_var (b)[0]) \
+ min_log2 (_pool_var (f))); \
_pool_var (hi) = (_pool_var (i) < _pool_var (bl) \
? _pool_var (hi) : _pool_var (len)); \
_pool_var (m) ^= _pool_var (f); \
if (_pool_var (hi) > _pool_var (lo)) \
{ \
(LO) = _pool_var (lo); \
(HI) = _pool_var (hi); \
do { BODY; } while (0); \
} \
_pool_var (lo) = _pool_var (hi) + 1; \
} \
} \
} while (0)
vmrglw vmrglh hi
for(i=1;i<=collision_buckets;i++)
int i
static uword min_log2(uword x)
Definition: clib.h:144
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
lo
u8 len
Definition: ip_types.api:49
static uword first_set(uword x)
Definition: clib.h:253
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112
#define BITS(x)
Definition: clib.h:61

Optimized iteration through pool.

Parameters
LOpointer to first element in chunk
HIpointer to last element in chunk
POOLpool to iterate across
BODYoperation to perform

Optimized version which assumes that BODY is smart enough to process multiple (LOW,HI) chunks. See also pool_foreach().

Definition at line 419 of file pool.h.

#define pool_free (   p)    (p) = _pool_free(p)

Free a pool.

Definition at line 407 of file pool.h.

#define pool_get (   P,
 
)    pool_get_aligned(P,E,0)

Allocate an object E from a pool P (unspecified alignment).

Definition at line 236 of file pool.h.

#define pool_get_aligned (   P,
  E,
  A 
)    _pool_get_aligned_internal(P,E,A,0)

Allocate an object E from a pool P with alignment A.

Definition at line 230 of file pool.h.

#define pool_get_aligned_will_expand (   P,
  YESNO,
  A 
)
Value:
do { \
pool_header_t * _pool_var (p) = pool_header (P); \
uword _pool_var (l); \
\
_pool_var (l) = 0; \
if (P) \
{ \
if (_pool_var (p)->max_elts) \
_pool_var (l) = _pool_var (p)->max_elts; \
else \
_pool_var (l) = vec_len (_pool_var (p)->free_indices); \
} \
\
/* Free elements, certainly won't expand */ \
if (_pool_var (l) > 0) \
YESNO=0; \
else \
{ \
/* Nothing on free list, make a new element and return it. */ \
YESNO = _vec_resize_will_expand \
(P, \
/* length_increment */ 1, \
/* new size */ (vec_len (P) + 1) * sizeof (P[0]), \
/* align */ (A)); \
} \
} while (0)
#define P(fmt,...)
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
Definition: pool.h:72
#define A(x)
Definition: main.c:1024
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112

See if pool_get will expand the pool or not.

Definition at line 242 of file pool.h.

#define pool_get_aligned_zero (   P,
  E,
  A 
)    _pool_get_aligned_internal(P,E,A,1)

Allocate an object E from a pool P with alignment A and zero it.

Definition at line 233 of file pool.h.

#define pool_get_will_expand (   P,
  YESNO 
)    pool_get_aligned_will_expand(P,YESNO,0)

Tell the caller if pool get will expand the pool.

Definition at line 272 of file pool.h.

#define pool_get_zero (   P,
 
)    pool_get_aligned_zero(P,E,0)

Allocate an object E from a pool P and zero it.

Definition at line 239 of file pool.h.

#define pool_init_fixed (   pool,
  max_elts 
)
Value:
{ \
_pool_init_fixed((void **)&(pool),sizeof(pool[0]),max_elts); \
}

initialize a fixed-size, preallocated pool

Definition at line 86 of file pool.h.

#define pool_is_free (   P,
 
)
Value:
({ \
pool_header_t * _pool_var (p) = pool_header (P); \
uword _pool_var (i) = (E) - (P); \
(_pool_var (i) < vec_len (P)) ? clib_bitmap_get (_pool_var (p)->free_bitmap, _pool_i) : 1; \
})
int i
#define P(fmt,...)
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
static uword clib_bitmap_get(uword *ai, uword i)
Gets the ith bit value from a bitmap.
Definition: bitmap.h:197
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112

Use free bitmap to query whether given element is free.

Definition at line 275 of file pool.h.

#define pool_is_free_index (   P,
 
)    pool_is_free((P),(P)+(I))

Use free bitmap to query whether given index is free.

Definition at line 283 of file pool.h.

#define pool_len (   p)    vec_len(p)

Number of elements in pool vector.

Note
You probably want to call pool_elts() instead.

Definition at line 140 of file pool.h.

#define pool_next_index (   P,
 
)
Value:
({ \
pool_header_t * _pool_var (p) = pool_header (P); \
uword _pool_var (rv) = (I) + 1; \
\
_pool_var(rv) = \
(_pool_var (rv) < vec_len (P) ? \
clib_bitmap_next_clear (_pool_var (p)->free_bitmap, _pool_var(rv)) \
: ~0); \
_pool_var(rv) = \
(_pool_var (rv) < vec_len (P) ? \
_pool_var (rv) : ~0); \
_pool_var(rv); \
})
#define P(fmt,...)
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u64 uword
Definition: types.h:112

Return next occupied pool index after i, useful for safe iteration.

Definition at line 522 of file pool.h.

#define pool_put (   P,
 
)
Value:
do { \
pool_header_t * _pool_var (p) = pool_header (P); \
uword _pool_var (l) = (E) - (P); \
ASSERT (! pool_is_free (P, E)); \
\
/* Add element to free bitmap and to free list. */ \
_pool_var (p)->free_bitmap = \
clib_bitmap_ori_notrim (_pool_var (p)->free_bitmap, \
_pool_var (l)); \
\
/* Preallocated pool? */ \
if (_pool_var (p)->max_elts) \
{ \
ASSERT(_pool_var(l) < _pool_var (p)->max_elts); \
_pool_var(p)->free_indices[_vec_len(_pool_var(p)->free_indices)] = \
_pool_var(l); \
_vec_len(_pool_var(p)->free_indices) += 1; \
} \
vec_add1 (_pool_var (p)->free_indices, _pool_var (l)); \
} while (0)
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:522
#define pool_is_free(P, E)
Use free bitmap to query whether given element is free.
Definition: pool.h:275
#define P(fmt,...)
static pool_header_t * pool_header(void *v)
Get pool header from user pool pointer.
Definition: pool.h:77
#define ASSERT(truth)
#define vec_is_member(v, e)
True if given pointer is within given vector.
u64 uword
Definition: types.h:112

Free an object E in pool P.

Definition at line 286 of file pool.h.

#define pool_put_index (   p,
  i 
)
Value:
do { \
typeof (p) _e = (p) + (i); \
pool_put (p, _e); \
} while (0)
int i
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:286

Free pool element with given index.

Definition at line 311 of file pool.h.

#define pool_validate_index (   v,
  i 
)
Value:
do { \
uword __pool_validate_index = (i); \
vec_validate_ha ((v), __pool_validate_index, \
pool_aligned_header_bytes, /* align */ 0); \
pool_header_validate_index ((v), __pool_validate_index); \
} while (0)
static void pool_header_validate_index(void *v, uword index)
Definition: pool.h:108
int i
#define pool_aligned_header_bytes
Align pool header so that pointers are naturally aligned.
Definition: pool.h:72
#define vec_validate_ha(V, I, H, A)
Make sure vector is long enough for given index (general version).
Definition: vec.h:417
u64 uword
Definition: types.h:112

Definition at line 116 of file pool.h.

Function Documentation

void fpool_free ( void *  )

+ Here is the caller graph for this function:

static uword pool_elts ( void *  v)
inlinestatic

Number of active elements in a pool.

Returns
Number of active elements in a pool

Definition at line 128 of file pool.h.

+ Here is the call graph for this function:

static uword pool_free_elts ( void *  v)
inlinestatic

Queries whether pool has at least N_FREE free elements.

Definition at line 168 of file pool.h.

+ Here is the call graph for this function:

static pool_header_t* pool_header ( void *  v)
inlinestatic

Get pool header from user pool pointer.

Definition at line 77 of file pool.h.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static uword pool_header_bytes ( void *  v)
inlinestatic

Memory usage of pool header.

Definition at line 150 of file pool.h.

+ Here is the call graph for this function:

static void pool_header_validate_index ( void *  v,
uword  index 
)
inlinestatic

Definition at line 108 of file pool.h.

+ Here is the call graph for this function:

static void pool_validate ( void *  v)
inlinestatic

Validate a pool.

Definition at line 93 of file pool.h.

+ Here is the call graph for this function: