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#if (__WIN32__)
16#elif ( __OSX__ || __APPLE__ || __linux__ )
18#else
19#error "UNKNOWN PLATFORM"
20#endif
21
22//------------------------------------------------------------------------------
25template<typename TYPE>
26TYPE*
27ArrayAlloc(size_t size)
28{
29 TYPE* buffer = (TYPE*)Memory::Alloc(Memory::ObjectArrayHeap, size * sizeof(TYPE));
30 if constexpr (!std::is_trivially_constructible<TYPE>::value)
31 {
32 for (size_t i = 0; i < size; ++i)
33 {
34 ::new( &buffer[i] ) TYPE;
35 }
36 }
37 return buffer;
38}
39
40//------------------------------------------------------------------------------
43template<typename TYPE>
44TYPE*
45ArrayAllocStack(size_t size)
46{
47 TYPE* buffer = (TYPE*)StackAlloc(size * sizeof(TYPE));
48 if constexpr (!std::is_trivially_constructible<TYPE>::value)
49 {
50 for (size_t i = 0; i < size; ++i)
51 {
52 ::new(&buffer[i]) TYPE;
53 }
54 }
55 return buffer;
56}
57
58//------------------------------------------------------------------------------
61template<typename TYPE>
62void
63ArrayFree(size_t size, TYPE* buffer)
64{
65 if constexpr (!std::is_trivially_destructible<TYPE>::value)
66 {
67 for (size_t i = 0; i < size; ++i)
68 {
69 buffer[i].~TYPE();
70 }
71 }
73}
74
75//------------------------------------------------------------------------------
78template<typename TYPE>
79void
80ArrayFreeStack(size_t size, TYPE* buffer)
81{
82 if constexpr (!std::is_trivially_destructible<TYPE>::value)
83 {
84 for (size_t i = 0; i < size; ++i)
85 {
86 buffer[i].~TYPE();
87 }
88 }
89 StackFree((void*)buffer);
90}
Nebula compiler specific defines and configuration.
void ArrayFreeStack(size_t size, TYPE *buffer)
Definition memory.h:80
TYPE * ArrayAllocStack(size_t size)
Definition memory.h:45
void ArrayFree(size_t size, TYPE *buffer)
Definition memory.h:63
TYPE * ArrayAlloc(size_t size)
Definition memory.h:27
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.
#define StackFree(ptr)
Definition posixmemory.h:31
#define StackAlloc(size)
Definition posixmemory.h:30
Memory subsystem features for win32.