xref: /aosp_15_r20/external/lzma/CPP/Common/NewHandler.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // Common/NewHandler.h
2 
3 #ifndef ZIP7_INC_COMMON_NEW_HANDLER_H
4 #define ZIP7_INC_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 DOCs:
14   Since ISO C++98, operator new throws std::bad_alloc when memory allocation fails.
15   MSVC 6.0 returned a null pointer on an allocation failure.
16   Beginning in VS2002, operator new conforms to the standard and throws on failure.
17 
18   By default, the compiler also generates defensive null checks to prevent
19   these older-style allocators from causing an immediate crash on failure.
20   The /Zc:throwingNew option tells the compiler to leave out these null checks,
21   on the assumption that all linked memory allocators conform to the standard.
22 
23 The operator new() in some MSVC versions doesn't throw exception std::bad_alloc.
24 MSVC 6.0 (_MSC_VER == 1200) doesn't throw exception.
25 The code produced by some another MSVC compilers also can be linked
26 to library that doesn't throw exception.
27 We suppose that code compiled with VS2015+ (_MSC_VER >= 1900) throws exception std::bad_alloc.
28 For older _MSC_VER versions we redefine operator new() and operator delete().
29 Our version of operator new() throws CNewException() exception on failure.
30 
31 It's still allowed to use redefined version of operator new() from "NewHandler.cpp"
32 with any compiler. 7-Zip's code can work with std::bad_alloc and CNewException() exceptions.
33 But if you use some additional code (outside of 7-Zip's code), you must check
34 that redefined version of operator new() is not problem for your code.
35 */
36 
37 #include <stddef.h>
38 
39 #ifdef _WIN32
40 // We can compile my_new and my_delete with _fastcall
41 /*
42 void * my_new(size_t size);
43 void my_delete(void *p) throw();
44 // void * my_Realloc(void *p, size_t newSize, size_t oldSize);
45 */
46 #endif
47 
48 
49 #if defined(_MSC_VER) && (_MSC_VER < 1600)
50   // If you want to use default operator new(), you can disable the following line
51   #define Z7_REDEFINE_OPERATOR_NEW
52 #endif
53 
54 
55 #ifdef Z7_REDEFINE_OPERATOR_NEW
56 
57 // std::bad_alloc can require additional DLL dependency.
58 // So we don't define CNewException as std::bad_alloc here.
59 
60 class CNewException {};
61 
62 void *
63 #ifdef _MSC_VER
64 __cdecl
65 #endif
66 operator new(size_t size);
67 
68 /*
69 #if 0 && defined(_MSC_VER) && _MSC_VER == 1600
70   #define Z7_OPERATOR_DELETE_SPEC_THROW0
71 #else
72   #define Z7_OPERATOR_DELETE_SPEC_THROW0 throw()
73 #endif
74 */
75 #if defined(_MSC_VER) && _MSC_VER == 1600
76 #pragma warning(push)
77 #pragma warning(disable : 4986) // 'operator delete': exception specification does not match previous declaration
78 #endif
79 
80 void
81 #ifdef _MSC_VER
82 __cdecl
83 #endif
84 operator delete(void *p) throw();
85 
86 void
87 #ifdef _MSC_VER
88 __cdecl
89 #endif
90 operator delete(void *p, size_t n) throw();
91 
92 #if defined(_MSC_VER) && _MSC_VER == 1600
93 #pragma warning(pop)
94 #endif
95 
96 
97 #else
98 
99 #include <new>
100 
101 #define CNewException std::bad_alloc
102 
103 #endif
104 
105 /*
106 #ifdef _WIN32
107 void *
108 #ifdef _MSC_VER
109 __cdecl
110 #endif
111 operator new[](size_t size);
112 
113 void
114 #ifdef _MSC_VER
115 __cdecl
116 #endif
117 operator delete[](void *p) throw();
118 #endif
119 */
120 
121 #endif
122