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"
11#if SC_COMPILER_FILC
12#include <stdfil.h>
13#endif
14namespace SC
15{
16struct SC_MEMORY_EXPORT Memory;
17struct SC_MEMORY_EXPORT MemoryAllocator;
18struct SC_MEMORY_EXPORT FixedAllocator;
19} // namespace SC
22
25{
30 static void* allocate(size_t numBytes, size_t alignment);
31
36 static void* reallocate(void* memory, size_t numBytes);
37
40 static void release(void* memory);
41};
42
45{
48 {
49 size_t numAllocate = 0;
50 size_t numReallocate = 0;
51 size_t numRelease = 0;
52 };
54
56 template <typename T, typename... U>
57 T* create(U&&... u)
58 {
59 void* rawMemory = allocate(nullptr, sizeof(T), alignof(T));
60 if (rawMemory)
61 {
62#if SC_COMPILER_FILC
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)
66 {
67 zsetcap(rawMemory, rawMemory, sizeof(T));
68 }
69#endif
70 T* t = reinterpret_cast<T*>(rawMemory);
71 placementNew(*t, forward<U>(u)...);
72 return t;
73 }
74 return nullptr;
75 }
76
82 void* allocate(const void* owner, size_t numBytes, size_t alignment)
83 {
85 return allocateImpl(owner, numBytes, alignment);
86 }
87
92 void* reallocate(void* memory, size_t numBytes)
93 {
95 return reallocateImpl(memory, numBytes);
96 }
97
100 void release(void* memory)
101 {
102 if (memory != nullptr)
103 {
105 }
106 return releaseImpl(memory);
107 }
108
114 virtual void* allocateImpl(const void* owner, size_t numBytes, size_t alignment) = 0;
115
117 virtual void* reallocateImpl(void* memory, size_t numBytes) = 0;
118
120 virtual void releaseImpl(void* memory) = 0;
121
122 virtual ~MemoryAllocator() {}
123};
124
127{
128 FixedAllocator(void* memory, size_t capacityBytes);
129
130 const void* data() const { return memory; }
131
132 size_t size() const { return position; }
133 size_t capacity() const { return capacityBytes; }
134
135 protected:
136 // Not using a span here to avoid depending on Span<T> in this header
137 void* memory = nullptr;
138 size_t capacityBytes = 0;
139
140 void* lastAllocation = nullptr;
141 size_t lastAllocatedSize = 0;
142 size_t position = 0;
143
144 virtual void* allocateImpl(const void* owner, size_t numBytes, size_t alignment) override;
145 virtual void* reallocateImpl(void* memory, size_t numBytes) override;
146 virtual void releaseImpl(void* memory) override;
147};
148
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.