Nebula
Loading...
Searching...
No Matches
win32heap.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
13#include "core/types.h"
16#include "util/array.h"
17#include "util/list.h"
18
19//------------------------------------------------------------------------------
20namespace Win32
21{
23{
24public:
26 static void Setup();
28 Win32Heap(const char* name, size_t initialSize=0, size_t maxSize=0);
30 ~Win32Heap();
32 const char* GetName() const;
34 void* Alloc(size_t size);
36 void* Realloc(void* ptr, size_t newSize);
38 void Free(void* ptr);
39
40 #if NEBULA_MEMORY_STATS
42 struct Stats
43 {
44 const char* name;
45 int allocCount;
46 int allocSize;
47 };
49 static Util::Array<Stats> GetAllHeapStats();
51 static bool ValidateAllHeaps();
53 bool ValidateHeap() const;
55 void DumpLeaks();
57 static void DumpLeaksAllHeaps();
59 long GetAllocCount() const;
61 long GetAllocSize() const;
63 static void DumpHeapMemoryLeaks(const char* heapName, HANDLE hHeap);
64 #endif
65
66private:
69
70 HANDLE heap;
71 const char* name;
72
73 #if NEBULA_MEMORY_STATS
74 long volatile allocCount;
75 long volatile allocSize;
76 static Threading::CriticalSection* criticalSection;
77 static Util::List<Win32Heap*>* list;
79 #endif
80};
81
82//------------------------------------------------------------------------------
85inline const char*
87{
88 n_assert(0 != this->name);
89 return this->name;
90}
91
92//------------------------------------------------------------------------------
95__forceinline void*
96Win32Heap::Alloc(size_t size)
97{
98 #if NEBULA_MEMORY_STATS
99 Threading::Interlocked::Increment((volatile int*)&this->allocCount);
100 // __HeapAlloc16 will always add 16 bytes for memory alignment padding
101 Threading::Interlocked::Add((volatile int*)&this->allocSize, int(size + 16));
102 #endif
103 void* ptr = Memory::__HeapAlloc16(this->heap, HEAP_GENERATE_EXCEPTIONS, size);
104 return ptr;
105}
106
107//------------------------------------------------------------------------------
110__forceinline void*
111Win32Heap::Realloc(void* ptr, size_t size)
112{
113 #if NEBULA_MEMORY_STATS
114 size_t curSize = Memory::__HeapSize16(this->heap, 0, ptr);
115 // __HeapAlloc16 will always add 16 bytes for memory alignment padding
116 Threading::Interlocked::Add((volatile int*)&this->allocSize, int(size - curSize + 16));
117 #endif
118 void* newPtr = Memory::__HeapReAlloc16(this->heap, HEAP_GENERATE_EXCEPTIONS, ptr, size);
119 return newPtr;
120}
121
122//------------------------------------------------------------------------------
125__forceinline void
127{
128 n_assert(0 != ptr);
129 #if NEBULA_MEMORY_STATS
130 size_t size = Memory::__HeapSize16(this->heap, 0, ptr);
131 Threading::Interlocked::Add((volatile int*)&this->allocSize, -int(size));
132 Threading::Interlocked::Decrement((volatile int*)&this->allocCount);
133 #endif
134 BOOL success = Memory::__HeapFree16(this->heap, 0, ptr);
135 n_assert(0 != success);
136}
137
138} // namespace Win32Heap
139//------------------------------------------------------------------------------
Critical section objects are used to protect a portion of code from parallel execution.
Nebula's dynamic array class.
Definition array.h:60
the list iterator
Definition list.h:76
Implements a doubly linked list.
Definition list.h:20
Win32 implementation of the class Memory::Heap.
Definition win32heap.h:23
HANDLE heap
Definition win32heap.h:70
void Free(void *ptr)
free a block of memory which has been allocated from this heap
Definition win32heap.h:126
const char * GetName() const
get heap name
Definition win32heap.h:86
~Win32Heap()
destructor
Definition win32heap.cc:68
void * Alloc(size_t size)
allocate a block of memory from the heap
Definition win32heap.h:96
const char * name
Definition win32heap.h:71
void * Realloc(void *ptr, size_t newSize)
re-allocate a block of memory
Definition win32heap.h:111
static void Setup()
static setup method (called by Core::SysFunc::Setup)
Definition win32heap.cc:25
Win32Heap()
default constructor not allowed
#define n_assert(exp)
Definition debug.h:50
__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
__forceinline BOOL __HeapFree16(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem)
HeapFree replacement which always returns 16-byte aligned addresses.
Definition win32memoryconfig.h:155
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
[TODO: Describe Win32 subsystem]