Struct MemoryResource

Synopsis

#include <src/c4/memory_resource.hpp>

struct MemoryResource

Description

c++-style allocation

C++17-style memory_resource base class. See http://en.cppreference.com/w/cpp/experimental/memory_resource

Mentioned in

Inheritance

Decsendents: MemoryResourceCounts, DerivedMemoryResource, MemoryResourceMalloc

Summary

~MemoryResource
allocate
reallocate
deallocate

Source

Lines 117-147 in src/c4/memory_resource.hpp.

struct MemoryResource
{
    const char *name = nullptr;
    virtual ~MemoryResource() {}

    void* allocate(size_t sz, size_t alignment=alignof(max_align_t), void *hint=nullptr)
    {
        void *mem = this->do_allocate(sz, alignment, hint);
        C4_CHECK_MSG(mem != nullptr, "could not allocate %lu bytes", sz);
        return mem;
    }

    void* reallocate(void* ptr, size_t oldsz, size_t newsz, size_t alignment=alignof(max_align_t))
    {
        void *mem = this->do_reallocate(ptr, oldsz, newsz, alignment);
        C4_CHECK_MSG(mem != nullptr, "could not reallocate from %lu to %lu bytes", oldsz, newsz);
        return mem;
    }

    void deallocate(void* ptr, size_t sz, size_t alignment = alignof(max_align_t))
    {
        this->do_deallocate(ptr, sz, alignment);
    }

protected:

    virtual void* do_allocate(size_t sz, size_t alignment, void* hint) = 0;
    virtual void* do_reallocate(void* ptr, size_t oldsz, size_t newsz, size_t alignment) = 0;
    virtual void  do_deallocate(void* ptr, size_t sz, size_t alignment) = 0;

};





Add Discussion as Guest

Log in