Nebula
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//------------------------------------------------------------------------------
18constexpr uint64_t
19operator"" _KB(const unsigned long long val)
20{
21 return val * 1024;
22}
23
24//------------------------------------------------------------------------------
27constexpr uint64_t
28operator"" _MB(const unsigned long long val)
29{
30 return val * 1024 * 1024;
31}
32
33//------------------------------------------------------------------------------
36constexpr uint64_t
37operator"" _GB(const unsigned long long val)
38{
39 return val * 1024 * 1024 * 1024;
40}
41
42#if (__WIN32__)
44#elif ( __OSX__ || __APPLE__ || __linux__ )
46#else
47#error "UNKNOWN PLATFORM"
48#endif
49
51{
54
55 void Realloc(size_t numBytes);
56
57 char* heap;
58 size_t iterator;
59 size_t capacity;
60};
61
63
64//------------------------------------------------------------------------------
67template<typename TYPE>
68TYPE*
69ArrayAlloc(size_t size)
70{
71 TYPE* buffer = (TYPE*)Memory::Alloc(Memory::ObjectArrayHeap, size * sizeof(TYPE));
72 if constexpr (!std::is_trivially_constructible<TYPE>::value)
73 {
74 for (size_t i = 0; i < size; ++i)
75 {
76 ::new( &buffer[i] ) TYPE;
77 }
78 }
79 return buffer;
80}
81
82//------------------------------------------------------------------------------
85template<typename TYPE>
86TYPE*
87ArrayAllocStack(size_t size)
88{
89 TYPE* buffer = (TYPE*)(N_ThreadLocalMiniHeap.heap + N_ThreadLocalMiniHeap.iterator);
90 N_ThreadLocalMiniHeap.iterator += size * sizeof(TYPE);
91
92 // Bounds check. This can never be disabled, as we might go OOB, which can
93 // cause buffer overflows and other security issues.
94 if (N_ThreadLocalMiniHeap.iterator >= N_ThreadLocalMiniHeap.capacity)
95 {
96 // If you run into this error, you're using too much stack memory.
97 // Consider using a separate allocator!
98 n_error("ArrayAllocStack is out of bounds!");
99 return nullptr;
100 }
101
102 if constexpr (!std::is_trivially_constructible<TYPE>::value)
103 {
104 for (size_t i = 0; i < size; ++i)
105 {
106 ::new(&buffer[i]) TYPE;
107 }
108 }
109 return buffer;
110}
111
112//------------------------------------------------------------------------------
115template<typename TYPE>
116void
117ArrayFree(size_t size, TYPE* buffer)
118{
119 if constexpr (!std::is_trivially_destructible<TYPE>::value)
120 {
121 for (size_t i = 0; i < size; ++i)
122 {
123 buffer[i].~TYPE();
124 }
125 }
127}
128
129//------------------------------------------------------------------------------
132template<typename TYPE>
133void
134ArrayFreeStack(size_t size, TYPE* buffer)
135{
136 char* topPtr = (N_ThreadLocalMiniHeap.heap + N_ThreadLocalMiniHeap.iterator - size * sizeof(TYPE));
137 n_assert(buffer == (TYPE*)topPtr);
138 if constexpr (!std::is_trivially_destructible<TYPE>::value)
139 {
140 for (size_t i = 0; i < size; ++i)
141 {
142 buffer[i].~TYPE();
143 }
144 }
145 N_ThreadLocalMiniHeap.iterator -= size * sizeof(TYPE);
146}
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:134
TYPE * ArrayAllocStack(size_t size)
Definition memory.h:87
void ArrayFree(size_t size, TYPE *buffer)
Definition memory.h:117
TYPE * ArrayAlloc(size_t size)
Definition memory.h:69
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:51
void Realloc(size_t numBytes)
Definition memory.cc:34
char * heap
Definition memory.h:57
~ThreadLocalMiniHeap()
Definition memory.cc:22
size_t capacity
Definition memory.h:59
ThreadLocalMiniHeap()
Definition memory.cc:11
size_t iterator
Definition memory.h:58
Memory subsystem features for win32.