7 #ifndef __platform_memory_h__ 8 #define __platform_memory_h__ 10 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64 14 #define FE_EXPORT_MEM_VIA_FUNC 17 #if FE_CODEGEN<=FE_DEBUG 18 #define FE_CHECK_NEW TRUE 20 #define FE_CHECK_NEW FALSE 25 #if defined(__x86_64__) || defined(__aarch64__) 26 #define FE_HEAP_CHECKABLE FALSE 28 #define FE_HEAP_CHECKABLE FE_CHECK_NEW 32 #if FE_CODEGEN<=FE_DEBUG 33 #define FE_CHECK_HEAP FE_HEAP_CHECKABLE 35 #define FE_CHECK_HEAP FALSE 38 #define FE_LOCK_MEMORY (FE_CHECK_HEAP || FE_CHECK_NEW) 40 #ifndef FE_MEM_ALIGNMENT 42 #define FE_MEM_ALIGNMENT 16 44 #define FE_MEM_ALIGNMENT 0 50 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64 || defined(__clang__) 51 #define FE_SAFE_COUNTED_MUTEX FALSE 53 #define FE_SAFE_COUNTED_MUTEX TRUE 62 typedef void* ( FE_CDECL allocateFunction )( FE_UWORD byteCount );
63 typedef void ( FE_CDECL deallocateFunction )(
void* pMemory );
64 typedef void* ( FE_CDECL reallocateFunction )(
void* pMemory, FE_UWORD byteCount );
65 typedef void ( FE_CDECL printFunction )(
const char* pAscii );
69 #if FE_COMPILER==FE_DMC 70 extern allocateFunction* gs_pAllocateFunction;
71 extern deallocateFunction* gs_pDeallocateFunction;
72 extern reallocateFunction* gs_pReallocateFunction;
73 extern printFunction* gs_pPrintFunction;
74 extern void* gs_pHeapBase;
75 extern I32 gs_newCheck;
77 FE_MEM_PORT
extern allocateFunction* gs_pAllocateFunction;
78 FE_MEM_PORT
extern deallocateFunction* gs_pDeallocateFunction;
79 FE_MEM_PORT
extern reallocateFunction* gs_pReallocateFunction;
80 FE_MEM_PORT
extern printFunction* gs_pPrintFunction;
81 FE_MEM_PORT
extern void* gs_pHeapBase;
82 FE_MEM_PORT
extern I32 gs_newCheck;
85 FE_MEM_PORT
void * FE_CDECL ex_allocate(FE_UWORD byteCount);
86 FE_MEM_PORT
void * FE_CDECL ex_reallocate(
void *pMemory, FE_UWORD byteCount);
87 FE_MEM_PORT
void FE_CDECL ex_deallocate(
void *pMemory);
93 void* allocate( FE_UWORD byteCount )
96 return malloc(byteCount);
98 #ifdef FE_EXPORT_MEM_VIA_FUNC 99 return ex_allocate(byteCount);
101 return gs_pAllocateFunction( byteCount );
107 void deallocate(
void* pMemory )
115 #ifdef FE_EXPORT_MEM_VIA_FUNC 116 ex_deallocate(pMemory);
118 gs_pDeallocateFunction( pMemory );
123 void* reallocate(
void* pMemory, FE_UWORD byteCount )
126 return realloc(pMemory, byteCount);
128 #ifdef FE_EXPORT_MEM_VIA_FUNC 129 return ex_reallocate(pMemory, byteCount);
131 return gs_pReallocateFunction( pMemory, byteCount );
142 static bool isHeap(
void* pPtr);
143 static bool isHeapOrCannotTell(
void* pPtr);
144 static bool newWorks(
void);
146 #if FE_SAFE_COUNTED_MUTEX 147 static void markForDeath(Mutex*& a_rpMutex,I32* a_pCount);
148 static void markForDeath(RecursiveMutex*& a_rpMutex,I32* a_pCount);
149 static void clearDeadPool(
void);
153 #if FE_SAFE_COUNTED_MUTEX 154 static I32 ms_mutexCount;
155 static RecursiveMutex* ms_pMutex;
157 static RecursiveMutex ms_mutex;
162 #if FE_SAFE_COUNTED_MUTEX 166 CountedMutex(Mutex*& a_rpMutex,I32* a_pCount)
168 m_ppMutex= &a_rpMutex;
171 CountedMutex(RecursiveMutex*& a_rpMutex,I32* a_pCount)
173 m_ppMutex=
reinterpret_cast<Mutex**
>(&a_rpMutex);
180 static std::vector<CountedMutex> ms_deadPool;
185 inline bool Memory::isHeap(
void* pPtr)
189 #elif FE_OS==FE_LINUX 190 inline bool Memory::isHeap(
void* pPtr)
194 return pPtr < (
void*)¤t_stack;
196 #elif FE_OS==FE_WIN32 || FE_OS==FE_WIN64 197 inline bool Memory::isHeap(
void* pPtr)
208 return pPtr >= gs_pHeapBase;
211 inline bool Memory::isHeap(
void* pPtr)
215 return pPtr < (
void*)¤t_stack;
218 #error Memory::isHeap not implemented 221 inline bool Memory::newWorks(
void)
226 char* byte=
new char();
228 gs_newCheck=gs_pHeapBase? 1: -1;
230 return gs_newCheck>0;
236 inline bool Memory::isHeapOrCannotTell(
void* pPtr)
239 return !newWorks() || Memory::isHeap(pPtr);
245 FE_DL_EXPORT
void FE_CDECL setAllocateFunction(allocateFunction *p_fn);
246 FE_DL_EXPORT
void FE_CDECL setDeallocateFunction(deallocateFunction *p_fn);
247 FE_DL_EXPORT
void FE_CDECL setPrintFunction(printFunction *p_fn);
252 inline void* _cdecl
operator new(
size_t byteCount )
253 {
return fe::allocate( byteCount ); }
254 inline void* _cdecl
operator new[](
size_t byteCount )
255 {
return operator new( byteCount ); }
257 inline void _cdecl
operator delete(
void* pMemory )
258 { fe::deallocate( pMemory ); }
259 inline void _cdecl
operator delete[](
void* pMemory )
260 {
operator delete( pMemory ); }
264 #if (FE_COMPILER==FE_GNU && __GNUC__>6) || (defined(__clang__) && FE_CPLUSPLUS >= 201703L) 265 #define FE_NO_DYNAMIC_EXCEPTIONS 1 267 #define FE_NO_DYNAMIC_EXCEPTIONS 0 270 #ifndef FE_USE_HOST_NEW 271 #if !defined(__clang__) 272 void* FE_CDECL
operator new(
size_t byteCount );
273 void* FE_CDECL
operator new[](
size_t byteCount );
274 void FE_CDECL
operator delete(
void* pMemory );
275 void FE_CDECL
operator delete[](
void* pMemory );
276 #elif FE_CPLUSPLUS >= 201703L 277 void* FE_CDECL
operator new(
size_t byteCount ) noexcept(
false);
278 void* FE_CDECL
operator new[](
size_t byteCount ) noexcept(
false);
279 void FE_CDECL
operator delete(
void* pMemory )
throw ();
280 void FE_CDECL
operator delete[](
void* pMemory )
throw ();
282 void* FE_CDECL
operator new(
size_t byteCount )
throw (std::bad_alloc);
283 void* FE_CDECL
operator new[](
size_t byteCount )
throw (std::bad_alloc);
284 void FE_CDECL
operator delete(
void* pMemory )
throw ();
285 void FE_CDECL
operator delete[](
void* pMemory )
throw ();
kernel
Definition: namespace.dox:3
Basic memory management.
Definition: src/platform/memory.h:139