4#include "../Foundation/Compiler.h"
5#ifndef SC_EXPORT_LIBRARY_MEMORY
6#define SC_EXPORT_LIBRARY_MEMORY 0
8#define SC_MEMORY_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_MEMORY)
10#include "../Foundation/PrimitiveTypes.h"
16struct SC_MEMORY_EXPORT Memory;
17struct SC_MEMORY_EXPORT MemoryAllocator;
18struct SC_MEMORY_EXPORT FixedAllocator;
30 static void*
allocate(
size_t numBytes,
size_t alignment);
56 template <
typename T,
typename... U>
59 void* rawMemory =
allocate(
nullptr,
sizeof(T),
alignof(T));
63 const size_t pointerAlignment =
sizeof(
void*);
64 const size_t address =
reinterpret_cast<size_t>(rawMemory);
65 if ((address & (pointerAlignment - 1)) == 0 and (
sizeof(T) & (pointerAlignment - 1)) == 0)
67 zsetcap(rawMemory, rawMemory,
sizeof(T));
70 T* t =
reinterpret_cast<T*
>(rawMemory);
82 void*
allocate(
const void* owner,
size_t numBytes,
size_t alignment)
102 if (memory !=
nullptr)
114 virtual void*
allocateImpl(
const void* owner,
size_t numBytes,
size_t alignment) = 0;
130 const void* data()
const {
return memory; }
132 size_t size()
const {
return position; }
133 size_t capacity()
const {
return capacityBytes; }
137 void* memory =
nullptr;
138 size_t capacityBytes = 0;
140 void* lastAllocation =
nullptr;
141 size_t lastAllocatedSize = 0;
144 virtual void*
allocateImpl(
const void* owner,
size_t numBytes,
size_t alignment)
override;
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition Compiler.h:282
A MemoryAllocator implementation using a finite slice of memory.
Definition Memory.h:127
virtual void * reallocateImpl(void *memory, size_t numBytes) override
Re-allocate virtual function to be reimplemented.
virtual void * allocateImpl(const void *owner, size_t numBytes, size_t alignment) override
Allocate virtual function to be reimplemented.
virtual void releaseImpl(void *memory) override
Release virtual function to be reimplemented.
Holds Statistics about how many allocations/release have been issued.
Definition Memory.h:48
size_t numAllocate
How many times MemoryAllocator::allocate has been called.
Definition Memory.h:49
size_t numRelease
How many times MemoryAllocator::release has been called.
Definition Memory.h:51
size_t numReallocate
How many times MemoryAllocator::reallocate has been called.
Definition Memory.h:50
Customizable functions to allocate, reallocate and deallocate memory.
Definition Memory.h:45
virtual void releaseImpl(void *memory)=0
Release virtual function to be reimplemented.
void release(void *memory)
Free memory allocated by MemoryAllocator::allocate and / or reallocated by MemoryAllocator::reallocat...
Definition Memory.h:100
Statistics statistics
Holds statistics about how many allocations/release have been issued.
Definition Memory.h:53
void * allocate(const void *owner, size_t numBytes, size_t alignment)
Allocates numBytes bytes of memory.
Definition Memory.h:82
virtual void * allocateImpl(const void *owner, size_t numBytes, size_t alignment)=0
Allocate virtual function to be reimplemented.
void * reallocate(void *memory, size_t numBytes)
Change size of already allocated memory block.
Definition Memory.h:92
virtual void * reallocateImpl(void *memory, size_t numBytes)=0
Re-allocate virtual function to be reimplemented.
T * create(U &&... u)
Allocate and construct an object of type T using this allocator.
Definition Memory.h:57
Centralized functions to allocate, reallocate and deallocate memory.
Definition Memory.h:25
static void * allocate(size_t numBytes, size_t alignment)
Allocates numBytes bytes of memory.
static void release(void *memory)
Free memory allocated by Memory::allocate and / or reallocated by Memory::reallocate.
static void * reallocate(void *memory, size_t numBytes)
Change size of already allocated memory block.