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
49extern thread_local char ThreadLocalMiniHeap[];
50extern thread_local size_t ThreadLocalMiniHeapIterator;
51
52//------------------------------------------------------------------------------
55template<typename TYPE>
56TYPE*
57ArrayAlloc(size_t size)
58{
59 TYPE* buffer = (TYPE*)Memory::Alloc(Memory::ObjectArrayHeap, size * sizeof(TYPE));
60 if constexpr (!std::is_trivially_constructible<TYPE>::value)
61 {
62 for (size_t i = 0; i < size; ++i)
63 {
64 ::new( &buffer[i] ) TYPE;
65 }
66 }
67 return buffer;
68}
69
70//------------------------------------------------------------------------------
73template<typename TYPE>
74TYPE*
75ArrayAllocStack(size_t size)
76{
77 TYPE* buffer = (TYPE*)(ThreadLocalMiniHeap + ThreadLocalMiniHeapIterator);
78 ThreadLocalMiniHeapIterator += size * sizeof(TYPE);
79 if constexpr (!std::is_trivially_constructible<TYPE>::value)
80 {
81 for (size_t i = 0; i < size; ++i)
82 {
83 ::new(&buffer[i]) TYPE;
84 }
85 }
86 return buffer;
87}
88
89//------------------------------------------------------------------------------
92template<typename TYPE>
93void
94ArrayFree(size_t size, TYPE* buffer)
95{
96 if constexpr (!std::is_trivially_destructible<TYPE>::value)
97 {
98 for (size_t i = 0; i < size; ++i)
99 {
100 buffer[i].~TYPE();
101 }
102 }
104}
105
106//------------------------------------------------------------------------------
109template<typename TYPE>
110void
111ArrayFreeStack(size_t size, TYPE* buffer)
112{
113 char* topPtr = (ThreadLocalMiniHeap + ThreadLocalMiniHeapIterator - size * sizeof(TYPE));
114 n_assert(buffer == (TYPE*)topPtr);
115 if constexpr (!std::is_trivially_destructible<TYPE>::value)
116 {
117 for (size_t i = 0; i < size; ++i)
118 {
119 buffer[i].~TYPE();
120 }
121 }
122 ThreadLocalMiniHeapIterator -= size * sizeof(TYPE);
123}
#define n_assert(exp)
Definition debug.h:50
Nebula compiler specific defines and configuration.
thread_local size_t ThreadLocalMiniHeapIterator
Definition memory.cc:7
void ArrayFreeStack(size_t size, TYPE *buffer)
Definition memory.h:111
TYPE * ArrayAllocStack(size_t size)
Definition memory.h:75
void ArrayFree(size_t size, TYPE *buffer)
Definition memory.h:94
TYPE * ArrayAlloc(size_t size)
Definition memory.h:57
thread_local char ThreadLocalMiniHeap[]
Definition memory.cc:6
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.
Memory subsystem features for win32.