Nebula
Toggle main menu visibility
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1
#pragma once
2
//------------------------------------------------------------------------------
12
#include "
core/config.h
"
13
#include "
core/types.h
"
14
15
namespace
Memory
16
{
17
//------------------------------------------------------------------------------
20
__forceinline
unsigned
int
21
align
(
unsigned
int
alignant,
unsigned
int
alignment)
22
{
23
return
(alignant + alignment - 1) & ~(alignment - 1);
24
}
25
26
//------------------------------------------------------------------------------
29
__forceinline
unsigned
int
30
align
(
int
alignant,
int
alignment)
31
{
32
return
((
unsigned
int
)alignant + (
unsigned
int
)alignment - 1) & ~((
unsigned
int)alignment - 1);
33
}
34
35
36
//------------------------------------------------------------------------------
39
__forceinline
size_t
40
align
(
size_t
alignant,
size_t
alignment)
41
{
42
return
(alignant + alignment - 1) & ~(alignment - 1);
43
}
44
45
//------------------------------------------------------------------------------
48
__forceinline uintptr_t
49
alignptr
(uintptr_t alignant, uintptr_t alignment)
50
{
51
return
(alignant + alignment - 1) & ~(alignment - 1);
52
}
53
54
//------------------------------------------------------------------------------
57
__forceinline
unsigned
int
58
align_down
(
unsigned
int
alignant,
unsigned
int
alignment)
59
{
60
return
(alignant / alignment * alignment);
61
}
62
63
64
//------------------------------------------------------------------------------
67
__forceinline
size_t
68
align_down
(
size_t
alignant,
size_t
alignment)
69
{
70
return
(alignant / alignment * alignment);
71
}
72
73
//------------------------------------------------------------------------------
76
__forceinline uintptr_t
77
align_downptr
(uintptr_t alignant, uintptr_t alignment)
78
{
79
return
(alignant / alignment * alignment);
80
}
81
}
82
83
//------------------------------------------------------------------------------
86
constexpr
uint64_t
87
operator
""
_KB(
const
unsigned
long
long
val)
88
{
89
return
val * 1024;
90
}
91
92
//------------------------------------------------------------------------------
95
constexpr
uint64_t
96
operator
""
_MB(
const
unsigned
long
long
val)
97
{
98
return
val * 1024 * 1024;
99
}
100
101
//------------------------------------------------------------------------------
104
constexpr
uint64_t
105
operator
""
_GB(
const
unsigned
long
long
val)
106
{
107
return
val * 1024 * 1024 * 1024;
108
}
109
110
#if (__WIN32__)
111
#include "
memory/win32/win32memory.h
"
112
#elif ( __OSX__ || __APPLE__ || __linux__ )
113
#include "
memory/posix/posixmemory.h
"
114
#else
115
#error "UNKNOWN PLATFORM"
116
#endif
117
118
struct
ThreadLocalMiniHeap
119
{
120
ThreadLocalMiniHeap
();
121
~ThreadLocalMiniHeap
();
122
123
void
Realloc
(
size_t
numBytes);
124
125
char
*
heap
;
126
size_t
iterator
;
127
size_t
capacity
;
128
};
129
130
extern
thread_local
ThreadLocalMiniHeap
N_ThreadLocalMiniHeap
;
131
132
//------------------------------------------------------------------------------
135
template
<
typename
TYPE>
136
TYPE*
137
ArrayAlloc
(
size_t
size)
138
{
139
TYPE* buffer = (TYPE*)
Memory::Alloc
(
Memory::ObjectArrayHeap
, size *
sizeof
(TYPE));
140
if
constexpr
(!std::is_trivially_constructible<TYPE>::value)
141
{
142
for
(
size_t
i = 0; i < size; ++i)
143
{
144
::new( &buffer[i] ) TYPE;
145
}
146
}
147
return
buffer;
148
}
149
150
//------------------------------------------------------------------------------
153
template
<
typename
TYPE>
154
TYPE*
155
ArrayAllocStack
(
size_t
size)
156
{
157
if
(size == 0)
return
nullptr
;
158
TYPE* buffer = (TYPE*)(
N_ThreadLocalMiniHeap
.heap +
N_ThreadLocalMiniHeap
.iterator);
159
const
size_t
bytes = size *
sizeof
(TYPE) + (
sizeof
(
void
*) - 1);
160
buffer = (TYPE*)
Memory::alignptr
((uintptr_t)buffer,
sizeof
(
void
*));
161
162
// Bounds check. This can never be disabled, as we might go OOB, which can
163
// cause buffer overflows and other security issues.
164
if
(
N_ThreadLocalMiniHeap
.iterator + bytes >=
N_ThreadLocalMiniHeap
.capacity)
165
{
166
// If you run into this error, you're using too much stack memory.
167
// Consider using a separate allocator!
168
n_error
(
"ArrayAllocStack is out of bounds!"
);
169
return
nullptr
;
170
}
171
172
if
constexpr
(!std::is_trivially_constructible<TYPE>::value)
173
{
174
for
(
size_t
i = 0; i < size; ++i)
175
{
176
::new(&buffer[i]) TYPE;
177
}
178
}
179
N_ThreadLocalMiniHeap
.iterator += bytes;
180
return
buffer;
181
}
182
183
//------------------------------------------------------------------------------
186
template
<
typename
TYPE>
187
void
188
ArrayFree
(
size_t
size, TYPE* buffer)
189
{
190
if
constexpr
(!std::is_trivially_destructible<TYPE>::value)
191
{
192
for
(
size_t
i = 0; i < size; ++i)
193
{
194
buffer[i].~TYPE();
195
}
196
}
197
Memory::Free
(
Memory::ObjectArrayHeap
, (
void
*)buffer);
198
}
199
200
//------------------------------------------------------------------------------
203
template
<
typename
TYPE>
204
void
205
ArrayFreeStack
(
size_t
size, TYPE* buffer)
206
{
207
if
(size == 0)
208
return
;
209
const
size_t
bytes = size *
sizeof
(TYPE) + (
sizeof
(
void
*) - 1);
210
char
* topPtr = (
N_ThreadLocalMiniHeap
.heap +
N_ThreadLocalMiniHeap
.iterator - bytes);
211
topPtr = (
char
*)
Memory::alignptr
((uintptr_t)topPtr,
sizeof
(
void
*));
212
n_assert
(buffer == (TYPE*)topPtr);
213
if
constexpr
(!std::is_trivially_destructible<TYPE>::value)
214
{
215
for
(
size_t
i = 0; i < size; ++i)
216
{
217
buffer[i].~TYPE();
218
}
219
}
220
N_ThreadLocalMiniHeap
.iterator -= bytes;
221
}
n_error
void __cdecl n_error(const char *msg,...)
This function is called when a serious situation is encountered which requires abortion of the applic...
Definition
debug.cc:138
n_assert
#define n_assert(exp)
Definition
debug.h:50
config.h
Nebula compiler specific defines and configuration.
N_ThreadLocalMiniHeap
ThreadLocalMiniHeap N_ThreadLocalMiniHeap
Definition
memory.cc:6
ArrayFreeStack
void ArrayFreeStack(size_t size, TYPE *buffer)
Definition
memory.h:205
ArrayAllocStack
TYPE * ArrayAllocStack(size_t size)
Definition
memory.h:155
ArrayFree
void ArrayFree(size_t size, TYPE *buffer)
Definition
memory.h:188
ArrayAlloc
TYPE * ArrayAlloc(size_t size)
Definition
memory.h:137
Memory
Definition
arenaallocator.h:31
Memory::align
__forceinline unsigned int align(unsigned int alignant, unsigned int alignment)
Definition
memory.h:21
Memory::align_downptr
__forceinline uintptr_t align_downptr(uintptr_t alignant, uintptr_t alignment)
Definition
memory.h:77
Memory::Alloc
void * Alloc(HeapType heapType, size_t size, size_t alignment)
Allocate a block of memory from one of the global heaps.
Definition
osxmemory.cc:56
Memory::Free
void Free(HeapType heapType, void *ptr)
Free a block of memory.
Definition
osxmemory.cc:136
Memory::align_down
__forceinline unsigned int align_down(unsigned int alignant, unsigned int alignment)
Definition
memory.h:58
Memory::ObjectArrayHeap
@ ObjectArrayHeap
Definition
osxmemoryconfig.h:28
Memory::alignptr
__forceinline uintptr_t alignptr(uintptr_t alignant, uintptr_t alignment)
Definition
memory.h:49
posixmemory.h
Memory subsystem features for the Posix platform.
ThreadLocalMiniHeap
Definition
memory.h:119
ThreadLocalMiniHeap::Realloc
void Realloc(size_t numBytes)
Definition
memory.cc:34
ThreadLocalMiniHeap::heap
char * heap
Definition
memory.h:125
ThreadLocalMiniHeap::~ThreadLocalMiniHeap
~ThreadLocalMiniHeap()
Definition
memory.cc:22
ThreadLocalMiniHeap::capacity
size_t capacity
Definition
memory.h:127
ThreadLocalMiniHeap::ThreadLocalMiniHeap
ThreadLocalMiniHeap()
Definition
memory.cc:11
ThreadLocalMiniHeap::iterator
size_t iterator
Definition
memory.h:126
types.h
win32memory.h
Memory subsystem features for win32.
code
foundation
memory
memory.h
Generated on
for Nebula. Dark theme by
Tilen Majerle
. All rights reserved.