对象建立前的空间配置,和对象析构后的空间释放,由<stl_alloc.h>负责,SGI对此的设计哲学如下:
- 向 system heap 系统堆空间申请
- 考虑多线程情况
- 考虑内存不足的情况
- 考虑过多小块的内存可能造成内存碎片的问题
C++ 的内存分配动作是 ::operator new()
,而内存的释放动作是::operator delete()
来进行,我们在设计过程中,这两个函数由C的malloc()
和free()
函数代替,因为 ::operator new() 在申请失败的情况下会抛出异常而malloc()申请失败会返回空指针,SGI正是以malloc()和free()完成内存的配置与释放
考虑到小块内存可能造成内存碎片的问题,SGI设计了双级配置器,第一级配置器直接使用malloc()和free()
第二级配置器则视情况蚕蛹不同的策略:当内存块超过128字节,视为足够大,便呼叫第一级配置器,当配置内存块小于128字节,视之为过小,为了降低额外负担,便采用了复杂的memory pool 内存池整理方式,而不再求助于第一级配置器
整个设计究竟只开放第一级配置器,或是同时开放第二级配置器,取决于 __USE__MALLOC 是否被定义
第一级配置器
#ifndef MY_ALLOC_H
#define MY_ALLOC_H
#include<iostream>
namespace zyq
{
#if 0
#include new
#define __THROW_BAD_ALLOC throw::bad alloc;
#elif !defined(_THROW_BAD_ALLOC)
#define __THROW_BAD_ALLOC std::cerr<<"out of memory"<<std::endl; exit(1)
#endif
template<int inst>
class __malloc_alloc_template
{
public:
using PFUN = void (*)();
private:
static void* oom_malloc(size_t n) //allocate申请空间内存不足 调用该函数
{
void* result = NULL;
void (*my_malloc_handler) () = nullptr;
for (;;)
{
my_malloc_handler = __malloc_alloc_oom_handler;
if (my_malloc_handler == nullptr)
{
__THROW_BAD_ALLOC;
}
my_malloc_handler(); //调动释放内存
result = malloc(n); //申请空间
if (result != nullptr)
{
return result;
}
}
}
static void* oom_realloc(void* p, size_t new_sz) //reallocate事情空间内存不去 与上同理
{
void* result = NULL;
void (*my_malloc_handler) () = nullptr;
for (;;)
{
my_malloc_handler = __malloc_alloc_oom_handler;
if (my_malloc_handler == nullptr)
{
__THROW_BAD_ALLOC;
}
my_malloc_handler(); //调动释放内存
result = realloc(p, new_sz); //申请空间
if (result != nullptr)
{
return result;
}
}
}
static PFUN __malloc_alloc_oom_handler; //解决内存不足
public:
static void* allocate(size_t n) //空间申请 malloc
{
void* result = malloc(n);
if (result == nullptr)
{
return oom_malloc(n);
}
return result;
}
static void deallocate(void* p, size_t n) //空间释放 free
{
free(p);
}
static void* reallocate(void* p, size_t old_sz, size_t new_sz) //realloc
{
void* result = realloc(p, new_sz);
if (result == nullptr)
{
result = oom_realloc(p, new_sz);
}
return result;
}
static PFUN set_malloc_handler(PFUN p) //处理内存不足
{
PFUN old = __malloc_alloc_oom_handler;
__malloc_alloc_oom_handler = p;
return old;
}
};
template<int inst>
typename __malloc_alloc_template<inst>::PFUN __malloc_alloc_template<inst>::__malloc_alloc_oom_handler = nullptr;
//void(*__malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = nullptr; //类外声明
//typedef __malloc_alloc_template<0> malloc_alloc;
using malloc_alloc = __malloc_alloc_template<0>;
}
#endif // !MY_ALLOC_H
#include"my_alloc.h"
using namespace std;
char* cpa = (char*)malloc(sizeof(char) * 10000000);
void fun()
{
if (cpa != nullptr)
{
free(cpa);
}
cpa = nullptr;
zyq::malloc_alloc::set_malloc_handler(nullptr);
}
int main()
{
zyq::malloc_alloc::set_malloc_handler(fun);
int* p = (int*)zyq::malloc_alloc::allocate(sizeof(int) * 100);
return 0;
}
第二级配置器
第二级配置器多了一些机制,避免太多太小内存块造成内存碎片,小额区块带来的其实不仅是内存碎片的问题,配置时的额外负担也是一大问题,额外负担永远无法避免,毕竟系统要靠这多出来的空间来管理内存,但是内存块越小,额外负担所占的比例就越大,越显得浪费
#ifndef MY_ALLOC_H
#define MY_ALLOC_H
#include<iostream>
namespace zyq
{
#if 0
#include new
#define __THROW_BAD_ALLOC throw::bad alloc;
#elif !defined(_THROW_BAD_ALLOC)
#define __THROW_BAD_ALLOC std::cerr<<"out of memory"<<std::endl; exit(1)
#endif
template<int inst>
class __malloc_alloc_template
{
public:
using PFUN = void (*)();
private:
static void* oom_malloc(size_t n) //allocate申请空间内存不足 调用该函数
{
void* result = NULL;
void (*my_malloc_handler) () = nullptr;
for (;;)
{
my_malloc_handler = __malloc_alloc_oom_handler;
if (my_malloc_handler == nullptr)
{
__THROW_BAD_ALLOC;
}
my_malloc_handler(); //调动释放内存
result = malloc(n); //申请空间
if (result != nullptr)
{
return result;
}
}
}
static void* oom_realloc(void* p, size_t new_sz) //reallocate申请空间内存不足 与上同理
{
void* result = NULL;
void (*my_malloc_handler) () = nullptr;
for (;;)
{
my_malloc_handler = __malloc_alloc_oom_handler;
if (my_malloc_handler == nullptr)
{
__THROW_BAD_ALLOC;
}
my_malloc_handler(); //调动释放内存
result = realloc(p, new_sz); //申请空间
if (result != nullptr)
{
return result;
}
}
}
static PFUN __malloc_alloc_oom_handler; //设计为 解决内存不足 为了释放的空间
public:
static void* allocate(size_t n) //空间申请 malloc
{
void* result = malloc(n);
if (result == nullptr)
{
return oom_malloc(n);
}
return result;
}
static void deallocate(void* p, size_t n) //空间释放 free
{
free(p);
}
static void* reallocate(void* p, size_t old_sz, size_t new_sz) //realloc
{
void* result = realloc(p, new_sz);
if (result == nullptr)
{
result = oom_realloc(p, new_sz);
}
return result;
}
static PFUN set_malloc_handler(PFUN p) //处理内存不足
{
PFUN old = __malloc_alloc_oom_handler;
__malloc_alloc_oom_handler = p;
return old;
}
};
template<int inst>
typename __malloc_alloc_template<inst>::PFUN __malloc_alloc_template<inst>::__malloc_alloc_oom_handler = nullptr;
//void(*__malloc_alloc_template<inst>::__malloc_alloc_oom_handler)() = nullptr; //类外声明
//typedef __malloc_alloc_template<0> malloc_alloc;
using malloc_alloc = __malloc_alloc_template<0>;
// 二级空间适配器
enum {__ALIGN = 8};
enum {__MAX_BYTES = 128};
enum { __NFREELISTS = __MAX_BYTES / __ALIGN };
template<bool threads,int inst>
class __default_alloc_template
{
union obj
{
union obj* free_list_link; //next
//char client_data[1];
};
private:
static obj* volatile free_list[__NFREELISTS]; //volatile 从内存取 不放入寄存器 防止优化
static char* start_free;
static char* end_free;
static size_t heap_size; //total
static size_t ROUND_UP(size_t bytes) //提升至8倍数
{
return bytes + __ALIGN - 1 & ~(__ALIGN - 1);
// 1 7 0000 1000 1111 1000 0000 0111
}
static size_t FREELIST_INDEX(size_t bytes) //每八位 一分组
{
return (bytes + __ALIGN - 1) / __ALIGN - 1;
}
static char* chunk_alloc(size_t size, int& nobjs) //分配空间
{
char* result = NULL;
size_t total_bytes = size * nobjs;
size_t bytes_left = end_free - start_free;
if (bytes_left >= total_bytes)
{
result = start_free;
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 = start_free + total_bytes;
return result;
}
else
{
size_t bytes_to_get = 2 * total_bytes + ROUND_UP(heap_size >> 4); //后面是调整值
if (bytes_left > 0) //剩余一部分小空间
{
obj* volatile* my_free_list = free_list + FREELIST_INDEX(bytes_left);
((obj*)start_free)->free_list_link = *(my_free_list);
*my_free_list = (obj*)start_free; //start_free 空间链接到了 需要分配的位置
}
start_free = (char*)malloc(bytes_to_get); //system heap
if (start_free == NULL)//申请失败
{
obj* volatile* my_free_list = NULL;
obj* p = NULL;
for (int i = size; i <= __MAX_BYTES; i += __ALIGN)
{
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);
}
}
start_free = (char*)malloc_alloc::allocate(bytes_to_get);//寻求一级适配器帮助
}
end_free = start_free + bytes_to_get;
heap_size += bytes_to_get;
return chunk_alloc(size, nobjs);
}
}
static void* refill(size_t size) //填充 分配
{
int nobjs = 20;
char* chunk = chunk_alloc(size, nobjs);
if (nobjs == 1)
{
return chunk; //直接返回给用户
}
//进行连接
obj* volatile* my_free_list = NULL;
obj* result = (obj*)chunk;
obj* current_obj = NULL;
obj* next_obj = NULL;
int i = 0;
my_free_list = free_list + FREELIST_INDEX(size);
*my_free_list = next_obj = (obj*)(chunk + size); //偏移size个字节
//跳过第一个已经分配的字节
for (i = 1;; ++i) //free_list_link 连接过程
{
current_obj = next_obj;
next_obj = (obj*)((char*)next_obj + size);
if (i = nobjs - 1)//说明是最后一块
{
current_obj->free_list_link = NULL;
break;
}
current_obj->free_list_link = next_obj;
}
return (void*)result;
}
public:
static void* allocate(size_t size)
{
if (size > (size_t)__MAX_BYTES) //大于128
{
return malloc_alloc::allocate(size);
}
obj* result = nullptr;
obj* volatile* my_free_list = nullptr; //二级指针
my_free_list = free_list + FREELIST_INDEX(size);
result = *my_free_list;
if (result == nullptr) //链表结点不存在
{
void* r = refill(ROUND_UP(size));
return r;
}
*my_free_list = result->free_list_link; //头删
return result;
}
static void deallocate(void* p, size_t n)
{
if (n > (size_t)__MAX_BYTES) //转至一级适配器
{
malloc_alloc::deallocate(p, n);
return;
}
obj* q = (obj*)p;
obj* volatile* my_free_list = free_list + FREELIST_INDEX(n);
//头插
q->free_list_link = *my_free_list;
*my_free_list = q;
return;
}
static void* reallocate(void* p, size_t old_sz, size_t new_sz)
{
if (old_sz > (size_t)__MAX_BYTES && new_sz > (size_t)__MAX_BYTES)
{
return malloc_alloc::reallocate(p, old_sz, new_sz);//调用一级配置器
}
if (ROUND_UP(old_sz) == ROUND_UP(new_sz)) //old 22 new 24
{
return p;
}
size_t sz = old_sz < new_sz ? old_sz : new_sz;
void* s = allocate(new_sz);
memmove(s, p, sz); //将p内容给至s
deallocate(p, old_sz); //释放旧的大小
return s;
}
};
template<bool threads, int inst>
typename __default_alloc_template<threads, inst>::obj* volatile
__default_alloc_template<threads, inst>::free_list[__NFREELISTS] = {};
template<bool threads,int inst>
char* __default_alloc_template<threads, inst>::start_free = nullptr;
template<bool threads, int inst>
char* __default_alloc_template<threads, inst>::end_free = nullptr;
template<bool threads, int inst>
size_t __default_alloc_template<threads, inst>::heap_size = 0;
//
//SGI STL
#ifdef __USE_MALLOC //宏定义控制使用一级配置器或者二级配置器
typedef __malloc_alloc_template<0> malloc_alloc;
typedef malloc_alloc alloc;
#else
typedef __default_alloc_template<0, 0> alloc;
#endif // __USE_MALLOC
template<class T,class Alloc>
class simple_alloc
{
public:
static T* alloccate(size_t n) //n T类型
{
return (T*)(Alloc::allocate(sizeof(T) * n));
}
static T* allocate() //申请一个T类型空间
{
return (T*)(Alloc::allocate(sizeof(T))); //operator new
}
static void deallocate(T* p, size_t n) //n T
{
if (p == NULL) return;
Alloc::deallocate(p, sizeof(T) * n);
}
static void deallocate(T* p)
{
if (p == NULL) return;
Alloc::deallocate(p, sizeof(T));
}
};
}
#endif // !MY_ALLOC_H