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 "../Common/CompilerMacrosExport.h"
5#include "../Common/CompilerMacrosType.h"
6#ifndef SC_EXPORT_LIBRARY_MEMORY
7#define SC_EXPORT_LIBRARY_MEMORY 0
8#endif
9#define SC_MEMORY_EXPORT SC_COMPILER_LIBRARY_EXPORT(SC_EXPORT_LIBRARY_MEMORY)
10
11#include "../Common/Assert.h"
12#include "../Common/CompilerMove.h"
13#include "../Common/PlacementNew.h"
14#include "../Common/PrimitiveDefinitions.h"
15#if SC_COMPILER_FILC
16#include <stdfil.h>
17#endif
18namespace SC
19{
20SC_DECLARE_ASSERT_PROVIDER(MemoryAssert, SC_MEMORY_EXPORT);
21
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)
25
26struct SC_MEMORY_EXPORT Memory;
27struct SC_MEMORY_EXPORT MemoryAllocator;
28struct SC_MEMORY_EXPORT FixedAllocator;
29} // namespace SC
32
35{
40 static void* allocate(size_t numBytes, size_t alignment);
41
46 static void* reallocate(void* memory, size_t numBytes);
47
50 static void release(void* memory);
51
53 static void move(void* dst, const void* src, size_t numBytes);
54
56 static void set(void* dst, int c, size_t numBytes);
57
59 static void copy(void* dst, const void* src, size_t numBytes);
60};
61
64{
67 {
68 size_t numAllocate = 0;
69 size_t numReallocate = 0;
70 size_t numRelease = 0;
71 };
73
75 template <typename T, typename... U>
76 T* create(U&&... u)
77 {
78 void* rawMemory = allocate(nullptr, sizeof(T), alignof(T));
79 if (rawMemory)
80 {
81#if SC_COMPILER_FILC
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)
85 {
86 zsetcap(rawMemory, rawMemory, sizeof(T));
87 }
88#endif
89 T* t = reinterpret_cast<T*>(rawMemory);
90 placementNew(*t, forward<U>(u)...);
91 return t;
92 }
93 return nullptr;
94 }
95
101 void* allocate(const void* owner, size_t numBytes, size_t alignment)
102 {
104 return allocateImpl(owner, numBytes, alignment);
105 }
106
111 void* reallocate(void* memory, size_t numBytes)
112 {
114 return reallocateImpl(memory, numBytes);
115 }
116
119 void release(void* memory)
120 {
121 if (memory != nullptr)
122 {
124 }
125 return releaseImpl(memory);
126 }
127
133 virtual void* allocateImpl(const void* owner, size_t numBytes, size_t alignment) = 0;
134
136 virtual void* reallocateImpl(void* memory, size_t numBytes) = 0;
137
139 virtual void releaseImpl(void* memory) = 0;
140
141 virtual ~MemoryAllocator() {}
142};
143
146{
147 FixedAllocator(void* memory, size_t capacityBytes);
148
149 const void* data() const { return memory; }
150
151 size_t size() const { return position; }
152 size_t capacity() const { return capacityBytes; }
153
154 protected:
155 // Not using a span here to avoid depending on Span<T> in this header
156 void* memory = nullptr;
157 size_t capacityBytes = 0;
158
159 void* lastAllocation = nullptr;
160 size_t lastAllocatedSize = 0;
161 size_t position = 0;
162
163 virtual void* allocateImpl(const void* owner, size_t numBytes, size_t alignment) override;
164 virtual void* reallocateImpl(void* memory, size_t numBytes) override;
165 virtual void releaseImpl(void* memory) override;
166};
167
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.