zoukankan      html  css  js  c++  java
  • 《STL源码剖析》chapter2空间配置器allocator

    为什么不说allocator是内存配置器而说是空间配置器,因为空间不一定是内存,也可以是磁盘或其他辅助介质。是的,你可以写一个allocator,直接向硬盘取空间。sgi stl提供的配置器,配置的对象是内存。

     stl中allocator用法参考以前的http://www.cnblogs.com/youxin/archive/2012/06/07/2540170.html。

    书中jj allocator类

    #ifndef _JJALLOC
    #define _JJALLOC
    #include<new> //for placement new
    #include<cstddef> //for ptrdiff_t ,size_t
    #include<cstdlib> //for exit()
    #include<climits> //for UINX_MAX
    #include<iostream> //for cerr
    
    namespace  JJ
    {
    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;
            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 constuctor of t1
    }
    
    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;
        };
        //需要加上以下2个函数,windows的编译器用到了allocator不同类型的拷贝, 
         allocator()  
            {  
                return ;  
            }  
                  
            template <class U>  
            allocator(const allocator<U>& c )  
            {  
            }  
    
        //hint user for locality,第2个参数是个提示,实现上可能会利用它来增进区域性(locality),或完全忽略之
        
        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
    #include"2jjalloc.h"
    #include<vector>
    #include<iostream>
    using namespace std;
    
    int main()
    {
        int ia[5]={0,1,2,3,4};
        unsigned int i;
        
        vector<int,JJ::allocator<int> > iv(ia,ia+5);
        for(i=0;i<iv.size();i++)
            cout<<iv[i]<<ends;
        cout<<endl;
    }

    这个allocator只能有限程序带票PJ STL,

  • 相关阅读:
    Centos7下PyCharm2019安装
    企业的web项目类型、企业项目开发流程、立项申请阶段、需求分析、导航菜单、轮播图、退出登录、登录注册、课程列表、创建虚拟环境、依赖包安装、创建项目、调整目录、创建代码版本git、日志配置、异常处理、创建数据库、
    增量式爬取阳光热线网
    分布式爬取阳光热线网
    什么是接口测试,为什么做接口测试
    什么是接口、接口优势、类型(详解)
    187. Repeated DNA Sequences
    274. H-Index
    299. Bulls and Cows
    36. Valid Sudoku
  • 原文地址:https://www.cnblogs.com/youxin/p/3693243.html
Copyright © 2011-2022 走看看