aboutsummaryrefslogtreecommitdiff
path: root/CPP/Common/NewHandler.h
diff options
context:
space:
mode:
authorIgor Pavlov <87184205+ip7z@users.noreply.github.com>2021-12-27 00:00:00 +0000
committerIgor Pavlov <87184205+ip7z@users.noreply.github.com>2022-03-18 15:35:13 +0500
commitf19f813537c7aea1c20749c914e756b54a9c3cf5 (patch)
tree816ba62ca7c0fa19f2eb46d9e9d6f7dd7c3a744d /CPP/Common/NewHandler.h
parent98e06a519b63b81986abe76d28887f6984a7732b (diff)
download7zip-21.07.tar.gz
7zip-21.07.tar.bz2
7zip-21.07.zip
'21.07'21.07
Diffstat (limited to 'CPP/Common/NewHandler.h')
-rw-r--r--CPP/Common/NewHandler.h88
1 files changed, 88 insertions, 0 deletions
diff --git a/CPP/Common/NewHandler.h b/CPP/Common/NewHandler.h
new file mode 100644
index 0000000..aedeca6
--- /dev/null
+++ b/CPP/Common/NewHandler.h
@@ -0,0 +1,88 @@
1// Common/NewHandler.h
2
3#ifndef __COMMON_NEW_HANDLER_H
4#define __COMMON_NEW_HANDLER_H
5
6/*
7NewHandler.h and NewHandler.cpp allows to solve problem with compilers that
8don't throw exception in operator new().
9
10This file must be included before any code that uses operators new() or delete()
11and you must compile and link "NewHandler.cpp", if you use some old MSVC compiler.
12
13The operator new() in some MSVC versions doesn't throw exception std::bad_alloc.
14MSVC 6.0 (_MSC_VER == 1200) doesn't throw exception.
15The code produced by some another MSVC compilers also can be linked
16to library that doesn't throw exception.
17We suppose that code compiled with VS2015+ (_MSC_VER >= 1900) throws exception std::bad_alloc.
18For older _MSC_VER versions we redefine operator new() and operator delete().
19Our version of operator new() throws CNewException() exception on failure.
20
21It's still allowed to use redefined version of operator new() from "NewHandler.cpp"
22with any compiler. 7-Zip's code can work with std::bad_alloc and CNewException() exceptions.
23But if you use some additional code (outside of 7-Zip's code), you must check
24that 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/*
32void * my_new(size_t size);
33void 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
50class CNewException {};
51
52void *
53#ifdef _MSC_VER
54__cdecl
55#endif
56operator new(size_t size);
57
58void
59#ifdef _MSC_VER
60__cdecl
61#endif
62operator 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
74void *
75#ifdef _MSC_VER
76__cdecl
77#endif
78operator new[](size_t size);
79
80void
81#ifdef _MSC_VER
82__cdecl
83#endif
84operator delete[](void *p) throw();
85#endif
86*/
87
88#endif