diff options
Diffstat (limited to 'vppinfra/vppinfra/heap.h')
-rw-r--r-- | vppinfra/vppinfra/heap.h | 156 |
1 files changed, 96 insertions, 60 deletions
diff --git a/vppinfra/vppinfra/heap.h b/vppinfra/vppinfra/heap.h index 604187c0..8c1aae46 100644 --- a/vppinfra/vppinfra/heap.h +++ b/vppinfra/vppinfra/heap.h @@ -40,7 +40,7 @@ Usage. To declare a null heap: T * heap = 0; - + To allocate: offset = heap_alloc (heap, size, handle); @@ -68,7 +68,8 @@ #include <vppinfra/bitmap.h> /* Doubly linked list of elements. */ -typedef struct { +typedef struct +{ /* Offset of this element (plus free bit). If element is free, data at offset contains pointer to free list. */ u32 offset; @@ -80,21 +81,34 @@ typedef struct { /* Use high bit of offset as free bit. */ #define HEAP_ELT_FREE_BIT (1 << 31) -always_inline uword heap_is_free (heap_elt_t * e) -{ return (e->offset & HEAP_ELT_FREE_BIT) != 0; } +always_inline uword +heap_is_free (heap_elt_t * e) +{ + return (e->offset & HEAP_ELT_FREE_BIT) != 0; +} -always_inline uword heap_offset (heap_elt_t * e) -{ return e->offset &~ HEAP_ELT_FREE_BIT; } +always_inline uword +heap_offset (heap_elt_t * e) +{ + return e->offset & ~HEAP_ELT_FREE_BIT; +} -always_inline heap_elt_t * heap_next (heap_elt_t * e) -{ return e + e->next; } +always_inline heap_elt_t * +heap_next (heap_elt_t * e) +{ + return e + e->next; +} -always_inline heap_elt_t * heap_prev (heap_elt_t * e) -{ return e + e->prev; } +always_inline heap_elt_t * +heap_prev (heap_elt_t * e) +{ + return e + e->prev; +} -always_inline uword heap_elt_size (void * v, heap_elt_t * e) +always_inline uword +heap_elt_size (void *v, heap_elt_t * e) { - heap_elt_t * n = heap_next (e); + heap_elt_t *n = heap_next (e); uword next_offset = n != e ? heap_offset (n) : vec_len (v); return next_offset - heap_offset (e); } @@ -106,24 +120,25 @@ always_inline uword heap_elt_size (void * v, heap_elt_t * e) #define HEAP_N_BINS (2 * HEAP_SMALL_BINS) /* Header for heaps. */ -typedef struct { +typedef struct +{ /* Vector of used and free elements. */ - heap_elt_t * elts; + heap_elt_t *elts; /* For elt_bytes < sizeof (u32) we need some extra space per elt to store free list index. */ - u32 * small_free_elt_free_index; + u32 *small_free_elt_free_index; /* Vector of free indices of elts array. */ - u32 * free_elts; + u32 *free_elts; /* Indices of free elts indexed by size bin. */ - u32 ** free_lists; + u32 **free_lists; - format_function_t * format_elt; + format_function_t *format_elt; /* Used for validattion/debugging. */ - uword * used_elt_bitmap; + uword *used_elt_bitmap; /* First and last element of doubly linked chain of elements. */ u32 head, tail; @@ -142,13 +157,20 @@ typedef struct { /* Start of heap elements is always cache aligned. */ #define HEAP_DATA_ALIGN (CLIB_CACHE_LINE_BYTES) -always_inline heap_header_t * heap_header (void * v) -{ return vec_header (v, sizeof (heap_header_t)); } +always_inline heap_header_t * +heap_header (void *v) +{ + return vec_header (v, sizeof (heap_header_t)); +} -always_inline uword heap_header_bytes () -{ return vec_header_bytes (sizeof (heap_header_t)); } +always_inline uword +heap_header_bytes () +{ + return vec_header_bytes (sizeof (heap_header_t)); +} -always_inline void heap_dup_header (heap_header_t * old, heap_header_t * new) +always_inline void +heap_dup_header (heap_header_t * old, heap_header_t * new) { uword i; @@ -165,38 +187,42 @@ always_inline void heap_dup_header (heap_header_t * old, heap_header_t * new) /* Make a duplicate copy of a heap. */ #define heap_dup(v) _heap_dup(v, vec_len (v) * sizeof (v[0])) -always_inline void * _heap_dup (void * v_old, uword v_bytes) +always_inline void * +_heap_dup (void *v_old, uword v_bytes) { - heap_header_t * h_old, * h_new; - void * v_new; + heap_header_t *h_old, *h_new; + void *v_new; h_old = heap_header (v_old); - if (! v_old) + if (!v_old) return v_old; v_new = 0; - v_new = _vec_resize (v_new, _vec_len (v_old), v_bytes, sizeof (heap_header_t), - HEAP_DATA_ALIGN); + v_new = + _vec_resize (v_new, _vec_len (v_old), v_bytes, sizeof (heap_header_t), + HEAP_DATA_ALIGN); h_new = heap_header (v_new); heap_dup_header (h_old, h_new); clib_memcpy (v_new, v_old, v_bytes); return v_new; } -always_inline uword heap_elts (void * v) +always_inline uword +heap_elts (void *v) { - heap_header_t * h = heap_header (v); + heap_header_t *h = heap_header (v); return h->used_count; } -uword heap_bytes (void * v); +uword heap_bytes (void *v); -always_inline void * _heap_new (u32 len, u32 n_elt_bytes) +always_inline void * +_heap_new (u32 len, u32 n_elt_bytes) { - void * v = _vec_resize (0, len, (uword) len*n_elt_bytes, - sizeof (heap_header_t), - HEAP_DATA_ALIGN); + void *v = _vec_resize (0, len, (uword) len * n_elt_bytes, + sizeof (heap_header_t), + HEAP_DATA_ALIGN); heap_header (v)->elt_bytes = n_elt_bytes; return v; } @@ -204,28 +230,31 @@ always_inline void * _heap_new (u32 len, u32 n_elt_bytes) #define heap_new(v) (v) = _heap_new (0, sizeof ((v)[0])) always_inline void -heap_set_format (void * v, format_function_t * format_elt) +heap_set_format (void *v, format_function_t * format_elt) { ASSERT (v); heap_header (v)->format_elt = format_elt; } always_inline void -heap_set_max_len (void * v, uword max_len) +heap_set_max_len (void *v, uword max_len) { ASSERT (v); heap_header (v)->max_len = max_len; } -always_inline uword heap_get_max_len (void * v) -{ return v ? heap_header (v)->max_len : 0; } +always_inline uword +heap_get_max_len (void *v) +{ + return v ? heap_header (v)->max_len : 0; +} /* Create fixed size heap with given block of memory. */ always_inline void * -heap_create_from_memory (void * memory, uword max_len, uword elt_bytes) +heap_create_from_memory (void *memory, uword max_len, uword elt_bytes) { - heap_header_t * h; - void * v; + heap_header_t *h; + void *v; if (max_len * elt_bytes < sizeof (h[0])) return 0; @@ -236,7 +265,7 @@ heap_create_from_memory (void * memory, uword max_len, uword elt_bytes) h->elt_bytes = elt_bytes; h->flags = HEAP_IS_STATIC; - v = (void *) (memory + heap_header_bytes ()); + v = (void *) (memory + heap_header_bytes ()); _vec_len (v) = 0; return v; } @@ -267,11 +296,11 @@ do { \ #define heap_elt_at_index(v,index) vec_elt_at_index(v,index) always_inline heap_elt_t * -heap_get_elt (void * v, uword handle) +heap_get_elt (void *v, uword handle) { - heap_header_t * h = heap_header (v); - heap_elt_t * e = vec_elt_at_index (h->elts, handle); - ASSERT (! heap_is_free (e)); + heap_header_t *h = heap_header (v); + heap_elt_t *e = vec_elt_at_index (h->elts, handle); + ASSERT (!heap_is_free (e)); return e; } @@ -282,19 +311,18 @@ heap_get_elt (void * v, uword handle) }) always_inline uword -heap_is_free_handle (void * v, uword heap_handle) +heap_is_free_handle (void *v, uword heap_handle) { - heap_header_t * h = heap_header (v); - heap_elt_t * e = vec_elt_at_index (h->elts, heap_handle); + heap_header_t *h = heap_header (v); + heap_elt_t *e = vec_elt_at_index (h->elts, heap_handle); return heap_is_free (e); } -extern uword heap_len (void * v, word handle); +extern uword heap_len (void *v, word handle); /* Low level allocation call. */ -extern void * _heap_alloc (void * v, uword size, uword alignment, - uword elt_bytes, - uword * offset, uword * handle); +extern void *_heap_alloc (void *v, uword size, uword alignment, + uword elt_bytes, uword * offset, uword * handle); #define heap_alloc_aligned(v,size,align,handle) \ ({ \ @@ -308,14 +336,22 @@ extern void * _heap_alloc (void * v, uword size, uword alignment, #define heap_alloc(v,size,handle) heap_alloc_aligned((v),(size),0,(handle)) -extern void heap_dealloc (void * v, uword handle); -extern void heap_validate (void * v); +extern void heap_dealloc (void *v, uword handle); +extern void heap_validate (void *v); /* Format heap internal data structures as string. */ -extern u8 * format_heap (u8 * s, va_list * va); +extern u8 *format_heap (u8 * s, va_list * va); -void * _heap_free (void * v); +void *_heap_free (void *v); #define heap_free(v) (v)=_heap_free(v) #endif /* included_heap_h */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ |