Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
Memory.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Compiler.h"
5#ifndef SC_EXPORT_LIBRARY_MEMORY
6#define SC_EXPORT_LIBRARY_MEMORY 0
7#endif
8#define SC_MEMORY_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_MEMORY)
9
10#include "../Foundation/PrimitiveTypes.h"
11namespace SC
12{
13struct SC_MEMORY_EXPORT Memory;
14struct SC_MEMORY_EXPORT MemoryAllocator;
15struct SC_MEMORY_EXPORT FixedAllocator;
16} // namespace SC
19
22{
27 static void* allocate(size_t numBytes, size_t alignment);
28
33 static void* reallocate(void* memory, size_t numBytes);
34
37 static void release(void* memory);
38};
39
42{
45 {
46 size_t numAllocate = 0;
47 size_t numReallocate = 0;
48 size_t numRelease = 0;
49 };
51
53 template <typename T, typename... U>
54 T* create(U&&... u)
55 {
56 T* t = reinterpret_cast<T*>(allocate(nullptr, sizeof(T), alignof(T)));
57 if (t)
58 {
59 placementNew(*t, forward<U>(u)...);
60 }
61 return t;
62 }
63
69 void* allocate(const void* owner, size_t numBytes, size_t alignment)
70 {
72 return allocateImpl(owner, numBytes, alignment);
73 }
74
79 void* reallocate(void* memory, size_t numBytes)
80 {
82 return reallocateImpl(memory, numBytes);
83 }
84
87 void release(void* memory)
88 {
89 if (memory != nullptr)
90 {
92 }
93 return releaseImpl(memory);
94 }
95
101 virtual void* allocateImpl(const void* owner, size_t numBytes, size_t alignment) = 0;
102
104 virtual void* reallocateImpl(void* memory, size_t numBytes) = 0;
105
107 virtual void releaseImpl(void* memory) = 0;
108
109 virtual ~MemoryAllocator() {}
110};
111
114{
115 FixedAllocator(void* memory, size_t capacityBytes);
116
117 const void* data() const { return memory; }
118
119 size_t size() const { return position; }
120 size_t capacity() const { return capacityBytes; }
121
122 protected:
123 // Not using a span here to avoid depending on Span<T> in this header
124 void* memory = nullptr;
125 size_t capacityBytes = 0;
126
127 void* lastAllocation = nullptr;
128 size_t lastAllocatedSize = 0;
129 size_t position = 0;
130
131 virtual void* allocateImpl(const void* owner, size_t numBytes, size_t alignment) override;
132 virtual void* reallocateImpl(void* memory, size_t numBytes) override;
133 virtual void releaseImpl(void* memory) override;
134};
135
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.