4#ifndef MEMORY_POSIXMEMORY_H
5#define MEMORY_POSIXMEMORY_H
24#if NEBULA_MEMORY_STATS
31#define StackAlloc(size) alloca(size);
35#define explicit_bzero bzero;
48 int err = posix_memalign(&allocPtr, align, size);
51 explicit_bzero(allocPtr,size);
54 #if NEBULA_MEMORY_STATS
55 SIZE_T s = HeapSize(
Heaps[heapType], 0, allocPtr);
72 #if NEBULA_MEMORY_STATS
73 SIZE_T oldSize = HeapSize(
Heaps[heapType], 0, ptr);
75 void* allocPtr = realloc(ptr, size);
76 #if NEBULA_MEMORY_STATS
77 SIZE_T newSize = HeapSize(
Heaps[heapType], 0, allocPtr);
95 #if NEBULA_MEMORY_STATS
99 #if NEBULA_MEMORY_STATS
100 size = HeapSize(
Heaps[heapType], 0, ptr);
104 #if NEBULA_MEMORY_STATS
119 void* ret = mmap(
nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, 0, 0);
130 auto ret = madvise(ptr, size, MADV_DONTNEED);
132 ret = mprotect(ptr, size, PROT_NONE);
142 auto ret = mprotect(ptr, size, PROT_READ | PROT_WRITE);
143 explicit_bzero(ptr, size);
153 auto ret = madvise(ptr, size, MADV_DONTNEED);
155 ret = munmap(ptr, size);
164Copy(
const void* from,
void* to,
size_t numBytes)
171 memcpy(to, from, numBytes);
180Move(
const void* from,
void* to,
size_t numBytes)
187 memmove(to, from, numBytes);
204 if constexpr (std::is_trivially_move_assignable<T>::value && std::is_trivially_move_constructible<T>::value)
206 memmove((
void*)to, (
const void*)from, numElements *
sizeof(T));
210 std::move(from, from + numElements, to);
228 if constexpr (std::is_trivially_copyable<T>::value)
230 memcpy(to, from, numElements *
sizeof(T));
234 std::copy(from, from + numElements, to);
256Clear(
void* ptr,
size_t numBytes)
258 memset(ptr, 0, numBytes);
266Fill(
void* ptr,
size_t numBytes,
unsigned char value)
268 memset(ptr, value, numBytes);
280 size_t len = (
unsigned int) strlen(from) + 1;
291IsOverlapping(
const unsigned char* srcPtr,
size_t srcSize,
const unsigned char* dstPtr,
size_t dstSize)
293 if (srcPtr == dstPtr)
297 else if (srcPtr > dstPtr)
299 return (srcPtr + srcSize) > dstPtr;
303 return (dstPtr + dstSize) > srcPtr;
324 MEMORYSTATUS stats = { NULL };
325 GlobalMemoryStatus(&stats);
327 result.totalPhysical = (
unsigned int) stats.dwTotalPhys;
328 result.availPhysical = (
unsigned int) stats.dwAvailPhys;
329 result.totalVirtual = (
unsigned int) stats.dwTotalVirtual;
330 result.availVirtual = (
unsigned int) stats.dwAvailVirtual;
344#if NEBULA_MEMORY_STATS
345extern bool Validate();
#define n_assert(exp)
Definition debug.h:50
Nebula compiler specific defines and configuration.
Definition arenaallocator.h:31
TotalMemoryStatus GetTotalMemoryStatus()
Get the system's total memory status.
Definition osxmemory.cc:201
__forceinline void Move(const void *from, void *to, size_t numBytes)
Move a chunk of memory, can handle overlapping regions.
Definition posixmemory.h:180
__forceinline void FreeVirtual(void *ptr, size_t size)
free virtual memory
Definition posixmemory.h:151
int volatile TotalAllocSize
Definition win32memory.cc:17
void Copy(const void *from, void *to, size_t numBytes)
Copy a chunk of memory (note the argument order is different from memcpy()!
Definition osxmemory.cc:213
int volatile TotalAllocCount
Definition win32memory.cc:16
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
__forceinline void CommitVirtual(void *ptr, size_t size)
commit virtual memory
Definition posixmemory.h:140
__forceinline void CopyElements(const T *from, T *to, size_t numElements)
Copy a chunk of memory (note the argument order is different from memcpy()!
Definition posixmemory.h:221
int volatile HeapTypeAllocCount[NumHeapTypes]
Definition win32memory.cc:18
void Fill(void *ptr, size_t numBytes, unsigned char value)
Fill memory with a specific byte.
Definition osxmemory.cc:239
int volatile HeapTypeAllocSize[NumHeapTypes]
Definition win32memory.cc:19
void Free(HeapType heapType, void *ptr)
Free a block of memory.
Definition osxmemory.cc:136
void * Realloc(HeapType heapType, void *ptr, size_t size)
Re-Allocate a block of memory from one of the global heaps.
Definition osxmemory.cc:99
__forceinline void * AllocVirtual(size_t size)
allocate a range of virtual memory space
Definition posixmemory.h:117
char * DuplicateCString(const char *from)
Duplicate a 0-terminated string, this method should no longer be used!
Definition osxmemory.cc:166
HeapType
Heap types are defined here.
Definition osxmemoryconfig.h:25
@ NumHeapTypes
Definition osxmemoryconfig.h:36
@ StringDataHeap
Definition osxmemoryconfig.h:31
__forceinline void MoveElements(const T *from, T *to, size_t numElements)
Move a chunk of memory, can handle overlapping regions.
Definition posixmemory.h:197
bool IsOverlapping(const unsigned char *srcPtr, size_t srcSize, const unsigned char *dstPtr, size_t dstSize)
Test if 2 areas of memory areas are overlapping.
Definition osxmemory.cc:180
__forceinline void DecommitVirtual(void *ptr, size_t size)
decommit virtual memory
Definition posixmemory.h:128
__forceinline void CopyToGraphicsMemory(const void *from, void *to, size_t numBytes)
Copy data from a system memory buffer to graphics resource memory.
Definition posixmemory.h:245
void Clear(void *ptr, size_t numBytes)
Overwrite a chunk of memory with 0's.
Definition osxmemory.cc:229
malloc_zone_t * Heaps[NumHeapTypes]
Heap pointers are defined here.
Definition osxmemoryconfig.cc:12
Math::float2 size
Definition histogramcontext.cc:35
int Decrement(int volatile *var)
interlocked decrement, return result
Definition gccinterlocked.cc:157
int Add(int volatile *var, int add)
interlocked add
Definition gccinterlocked.cc:22
int Increment(int volatile *var)
interlocked increment, return result
Definition gccinterlocked.cc:148
Central config file for memory setup on the Posix platform.
Get the system's total current memory, this does not only include Nebula's memory allocations but the...
Definition osxmemory.h:52
unsigned int totalVirtual
Definition osxmemory.h:55
unsigned int totalPhysical
Definition osxmemory.h:53
unsigned int availVirtual
Definition osxmemory.h:56
unsigned int availPhysical
Definition osxmemory.h:54