Nebula
Loading...
Searching...
No Matches
win32memoryconfig.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
12#include "core/config.h"
13#include "core/debug.h"
14
15namespace Memory
16{
17
18//------------------------------------------------------------------------------
27{
28 DefaultHeap = 0, // for stuff that doesn't fit into any category
29 ObjectHeap, // heap for global new allocator
30 ObjectArrayHeap, // heap for global new[] allocator
31 ResourceHeap, // heap for resource data (like animation buffers)
32 ScratchHeap, // for short-lived scratch memory (encode/decode buffers, etc...)
33 StringDataHeap, // special heap for string data
34 StreamDataHeap, // special heap for stream data like memory streams, zip file streams, etc...
35 PhysicsHeap, // physics engine allocations go here
36 AppHeap, // for general Application layer stuff
37 NetworkHeap, // for network layer
38 ScriptingHeap, // for scripting layers
39
42};
43
44//------------------------------------------------------------------------------
49extern HANDLE volatile Heaps[NumHeapTypes];
50
51//------------------------------------------------------------------------------
58extern void SetupHeaps();
59
60//------------------------------------------------------------------------------
64extern const char* GetHeapTypeName(HeapType heapType);
65
66//------------------------------------------------------------------------------
73//------------------------------------------------------------------------------
78__forceinline unsigned char*
80{
81 unsigned char paddingMask = size_t(ptr) & 15;
82 ptr = (unsigned char*)(size_t(ptr + 16) & ~15);
83 ptr[-1] = paddingMask;
84 return ptr;
85}
86
87//------------------------------------------------------------------------------
92__forceinline unsigned char*
93__HeapUnalignPointer16(unsigned char* ptr)
94{
95 return (unsigned char*)(size_t(ptr - 16) | ptr[-1]);
96}
97
98//------------------------------------------------------------------------------
104__forceinline LPVOID
105__HeapAlloc16(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes)
106{
107 unsigned char* ptr = (unsigned char*) ::HeapAlloc(hHeap, dwFlags, dwBytes + 16);
109 return (LPVOID) ptr;
110}
111
112//------------------------------------------------------------------------------
118__forceinline LPVOID
119__HeapReAlloc16(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem, SIZE_T dwBytes)
120{
121 // restore unaligned pointer
122 unsigned char* ptr = (unsigned char*) lpMem;
123 unsigned char* rawPtr = __HeapUnalignPointer16(ptr);
124
125 // perform re-alloc, NOTE: if re-allocation can't happen in-place,
126 // we need to handle the allocation ourselves, in order not to destroy
127 // the original data because of different alignment!!!
128 ptr = (unsigned char*) ::HeapReAlloc(hHeap, (dwFlags | HEAP_REALLOC_IN_PLACE_ONLY), rawPtr, dwBytes + 16);
129 if (0 == ptr)
130 {
131 SIZE_T rawSize = ::HeapSize(hHeap, dwFlags, rawPtr);
132 // re-allocate manually because padding may be different!
133 ptr = (unsigned char*) ::HeapAlloc(hHeap, dwFlags, dwBytes + 16);
135 SIZE_T copySize = dwBytes <= (rawSize - 16) ? dwBytes : (rawSize - 16);
136 ::CopyMemory(ptr, lpMem, copySize);
137 // release old mem block
138 ::HeapFree(hHeap, dwFlags, rawPtr);
139 }
140 else
141 {
142 // was re-allocated in place
144 }
145 return (LPVOID) ptr;
146}
147
148//------------------------------------------------------------------------------
154__forceinline BOOL
155__HeapFree16(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem)
156{
157 unsigned char* ptr = (unsigned char*) lpMem;
158 ptr = __HeapUnalignPointer16(ptr);
159 return ::HeapFree(hHeap, dwFlags, ptr);
160}
161
162//------------------------------------------------------------------------------
166__forceinline SIZE_T
167__HeapSize16(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
168{
169 unsigned char* ptr = (unsigned char*) lpMem;
170 ptr = __HeapUnalignPointer16(ptr);
171 return ::HeapSize(hHeap, dwFlags, ptr);
172}
173
174} // namespace Memory
175//------------------------------------------------------------------------------
Nebula debug macros.
Nebula compiler specific defines and configuration.
Definition arenaallocator.h:31
__forceinline unsigned char * __HeapUnalignPointer16(unsigned char *ptr)
Helper function for Heap16 functions: "un-aligns" pointer through the padding mask stored in the byte...
Definition win32memoryconfig.h:93
const char * GetHeapTypeName(HeapType heapType)
Returns a human readable name for a heap type.
Definition osxmemoryconfig.cc:52
__forceinline unsigned char * __HeapAlignPointerAndWritePadding16(unsigned char *ptr)
Global PoolArrayAllocator objects, these are all setup in a central place in the Memory::SetupHeaps()...
Definition win32memoryconfig.h:79
HeapType
Heap types are defined here.
Definition osxmemoryconfig.h:25
@ NumHeapTypes
Definition osxmemoryconfig.h:36
@ StreamDataHeap
Definition osxmemoryconfig.h:32
@ AppHeap
Definition osxmemoryconfig.h:34
@ ResourceHeap
Definition osxmemoryconfig.h:29
@ DefaultHeap
Definition osxmemoryconfig.h:26
@ ScriptingHeap
Definition posixmemoryconfig.h:38
@ ObjectHeap
Definition osxmemoryconfig.h:27
@ InvalidHeapType
Definition osxmemoryconfig.h:37
@ PhysicsHeap
Definition osxmemoryconfig.h:33
@ NetworkHeap
Definition posixmemoryconfig.h:37
@ ObjectArrayHeap
Definition osxmemoryconfig.h:28
@ ScratchHeap
Definition osxmemoryconfig.h:30
@ StringDataHeap
Definition osxmemoryconfig.h:31
__forceinline SIZE_T __HeapSize16(HANDLE hHeap, DWORD dwFlags, LPCVOID lpMem)
HeapSize replacement function.
Definition win32memoryconfig.h:167
__forceinline LPVOID __HeapReAlloc16(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem, SIZE_T dwBytes)
HeapReAlloc replacement for 16-byte alignment.
Definition win32memoryconfig.h:119
__forceinline LPVOID __HeapAlloc16(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes)
HeapAlloc replacement which always returns 16-byte aligned addresses.
Definition win32memoryconfig.h:105
void SetupHeaps()
Setup the global heaps.
Definition osxmemoryconfig.cc:19
__forceinline BOOL __HeapFree16(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem)
HeapFree replacement which always returns 16-byte aligned addresses.
Definition win32memoryconfig.h:155
malloc_zone_t * Heaps[NumHeapTypes]
Heap pointers are defined here.
Definition osxmemoryconfig.cc:12