Nebula
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/config.h"
13
14//------------------------------------------------------------------------------
17constexpr uint64_t
18operator"" _KB(const unsigned long long val)
19{
20 return val * 1024;
21}
22
23//------------------------------------------------------------------------------
26constexpr uint64_t
27operator"" _MB(const unsigned long long val)
28{
29 return val * 1024 * 1024;
30}
31
32//------------------------------------------------------------------------------
35constexpr uint64_t
36operator"" _GB(const unsigned long long val)
37{
38 return val * 1024 * 1024 * 1024;
39}
40
41#if (__WIN32__)
43#elif ( __OSX__ || __APPLE__ || __linux__ )
45#else
46#error "UNKNOWN PLATFORM"
47#endif
48
50{
53
54 void Realloc(size_t numBytes);
55
56 char* heap;
57 size_t iterator;
58 size_t capacity;
59};
60
62
63//------------------------------------------------------------------------------
66template<typename TYPE>
67TYPE*
68ArrayAlloc(size_t size)
69{
70 TYPE* buffer = (TYPE*)Memory::Alloc(Memory::ObjectArrayHeap, size * sizeof(TYPE));
71 if constexpr (!std::is_trivially_constructible<TYPE>::value)
72 {
73 for (size_t i = 0; i < size; ++i)
74 {
75 ::new( &buffer[i] ) TYPE;
76 }
77 }
78 return buffer;
79}
80
81//------------------------------------------------------------------------------
84template<typename TYPE>
85TYPE*
86ArrayAllocStack(size_t size)
87{
88 TYPE* buffer = (TYPE*)(N_ThreadLocalMiniHeap.heap + N_ThreadLocalMiniHeap.iterator);
89 N_ThreadLocalMiniHeap.iterator += size * sizeof(TYPE);
90
91 // Bounds check. This can never be disabled, as we might go OOB, which can
92 // cause buffer overflows and other security issues.
93 if (N_ThreadLocalMiniHeap.iterator >= N_ThreadLocalMiniHeap.capacity)
94 {
95 // If you run into this error, you're using too much stack memory.
96 // Consider using a separate allocator!
97 n_error("ArrayAllocStack is out of bounds!");
98 return nullptr;
99 }
100
101 if constexpr (!std::is_trivially_constructible<TYPE>::value)
102 {
103 for (size_t i = 0; i < size; ++i)
104 {
105 ::new(&buffer[i]) TYPE;
106 }
107 }
108 return buffer;
109}
110
111//------------------------------------------------------------------------------
114template<typename TYPE>
115void
116ArrayFree(size_t size, TYPE* buffer)
117{
118 if constexpr (!std::is_trivially_destructible<TYPE>::value)
119 {
120 for (size_t i = 0; i < size; ++i)
121 {
122 buffer[i].~TYPE();
123 }
124 }
126}
127
128//------------------------------------------------------------------------------
131template<typename TYPE>
132void
133ArrayFreeStack(size_t size, TYPE* buffer)
134{
135 char* topPtr = (N_ThreadLocalMiniHeap.heap + N_ThreadLocalMiniHeap.iterator - size * sizeof(TYPE));
136 n_assert(buffer == (TYPE*)topPtr);
137 if constexpr (!std::is_trivially_destructible<TYPE>::value)
138 {
139 for (size_t i = 0; i < size; ++i)
140 {
141 buffer[i].~TYPE();
142 }
143 }
144 N_ThreadLocalMiniHeap.iterator -= size * sizeof(TYPE);
145}
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
#define n_assert(exp)
Definition debug.h:50
Nebula compiler specific defines and configuration.
thread_local ThreadLocalMiniHeap N_ThreadLocalMiniHeap
Definition memory.cc:6
void ArrayFreeStack(size_t size, TYPE *buffer)
Definition memory.h:133
TYPE * ArrayAllocStack(size_t size)
Definition memory.h:86
void ArrayFree(size_t size, TYPE *buffer)
Definition memory.h:116
TYPE * ArrayAlloc(size_t size)
Definition memory.h:68
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
void Free(HeapType heapType, void *ptr)
Free a block of memory.
Definition osxmemory.cc:136
@ ObjectArrayHeap
Definition osxmemoryconfig.h:28
Memory subsystem features for the Posix platform.
Definition memory.h:50
void Realloc(size_t numBytes)
Definition memory.cc:34
char * heap
Definition memory.h:56
~ThreadLocalMiniHeap()
Definition memory.cc:22
size_t capacity
Definition memory.h:58
ThreadLocalMiniHeap()
Definition memory.cc:11
size_t iterator
Definition memory.h:57
Memory subsystem features for win32.