1 // Common/NewHandler.h 2 3 #ifndef __COMMON_NEW_HANDLER_H 4 #define __COMMON_NEW_HANDLER_H 5 6 /* 7 NewHandler.h and NewHandler.cpp allows to solve problem with compilers that 8 don't throw exception in operator new(). 9 10 This file must be included before any code that uses operators new() or delete() 11 and you must compile and link "NewHandler.cpp", if you use some old MSVC compiler. 12 13 The operator new() in some MSVC versions doesn't throw exception std::bad_alloc. 14 MSVC 6.0 (_MSC_VER == 1200) doesn't throw exception. 15 The code produced by some another MSVC compilers also can be linked 16 to library that doesn't throw exception. 17 We suppose that code compiled with VS2015+ (_MSC_VER >= 1900) throws exception std::bad_alloc. 18 For older _MSC_VER versions we redefine operator new() and operator delete(). 19 Our version of operator new() throws CNewException() exception on failure. 20 21 It's still allowed to use redefined version of operator new() from "NewHandler.cpp" 22 with any compiler. 7-Zip's code can work with std::bad_alloc and CNewException() exceptions. 23 But if you use some additional code (outside of 7-Zip's code), you must check 24 that redefined version of operator new() is not problem for your code. 25 */ 26 27 #include <stddef.h> 28 29 #ifdef _WIN32 30 // We can compile my_new and my_delete with _fastcall 31 /* 32 void * my_new(size_t size); 33 void my_delete(void *p) throw(); 34 // void * my_Realloc(void *p, size_t newSize, size_t oldSize); 35 */ 36 #endif 37 38 39 #if defined(_MSC_VER) && (_MSC_VER < 1900) 40 // If you want to use default operator new(), you can disable the following line 41 #define _7ZIP_REDEFINE_OPERATOR_NEW 42 #endif 43 44 45 #ifdef _7ZIP_REDEFINE_OPERATOR_NEW 46 47 // std::bad_alloc can require additional DLL dependency. 48 // So we don't define CNewException as std::bad_alloc here. 49 50 class CNewException {}; 51 52 void * 53 #ifdef _MSC_VER 54 __cdecl 55 #endif 56 operator new(size_t size); 57 58 void 59 #ifdef _MSC_VER 60 __cdecl 61 #endif 62 operator delete(void *p) throw(); 63 64 #else 65 66 #include <new> 67 68 #define CNewException std::bad_alloc 69 70 #endif 71 72 /* 73 #ifdef _WIN32 74 void * 75 #ifdef _MSC_VER 76 __cdecl 77 #endif 78 operator new[](size_t size); 79 80 void 81 #ifdef _MSC_VER 82 __cdecl 83 #endif 84 operator delete[](void *p) throw(); 85 #endif 86 */ 87 88 #endif 89