当前位置:Linux教程 - Linux - 类STL的内存分配,释放接口

类STL的内存分配,释放接口

DarkSpy 于 2002/7/30 @ ConeosBraintel Software
近几日一直在开发偶的 EasyCode Windows 版(EasyCode Pro),不过,对于内存管理,自己写了一套,不用借助任何的 include 文件。
由于时间关系,没有写自己的 set_handler 代码,不过有时间会加上去的 :)
该代码您可以任意使用,但是如果出现意外,如硬盘烧毁,无故断电,光驱飞盘等等现象,DarkSpy一律不负任何责任 :-)
有一部分代码没有很完善,不过90%可以正常使用。
代码全部遵循是ISO C++98标准,如果您的编译器无法通过,则是编译器不支持,而不是代码问题。
如果您修改了它,请给我一份拷贝,谢谢!

#ifdef __GNUC__
typedef long unsigned int size_t;
#else
#define size_t unsigned int
#endif
namespace __easycode_pro
{
template <typename T>
struct _type_traits
{
typedef T _type;
~_type_traits() { _type().~_type(); }
};
template <typename T>
struct _type_traits<T *>
{
typedef _type_traits<T *> _type;
~_type_traits() { _type().~_type(); }
};
template <typename T>
struct _type_traits<const T>
{
typedef _type_traits<T> _type;
~_type_traits() { _type().~_type(); }
};
template <typename T>
struct _type_traits<const T *>
{
typedef _type_traits<T *> _type;
~_type_traits() { _type().~_type(); }
};
template <>
struct _type_traits<int>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned int>
{
~_type_traits() { }
};
template <>
struct _type_traits<int *>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned int *>
{
~_type_traits() { }
};
template <>
struct _type_traits<char>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned char>
{
~_type_traits() { }
};
template <>
struct _type_traits<char *>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned char *>
{
~_type_traits() { }
};
template <>
struct _type_traits<long>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned long>
{
~_type_traits() { }
};
template <>
struct _type_traits<long *>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned long *>
{
~_type_traits() { }
};
template <>
struct _type_traits<short>
{
~_type_traits() { }
};
template <>
struct _type_traits<unsigned short>
{
~_type_traits() { }
};
template <>
struct _type_traits<short *>
{
~_type_traits() { }
};
template <>
struct _type_traits<bool>
{
~_type_traits() { }
};
template <>
struct _type_traits<float>
{
~_type_traits() { }
};
template <>
struct _type_traits<float *>
{
~_type_traits() { }
};
template <>
struct _type_traits<double>
{
~_type_traits() { }
};
template <>
struct _type_traits<double *>
{
~_type_traits() { }
};
};

namespace __easycode_pro
{
template <typename T>
class allocator
{
protected:
void * operator new (size_t size)
{ return ::operator new (size); }
void operator delete(void * p)
{ ::operator delete (p); }

void * operator new[](size_t size, int i)
{ return ::operator new (size * i + 1); }
void operator delete[](void *p, int i)
{ ::operator delete (p); }

void * operator new(size_t size, void *p)
{ return p; }
void operator delete(void *p, void *p2)
{ ::operator delete(p); }
public:
typedef T t_type;
typedef t_type* t_pointer;
typedef t_type& t_reference;
typedef int t_value;
public:
allocator(t_pointer pt = 0) { }
~allocator() { }

static t_pointer allocate(allocator<T> p, int how_many) throw(bool);
static void deallocate(t_pointer p) throw(bool);

template <typename U>
static t_pointer placement_allocate(U *p) throw(bool);
template <typename U>
static void placement_deallocate(U *p) throw(bool);
t_pointer operator()(t_pointer t) { return t; }
};

template <typename T, typename Alloc = allocator<T> >
struct mem_alloc
{
static typename Alloc::t_pointer alloc(int how_many)
{ return Alloc::allocate(Alloc(), how_many); }
static void dealloc(T *p)
{ Alloc::deallocate(p); }
template <typename U>
static typename Alloc::t_pointer p_alloc(U *p)
{ return Alloc::template placement_allocate<T>(p); }
template <typename U>
static void p_dealloc(U *p)
{ Alloc::template placement_deallocate<T>(p); }
};

template <typename T>
struct __convert
{
T *data;
__convert() : data(0) {}
operator T(){ return *data; }
operator void *(){}
void * got(T &p) { return &p; }
};
};

#define set_alloc_name(_alloc_type)
typedef __easycode_pro::mem_alloc<_alloc_type> _alloc_type##_allocator

#define get_address(__type, __value)
__easycode_pro::__convert<__type>().got(__value)

template <typename T> inline
typename __easycode_pro::allocator<T>::t_pointer
__easycode_pro::allocator<T>::allocate(__easycode_pro::allocator<T> p, int how_many)
throw(bool)
{
t_pointer tmp;
if(how_many<=0) //allocator one of sizeof type memory
tmp = (t_pointer)(allocator<T>::operator new(sizeof(T)));
else tmp = (t_pointer)(allocator<T>::operator new[](sizeof(T), how_many));
if(!tmp) throw(false);
p(tmp);
return tmp;
}

template <typename T> inline
void __easycode_pro::allocator<T>::deallocate(t_pointer p)
throw(bool)
{
get_address(t_type, *p);
__easycode_pro::allocator<T>::operator delete[](p, 0);
}

template <typename T>
template <typename U> inline
typename __easycode_pro::allocator<T>::t_pointer
__easycode_pro::allocator<T>::placement_allocate(U *p)
throw(bool)
{
const int __ADJUST = (sizeof(U) + 3) & ~ (3);

U * u_tmp(0);
p = allocator<U>::allocate(u_tmp, __ADJUST);
delete u_tmp;

t_pointer tmp;
tmp = (t_pointer)(allocator<T>::operator new(sizeof(t_type), p));
if(!tmp) throw(false);
return tmp;
}

template <typename T>
template <typename U> inline
void __easycode_pro::allocator<T>::placement_deallocate(U *p)
throw(bool)
{
get_address(t_type, *p);
_type_traits<U>().~_type_traits<U>();
__easycode_pro::allocator<T>::operator delete(p, p);
}
那么我们如何分配内存呢?
自己看吧!

main()
{
using namespace __easycode_pro;
int * p;
char *c;
set_alloc_name(int);
set_alloc_name(char);

// 普通的分配
p = int_allocator::alloc(10);
c = char_allocator::alloc(20);
int_allocator::dealloc(p);
char_allocator::dealloc(c);

c = char_allocator::alloc(20);
p = int_allocator::p_alloc(c); // p 分配在 c 的空间内,边界已经在代码中手工调整
int_allocator::p_dealloc(p); // 释放
}

如果您有任何意见或者疑义,可以和DarkSpy联系,谢谢。。。