`
mqzhuang
  • 浏览: 185276 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论
文章列表
终于将《Glibc内存管理--ptmalloc2源代码分析》这个系列的文章完成了,最后附上全文的pdf和ppt,供大家交流分享。
5.8.3 sYSTRIm()和munmap_chunk() sYSTRIm() 函数源代码如下: /* sYSTRIm is an inverse of sorts to sYSMALLOc. It gives memory back to the system (via negative arguments to sbrk) if there is unused memory at the `high' end of the malloc pool. It is called automatically by free() when top spa ...
5.8.2 _int_free() _int_free() 函数的实现源代码如下: static void #ifdef ATOMIC_FASTBINS _int_free(mstate av, mchunkptr p, int have_lock) #else _int_free(mstate av, mchunkptr p) #endif { INTERNAL_SIZE_T size; /* its size */ mfastbinptr* fb; /* associa ...
5.8 内存释放free 5.8.1 public_fREe() public_fREe() 函数的源代码如下: void public_fREe(Void_t* mem) { mstate ar_ptr; mchunkptr p; /* chunk corresponding to mem */ void (*hook) (__malloc_ptr_t, __const __malloc_ptr_t) = force_reg (__free_hook); if (__buil ...
5.7.2.5 malloc_consolidate() malloc_consolidate() 函数用于将 fast bins 中的 chunk 合并,并加入 unsorted bin 中,其实现源代码如下: /* ------------------------- malloc_consolidate ------------------------- malloc_consolidate is a specialized version of free() that tears down chunks held in fastbins ...
5.7.2.4 sYSMALLOc() 当_int_malloc() 函数尝试从 fast bins , last remainder chunk , small bins , large bins 和 top chunk 都失败之后,就会使用 sYSMALLOc() 函数直接向系统申请内存用于分配所需的 chunk 。其实现源代码如下: /* sysmalloc handles malloc cases requiring more memory from the system. On entry, it is assumed tha ...
5.7.2.3 分配large bin chunk(二)  如果通过上面的方式从最合适的 small bin 或 large bin 中都没有分配到需要的 chunk ,则查看比当前 bin 的 index 大的 small bin 或 large bin 是否有空闲 chunk 可利用来分配所需的 chunk 。源代码实现如下 /* Search for a chunk by scanning bins, starting with next largest bin. This search is strictl ...
5.7.2.3 分配large bin chunk(一) 如果所需的 chunk 不属于 small bins ,首先会执行如下的代码段: /* If this is a large request, consolidate fastbins before continuing. While it might look excessive to kill all fastbins before even seeing if there is space available, this avoids fragmentation ...
5.7.2.2 分配small bin chunk   如果所需的 chunk 大小属于 small bin ,则会执行如下的代码: /* If a small request, check regular bin. Since these "smallbins" hold one size each, no searching within bins is necessary. (For a large request, we need to wait until unsorted chunks are p ...
5.7 内存分配malloc()   Ptmalloc2 主要的内存分配函数为 malloc() ,但源代码中并不能找到该函数,该函数是用宏定义为 public_mALLOc() ,因为该函数在不同的编译条件下,具有不同的名称。 public_mALLOc() 函数只是简单的封装 _int_malloc() 函数, _int_malloc() 函数才是内存分配的核心实现。下面我们将分析 malloc 的实现。   5.7.1 public_mALLOc     先给出源代码: Void_t* public_mALLOc(size_t byt ...
5.6.7 grow_heap,shrink_heap,delete_heap,heap_trim   这几个函数实现 sub_heap 和增长和收缩, grow_heap() 函数主要将 sub_heap 中可读可写区域扩大; shrink_heap() 函数缩小 sub_heap 的虚拟内存区域,减小该 sub_heap 的虚拟内存占用量; delete_heap() 为一个宏,如果 sub_heap 中所有的内存都空闲,使用该宏函数将 sub_heap 的虚拟内存还回给操作系统; heap_trim() 函数根据 sub_heap 的 ...
5.6.5 new_heap() New_heap() 函数负责从 mmap 区域映射一块内存来作为 sub_heap ,在 32 位系统上,该函数每次映射 1M 内存,映射的内存块地址按 1M 对齐;在 64 为系统上,该函数映射 64M 内存,映射的内存块地址按 64M 对齐。 New_heap() 函数只是映射一块虚拟地址空间,该空间不可读写,不会被 swap 。 New_heap() 函数的实现源代码如下: /* If consecutive mmap (0, HEAP_MAX_SIZE << 1, ...) ca ...
5.6.3 Arena_get2() 在5.6.2 节中提到, arena_get 宏尝试查看线程的私用实例中是否包含一个分配区,如果不存在分配区或是存在分配区,但对该分配区加锁失败,就会调用 arena_get2() 函数获得一个分配区,下面将分析 arena_get2 ...
5.6  多分配区支持 由于只有一个主分配区从堆中分配小内存块,而稍大的内存块都必须从 mmap 映射区域分配,如果有多个线程都要分配小内存块,但多个线程是不能同时调用 sbrk() 函数的,因为只有一个函数调用 ...
5.5.3  ptmalloc_lock_all(),ptmalloc_unlock_all(),ptmalloc_unlock_all2() /* Magic value for the thread-specific arena pointer when malloc_atfork() is in use. */ #define ATFORK_ARENA_PTR ((Void_t*)-1) /* The following hooks are used while the `atfork' handling mechanism is a ...
Global site tag (gtag.js) - Google Analytics