2. VPP源码分析(内存管理之抽象数据结构)

简介: 1.2. 抽象数据结构 1.2.1. vector CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".

1.2. 抽象数据结构

1.2.1. vector

7
CLIB vectors are ubiquitous dynamically resized arrays with by user defined "headers".
Many CLIB data structures (e.g. hash, heap, pool) are vectors with various different headers.

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结合
8

Many macros have _a variants supporting alignment of vector data and _h variants supporting non zero length vector headers.
The _ha variants support both
9
10

1.2.2. bitmap

Set and get bits with indexes.Lots of them. Implemented as a uword vector.
typedef uword clib_bitmap_t;
12

uword *bitmap = 0;
clib_bitmap_alloc(bitmap, 100);     //Allocate bitmap for 100 bits
clib_bitmap_validate(bitmap, 200); //Make room for 201 bits
clib_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 included
uword 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);

1.2.3. pool

Fixed sized element allocator. Based on a vec and a bitmap.
12

  • free_bitmap:Bitmap of indices of free objects.
  • free_indices:Vector of free indices. One element for each set bit in bitmap.

The following fields are set for fixed-size, preallocated pools

  • max_elts:Maximum size of the pool, in elements
  • mmap_base, mmap_size:mmap segment info: base + length
    13
T *pool = 0;
T *e;
pool_get(pool, e);
//pool_get_aligned(pool, e, 4);

//Query if element is free
pool_is_free(pool,e);
pool_is_free_index(pool, e – pool);

//Unalloc element
pool_put(pool,e);

//Free the whole pool
pool_free(pool);

// Allocated element count
pool_elts(pool);

1.2.4. heap

Variable element size allocator. 和pool类似只不过pool中的每个元素是定长的而heap中的元素是变长的。
pool和heap是建立在vec和bitmap上的两种高级数据结构,而mheap是实现这些数据结构的最基础的内存管理单元,这里不要混淆了。

  • elts:Vector of used and free elements.
  • small_free_elt_free_index:For elt_bytes < sizeof (u32) we need some extra space per elt to store free list index.
  • free_elts:Vector of free indices of elts array.
  • free_lists:Indices of free elts indexed by size bin.
  • used_elt_bitmap:Used for validattion/debugging.
  • head, tail:First and last element of doubly linked chain of elements.
  • elt_bytes:Number of bytes in a help element.
T *heap = 0;
T *e;

//Allocate 4xT.
U32 handle;
u32 offset = heap_alloc(heap, 4, handle);

//Allocated heap[offset] – heap[offset + 4]
//Object size
heap_size(heap, handle);

//Deallocate
heap_dealloc(heap, handle);

Rarely used (pools are faster). Still efficient.
To be used if you need variably sized allocations.
e.g. classifier

1.3. 内存管理的目的

提高cache命中率!!!
14

目录
相关文章
|
4月前
|
存储 Go iOS开发
掌握Go语言:探索Go语言指针,解锁高效内存操作与动态数据结构的奥秘(19)
掌握Go语言:探索Go语言指针,解锁高效内存操作与动态数据结构的奥秘(19)
|
11月前
|
编译器 程序员 测试技术
详解动态内存管理【malloc/calloc/realloc/free函数/柔性数组】【C语言/进阶/数据结构基础】
详解动态内存管理【malloc/calloc/realloc/free函数/柔性数组】【C语言/进阶/数据结构基础】
191 0
|
4月前
|
存储 监控 NoSQL
Redis处理大量数据主要依赖于其内存存储结构、高效的数据结构和算法,以及一系列的优化策略
【5月更文挑战第15天】Redis处理大量数据依赖内存存储、高效数据结构和优化策略。选择合适的数据结构、利用批量操作减少网络开销、控制批量大小、使用Redis Cluster进行分布式存储、优化内存使用及监控调优是关键。通过这些方法,Redis能有效处理大量数据并保持高性能。
76 1
|
2月前
|
消息中间件 存储 NoSQL
Redis数据结构—跳跃表 skiplist 实现源码分析
Redis 是一个内存中的数据结构服务器,使用跳跃表(skiplist)来实现有序集合。跳跃表是一种概率型数据结构,支持平均 O(logN) 查找复杂度,它通过多层链表加速查找,同时保持有序性。节点高度随机生成,最大为 32 层,以平衡查找速度和空间效率。跳跃表在 Redis 中用于插入、删除和按范围查询元素,其内部节点包含对象、分值、后退指针和多个前向指针。Redis 源码中的 `t_zset.c` 文件包含了跳跃表的具体实现细节。
|
3月前
|
存储 编译器 C语言
C语言的联合体:一种节省内存的数据结构
C语言的联合体:一种节省内存的数据结构
|
4月前
|
存储 NoSQL 算法
Redis源码、面试指南(2)内存编码数据结构(下)
Redis源码、面试指南(2)内存编码数据结构
52 4
|
4月前
|
存储 NoSQL API
Redis源码、面试指南(2)内存编码数据结构(上)
Redis源码、面试指南(2)内存编码数据结构
55 0
|
4月前
|
存储 API
milvus insert api的数据结构源码分析
milvus insert api的数据结构源码分析
1043 6
milvus insert api的数据结构源码分析
|
4月前
|
存储 缓存 NoSQL
Redis 数据结构+线程模型+持久化+内存淘汰+分布式
Redis 数据结构+线程模型+持久化+内存淘汰+分布式
504 0
|
4月前
|
存储 NoSQL Java
基于内存的分布式NoSQL数据库Redis(二)数据结构与通用命令
基于内存的分布式NoSQL数据库Redis(二)数据结构与通用命令
203 0

热门文章

最新文章