15#include <vsg/core/Export.h>
23 enum AllocatorType : uint8_t
25 ALLOCATOR_TYPE_NO_DELETE = 0,
26 ALLOCATOR_TYPE_NEW_DELETE,
27 ALLOCATOR_TYPE_MALLOC_FREE,
28 ALLOCATOR_TYPE_VSG_ALLOCATOR
31 enum AllocatorAffinity : uint32_t
33 ALLOCATOR_AFFINITY_OBJECTS,
34 ALLOCATOR_AFFINITY_DATA,
35 ALLOCATOR_AFFINITY_NODES,
36 ALLOCATOR_AFFINITY_PHYSICS,
37 ALLOCATOR_AFFINITY_LAST = ALLOCATOR_AFFINITY_PHYSICS + 1
44 explicit Allocator(
size_t in_defaultAlignment = 4) :
45 defaultAlignment(in_defaultAlignment) {}
46 explicit Allocator(std::unique_ptr<Allocator> in_nestedAllocator,
size_t in_defaultAlignment = 4) :
47 defaultAlignment(in_defaultAlignment), nestedAllocator(std::move(in_nestedAllocator)) {}
54 virtual void*
allocate(std::size_t size, AllocatorAffinity allocatorAffinity = ALLOCATOR_AFFINITY_OBJECTS) = 0;
57 virtual bool deallocate(
void* ptr, std::size_t size) = 0;
71 AllocatorType allocatorType = ALLOCATOR_TYPE_VSG_ALLOCATOR;
73 virtual void setBlockSize(AllocatorAffinity allocatorAffinity,
size_t blockSize) = 0;
76 virtual void report(std::ostream& out)
const = 0;
78 mutable std::mutex mutex;
79 size_t defaultAlignment = 4;
83 std::unique_ptr<Allocator> nestedAllocator;
87 extern VSG_DECLSPEC
void* allocate(std::size_t size, AllocatorAffinity allocatorAffinity = ALLOCATOR_AFFINITY_OBJECTS);
90 extern VSG_DECLSPEC
void deallocate(
void* ptr, std::size_t size = 0);
93 template<
typename T, vsg::AllocatorAffinity A>
108 value_type* allocate(std::size_t n)
110 return static_cast<value_type*
>(vsg::allocate(n *
sizeof(value_type), A));
113 void deallocate(value_type* ptr, std::size_t n)
115 vsg::deallocate(ptr, n *
sizeof(value_type));
119 template<
class T,
class U, vsg::AllocatorAffinity A>
120 bool operator==(
const allocator_affinity_adapter<T, A>&,
const allocator_affinity_adapter<U, A>&) {
return true; }
122 template<
class T,
class U, vsg::AllocatorAffinity A>
123 bool operator!=(
const allocator_affinity_adapter<T, A>&,
const allocator_affinity_adapter<U, A>&) {
return false; }
126 using allocator_affinity_objects = allocator_affinity_adapter<T, vsg::ALLOCATOR_AFFINITY_OBJECTS>;
128 using allocator_affinity_data = allocator_affinity_adapter<T, vsg::ALLOCATOR_AFFINITY_DATA>;
130 using allocator_affinity_nodes = allocator_affinity_adapter<T, vsg::ALLOCATOR_AFFINITY_NODES>;
132 using allocator_affinity_physics = allocator_affinity_adapter<T, vsg::ALLOCATOR_AFFINITY_PHYSICS>;
Definition Allocator.h:42
virtual size_t totalReservedSize() const =0
return the total reserved size of allocated MemoryBlocks
virtual size_t totalAvailableSize() const =0
return the total available size of allocated MemoryBlocks
virtual void report(std::ostream &out) const =0
report stats about blocks of memory allocated.
static std::unique_ptr< Allocator > & instance()
Allocator singleton.
virtual size_t deleteEmptyMemoryBlocks()=0
delete any MemoryBlock that are empty
virtual void * allocate(std::size_t size, AllocatorAffinity allocatorAffinity=ALLOCATOR_AFFINITY_OBJECTS)=0
allocate from the pool of memory blocks, or allocate from a new memory block
virtual bool deallocate(void *ptr, std::size_t size)=0
deallocate, returning data to pool.
virtual size_t totalMemorySize() const =0
return the total memory size of allocated MemoryBlocks
Definition Allocator.h:104
std container adapter for allocating with specific affinity
Definition Allocator.h:95