标签:style log com bad bsp 统一 blog and hand
static void *allocate(size_t n) { void *result = malloc(n); if (NULL == result) result = oom_malloc(n); return result; }
deallocate对free函数简单封装 :
static void deallocate(void *p, size_t) { free(p); }
oom_malloc调用外部提供的malloc失败处理函数,然后重新试着再次调用malloc。重复执行此过程,直到malloc成功为止 :
template <int inst> void* __malloc_alloc<inst>::oom_malloc(size_t n) { void (*my_malloc_handler)(); void *result; for (;;) { my_malloc_handler = malloc_alloc_oom_handler; if (NULL == my_malloc_handler) __THROW_BAD_ALLOC; (*my_malloc_handler)(); result = malloc(n); if (result) return result; } }
第二级空间配置器
//自由链表 union obj { union obj *free_list_link; char data[1]; }; //自由链表数组 static obj *volatile free_list[__NFREELISTS];
template <bool threads> void *__default_alloc<threads>::allocate(size_t n) { obj *volatile *my_free_list; obj *result; if (n > (size_t)__MAX_BYTES) //调用第一级配置器 return malloc_alloc::allocate(n); my_free_list = free_list + FREELIST_INDEX(n); result = *my_free_list; if (result == NULL) { //第n号链表无内存块,则准备重新填充该链表 void *r = refill(ROUND_UP(n)); return r; } *my_free_list = result->free_list_link; return result; }
template <bool threads> void *__default_alloc<threads>::refill(size_t n) { int nobjs = __NOBJS; char *chunk = chunk_alloc(n, nobjs); //从内存池获取内存 if (nobjs == 1) //只能分配一块,则直接返回给调用者 return chunk; obj *volatile *my_free_list; obj *result, *next_obj, *current_obj; result = (obj *)chunk; my_free_list = free_list + FREELIST_INDEX(n); *my_free_list = next_obj = (obj *)(chunk + n); for (int i = 1; i < nobjs - 1; i++) //将剩下的区块添加进链表 { current_obj = next_obj; next_obj = (obj *)(char *)(next_obj + n); current_obj->free_list_link = next_obj; } //最后一块 current_obj = next_obj; current_obj->free_list_link = NULL; return result; }
if (size_left >= total_size) //内存池剩余空间满足需求 { result = start_free; start_free += total_size; return result; }
若内存池大小无法满足20个内存节点的大小,但至少满足1个内存节点,则直接返回相应的内存节点大小的内存块给refill;
else if (size_left >= size) //剩余空间不能全部满足,但至少满足一块 { nobjs = size_left / size; result = start_free; start_free += nobjs * size; return result;
若内存池连1个内存节点大小的内存块都无法提供,则chunk_alloc函数会将内存池中那一点点的内存大小分配给其他合适的链表,然后去调用malloc函数分配的内存大小为所需的两倍。若malloc成功,则返回相应的内存大小给refill;若malloc失败,会先搜寻其他链表的可用的内存块,添加到内存池,然后递归调用chunk_alloc函数来分配内存,若其他链表也无内存块可用,则只能调用第一级空间配置器,因为第一级空间配置器有malloc失败的出错处理函数,最终的希望只能寄托在那里了。
template <bool threads> char *__default_alloc<threads>::chunk_alloc(size_t size, int& nobjs) { size_t total_size = size * nobjs; char *result; size_t size_left = end_free - start_free; if (size_left >= total_size) //内存池剩余空间满足需求 { result = start_free; start_free += total_size; return result; } else if (size_left >= size) //剩余空间不能全部满足,但至少满足一块 { nobjs = size_left / size; result = start_free; start_free += nobjs * size; return result; } else //连一个区块都无法满足 { if (size_left > 0) //将残余内存分配给其他合适的链表 { obj *volatile *my_free_list = free_list + FREELIST_INDEX(size_left); ((obj *)start_free)->free_list_link = *my_free_list; //在头部插入 *my_free_list = (obj *)start_free; } size_t bytes_to_get = 2 * total_size + ROUND_UP(heap_size >> 4); start_free = (char *)malloc(bytes_to_get); if (start_free == NULL) //堆空间不足 { int i; obj *volatile *my_free_list; obj *p; for (i = size; i < __MAX_BYTES; i++) { my_free_list = free_list + FREELIST_INDEX(i); p = *my_free_list; if (p != NULL) { *my_free_list = p->free_list_link; start_free = (char *)p; end_free = start_free + i; return chunk_alloc(size, nobjs); } } end_free = NULL; //调用第一级配置器 start_free = (char *)malloc_alloc::allocate(bytes_to_get); } heap_size += bytes_to_get; end_free = start_free + heap_size; return chunk_alloc(size, nobjs); } }
void __default_alloc<threads>::deallocate(void *p, size_t n) { //大于__MAX_BYTES,则释放该内存 if (n > (size_t)__MAX_BYTES) malloc_alloc::deallocate(p, n); obj *q = (obj *)p; obj *volatile *my_free_list; my_free_list = free_list + FREELIST_INDEX(n); //小于__MAX_BYTES,则回收区块,并未释放 q->free_list_link = *my_free_list; *my_free_list = q; }
内存对外接口
STL对外提供了一个simple_alloc类,该类提供统一的接口:allocate函数、deallocate函数,使得外部无需关心使用的是几级内存配置器。另外simple_alloc类将外部所需的对象个数转换为字节。如下。
template <typename T, typename Alloc> class simple_alloc { public: static T *allocate(size_t n) // 个数 { return n == 0 ? 0 : (T*)Alloc::allocate(n * sizeof(T)); // 将个数转换为字节 } static T *allocate(void) { return (T*)Alloc::allocate(sizeof(T)); } static void deallocate(T *p, size_t n) // 个数 { if (n != 0) Alloc::deallocate(p, n * sizeof(T)); } static void deallocate(T *p) { Alloc::deallocate(p, sizeof(T)); } };
标签:style log com bad bsp 统一 blog and hand
原文地址:http://www.cnblogs.com/zxiner/p/7197054.html