2#ifndef MEMORY_POSIXMEMORY_H
3#define MEMORY_POSIXMEMORY_H
23#if NEBULA_MEMORY_STATS
30#define StackAlloc(size) alloca(size);
43 int err = posix_memalign(&allocPtr, align, size);
46 explicit_bzero(allocPtr,size);
49 #if NEBULA_MEMORY_STATS
50 SIZE_T s = HeapSize(
Heaps[heapType], 0, allocPtr);
67 #if NEBULA_MEMORY_STATS
68 SIZE_T oldSize = HeapSize(
Heaps[heapType], 0, ptr);
70 void* allocPtr = realloc(ptr, size);
71 #if NEBULA_MEMORY_STATS
72 SIZE_T newSize = HeapSize(
Heaps[heapType], 0, allocPtr);
90 #if NEBULA_MEMORY_STATS
94 #if NEBULA_MEMORY_STATS
99 #if NEBULA_MEMORY_STATS
114 void* ret = mmap(
nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, 0, 0);
125 auto ret = madvise(ptr, size, MADV_DONTNEED);
127 ret = mprotect(ptr, size, PROT_NONE);
137 auto ret = mprotect(ptr, size, PROT_READ | PROT_WRITE);
138 explicit_bzero(ptr, size);
148 auto ret = madvise(ptr, size, MADV_DONTNEED);
150 ret = munmap(ptr, size);
159Copy(
const void* from,
void* to,
size_t numBytes)
166 memcpy(to, from, numBytes);
175Move(
const void* from,
void* to,
size_t numBytes)
182 memmove(to, from, numBytes);
199 memmove((
void*)to, (
const void*)from, numElements *
sizeof(T));
216 memcpy(to, from, numElements *
sizeof(T));
238Clear(
void* ptr,
size_t numBytes)
240 memset(ptr, 0, numBytes);
248Fill(
void* ptr,
size_t numBytes,
unsigned char value)
250 memset(ptr, value, numBytes);
262 size_t len = (
unsigned int) strlen(from) + 1;
273IsOverlapping(
const unsigned char* srcPtr,
size_t srcSize,
const unsigned char* dstPtr,
size_t dstSize)
275 if (srcPtr == dstPtr)
279 else if (srcPtr > dstPtr)
281 return (srcPtr + srcSize) > dstPtr;
285 return (dstPtr + dstSize) > srcPtr;
294struct TotalMemoryStatus
302inline TotalMemoryStatus
306 MEMORYSTATUS stats = { NULL };
307 GlobalMemoryStatus(&stats);
308 TotalMemoryStatus result;
309 result.totalPhysical = (
unsigned int) stats.dwTotalPhys;
310 result.availPhysical = (
unsigned int) stats.dwAvailPhys;
311 result.totalVirtual = (
unsigned int) stats.dwTotalVirtual;
312 result.availVirtual = (
unsigned int) stats.dwAvailVirtual;
315 TotalMemoryStatus result;
326#if NEBULA_MEMORY_STATS
327extern 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:175
__forceinline void FreeVirtual(void *ptr, size_t size)
free virtual memory
Definition posixmemory.h:146
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:135
__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:209
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:112
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:192
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:123
__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:227
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.
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