您的位置:首页 > 其它

设计一个简单的空间配置器 JJ::allocator

2014-11-22 09:39 295 查看
#ifndef _JJALLOC_
#define _JJALLOC_
#include<new>//for placement new
#include<cstdlib>//for ptrdiff_t,size_t
#include<climits>//for UINT_MAX
#include<iostream>//for cerr

namespace JJ
{

template<class T>
inline T* _allocate(ptrdiff_t size,T*){
//当operator new申请一个内存失败的时候.
//如果存在客户指定的处理函数,则调用处理函数(new_handler),如果不存在则抛出一个异常
//参数为0,则抛出异常
set_new_handler(0);
T* tmp=(T*)(::operator new((size_t)(size*sizeof(T))));
if(tmp==0){
cerr<<"out of memory"<<endl;
exit(1);
}
return tmp;
}

template<class T>
inline void _deallocate(T* buffer){
::operator delete(buffer);
}

template<class T1,class T2>
inline void _construct(T1*p,const T2& value){
new(p) T1(value);
}
template<class T>
inline void _destroy(T* ptr){
ptr->~T();
}

template<class T>
class allocator{
public:
typedef T           value_type;
typedef T*          pointer;
typedef const T*    const_pointer;
typedef T&          reference;
typedef const T&    const_reference;
typedef size_t      size_type;
typedef ptrdiff_t   difference_type;

//rebind allocator of type U
template<class U>
struct rebind{
typedef allocator<U> other;
};

pointer allocate(size_type n,const void* hint=0){
return _allocate((difference_type)n,(pointer)0);
}

void deallocate(pointer p,size_type n) {_deallocate(p);}

void construct(pointer p,const T& value){
_construct(p,value);
}

void destroy(pointer p){ _destroy(p);}

pointer address(reference x) { return (pointer)&x;}

const_pointer const_address(const_reference x) {
return (const_pointer)&x;
}
size_type max_size() const{
return size_type(UINT_MAX/sizeof(T););
};
};

} //end of namespace JJ
#endif _JJALLOC_


1.类型介绍:

size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t
ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.
size_type是unsigned类型,表示容器中元素长度或者下标,vector<int>::size_type i = 0;
difference_type是signed类型,表示迭代器差距,vector<int>:: difference_type = iter1-iter2.

2.

<span style="font-family:Microsoft YaHei;">set_new_handler(0)介绍</span>


当operator new申请一个内存失败的时候,它会进行如下的处理步骤:

1、如果存在客户指定的处理函数,则调用处理函数(new_handler),如果不存在则抛出一个异常

2、继续申请内存分配请求。

3、判断申请内存是否成功,如果成功则返回内存指针,如果失败转向处理步骤1

为了自定义这个“用以处理内存不足”的函数new_handler,用户可以调用set_new_handler进行设置

这两个函数声明如下:

namespace std{

typedef void (*new_handler)();

new_handler set_new_handler(new_handler p) throw();

}

其中,new_handler是个typedef,定义一个函数指针,该函数没有参数,也没有返回值;

set_new_handler用于设置处理函数,设置p为当前处理函数,并返回之前的new_handler
当参数p为0时,即抛出异常
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: