自定义 allocator

// file: HHAllocator.h
#ifndef _HHALLOCATOR_
#define _HHALLOCATOR_

#include <new>    //for replace new
#include <cstddef>    //for ptrdiff_t, size_t
#include <cstdlib>    //for exit()
#include <climits>    //for UINT_MAX
#include <iostream>    //for cerr

namespace HH
{
    //模板类设计
    //内存分配
    template <class T>
    inline T* _allocate(ptrdiff_t size, T *){
        set_new_handler(0); //处理内存分配失败的情况
        T *tmp = (T*) (::operator new ((size_t)(size * sizeof(T))));
        if(tmp == 0){
            std::cerr << "out of memory." << std::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);    // placement new, invoke ctor of T1
    }

    //对象析构
    template <class T>
    inline void _destroy(T* ptr){
        ptr->~T();
    }

    template <class T>
    class allocator {
    public:
        // 定义各种type
        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;    //ptrdiff_t类型变量通常用来保存两个指针减法操作的结果。ptrdiff_t 与 size_t 类型一样,ptrdiff_t 也是一种与机器相关的类型,在 cstddef 头文件中定义。size_t 是unsigned 类型,而 ptrdiff_t 则是 signed 整型. size_t 类型用于指明数组长度,它必须是一个正数;ptrdiff_t 类型则应保证足以存放同一数组中两个指针之间的差距,它有可能是负数

        //一个嵌套的模板类,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);
        }

        //默认析构器, 归还空间
        pointer deallocate(pointer p, size_type n){
            _deallocate(p);
        }

        //拷贝构造器, 等同于 new((void*)p) T(value);
        void construct(pointer p, const T& value){
            _construct(p, value);
        }

        //析构器, 等同于 P->~T();
        void destroy(pointer p){
            _destroy(p);
        }

        //返回对象的地址
        pointer address(reference x){
            return (pointer) &x;
        }

        //返回 const 对象的地址
        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 HH

#endif // _HHALLOCATE_


// file : TestHHAllocator.cpp
#include "HHAllocator.h"
#include <vector>
#include <iostream>

using namespace std;

int main(void){
    int ia[5] ={0,1,2,3,4};
    vector<int, HH::allocator<int> > iv(ia, ia+5);

    for(int i=0; i<iv.size(); i++){
        cout << "iv[" <<i <<"] = " << iv[i] << endl;
    }

    return 0;
}

//g++ TestHHAllocator.cpp -o testAlloc -fpermissive

results matching ""

    No results matching ""