Nebula
Loading...
Searching...
No Matches
posixmemory.h
Go to the documentation of this file.
1#pragma once
2#ifndef MEMORY_POSIXMEMORY_H
3#define MEMORY_POSIXMEMORY_H
4//------------------------------------------------------------------------------
13#include "core/config.h"
14#include "core/debug.h"
17#include <malloc.h>
18#include <string.h>
19#include <sys/mman.h>
20
21namespace Memory
22{
23#if NEBULA_MEMORY_STATS
24extern int volatile TotalAllocCount;
25extern int volatile TotalAllocSize;
26extern int volatile HeapTypeAllocCount[NumHeapTypes];
27extern int volatile HeapTypeAllocSize[NumHeapTypes];
28#endif
29
30#define StackAlloc(size) alloca(size);
31#define StackFree(ptr)
32
33//------------------------------------------------------------------------------
37__forceinline void*
38Alloc(HeapType heapType, size_t size, size_t align = 16)
39{
40 n_assert(heapType < NumHeapTypes);
41 void* allocPtr = 0;
42 {
43 int err = posix_memalign(&allocPtr, align, size);
44 n_assert(err == 0);
45 #if NEBULA_DEBUG
46 explicit_bzero(allocPtr,size);
47 #endif
48 }
49 #if NEBULA_MEMORY_STATS
50 SIZE_T s = HeapSize(Heaps[heapType], 0, allocPtr);
55 #endif
56 return allocPtr;
57}
58
59//------------------------------------------------------------------------------
63__forceinline void*
64Realloc(HeapType heapType, void* ptr, size_t size)
65{
66 n_assert(heapType < NumHeapTypes);
67 #if NEBULA_MEMORY_STATS
68 SIZE_T oldSize = HeapSize(Heaps[heapType], 0, ptr);
69 #endif
70 void* allocPtr = realloc(ptr, size);
71 #if NEBULA_MEMORY_STATS
72 SIZE_T newSize = HeapSize(Heaps[heapType], 0, allocPtr);
73 Threading::Interlocked::Add(TotalAllocSize, int(newSize - oldSize));
74 Threading::Interlocked::Add(HeapTypeAllocSize[heapType], int(newSize - oldSize));
75 #endif
76 return allocPtr;
77}
78
79//------------------------------------------------------------------------------
83__forceinline void
84Free(HeapType heapType, void* ptr)
85{
86 // D3DX on the 360 likes to call the delete operator with a 0 pointer
87 if (0 != ptr)
88 {
89 n_assert(heapType < NumHeapTypes);
90 #if NEBULA_MEMORY_STATS
91 SIZE_T size = 0;
92 #endif
93 {
94 #if NEBULA_MEMORY_STATS
95 size = HeapSize(Heaps[heapType], 0, ptr);
96 #endif
97 free(ptr);
98 }
99 #if NEBULA_MEMORY_STATS
102 Threading::Interlocked::Add(HeapTypeAllocSize[heapType], -int(size));
104 #endif
105 }
106}
107
108//------------------------------------------------------------------------------
111__forceinline void*
112AllocVirtual(size_t size)
113{
114 void* ret = mmap(nullptr, size, PROT_NONE, MAP_ANON | MAP_PRIVATE, 0, 0);
115 n_assert(ret != nullptr);
116 return ret;
117}
118
119//------------------------------------------------------------------------------
122__forceinline void
123DecommitVirtual(void* ptr, size_t size)
124{
125 auto ret = madvise(ptr, size, MADV_DONTNEED);
126 n_assert(ret == 0);
127 ret = mprotect(ptr, size, PROT_NONE);
128 n_assert(ret == 0);
129}
130
131//------------------------------------------------------------------------------
134__forceinline void
135CommitVirtual(void* ptr, size_t size)
136{
137 auto ret = mprotect(ptr, size, PROT_READ | PROT_WRITE);
138 explicit_bzero(ptr, size);
139 n_assert(ret == 0);
140}
141
142//------------------------------------------------------------------------------
145__forceinline void
146FreeVirtual(void* ptr, size_t size)
147{
148 auto ret = madvise(ptr, size, MADV_DONTNEED);
149 n_assert(ret == 0);
150 ret = munmap(ptr, size);
151 n_assert(ret == 0);
152}
153
154//------------------------------------------------------------------------------
158__forceinline void
159Copy(const void* from, void* to, size_t numBytes)
160{
161 if (numBytes > 0)
162 {
163 n_assert(0 != from);
164 n_assert(0 != to);
165 n_assert(from != to);
166 memcpy(to, from, numBytes);
167 }
168}
169
170//------------------------------------------------------------------------------
174__forceinline void
175Move(const void* from, void* to, size_t numBytes)
176{
177 if (numBytes > 0)
178 {
179 n_assert(0 != from);
180 n_assert(0 != to);
181 n_assert(from != to);
182 memmove(to, from, numBytes);
183 }
184}
185
186//------------------------------------------------------------------------------
190template <typename T>
191__forceinline void
192MoveElements(const T* from, T* to, size_t numElements)
193{
194 if (numElements > 0)
195 {
196 n_assert(0 != from);
197 n_assert(0 != to);
198 n_assert(from != to);
199 memmove((void*)to, (const void*)from, numElements * sizeof(T));
200 }
201}
202
203//------------------------------------------------------------------------------
207template <typename T>
208__forceinline void
209CopyElements(const T* from, T* to, size_t numElements)
210{
211 if (numElements > 0)
212 {
213 n_assert(0 != from);
214 n_assert(0 != to);
215 n_assert(from != to);
216 memcpy(to, from, numElements * sizeof(T));
217 }
218}
219
220
221//------------------------------------------------------------------------------
226__forceinline void
227CopyToGraphicsMemory(const void* from, void* to, size_t numBytes)
228{
229 // no special handling on the Win32 platform
230 Memory::Copy(from, to, numBytes);
231}
232
233//------------------------------------------------------------------------------
237__forceinline void
238Clear(void* ptr, size_t numBytes)
239{
240 memset(ptr, 0, numBytes);
241}
242
243//------------------------------------------------------------------------------
247__forceinline void
248Fill(void* ptr, size_t numBytes, unsigned char value)
249{
250 memset(ptr, value, numBytes);
251}
252
253//------------------------------------------------------------------------------
258__forceinline char*
259DuplicateCString(const char* from)
260{
261 n_assert(0 != from);
262 size_t len = (unsigned int) strlen(from) + 1;
263 char* to = (char*) Memory::Alloc(Memory::StringDataHeap, len);
264 Memory::Copy((void*)from, to, len);
265 return to;
266}
267
268//------------------------------------------------------------------------------
272inline bool
273IsOverlapping(const unsigned char* srcPtr, size_t srcSize, const unsigned char* dstPtr, size_t dstSize)
274{
275 if (srcPtr == dstPtr)
276 {
277 return true;
278 }
279 else if (srcPtr > dstPtr)
280 {
281 return (srcPtr + srcSize) > dstPtr;
282 }
283 else
284 {
285 return (dstPtr + dstSize) > srcPtr;
286 }
287}
288
289//------------------------------------------------------------------------------
294struct TotalMemoryStatus
295{
296 unsigned int totalPhysical;
297 unsigned int availPhysical;
298 unsigned int totalVirtual;
299 unsigned int availVirtual;
300};
301
302inline TotalMemoryStatus
304{
305#if 0
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;
313 return result;
314#else
315 TotalMemoryStatus result;
316 return result;
317#endif
318}
319
320//------------------------------------------------------------------------------
326#if NEBULA_MEMORY_STATS
327extern bool Validate();
328#endif
329
330} // namespace Memory
331//------------------------------------------------------------------------------
332#endif
333
Nebula debug macros.
#define n_assert(exp)
Definition debug.h:50
Nebula compiler specific defines and configuration.
Allocates memory using the TLSF method (http://www.gii.upv.es/tlsf/files/ecrts04_tlsf....
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