1.1. mheap
1.1.1. mheap_t
- 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.
1.1.2. mheap_elt_t
- 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.
1.1.3. mheap_alloc函数过程
从虚拟内存空间映射一块size大的内存(am = 0x7ffff3a0ff54)
mmap_addr = mmap(0, size, PROT_READ | PROT_WRITE, flags, -1, 0);
- 将mmap_addr指针和页大小对其(av = 0x7ffff3a10000)
return (addr + mheap_page_size - 1) & ~(mheap_page_size - 1); - 从对齐页的位置往前减去mheap_t的空间(ah = 0x7ffff3a0f860)
ah = vec_aligned_header(v, sizeof (mheap_t), 16); - 再往后n个page,(ah + page = 0x7ffff3a10860)得到真正的mheap开始的位置【这边类似于补运算,在申请的空间内跳过page_size - mheap_size 的空间】
- (ah < am)
ah += mheap_page_size; // 这样可以保证mheap的其实位置在申请的空间内,且一定对其与page - 最后计算出vector的偏移
v = mheap_vector (h); // h + sizeof(mheap_t + vec_header_t) - 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)
根据用户请求object的size and alignment找到合适的bin(small_bin or large_bin),再从bin中找到合适的free elt
这里所有的操作都是基于数组的形式,elt间的双向链表也是以数组的形式实现的