Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

有关allocator配置器的一点小总结,自己实现allocator类

$
0
0

文章内容来自《STL源码剖析》中有关allocator配置器的一些小总结,后面附上一个自己定义的allocator类。


1、我们习惯的内存分配和释放是由 new 和delete来实现的,其内部细节是分步进行的,两部分:

      new : 分配空间-->调用构造函数             delete:  调用析构函数--> 释放空间
      而STL Allocator为了细化分工,分别对应上面的两部分
      分配空间: alloc:allocator()  释放空间:alloc:deallocate()
      对象构造: 用::construct()       对象析构:用::destroy()
2、construct() 中两个参数:指针p和value。
inline void construct(T1* p, const T2 &value)
        new (p) T1(value) ;
一定要记住上面可以是两种类型的;还有new是定位构造。
3、C++的内存配置基本操作是::operator new 释放基本操作是::operator delete。
两个全局函数相当于C语言的malloc()和free()
    
4、C++ new handler机制是,你可以要求系统在内存配置需求无法满足时候,调用一个自己制定的函数。
换句话说,就是一旦::operator new无法完成任务时,在丢出 std::bad_alloc 异常状态之前,会先调用由客户端制定的处理例程。该例程通常被称为new-handler。
    set_new_handler(0);这样代表不声明任何自己的处理例程,直接按照其默认流程执行。
    set_new_handler (new_handler new_p)执行自己定义的例程new_p

下面是头文件的内容:
#ifndef _MYALLOCATOR_H_
#define _MYALLOCATOR_H_

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

namespace jmy
{
    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)
        {
            cerr << "out of memory" << endl;
        }
        return tmp;
    }

    template<class T>
    inline void _deallocate(T* buffer)
    {
        //operator delete must be coupled with operator new
        ::operator delete(buffer);
    }

    template<class T1, class T2>
    inline void _construct(T1* buffer, const T2& value)
    {
        //placement new invoke the constructor of T1
        new(buffer) 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 jmy


#endif    


作者:jmy99527 发表于2013-11-19 9:17:37 原文链接
阅读:10 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>