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"
13struct SC_MEMORY_EXPORT Memory;
14struct SC_MEMORY_EXPORT MemoryAllocator;
15struct SC_MEMORY_EXPORT FixedAllocator;
27 static void*
allocate(
size_t numBytes,
size_t alignment);
53 template <
typename T,
typename... U>
56 T* t =
reinterpret_cast<T*
>(
allocate(
nullptr,
sizeof(T),
alignof(T)));
69 void*
allocate(
const void* owner,
size_t numBytes,
size_t alignment)
89 if (memory !=
nullptr)
101 virtual void*
allocateImpl(
const void* owner,
size_t numBytes,
size_t alignment) = 0;
117 const void* data()
const {
return memory; }
119 size_t size()
const {
return position; }
120 size_t capacity()
const {
return capacityBytes; }
124 void* memory =
nullptr;
125 size_t capacityBytes = 0;
127 void* lastAllocation =
nullptr;
128 size_t lastAllocatedSize = 0;
131 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:277
A MemoryAllocator implementation using a finite slice of memory.
Definition Memory.h:114
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:45
size_t numAllocate
How many times MemoryAllocator::allocate has been called.
Definition Memory.h:46
size_t numRelease
How many times MemoryAllocator::release has been called.
Definition Memory.h:48
size_t numReallocate
How many times MemoryAllocator::reallocate has been called.
Definition Memory.h:47
Customizable functions to allocate, reallocate and deallocate memory.
Definition Memory.h:42
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:87
Statistics statistics
Holds statistics about how many allocations/release have been issued.
Definition Memory.h:50
void * allocate(const void *owner, size_t numBytes, size_t alignment)
Allocates numBytes bytes of memory.
Definition Memory.h:69
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:79
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:54
Centralized functions to allocate, reallocate and deallocate memory.
Definition Memory.h:22
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.