OpenBarnyard
 
Loading...
Searching...
No Matches
TMemory_dlmalloc.h
Go to the documentation of this file.
1#pragma once
2#include "Thread/T2Mutex.h"
3
4#undef CreateMutex
5#undef FillMemory
6
8
9#ifdef TMEMORY_USE_DLMALLOC
10
11class TMemoryDL;
12
13class TMemoryDLContext
14{
15private:
16 inline static auto MallocNative = []( TMemoryDL* pMemModule, TSIZE size ) -> void* {
17 return malloc( size );
18 };
19 inline static auto CallocNative = []( TMemoryDL* pMemModule, TSIZE nitems, TSIZE size ) -> void* {
20 return calloc( nitems, size );
21 };
22 inline static auto ReallocNative = []( TMemoryDL* pMemModule, void* ptr, TSIZE size ) -> void* {
23 return realloc( ptr, size );
24 };
25 inline static auto MemalignNative = []( TMemoryDL* pMemModule, TSIZE alignment, TSIZE size ) -> void* {
26 return malloc( size );
27 };
28 inline static auto FreeNative = []( TMemoryDL* pMemModule, void* ptr ) -> void {
29 free( ptr );
30 };
31 inline static auto IdkNative = []( TMemoryDL* pMemModule, void* ptr, TSIZE size ) -> void {
32 };
33
34 friend TMemoryDL;
35
36public:
37 typedef void* ( *t_Malloc )( TMemoryDL* pMemModule, TSIZE size );
38 typedef void* ( *t_Calloc )( TMemoryDL* pMemModule, TSIZE nitems, TSIZE size );
39 typedef void* ( *t_Realloc )( TMemoryDL* pMemModule, void* ptr, TSIZE size );
40 typedef void ( *t_Idk )( TMemoryDL* pMemModule, void* ptr, TSIZE size );
41 typedef void* ( *t_Memalign )( TMemoryDL* pMemModule, TSIZE alignment, TSIZE size );
42 typedef void ( *t_Free )( TMemoryDL* pMemModule, void* ptr );
43
44public:
45 void* Malloc( TSIZE size ) { return s_cbMalloc( m_pMemModule, size ); }
46 void* Calloc( TSIZE nitems, TSIZE size ) { return s_cbCalloc( m_pMemModule, nitems, size ); }
47 void* Realloc( void* ptr, TSIZE size ) { return s_cbRealloc( m_pMemModule, ptr, size ); }
48 void* Memalign( TSIZE alignment, TSIZE size ) { return s_cbMemalign( m_pMemModule, alignment, size ); }
49 void Free( void* ptr ) { s_cbFree( m_pMemModule, ptr ); }
50
51public:
52 t_Malloc s_cbMalloc = MallocNative;
53 t_Calloc s_cbCalloc = CallocNative;
54 t_Realloc s_cbRealloc = ReallocNative;
55 t_Idk s_cbIdk = IdkNative;
56 t_Memalign s_cbMemalign = MemalignNative;
57 t_Free s_cbFree = FreeNative;
58
59 void* s_Sysheap = TNULL;
60 void* s_Heap = TNULL;
61 TMemoryDL* m_pMemModule;
62};
63
64class TMemoryDLHeap;
65
66typedef TUINT32 TMemoryDLHeapFlags;
67enum TMemoryHeapFlags_ : TMemoryDLHeapFlags
68{
69 TMemoryHeapFlags_UseMutex = BITFLAG( 0 ),
70 TMemoryHeapFlags_AllocAsPile = BITFLAG( 2 ),
71};
72
73extern class TMemoryDL* g_pMemoryDL;
74
75class TMemoryDL
76{
77public:
78 static constexpr int HEAP_MAXNAME = 51;
79
80 typedef TUINT32 Flags;
81 typedef TUINT32 Error;
82 typedef TUINT32 BlockSize;
83
84 enum Flags_ : Flags
85 {
86 Flags_Standard = 0,
87 Flags_NativeMethods = BITFLAG( 1 )
88 };
89
90 enum Error_ : Error
91 {
92 Error_Ok = 0,
93 Error_Heap = 1
94 };
95
96public:
97 TMemoryDL( Flags flags = Flags_Standard, BlockSize blockSize = 640 * 1024 * 1024 )
98 : m_Flags( flags ), m_GlobalSize( blockSize ), m_NumAllocatedBytes( 0 ) {}
99
100 void SetHeap( TMemoryDLHeap* a_pHeap ) { m_GlobalHeap = a_pHeap; }
101 TMemoryDLHeap* GetHeap() { return m_GlobalHeap; }
102 TMemoryDLContext& GetContext() { return m_Context; }
103
104public:
105 void dlheapfree( TMemoryDLHeap* heap, void* mem );
106 void dlheapdestroy( TMemoryDLHeap* heap );
107 void* dlheapmalloc( TMemoryDLHeap* heap, TSIZE size );
108 void* dlheapcalloc( TMemoryDLHeap* heap, TSIZE nitems, TSIZE size );
109 void* dlheaprealloc( TMemoryDLHeap* heap, void* mem, TSIZE newsize );
110 void* dlheapmemalign( TMemoryDLHeap* heap, TSIZE alignment, TSIZE size );
111 TMemoryDLHeap* dlheapcreateinplace( void* ptr, TSIZE heapSize, TMemoryDLHeapFlags flags, const TCHAR name[ HEAP_MAXNAME ] );
112 TMemoryDLHeap* dlheapcreatesubheap( TMemoryDLHeap* heap, TSIZE size, TMemoryDLHeapFlags flags, const TCHAR name[ HEAP_MAXNAME ] );
113 TMemoryDLHeap* dlheapcreate( TMemoryDLHeap* heap, TSIZE size, TMemoryDLHeapFlags flags, const TCHAR name[ HEAP_MAXNAME ] ) { return TMemoryDL::dlheapcreatesubheap( heap, size, flags, name ); }
114
115 TMemoryDLHeap* CreateHeapInPlace( void* ptr, TSIZE heapSize, TMemoryDLHeapFlags flags, const TCHAR name[ HEAP_MAXNAME ] ) { return TMemoryDL::dlheapcreateinplace( ptr, heapSize, flags, name ); }
116
117 void OutOfMem( TMemoryDLHeap* heap, TSIZE size );
118 void Shutdown();
119 void AcquireMutex() { m_Mutex.Lock(); }
120 void ReleaseMutex() { m_Mutex.Unlock(); }
121 unsigned long long GetNumOfAllocatedBytes() { return m_NumAllocatedBytes; }
122
123private:
124 Flags GetFlags() { return m_Flags; }
125
126public:
127 /*
128 * Platform specific methods
129 * Define them in TMemory_{Platform}.cpp
130 */
131
132 Error Init();
133
134public:
135private:
136private:
137 Flags m_Flags;
138 BlockSize m_GlobalSize;
139 TMemoryDLHeap* m_GlobalHeap;
140 TMemoryDLContext m_Context;
141 T2Mutex m_Mutex;
142 unsigned long long m_NumAllocatedBytes;
143};
144
145class TMemoryDLHeap
146{
147public:
148 friend TMemoryDL;
149
150public:
151 void* Malloc( TSIZE size ) { return m_pOwnerBlock->dlheapmalloc( this, size ); }
152 void* Calloc( TSIZE nitems, TSIZE size ) { return m_pOwnerBlock->dlheapcalloc( this, nitems, size ); }
153 void* Realloc( void* mem, TSIZE newsize ) { return m_pOwnerBlock->dlheaprealloc( this, mem, newsize ); }
154 void* Memalign( TSIZE alignment, TSIZE size ) { return m_pOwnerBlock->dlheapmemalign( this, alignment, size ); }
155 void Free( void* mem ) { m_pOwnerBlock->dlheapfree( this, mem ); }
156
157 void SetName( const TCHAR* name )
158 {
159 strncpy_s( m_Name, name, TMemoryDL::HEAP_MAXNAME );
160 m_Name[ TMemoryDL::HEAP_MAXNAME ] = '\0';
161 }
162
163 void* GetMSpace() const { return m_MSpace; }
164
165private:
166 void CreateMutex() { m_Mutex.Create(); }
167 void DestroyMutex() { m_Mutex.Destroy(); }
168
169 static void* AllocAsPile( TMemoryDLHeap* heap, TSIZE size, TSIZE alignment = 4 );
170
171private:
172 TMemoryDL* m_pOwnerBlock;
173 TMemoryDLHeapFlags m_Flags;
174 T2Mutex m_Mutex;
175 TCHAR* m_SubHeapBuffer;
176 void* m_MSpace;
177 TCHAR* m_PileData;
178 TUINT32 m_PileSize;
179 TCHAR m_Name[ TMemoryDL::HEAP_MAXNAME + 1 ];
180};
181
182#endif // TMEMORY_USE_DLMALLOC
183
#define BITFLAG(x)
Definition Defines.h:10
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
uint32_t TUINT32
Definition Typedefs.h:13