《STL源码剖析》—— 空间配置器(四)
来源:互联网 发布:java商城开源 编辑:程序博客网 时间:2024/05/22 00:51
一、第二级配置器 __default_alloc_template 剖析
为了方便管理,SGI第二级配置器会主动将任何小额区块的内存需求量上调至 8 的倍数,并维护 16 个free-lists,各自管理大小分别为 8, 16, 24, 32, 40, 48, 56,64, 72,80,88,96,104,112,120,128 bytes的小额区块。free-lists的节点结构如下:
union obj { union obj * free_list_link; char client_data[1]; /* The client sees this. */ };
注意:上述 obj 所用的是 union,由于 union 之故,从其第一字段观之,obj可被视为一个指针,指向相同形式的另一个 obj。从其第二字段观之,obj可被视为一个指针,指向实际区块。
// 以下是第二级配置器// 注意,无"template型别参数",且第二参数完全没派上用场// 第一参数用于多线程环境template <bool threads, int inst>class __default_alloc_template {private: // Really we should use static const int x = N // instead of enum { x = N }, but few compilers accept the former.# ifndef __SUNPRO_CC enum {__ALIGN = 8}; // 小型区块的上调边界 enum {__MAX_BYTES = 128}; // 小型区块的上限 enum {__NFREELISTS = __MAX_BYTES/__ALIGN}; // free-lists 个数# endif // ROUND_UP()将 bytes 上调至 8 的倍数 static size_t ROUND_UP(size_t bytes) { return (((bytes) + __ALIGN-1) & ~(__ALIGN - 1)); }__PRIVATE: union obj { // free-lists的节点构造 union obj * free_list_link; char client_data[1]; /* The client sees this. */ };private:# ifdef __SUNPRO_CC static obj * __VOLATILE free_list[]; // Specifying a size results in duplicate def for 4.1# else// 16个free-lists static obj * __VOLATILE free_list[__NFREELISTS]; # endif// 以下函数根据区块大小,决定使用第 n 号free-list。n从0起算static size_t FREELIST_INDEX(size_t bytes) {return (((bytes) + __ALIGN-1)/__ALIGN - 1);}// 返回一个大小为 n 的对象,并可能加入大小为 n 的其他区块到 free list static void *refill(size_t n);// 配置一大块空间,可容纳 nobjs 个大小为"size"的区块// 如果配置 nobjs 个区块有所不便,nobjs可能会降低static char *chunk_alloc(size_t size, int &nobjs); // Chunk allocation state. static char *start_free; // 内存池起始位置。只在 chunk_alloc() 中变化 static char *end_free; // 内存池结束位置。只在 chunk_alloc() 中变化 static size_t heap_size;// 下面三个条件编译给多线程条件下使用锁提供必要支持# ifdef __STL_SGI_THREADS static volatile unsigned long __node_allocator_lock; static void __lock(volatile unsigned long *); static inline void __unlock(volatile unsigned long *);# endif# ifdef __STL_PTHREADS static pthread_mutex_t __node_allocator_lock;# endif# ifdef __STL_WIN32THREADS static CRITICAL_SECTION __node_allocator_lock; static bool __node_allocator_lock_initialized; public: __default_alloc_template() {// This assumes the first constructor is called before threads// are started. if (!__node_allocator_lock_initialized) { InitializeCriticalSection(&__node_allocator_lock); __node_allocator_lock_initialized = true; } } private:# endif // 用于多线程环境下锁定操作 class lock { public: lock() { __NODE_ALLOCATOR_LOCK; } ~lock() { __NODE_ALLOCATOR_UNLOCK; } }; friend class lock;public: static void * allocate(size_t n);static void deallocate(void *p, size_t n);static void * reallocate(void *p, size_t old_sz, size_t new_sz);} ;// 以下是 static data member 的定义与初值设定template <bool threads, int inst>char *__default_alloc_template<threads, inst>::start_free = 0;template <bool threads, int inst>char *__default_alloc_template<threads, inst>::end_free = 0;template <bool threads, int inst>size_t __default_alloc_template<threads, inst>::heap_size = 0;template <bool threads, int inst>__default_alloc_template<threads, inst>::obj * __VOLATILE__default_alloc_template<threads, inst> ::free_list[# ifdef __SUNPRO_CC __NFREELISTS# else __default_alloc_template<threads, inst>::__NFREELISTS# endif] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
二、空间配置函数 allocate()
此函数首先判断区块大小,大于 128 bytes 就调用第一级配置器,小于 128 bytes 就检查对应的 free list。如果 free list 之内有可用的区块,就直接拿来用,如果没有可用区块,就将区块大小上调至 8 倍数边界,然后调用 refill(),准备为 free list 重新填充空间。
/* n must be > 0 */ static void * allocate(size_t n) { obj * __VOLATILE * my_free_list; obj * __RESTRICT result;// 大于 128 就调用第一级配置器 if (n > (size_t) __MAX_BYTES) { return(malloc_alloc::allocate(n)); }// 寻找 16 个free lists中适当的一个 my_free_list = free_list + FREELIST_INDEX(n); // Acquire the lock here with a constructor call. // This ensures that it is released in exit or during stack // unwinding.# ifndef _NOTHREADS /*REFERENCED*/ lock lock_instance;# endif result = *my_free_list; if (result == 0) {// 没找到可用的 free list,准备重新填充 free list void *r = refill(ROUND_UP(n)); return r; }// 调整 free list *my_free_list = result -> free_list_link; return (result); };
该函数首先判断区块大小,大于 128 bytes 就调用第一级配置器,小于 128 bytes 就找出对应的 free list,将区块回收。
/* p 不可以是 0 */static void deallocate(void *p, size_t n) {obj *q = (obj *)p; obj * __VOLATILE * my_free_list;// 大于 128 就调用第一级配置器 if (n > (size_t) __MAX_BYTES) { malloc_alloc::deallocate(p, n); return; }// 寻找对应的 free list my_free_list = free_list + FREELIST_INDEX(n); // acquire lock# ifndef _NOTHREADS /*REFERENCED*/ lock lock_instance;# endif /* _NOTHREADS */// 调整 free list,回收区块 q -> free_list_link = *my_free_list; *my_free_list = q; // lock is released here}
四、重新填充 free lists
前面的 allocate(),当它发现 free list 中没有可用区块时,就调用 refille(),准备为 free list 重新填充空间。新的空间将取自内存池(经由 chunk_alloc()完成)。缺省取得 20 个新节点(新区块),但万一内存池空间不足,获得的节点数(区块数)可能小于 20。
// 返回一个大小为 n 的对象,并且有时候会为适当的 free list 增加节点// 假设 n 已经适当上调至 8 的倍数/* We hold the allocation lock. */template <bool threads, int inst>void* __default_alloc_template<threads, inst>::refill(size_t n){ int nobjs = 20;// 调用 chunk_alloc(),尝试取得 nobjs 个区块作为 free list 的新节点// 注意参数 nobjs 是 pass by reference char * chunk = chunk_alloc(n, nobjs); obj * __VOLATILE * my_free_list; obj * result; obj * current_obj, * next_obj; int i;// 如果只获得一个区块,这个区块就分配给调用者用,free list无新节点 if (1 == nobjs) return(chunk);// 否则准备调整 free list,纳入新节点 my_free_list = free_list + FREELIST_INDEX(n); // 以下在 chunk 空间内建立 free listresult = (obj *)chunk; // 这一块准备返回给客户端// 以下导引 free list 指向新配置的空间(取自内存池)*my_free_list = next_obj = (obj *)(chunk + n);// 以下将 free list的各节点串接起来for (i = 1; ; i++) { // 从 1 开始,因为第 0 个将返回给客端 current_obj = next_obj; next_obj = (obj *)((char *)next_obj + n); if (nobjs - 1 == i) { current_obj -> free_list_link = 0; break; } else { current_obj -> free_list_link = next_obj; }} return(result);}
五、内存池
从内存池中取空间给 free list使用,是chunk_alloc()的工作:
// 假设 size 已经适当上调至 8 的倍数// 注意参数 nobjs 是 pass by referencetemplate <bool threads, int inst>char*__default_alloc_template<threads, inst>::chunk_alloc(size_t size, int& nobjs){ char * result; size_t total_bytes = size * nobjs; size_t bytes_left = end_free - start_free; // 内存池剩余空间 if (bytes_left >= total_bytes) {// 内存池剩余空间完全满足需求量 result = start_free; start_free += total_bytes; return(result); } else if (bytes_left >= size) { // 内存池剩余空间不能完全满足需求量,但足够供应一个(含)以上的区块 nobjs = bytes_left/size; total_bytes = size * nobjs; result = start_free; start_free += total_bytes; return(result); } else { // 内存池剩余空间连一个区块的大小都无法提供 size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4); // 以下试着让内存池中的残余零头还有利用价值(零头也应该是 8 的倍数) if (bytes_left > 0) {// 内存池内还有一些零头,先配给适当的free list// 首先寻找适当的 free list obj * __VOLATILE * my_free_list = free_list + FREELIST_INDEX(bytes_left);// 调整 free list,将内存池中的残余空间编入 ((obj *)start_free) -> free_list_link = *my_free_list; *my_free_list = (obj *)start_free; }// 配置 heap 空间,用来补充内存池 start_free = (char *)malloc(bytes_to_get); if (0 == start_free) {// heap空间不足,malloc()失败 int i; obj * __VOLATILE * my_free_list, *p; // Try to make do with what we have. That can't // hurt. We do not try smaller requests, since that tends // to result in disaster on multi-process machines. // 试着检视我们手上拥有的东西。这不会造成伤害。我们不打算尝试配置 // 较小的区块,因为那在多进程(multi-process)机器上容易导致灾难 // 以下搜寻适当的 free list // 所谓适当是指"尚有未用区块,且区块够大"之 free list for (i = size; i <= __MAX_BYTES; i += __ALIGN) { my_free_list = free_list + FREELIST_INDEX(i); p = *my_free_list; if (0 != p) { // free list内尚有未用区块 // 调整free list以释出未用区块 *my_free_list = p -> free_list_link; start_free = (char *)p; end_free = start_free + i;// 递归调用自己,为了修正 nobjs return(chunk_alloc(size, nobjs));// 注意,任何残余零头终将被编入适当的free-list中备用 } }end_free = 0;// 如果出现意外,到处都没内存可用// 调用第一级配置器,看看 out-of-memory 机制能否尽点力start_free = (char *)malloc_alloc::allocate(bytes_to_get); // 这会导致抛出异常(exception),或内存不足的情况获得改善 } heap_size += bytes_to_get; end_free = start_free + bytes_to_get;// 递归调用自己,为了修正 nobjs return(chunk_alloc(size, nobjs)); }
上述的chunk_alloc()函数以 end_free - start_free 来判断内存池的水量。如果水量充足,就直接调用 20 个区块返回给 free list。如果水量不足以提供 20 个区块,但还最后供应一个以上的区块,就拔出这不足 20 个区块的空间出去。这时候其 pass by reference的nobjs参数将被修改为实际能够供应的区块数。如果内存池连一个区块空间都无法供应,对客端显然无法交待,此时便需利用 malloc() 从head中配置内存,为内存池注入源头活水以应付需求。新水量的大小为需求量的两倍,再加上一个随着配置次数增加而愈来愈大的附加量。
- 《STL源码剖析》—— 空间配置器(四)
- STL源码剖析(四)---空间配置器
- 《STL源码剖析》—— 空间配置器(二)
- 《STL源码剖析》—— 空间配置器(三)
- 《STL源码剖析》—— 空间配置器(五)
- STL源码剖析——空间配置器和迭代器
- STL源码剖析——空间配置器
- STL源码剖析——空间配置器
- STL源码剖析——空间配置器
- STL源码剖析——空间配置器
- STL源码剖析——空间配置器
- STL源码剖析 — 空间配置器(allocator)
- 《STL源码剖析》STL空间配置器
- SGI STL空间配置器(STL源码剖析):
- SGI STL空间配置器(STL源码剖析)
- STL源码剖析---空间配置器
- STL源码剖析---空间配置器
- STL源码剖析之空间配置器
- asp的排序好像对数据库是文本类是无效往的
- Oracle预估的基数算法
- POJ 2195 费用流
- CodeChef Easy Draughts
- eval
- 《STL源码剖析》—— 空间配置器(四)
- 利用verilog将二进制码转换为十进制BCD码
- 鼠标键盘全局HOOK
- php 5.2以上的ereg Deprecated: Function ereg() is deprecated in 使用pregmatch 正则解决的
- delete from... not exisits
- 如何将App程序发布到苹果App Store
- 线性表的完整操作
- AppClean—Mac的第三方强大的卸载工具
- 迷宫