15 #include <vsg/core/MemorySlots.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_LAST = ALLOCATOR_AFFINITY_NODES + 1
43 Allocator(std::unique_ptr<Allocator> in_nestedAllocator = {});
51 virtual void*
allocate(std::size_t size, AllocatorAffinity allocatorAffinity = ALLOCATOR_AFFINITY_OBJECTS);
69 virtual void report(std::ostream& out)
const;
71 AllocatorType allocatorType = ALLOCATOR_TYPE_VSG_ALLOCATOR;
72 AllocatorType memoryBlocksAllocatorType = ALLOCATOR_TYPE_NEW_DELETE;
73 int memoryTracking = MEMORY_TRACKING_DEFAULT;
80 MemoryBlock(
size_t blockSize,
int memoryTracking, AllocatorType in_allocatorType);
83 void* allocate(std::size_t size);
84 bool deallocate(
void* ptr, std::size_t size);
87 const AllocatorType allocatorType;
88 uint8_t* memory =
nullptr;
96 std::map<void*, std::shared_ptr<MemoryBlock>> memoryBlocks;
97 std::shared_ptr<MemoryBlock> latestMemoryBlock;
102 void* allocate(std::size_t size);
103 bool deallocate(
void* ptr, std::size_t size);
105 size_t deleteEmptyMemoryBlocks();
106 size_t totalAvailableSize()
const;
107 size_t totalReservedSize()
const;
108 size_t totalMemorySize()
const;
111 MemoryBlocks* getMemoryBlocks(AllocatorAffinity allocatorAffinity);
113 MemoryBlocks* getOrCreateMemoryBlocks(AllocatorAffinity allocatorAffinity,
const std::string& name,
size_t blockSize);
115 void setBlockSize(AllocatorAffinity allocatorAffinity,
size_t blockSize);
117 mutable std::mutex mutex;
119 double allocationTime = 0.0;
120 double deallocationTime = 0.0;
124 std::unique_ptr<Allocator> nestedAllocator;
126 std::vector<std::unique_ptr<MemoryBlocks>> allocatorMemoryBlocks;
130 extern VSG_DECLSPEC
void* allocate(std::size_t size, AllocatorAffinity allocatorAffinity = ALLOCATOR_AFFINITY_OBJECTS);
133 extern VSG_DECLSPEC
void deallocate(
void* ptr, std::size_t size = 0);
139 using value_type = T;
145 value_type* allocate(std::size_t n)
147 return static_cast<value_type*
>(vsg::allocate(n *
sizeof(value_type), vsg::ALLOCATOR_AFFINITY_NODES));
150 void deallocate(value_type* ptr, std::size_t n)
152 vsg::deallocate(ptr, n *
sizeof(value_type));
156 template<
class T,
class U>
159 template<
class T,
class U>
160 bool operator!=(
const allocator_affinity_nodes<T>&,
const allocator_affinity_nodes<U>&) {
return false; }
Definition: Allocator.h:41
void setMemoryTracking(int mt)
set the MemoryTracking member of the vsg::Allocator and all the MemoryBlocks that it manages.
virtual bool deallocate(void *ptr, std::size_t size)
deallocate, returning data to pool.
virtual size_t totalMemorySize() const
return the total memory size of allocated MemoryBlocks
virtual size_t deleteEmptyMemoryBlocks()
delete any MemoryBlock that are empty
virtual void * allocate(std::size_t size, AllocatorAffinity allocatorAffinity=ALLOCATOR_AFFINITY_OBJECTS)
allocate from the pool of memory blocks, or allocate from a new memory block
virtual void report(std::ostream &out) const
report stats about blocks of memory allocated.
virtual size_t totalAvailableSize() const
return the total available size of allocated MemoryBlocks
static std::unique_ptr< Allocator > & instance()
Allocator singleton.
virtual size_t totalReservedSize() const
return the total reserved size of allocated MemoryBlocks
Definition: MemorySlots.h:36
Definition: Allocator.h:79
Definition: Allocator.h:92
std container adapter for allocating with MEMORY_AFFINITY_NODES
Definition: Allocator.h:138