4#ifndef MEMORY_POSIXMEMORY_H
5#define MEMORY_POSIXMEMORY_H
25#if NEBULA_MEMORY_STATS
32#define StackAlloc(size) alloca(size);
45 int err = posix_memalign(&allocPtr, align, size);
48 explicit_bzero(allocPtr,size);
51 #if NEBULA_MEMORY_STATS
52 SIZE_T s = HeapSize(
Heaps[heapType], 0, allocPtr);
69 #if NEBULA_MEMORY_STATS
70 SIZE_T oldSize = HeapSize(
Heaps[heapType], 0, ptr);
72 void* allocPtr = realloc(ptr, size);
73 #if NEBULA_MEMORY_STATS
74 SIZE_T newSize = HeapSize(
Heaps[heapType], 0, allocPtr);
92 #if NEBULA_MEMORY_STATS
96 #if NEBULA_MEMORY_STATS
101 #if NEBULA_MEMORY_STATS
116 void* ret = mmap(
nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, 0, 0);
127 auto ret = madvise(ptr, size, MADV_DONTNEED);
129 ret = mprotect(ptr, size, PROT_NONE);
139 auto ret = mprotect(ptr, size, PROT_READ | PROT_WRITE);
140 explicit_bzero(ptr, size);
150 auto ret = madvise(ptr, size, MADV_DONTNEED);
152 ret = munmap(ptr, size);
161Copy(
const void* from,
void* to,
size_t numBytes)
168 memcpy(to, from, numBytes);
177Move(
const void* from,
void* to,
size_t numBytes)
184 memmove(to, from, numBytes);
201 if constexpr (std::is_trivially_move_assignable<T>::value && std::is_trivially_move_constructible<T>::value)
203 memmove((
void*)to, (
const void*)from, numElements *
sizeof(T));
207 std::move(from, from + numElements, to);
225 if constexpr (std::is_trivially_copyable<T>::value)
227 memcpy(to, from, numElements *
sizeof(T));
231 std::copy(from, from + numElements, to);
253Clear(
void* ptr,
size_t numBytes)
255 memset(ptr, 0, numBytes);
263Fill(
void* ptr,
size_t numBytes,
unsigned char value)
265 memset(ptr, value, numBytes);
277 size_t len = (
unsigned int) strlen(from) + 1;
288IsOverlapping(
const unsigned char* srcPtr,
size_t srcSize,
const unsigned char* dstPtr,
size_t dstSize)
290 if (srcPtr == dstPtr)
294 else if (srcPtr > dstPtr)
296 return (srcPtr + srcSize) > dstPtr;
300 return (dstPtr + dstSize) > srcPtr;
321 MEMORYSTATUS stats = { NULL };
322 GlobalMemoryStatus(&stats);
324 result.totalPhysical = (
unsigned int) stats.dwTotalPhys;
325 result.availPhysical = (
unsigned int) stats.dwAvailPhys;
326 result.totalVirtual = (
unsigned int) stats.dwTotalVirtual;
327 result.availVirtual = (
unsigned int) stats.dwAvailVirtual;
341#if NEBULA_MEMORY_STATS
342extern 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:177
__forceinline void FreeVirtual(void *ptr, size_t size)
free virtual memory
Definition posixmemory.h:148
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:137
__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:218
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:114
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:194
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:125
__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:242
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:36
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