C++ STL with jemalloc

前端 未结 5 1335
渐次进展
渐次进展 2021-02-05 13:56

How is it possible to use C++ STL containers with jemalloc (or any other malloc implementation)?

Is it as simple as include jemalloc/jemalloc.h

5条回答
  •  攒了一身酷
    2021-02-05 14:11

    Make yourself allocator. Do like this:

    #include 
    
    template
    struct RemoveConst
    {
        typedef T value_type;
    };
    
    template
    struct RemoveConst
    {
        typedef T value_type;
    };
    
    template 
    class YourAlloc {
    public:
        // type definitions
        typedef RemoveConst              Base;
        typedef typename Base::value_type   value_type;
        typedef value_type*                 pointer;
        typedef const value_type*           const_pointer;
        typedef value_type&                 reference;
        typedef const value_type&           const_reference;
        typedef std::size_t                 size_type;
        typedef std::ptrdiff_t              difference_type;
    
        // rebind allocator to type U
        template 
        struct rebind {
            typedef YourAlloc other;
        };
    
        // return address of values
        pointer address(reference value) const {
            return &value;
        }
        const_pointer address(const_reference value) const {
            return &value;
        }
    
        /* constructors and destructor
        * - nothing to do because the allocator has no state
        */
        YourAlloc() throw() {
        }
        YourAlloc(const YourAlloc&) throw() {
        }
        template 
        YourAlloc(const YourAlloc&) throw() {
        }
        ~YourAlloc() throw() {
        }
    
        // return maximum number of elements that can be allocated
        size_type max_size() const throw() {
            return std::numeric_limits::max() / sizeof(T);
        }
    
        // allocate but don't initialize num elements of type T
        pointer allocate(size_type num, const void* = 0) {
            return (pointer)je_malloc(num * sizeof(T));
        }
    
        // initialize elements of allocated storage p with value value
        void construct(pointer p, const T& value) {
            // initialize memory with placement new
            new((void*)p)T(value);
        }
    
        // destroy elements of initialized storage p
        void destroy(pointer p) {
            // destroy objects by calling their destructor
            p->~T();
        }
    
        // deallocate storage p of deleted elements
        void deallocate(pointer p, size_type num) {
            je_free(p);
        }
    };
    
    // return that all specializations of this allocator are interchangeable
    template 
    bool operator== (const YourAlloc&,
        const YourAlloc&) throw() {
        return true;
    }
    template 
    bool operator!= (const YourAlloc&,
        const YourAlloc&) throw() {
        return false;
    }
    
    int main()
    {
        std::vector> vector;
    
        return 0;
    }
    

    The code is copied from here

提交回复
热议问题