/* * Copyright (c) 2015 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Heaps of objects of type T (e.g. int, struct foo, ...). Usage. To declare a null heap: T * heap = 0; To allocate: offset = heap_alloc (heap, size, handle); New object is heap[offset] ... heap[offset + size] Handle is used to free/query object. To free object: heap_dealloc (heap, handle); To query the size of an object: heap_size (heap, handle) */ #ifndef included_heap_h #define included_heap_h #include #include #include #include #include /* Doubly linked list of elements. */ typedef struct { /* Offset of this element (plus free bit). If element is free, data at offset contains pointer to free list. */ u32 offset; /* Index of next and previous elements relative to current element. */ i32 next, prev; } heap_elt_t; /* 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_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_prev (heap_elt_t * e) { return e + e->prev; } always_inline uword heap_elt_size (void *v, heap_elt_t * 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); } /* Sizes are binned. Sizes 1 to 2^log2_small_bins have their own free lists. Larger sizes are grouped in powers of two. */ #define HEAP_LOG2_SMALL_BINS (5) #define HEAP_SMALL_BINS (1 << HEAP_LOG2_SMALL_BINS) #define HEAP_N_BINS (2 * HEAP_SMALL_BINS) /* Header for heaps. */ typedef struct { /* Vector of used and free elements. */ 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; /* Vector of free indices of elts array. */ u32 *free_elts; /* Indices of free elts indexed by size bin. */ u32 **free_lists; format_function_t *format_elt; /* Used for validation/debugging. */ uword *used_elt_bitmap; /* First and last element of doubly linked chain of elements. */ u32 head, tail; u32 used_count, max_len; /* Number of bytes in a help element. */ u32 elt_bytes; u32 flags; /* Static heaps are made from external memory given to us by user and are not re-sizable vectors. */ #define HEAP_IS_STATIC (1) } heap_header_t; /* 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 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) { uword i; new[0] = old[0]; new->elts = vec_dup (new->elts); new->free_elts = vec_dup (new->free_elts); new->free_lists = vec_dup (new->free_lists); for (i = 0; i < vec_len (new->free_lists); i++) new->free_lists[i] = vec_dup (new->free_lists[i]); new->used_elt_bitmap = clib_bitmap_dup (new->used_elt_bitmap); new->small_free_elt_free_index = vec_dup (new->small_free_elt_free_index); } /* 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) { heap_header_t *h_old, *h_new; void *v_new; h_old = heap_header (v_old); if (!v_old) return v_old; v_new = 0; v_new = _ve