本文共 2766 字,大约阅读时间需要 9 分钟。
The memory layout looks like this:
user header (aligned to uword boundary) vector length: number of elements user's pointer-> vector element #0 vector element #1 ...
vector结构经常和user定义的header结合
Many macros have _a variants supporting alignment of vector data and _h variants supporting non zero length vector headers.
The _ha variants support bothSet and get bits with indexes.Lots of them. Implemented as a uword vector.
typedef uword clib_bitmap_t;uword *bitmap = 0;clib_bitmap_alloc(bitmap, 100); //Allocate bitmap for 100 bitsclib_bitmap_validate(bitmap, 200); //Make room for 201 bitsclib_bitmap_set(bitmap, 33, 1);clib_bitmap_get(bitmap, 33);uword a = clib_bitmap_get_multiple(bitmap, 3, 10); //Get bits from 3 to 12 includeduword clib_bitmap_first_set(uword *ai);uword clib_bitmap_first_clear(uword *ai);uword clib_bitmap_next_set(uword *ai, uword i);uword clib_bitmap_next_clear(uword *ai, uword i);uword clib_bitmap_count_set_bits(uword *ai);
Fixed sized element allocator. Based on a vec and a bitmap.
The following fields are set for fixed-size, preallocated pools
T *pool = 0;T *e;pool_get(pool, e);//pool_get_aligned(pool, e, 4);//Query if element is freepool_is_free(pool,e);pool_is_free_index(pool, e – pool);//Unalloc elementpool_put(pool,e);//Free the whole poolpool_free(pool);// Allocated element countpool_elts(pool);
Variable element size allocator. 和pool类似只不过pool中的每个元素是定长的而heap中的元素是变长的。
pool和heap是建立在vec和bitmap上的两种高级数据结构,而mheap是实现这些数据结构的最基础的内存管理单元,这里不要混淆了。T *heap = 0;T *e;//Allocate 4xT.U32 handle;u32 offset = heap_alloc(heap, 4, handle);//Allocated heap[offset] – heap[offset + 4]//Object sizeheap_size(heap, handle);//Deallocateheap_dealloc(heap, handle);Rarely used (pools are faster). Still efficient.To be used if you need variably sized allocations.e.g. classifier
提高cache命中率!!!
转载地址:http://njxkm.baihongyu.com/