- fixed up dbgheap.cpp to compile for Linux (does not mean that it will work anyway!!!)
@@ -13,12 +13,14 @@ | ||
13 | 13 | |
14 | 14 | #ifdef _DEBUG_TRACE_LIBRARY_ |
15 | 15 | #include "dbgtrace.h" |
16 | -#endif //_DEBUG_TRACE_LIBRARY_ | |
16 | +#endif //_DEBUG_TRACE_LIBRARY_ | |
17 | + | |
18 | +#include <string.h> | |
19 | +#include <stdio.h> | |
20 | +#include <stdarg.h> | |
21 | + | |
22 | +#include <sdk/MacroUtils.h> | |
17 | 23 | |
18 | -#ifndef AINLINE | |
19 | -#define AINLINE __forceinline | |
20 | -#endif //AINLINE | |
21 | - | |
22 | 24 | /* |
23 | 25 | DebugHeap memory debugging environment |
24 | 26 |
@@ -90,8 +92,12 @@ | ||
90 | 92 | #endif //MEM_INTERRUPT |
91 | 93 | |
92 | 94 | typedef NativePageAllocator DebugFullPageHeapAllocator; |
93 | - | |
94 | -SYSTEM_INFO g_systemInfo; | |
95 | + | |
96 | +#ifdef _WIN32 | |
97 | +SYSTEM_INFO g_systemInfo; | |
98 | +#elif defined(__linux__) | |
99 | +unsigned long g_pageSize; | |
100 | +#endif //_WIN32 | |
95 | 101 | DebugFullPageHeapAllocator *_nativeAlloc = NULL; |
96 | 102 | pfnMemoryAllocWatch _memAllocWatchCallback = NULL; |
97 | 103 |
@@ -98,9 +104,15 @@ | ||
98 | 104 | #ifdef USE_FULL_PAGE_HEAP |
99 | 105 | |
100 | 106 | // Since we know that memory is allocated along pages, we can check for |
101 | -// invalid pointers given to the manager. | |
102 | -#define PAGE_MEM_ADJUST( ptr ) (void*)( (char*)ptr - ( (size_t)ptr % g_systemInfo.dwPageSize ) ) | |
107 | +// invalid pointers given to the manager. | |
108 | +#ifdef _WIN32 | |
109 | +#define _PAGE_SIZE_ACTUAL ( g_systemInfo.dwPageSize ) | |
110 | +#elif defined(__linux__) | |
111 | +#define _PAGE_SIZE_ACTUAL ( g_pageSize ) | |
112 | +#endif //CROSS PLATFORM MACRO | |
103 | 113 | |
114 | +#define PAGE_MEM_ADJUST( ptr ) (void*)( (char*)ptr - ( (size_t)ptr % _PAGE_SIZE_ACTUAL ) ) | |
115 | + | |
104 | 116 | inline static void* _win32_allocMemPage( size_t memSize ) |
105 | 117 | { |
106 | 118 | DebugFullPageHeapAllocator::pageHandle *handle = _nativeAlloc->Allocate( NULL, memSize ); |
@@ -141,7 +153,7 @@ | ||
141 | 153 | #define PAGE_MEM_ACTIVE_DEBUG_PATTERN 0x11 |
142 | 154 | #endif //PAGE_MEM_ACTIVE_DEBUG_PATTERN |
143 | 155 | |
144 | -#define MEM_PAGE_MOD( bytes ) ( ( (bytes) + g_systemInfo.dwPageSize - 1 ) / g_systemInfo.dwPageSize ) | |
156 | +#define MEM_PAGE_MOD( bytes ) ( ( (bytes) + _PAGE_SIZE_ACTUAL - 1 ) / _PAGE_SIZE_ACTUAL ) | |
145 | 157 | |
146 | 158 | #pragma pack(1) |
147 | 159 | struct _memIntro |
@@ -171,7 +183,7 @@ | ||
171 | 183 | |
172 | 184 | inline static size_t _win32_getRealPageSize( size_t objSize ) |
173 | 185 | { |
174 | - return ( MEM_PAGE_MOD( _getMetaSize( objSize ) ) * g_systemInfo.dwPageSize ); | |
186 | + return ( MEM_PAGE_MOD( _getMetaSize( objSize ) ) * _PAGE_SIZE_ACTUAL ); | |
175 | 187 | } |
176 | 188 | |
177 | 189 | inline static void* _win32_allocMem( size_t memSize ) |
@@ -363,10 +375,14 @@ | ||
363 | 375 | va_list argv; |
364 | 376 | |
365 | 377 | va_start( argv, fmt ); |
366 | - _vsnprintf( buf, sizeof( buf ), fmt, argv ); | |
378 | + vsnprintf( buf, sizeof( buf ), fmt, argv ); | |
367 | 379 | va_end( argv ); |
368 | - | |
369 | - OutputDebugStringA( buf ); | |
380 | + | |
381 | +#ifdef _WIN32 | |
382 | + OutputDebugStringA( buf ); | |
383 | +#elif defined(__linux__) | |
384 | + printf( "%s\n", buf ); | |
385 | +#endif //CROSS PLATFORM CODE | |
370 | 386 | } |
371 | 387 | #endif //PAGE_HEAP_MEMORY_STATS |
372 | 388 |
@@ -400,16 +416,16 @@ | ||
400 | 416 | #ifdef PAGE_HEAP_MEMORY_STATS |
401 | 417 | if ( blockCount != 0 ) |
402 | 418 | { |
403 | - OutputDebugStringW( L"Heap Memory Leak Protocol:\n" ); | |
419 | + OutputDebugStringFormat( "Heap Memory Leak Protocol:\n" ); | |
404 | 420 | OutputDebugStringFormat( |
405 | 421 | "* leaked memory: %u\n" \ |
406 | 422 | "* blocks/pages allocated: %u/%u [%u]\n", |
407 | 423 | memLeaked, |
408 | - blockCount, pageCount, blockCount * g_systemInfo.dwPageSize | |
424 | + blockCount, pageCount, blockCount * _PAGE_SIZE_ACTUAL | |
409 | 425 | ); |
410 | 426 | } |
411 | 427 | else |
412 | - OutputDebugStringW( L"No memory leaks detected." ); | |
428 | + OutputDebugStringFormat( "No memory leaks detected." ); | |
413 | 429 | #endif //PAGE_HEAP_MEMORY_STATS |
414 | 430 | } |
415 | 431 |
@@ -812,7 +828,7 @@ | ||
812 | 828 | return mem; |
813 | 829 | } |
814 | 830 | |
815 | -void* operator new( size_t memSize, const std::nothrow_t ) noexcept | |
831 | +void* operator new( size_t memSize, const std::nothrow_t& ) noexcept | |
816 | 832 | { |
817 | 833 | MEM_INTERRUPT( memSize != 0 ); |
818 | 834 |
@@ -831,7 +847,7 @@ | ||
831 | 847 | return mem; |
832 | 848 | } |
833 | 849 | |
834 | -void* operator new[]( size_t memSize, const std::nothrow_t ) noexcept | |
850 | +void* operator new[]( size_t memSize, const std::nothrow_t& ) noexcept | |
835 | 851 | { |
836 | 852 | MEM_INTERRUPT( memSize != 0 ); |
837 | 853 |
@@ -883,8 +899,12 @@ | ||
883 | 899 | // Call it before CRT initialization. |
884 | 900 | void DbgHeap_Init( void ) |
885 | 901 | { |
886 | -#ifdef USE_HEAP_DEBUGGING | |
887 | - GetSystemInfo( &g_systemInfo ); | |
902 | +#ifdef USE_HEAP_DEBUGGING | |
903 | +#ifdef _WIN32 | |
904 | + GetSystemInfo( &g_systemInfo ); | |
905 | +#elif defined(__linux__) | |
906 | + g_pageSize = (unsigned long)sysconf(_SC_PAGESIZE); | |
907 | +#endif //CROSS PLATFORM CODE | |
888 | 908 | |
889 | 909 | // Initialize watch callbacks. |
890 | 910 | _memAllocWatchCallback = NULL; |
@@ -961,8 +981,12 @@ | ||
961 | 981 | // Alternative entry point. |
962 | 982 | extern "C" |
963 | 983 | { |
964 | -#ifdef _DEBUGSDK_CRT_STARTUP | |
965 | -extern int mainCRTStartup( void ); | |
984 | +#ifdef _DEBUGSDK_CRT_STARTUP | |
985 | +#ifdef _WIN32 | |
986 | +extern int mainCRTStartup( void ); | |
987 | +#elif defined(__linux__) | |
988 | +extern int _start( void ); | |
989 | +#endif //CROSS PLATFORM CODE | |
966 | 990 | #elif defined(_DEBUGSDK_WIN32_STARTUP) |
967 | 991 | extern int _WinMainCRTStartup( void ); |
968 | 992 | #elif defined(_DEBUGSDK_WIN32DLL_STARTUP) |
@@ -973,10 +997,12 @@ | ||
973 | 997 | extern BOOL WINAPI _DllMainCRTStartup(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpReserved); |
974 | 998 | #endif |
975 | 999 | } |
976 | - | |
977 | -#define ENTRYPOINTCALL __stdcall | |
978 | 1000 | |
979 | 1001 | #ifdef _WIN32 |
1002 | +#define ENTRYPOINTCALL __stdcall | |
1003 | +#else | |
1004 | +#define ENTRYPOINTCALL | |
1005 | +#endif //CROSS PLATFORM MACRO | |
980 | 1006 | |
981 | 1007 | #ifdef _DEBUGSDK_CRT_STARTUP |
982 | 1008 | extern "C" int ENTRYPOINTCALL _DebugInit( void ) |
@@ -996,8 +1022,12 @@ | ||
996 | 1022 | DbgTrace_Init( stackSpace ); |
997 | 1023 | #endif |
998 | 1024 | |
999 | -#ifdef _DEBUGSDK_CRT_STARTUP | |
1000 | - int ret = mainCRTStartup(); | |
1025 | +#ifdef _DEBUGSDK_CRT_STARTUP | |
1026 | +#ifdef _WIN32 | |
1027 | + int ret = mainCRTStartup(); | |
1028 | +#elif defined(__linux__) | |
1029 | + int ret = _start(); | |
1030 | +#endif //CROSS PLATFORM CODE | |
1001 | 1031 | #elif defined(_DEBUGSDK_WIN32_STARTUP) |
1002 | 1032 | int ret = _WinMainCRTStartup(); |
1003 | 1033 | #elif defined(_DEBUGSDK_WIN32DLL_STARTUP) |
@@ -1012,5 +1042,3 @@ | ||
1012 | 1042 | DbgHeap_Shutdown(); |
1013 | 1043 | return ret; |
1014 | 1044 | } |
1015 | - | |
1016 | -#endif //_WIN32 |
@@ -22,15 +22,17 @@ | ||
22 | 22 | |
23 | 23 | #include <exception> |
24 | 24 | #include <new> |
25 | - | |
25 | + | |
26 | +#ifdef _MSC_VER | |
26 | 27 | #pragma warning(push) |
27 | -#pragma warning(disable: 4290) | |
28 | +#pragma warning(disable: 4290) | |
29 | +#endif //_MSC_VER | |
28 | 30 | |
29 | 31 | // Global allocator |
30 | 32 | void* operator new( size_t memSize ); |
31 | -void* operator new( size_t memSize, const std::nothrow_t nothrow ) noexcept; | |
33 | +void* operator new( size_t memSize, const std::nothrow_t& nothrow ) noexcept; | |
32 | 34 | void* operator new[]( size_t memSize ); |
33 | -void* operator new[]( size_t memSize, const std::nothrow_t nothrow ) noexcept; | |
35 | +void* operator new[]( size_t memSize, const std::nothrow_t& nothrow ) noexcept; | |
34 | 36 | void operator delete( void *ptr ) noexcept; |
35 | 37 | void operator delete[]( void *ptr ) noexcept; |
36 | 38 |
@@ -39,9 +41,11 @@ | ||
39 | 41 | void* DbgRealloc( void *ptr, size_t size ); |
40 | 42 | bool DbgAllocGetSize( void *ptr, size_t& sizeOut ); |
41 | 43 | void DbgFree( void *ptr ); |
44 | + | |
45 | +#ifdef _MSC_VER | |
46 | +#pragma warning(pop) | |
47 | +#endif //_MSC_VER | |
42 | 48 | |
43 | -#pragma warning(pop) | |
44 | - | |
45 | 49 | #endif //USE_HEAP_DEBUGGING |
46 | 50 | |
47 | 51 | typedef void (*pfnMemoryAllocWatch)( void *memPtr, size_t memSize ); |