1. VPP源码分析(内存管理之mheap)

本文详细介绍了mheap内存管理系统的工作原理和技术细节,包括mheap_t结构体定义、mheap_elt_t结构体及其内部数据结构组织方式,以及mheap_alloc函数的具体实现流程。此外还讲解了如何从mheap中按特定大小和对齐方式分配内存。
摘要由CSDN通过智能技术生成

1.1. mheap

1.1.1. mheap_t

1

  • first_free_elt_uoffset_by_bin: User offsets for head of doubly-linked list of free objects of this size.
  • non_empty_free_elt_heads: Bitmap of non-empty free list bins.
  • n_elts: Number of allocated objects.
  • max_size: Maximum size (in bytes) this heap is allowed to grow to.
    2

1.1.2. mheap_elt_t

3

  • prev_n_user_data: Number of mheap_size_t words of user data in previous object. Used to find mheap_elt_t for previous object.
  • prev_is_free: Set if previous object is free.
  • n_user_data: Number of mheap_size_t words of user data that follow this object.
  • is_free: Set if this object is on free list (and therefore following free_elt is valid).
  • user_data: For allocated objects: user data follows.
    User data is allocated in units of typeof (user_data[0]).
  • free_elt: For free objects, offsets of next and previous free objects of this size;
    ~0 means end of doubly-linked list.

This is stored in user data (guaranteed to be at least 8 bytes) but only for free objects.
4

1.1.3. mheap_alloc函数过程

5
从虚拟内存空间映射一块size大的内存(am = 0x7ffff3a0ff54)

mmap_addr = mmap(0, size, PROT_READ | PROT_WRITE, flags, -1, 0);
  1. 将mmap_addr指针和页大小对其(av = 0x7ffff3a10000)
    return (addr + mheap_page_size - 1) & ~(mheap_page_size - 1);
  2. 从对齐页的位置往前减去mheap_t的空间(ah = 0x7ffff3a0f860)
    ah = vec_aligned_header(v, sizeof (mheap_t), 16);
  3. 再往后n个page,(ah + page = 0x7ffff3a10860)得到真正的mheap开始的位置【这边类似于补运算,在申请的空间内跳过page_size - mheap_size 的空间】
  4. (ah < am)
    ah += mheap_page_size; // 这样可以保证mheap的其实位置在申请的空间内,且一定对其与page
  5. 最后计算出vector的偏移
    v = mheap_vector (h); // h + sizeof(mheap_t + vec_header_t)
  6. vector区域可以存放的数据大小为
    size = am + memory_size - v;

1.1.4. 从mheap中申请内存

void *
mheap_get_aligned(void *v, uword n_user_data_bytes, uword align, uword align_offset, uword * offset_return)
/* Search free lists for object with given size and alignment. */
static uword
mheap_get_search_free_list(void *v, uword *n_user_bytes_arg, uword align, uword align_offset)
/* Find bin for objects with size at least n_user_data_bytes. */
always_inline uword
user_data_size_to_bin_index(uword n_user_data_bytes)

6

根据用户请求object的size and alignment找到合适的bin(small_bin or large_bin),再从bin中找到合适的free elt
这里所有的操作都是基于数组的形式,elt间的双向链表也是以数组的形式实现的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值