aboutsummaryrefslogtreecommitdiff
path: root/CPP/Common/NewHandler.cpp
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.cpp
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.cpp')
-rw-r--r--CPP/Common/NewHandler.cpp163
1 files changed, 163 insertions, 0 deletions
diff --git a/CPP/Common/NewHandler.cpp b/CPP/Common/NewHandler.cpp
new file mode 100644
index 0000000..7e5b1d4
--- /dev/null
+++ b/CPP/Common/NewHandler.cpp
@@ -0,0 +1,163 @@
1// NewHandler.cpp
2
3#include "StdAfx.h"
4
5#include <stdlib.h>
6
7#include "NewHandler.h"
8
9// #define DEBUG_MEMORY_LEAK
10
11#ifndef DEBUG_MEMORY_LEAK
12
13#ifdef _7ZIP_REDEFINE_OPERATOR_NEW
14
15/*
16void * my_new(size_t size)
17{
18 // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
19 void *p = ::malloc(size);
20 if (p == 0)
21 throw CNewException();
22 return p;
23}
24
25void my_delete(void *p) throw()
26{
27 // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p);
28 ::free(p);
29}
30
31void * my_Realloc(void *p, size_t newSize, size_t oldSize)
32{
33 void *newBuf = my_new(newSize);
34 if (oldSize != 0)
35 memcpy(newBuf, p, oldSize);
36 my_delete(p);
37 return newBuf;
38}
39*/
40
41void *
42#ifdef _MSC_VER
43__cdecl
44#endif
45operator new(size_t size)
46{
47 // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
48 void *p = ::malloc(size);
49 if (p == 0)
50 throw CNewException();
51 return p;
52}
53
54void
55#ifdef _MSC_VER
56__cdecl
57#endif
58operator delete(void *p) throw()
59{
60 // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p);
61 ::free(p);
62}
63
64/*
65void *
66#ifdef _MSC_VER
67__cdecl
68#endif
69operator new[](size_t size)
70{
71 // void *p = ::HeapAlloc(::GetProcessHeap(), 0, size);
72 void *p = ::malloc(size);
73 if (p == 0)
74 throw CNewException();
75 return p;
76}
77
78void
79#ifdef _MSC_VER
80__cdecl
81#endif
82operator delete[](void *p) throw()
83{
84 // if (p == 0) return; ::HeapFree(::GetProcessHeap(), 0, p);
85 ::free(p);
86}
87*/
88
89#endif
90
91#else
92
93#include <stdio.h>
94
95// #pragma init_seg(lib)
96const int kDebugSize = 1000000;
97static void *a[kDebugSize];
98static int index = 0;
99
100static int numAllocs = 0;
101void * __cdecl operator new(size_t size)
102{
103 numAllocs++;
104 void *p = HeapAlloc(GetProcessHeap(), 0, size);
105 if (index < kDebugSize)
106 {
107 a[index] = p;
108 index++;
109 }
110 if (p == 0)
111 throw CNewException();
112 printf("Alloc %6d, size = %8u\n", numAllocs, (unsigned)size);
113 return p;
114}
115
116class CC
117{
118public:
119 CC()
120 {
121 for (int i = 0; i < kDebugSize; i++)
122 a[i] = 0;
123 }
124 ~CC()
125 {
126 for (int i = 0; i < kDebugSize; i++)
127 if (a[i] != 0)
128 return;
129 }
130} g_CC;
131
132
133void __cdecl operator delete(void *p)
134{
135 if (p == 0)
136 return;
137 /*
138 for (int i = 0; i < index; i++)
139 if (a[i] == p)
140 a[i] = 0;
141 */
142 HeapFree(GetProcessHeap(), 0, p);
143 numAllocs--;
144 printf("Free %d\n", numAllocs);
145}
146
147#endif
148
149/*
150int MemErrorVC(size_t)
151{
152 throw CNewException();
153 // return 1;
154}
155CNewHandlerSetter::CNewHandlerSetter()
156{
157 // MemErrorOldVCFunction = _set_new_handler(MemErrorVC);
158}
159CNewHandlerSetter::~CNewHandlerSetter()
160{
161 // _set_new_handler(MemErrorOldVCFunction);
162}
163*/