7 #ifndef __platform_memory_h__ 8 #define __platform_memory_h__ 10 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64 13 #define FE_EXPORT_MEM_VIA_FUNC 17 #if FE_CODEGEN<=FE_DEBUG 18 #define FE_CHECK_NEW TRUE 20 #define FE_CHECK_NEW FALSE 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) 41 #define FE_MEM_ALIGNMENT 16 43 #define FE_MEM_ALIGNMENT 0 46 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64 || defined(__clang__) 47 #define FE_SAFE_COUNTED_MUTEX FALSE 49 #define FE_SAFE_COUNTED_MUTEX TRUE 58 typedef void* ( FE_CDECL allocateFunction )( FE_UWORD byteCount );
59 typedef void ( FE_CDECL deallocateFunction )(
void* pMemory );
60 typedef void* ( FE_CDECL reallocateFunction )(
void* pMemory, FE_UWORD byteCount );
64 #if FE_COMPILER==FE_DMC 65 extern allocateFunction* gs_pAllocateFunction;
66 extern deallocateFunction* gs_pDeallocateFunction;
67 extern reallocateFunction* gs_pReallocateFunction;
68 extern void* gs_pHeapBase;
69 extern I32 gs_newCheck;
71 FE_MEM_PORT
extern allocateFunction* gs_pAllocateFunction;
72 FE_MEM_PORT
extern deallocateFunction* gs_pDeallocateFunction;
73 FE_MEM_PORT
extern reallocateFunction* gs_pReallocateFunction;
74 FE_MEM_PORT
extern void* gs_pHeapBase;
75 FE_MEM_PORT
extern I32 gs_newCheck;
78 FE_MEM_PORT
void * FE_CDECL ex_allocate(FE_UWORD byteCount);
79 FE_MEM_PORT
void * FE_CDECL ex_reallocate(
void *pMemory, FE_UWORD byteCount);
80 FE_MEM_PORT
void FE_CDECL ex_deallocate(
void *pMemory);
86 void* allocate( FE_UWORD byteCount )
89 return malloc(byteCount);
91 #ifdef FE_EXPORT_MEM_VIA_FUNC 92 return ex_allocate(byteCount);
94 return gs_pAllocateFunction( byteCount );
100 void deallocate(
void* pMemory )
103 #ifdef FE_MEM_RAWTEST 108 #ifdef FE_EXPORT_MEM_VIA_FUNC 109 ex_deallocate(pMemory);
111 gs_pDeallocateFunction( pMemory );
116 void* reallocate(
void* pMemory, FE_UWORD byteCount )
118 #ifdef FE_MEM_RAWTEST 119 return realloc(pMemory, byteCount);
121 #ifdef FE_EXPORT_MEM_VIA_FUNC 122 return ex_reallocate(pMemory, byteCount);
124 return gs_pReallocateFunction( pMemory, byteCount );
135 static bool isHeap(
void* pPtr);
136 static bool isHeapOrCannotTell(
void* pPtr);
137 static bool newWorks(
void);
140 #if FE_SAFE_COUNTED_MUTEX 141 static I32 ms_mutexCount;
142 static RecursiveMutex* ms_pMutex;
144 static RecursiveMutex ms_mutex;
150 inline bool Memory::isHeap(
void* pPtr)
154 #elif FE_OS==FE_LINUX 155 inline bool Memory::isHeap(
void* pPtr)
159 return pPtr < (
void*)¤t_stack;
161 #elif FE_OS==FE_WIN32 || FE_OS==FE_WIN64 162 inline bool Memory::isHeap(
void* pPtr)
173 return pPtr >= gs_pHeapBase;
176 inline bool Memory::isHeap(
void* pPtr)
180 return pPtr < (
void*)¤t_stack;
183 #error Memory::isHeap not implemented 186 inline bool Memory::newWorks(
void)
191 char* byte=
new char();
193 gs_newCheck=gs_pHeapBase? 1: -1;
195 return gs_newCheck>0;
201 inline bool Memory::isHeapOrCannotTell(
void* pPtr)
204 return !newWorks() || Memory::isHeap(pPtr);
210 FE_DL_EXPORT
void FE_CDECL setAllocateFunction(allocateFunction *p_fn);
211 FE_DL_EXPORT
void FE_CDECL setDeallocateFunction(deallocateFunction *p_fn);
216 inline void* _cdecl
operator new(
size_t byteCount )
217 {
return fe::allocate( byteCount ); }
218 inline void* _cdecl
operator new[](
size_t byteCount )
219 {
return operator new( byteCount ); }
221 inline void _cdecl
operator delete(
void* pMemory )
222 { fe::deallocate( pMemory ); }
223 inline void _cdecl
operator delete[](
void* pMemory )
224 {
operator delete( pMemory ); }
228 #if (FE_COMPILER==FE_GNU && __GNUC__>6) || (defined(__clang__) && __cplusplus >= 201703L) 229 #define FE_NO_DYNAMIC_EXCEPTIONS 1 231 #define FE_NO_DYNAMIC_EXCEPTIONS 0 234 #ifndef FE_USE_HOST_NEW 235 #if !defined(__clang__) 236 void* FE_CDECL
operator new(
size_t byteCount );
237 void* FE_CDECL
operator new[](
size_t byteCount );
238 void FE_CDECL
operator delete(
void* pMemory );
239 void FE_CDECL
operator delete[](
void* pMemory );
240 #elif __cplusplus >= 201703L 241 void* FE_CDECL
operator new(
size_t byteCount ) noexcept(
false);
242 void* FE_CDECL
operator new[](
size_t byteCount ) noexcept(
false);
243 void FE_CDECL
operator delete(
void* pMemory )
throw ();
244 void FE_CDECL
operator delete[](
void* pMemory )
throw ();
246 void* FE_CDECL
operator new(
size_t byteCount )
throw (std::bad_alloc);
247 void* FE_CDECL
operator new[](
size_t byteCount )
throw (std::bad_alloc);
248 void FE_CDECL
operator delete(
void* pMemory )
throw ();
249 void FE_CDECL
operator delete[](
void* pMemory )
throw ();
kernel
Definition: namespace.dox:3
Basic memory management.
Definition: memory.h:132