Class Allocator

Synopsis

#include <src/c4/allocator.hpp>

template< class T, class MemResProvider=MemResGlobal >
class Allocator : public detail::_AllocatorUtil< MemResProvider >

Description

An allocator is simply a proxy to a memory resource.

Parameters

T -

MemResProvider -

Mentioned in

Inheritance

Ancestors: _AllocatorUtil

Summary

operator==
operator!=
rebind
rebound
Allocator overload
operator= overload
select_on_container_copy_constructreturns a default-constructed polymorphic allocator object
allocate
deallocate
reallocate

Source

Lines 167-252 in src/c4/allocator.hpp.

template< class T, class MemResProvider=MemResGlobal >
class Allocator : public detail::_AllocatorUtil< MemResProvider >
{
public:

    using impl_type = detail::_AllocatorUtil< MemResProvider >;

    using value_type = T;
    using pointer = T*;
    using const_pointer = T const*;
    using reference = T&;
    using const_reference = T const&;
    using size_type = size_t;
    using difference_type = std::ptrdiff_t;
    using propagate_on_container_move_assigment = std::true_type;

public:

    template< class U, class MRProv >
    bool operator== (Allocator<U, MRProv> const& that) const
    {
        return this->resource() == that.resource();
    }
    template< class U, class MRProv >
    bool operator!= (Allocator<U, MRProv> const& that) const
    {
        return this->resource() != that.resource();
    }

public:

    template< class U, class MRProv > friend class Allocator;
    template< class U >
    struct rebind
    {
        using other = Allocator< U, MemResProvider >;
    };
    template< class U >
    typename rebind<U>::other rebound()
    {
        return typename rebind<U>::other(*this);
    }

public:

    using impl_type::impl_type;
    Allocator() : impl_type() {} // VS demands this

    template< class U > Allocator(Allocator<U, MemResProvider> const& that) : impl_type(that.resource()) {}

    Allocator(Allocator const&) = default;
    Allocator(Allocator     &&) = default;

    Allocator& operator= (Allocator const&) = default; // WTF? why? @see http://en.cppreference.com/w/cpp/memory/polymorphic_allocator
    Allocator& operator= (Allocator     &&) = default;

    /** returns a default-constructed polymorphic allocator object
     * @see http://en.cppreference.com/w/cpp/memory/polymorphic_allocator/select_on_container_copy_construction      */
    Allocator select_on_container_copy_construct() const { return Allocator(*this); }

    T* allocate(size_t num_objs, size_t alignment=alignof(T))
    {
        C4_ASSERT(this->resource() != nullptr);
        C4_ASSERT(alignment >= alignof(T));
        void* vmem = this->resource()->allocate(detail::size_for<T>(num_objs), alignment);
        T* mem = static_cast< T* >(vmem);
        return mem;
    }

    void deallocate(T * ptr, size_t num_objs, size_t alignment=alignof(T))
    {
        C4_ASSERT(this->resource() != nullptr);
        C4_ASSERT(alignment >= alignof(T));
        this->resource()->deallocate(ptr, detail::size_for<T>(num_objs), alignment);
    }

    T* reallocate(T* ptr, size_t oldnum, size_t newnum, size_t alignment=alignof(T))
    {
        C4_ASSERT(this->resource() != nullptr);
        C4_ASSERT(alignment >= alignof(T));
        void* vmem = this->resource()->reallocate(ptr, detail::size_for<T>(oldnum), detail::size_for<T>(newnum), alignment);
        T* mem = static_cast< T* >(vmem);
        return mem;
    }

};





Add Discussion as Guest

Log in