4#include "../Common/CompilerMacrosExport.h"
5#include "../Common/CompilerMacrosType.h"
6#ifndef SC_EXPORT_LIBRARY_MEMORY
7#define SC_EXPORT_LIBRARY_MEMORY 0
9#define SC_MEMORY_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_MEMORY)
11#include "../Common/Assert.h"
12#include "../Common/CompilerMove.h"
13#include "../Common/PlacementNew.h"
14#include "../Common/PrimitiveDefinitions.h"
20SC_DECLARE_ASSERT_PROVIDER(MemoryAssert, SC_MEMORY_EXPORT);
22#define SC_MEMORY_ASSERT_RELEASE(e) SC_ASSERT_PROVIDER_RELEASE(SC::MemoryAssert, e)
23#define SC_MEMORY_ASSERT_DEBUG(e) SC_ASSERT_PROVIDER_DEBUG(SC::MemoryAssert, e)
24#define SC_MEMORY_TRUST_RESULT(expression) SC_MEMORY_ASSERT_RELEASE(expression)
26struct SC_MEMORY_EXPORT Memory;
27struct SC_MEMORY_EXPORT MemoryAllocator;
28struct SC_MEMORY_EXPORT FixedAllocator;
40 static void*
allocate(
size_t numBytes,
size_t alignment);
53 static void move(
void* dst,
const void* src,
size_t numBytes);
56 static void set(
void* dst,
int c,
size_t numBytes);
59 static void copy(
void* dst,
const void* src,
size_t numBytes);
75 template <
typename T,
typename... U>
78 void* rawMemory =
allocate(
nullptr,
sizeof(T),
alignof(T));
82 const size_t pointerAlignment =
sizeof(
void*);
83 const size_t address =
reinterpret_cast<size_t>(rawMemory);
84 if ((address & (pointerAlignment - 1)) == 0 and (
sizeof(T) & (pointerAlignment - 1)) == 0)
86 zsetcap(rawMemory, rawMemory,
sizeof(T));
89 T* t =
reinterpret_cast<T*
>(rawMemory);
90 placementNew(*t, forward<U>(u)...);
101 void*
allocate(
const void* owner,
size_t numBytes,
size_t alignment)
121 if (memory !=
nullptr)
133 virtual void*
allocateImpl(
const void* owner,
size_t numBytes,
size_t alignment) = 0;
149 const void* data()
const {
return memory; }
151 size_t size()
const {
return position; }
152 size_t capacity()
const {
return capacityBytes; }
156 void* memory =
nullptr;
157 size_t capacityBytes = 0;
159 void* lastAllocation =
nullptr;
160 size_t lastAllocatedSize = 0;
163 virtual void*
allocateImpl(
const void* owner,
size_t numBytes,
size_t alignment)
override;
A MemoryAllocator implementation using a finite slice of memory.
Definition Memory.h:146
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:67
size_t numAllocate
How many times MemoryAllocator::allocate has been called.
Definition Memory.h:68
size_t numRelease
How many times MemoryAllocator::release has been called.
Definition Memory.h:70
size_t numReallocate
How many times MemoryAllocator::reallocate has been called.
Definition Memory.h:69
Customizable functions to allocate, reallocate and deallocate memory.
Definition Memory.h:64
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:119
Statistics statistics
Holds statistics about how many allocations/release have been issued.
Definition Memory.h:72
void * allocate(const void *owner, size_t numBytes, size_t alignment)
Allocates numBytes bytes of memory.
Definition Memory.h:101
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:111
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:76
Centralized functions to allocate, reallocate and deallocate memory.
Definition Memory.h:35
static void * allocate(size_t numBytes, size_t alignment)
Allocates numBytes bytes of memory.
static void set(void *dst, int c, size_t numBytes)
Set numBytes bytes of memory at dst to the value c.
static void move(void *dst, const void *src, size_t numBytes)
Move numBytes bytes of memory from src to dst. The memory areas may overlap.
static void copy(void *dst, const void *src, size_t numBytes)
Copy numBytes bytes of memory from src to dst. The memory areas must not overlap.
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.