X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvppinfra%2Fpool.h;h=10262e90ec796df61d7cfe28e84d5cfd58e8d85a;hb=47c40e2d944c9a29677d0542103207ba8372b66a;hp=14c6a75c13b3b6b16af14c64b0fd3893c16eda75;hpb=87bd96aa4ddf2f777f8b972062c205e27addbc4a;p=vpp.git diff --git a/src/vppinfra/pool.h b/src/vppinfra/pool.h index 14c6a75c13b..10262e90ec7 100644 --- a/src/vppinfra/pool.h +++ b/src/vppinfra/pool.h @@ -185,11 +185,13 @@ pool_free_elts (void *v) First search free list. If nothing is free extend vector of objects. */ -#define pool_get_aligned(P,E,A) \ +#define _pool_get_aligned_internal(P,E,A,Z) \ do { \ pool_header_t * _pool_var (p) = pool_header (P); \ uword _pool_var (l); \ \ + STATIC_ASSERT(A==0 || ((A % sizeof(P[0]))==0) || ((sizeof(P[0]) % A) == 0), \ + "Pool aligned alloc of incorrectly sized object"); \ _pool_var (l) = 0; \ if (P) \ _pool_var (l) = vec_len (_pool_var (p)->free_indices); \ @@ -199,8 +201,9 @@ do { \ /* Return free element from free list. */ \ uword _pool_var (i) = _pool_var (p)->free_indices[_pool_var (l) - 1]; \ (E) = (P) + _pool_var (i); \ - _pool_var (p)->free_bitmap = \ - clib_bitmap_andnoti (_pool_var (p)->free_bitmap, _pool_var (i)); \ + _pool_var (p)->free_bitmap = \ + clib_bitmap_andnoti_notrim (_pool_var (p)->free_bitmap, \ + _pool_var (i)); \ _vec_len (_pool_var (p)->free_indices) = _pool_var (l) - 1; \ } \ else \ @@ -218,12 +221,23 @@ do { \ pool_aligned_header_bytes, \ /* align */ (A)); \ E = vec_end (P) - 1; \ - } \ + } \ + if (Z) \ + memset(E, 0, sizeof(*E)); \ } while (0) +/** Allocate an object E from a pool P with alignment A */ +#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 and zero it */ +#define pool_get_aligned_zero(P,E,A) _pool_get_aligned_internal(P,E,A,1) + /** Allocate an object E from a pool P (unspecified alignment). */ #define pool_get(P,E) pool_get_aligned(P,E,0) +/** Allocate an object E from a pool P and zero it */ +#define pool_get_zero(P,E) pool_get_aligned_zero(P,E,0) + /** See if pool_get will expand the pool or not */ #define pool_get_aligned_will_expand(P,YESNO,A) \ do { \ @@ -254,6 +268,7 @@ do { \ } \ } while (0) +/** Tell the caller if pool get will expand the pool */ #define pool_get_will_expand(P,YESNO) pool_get_aligned_will_expand(P,YESNO,0) /** Use free bitmap to query whether given element is free. */ @@ -277,7 +292,9 @@ do { \ \ /* Add element to free bitmap and to free list. */ \ _pool_var (p)->free_bitmap = \ - clib_bitmap_ori (_pool_var (p)->free_bitmap, _pool_var (l)); \ + clib_bitmap_ori_notrim (_pool_var (p)->free_bitmap, \ + _pool_var (l)); \ + \ /* Preallocated pool? */ \ if (_pool_var (p)->max_elts) \ { \ @@ -323,6 +340,41 @@ do { \ /** Allocate N more free elements to pool (unspecified alignment). */ #define pool_alloc(P,N) pool_alloc_aligned(P,N,0) +/** + * Return copy of pool with alignment + * + * @param P pool to copy + * @param A alignment (may be zero) + * @return copy of pool + */ +#define pool_dup_aligned(P,A) \ +({ \ + typeof (P) _pool_var (new) = 0; \ + pool_header_t * _pool_var (ph), * _pool_var (new_ph); \ + u32 _pool_var (n) = pool_len (P); \ + _pool_var (new) = _vec_resize (_pool_var (new), _pool_var (n), \ + _pool_var (n) * sizeof ((P)[0]), \ + pool_aligned_header_bytes, (A)); \ + 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); \ +}) + +/** + * Return copy of pool without alignment + * + * @param P pool to copy + * @return copy of pool + */ +#define pool_dup(P) pool_dup_aligned(P,0) + /** Low-level free pool operator (do not call directly). */ always_inline void * _pool_free (void *v) @@ -473,6 +525,9 @@ do { \ (_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); \ }) @@ -485,7 +540,7 @@ do { \ } /** - * @brief Remove all elemenets from a pool in a safe way + * @brief Remove all elements from a pool in a safe way * * @param VAR each element in the pool * @param POOL The pool to flush