]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/smart_ptr/src/sp_debug_hooks.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / smart_ptr / src / sp_debug_hooks.cpp
1 //
2 // sp_debug_hooks.cpp
3 //
4 // Copyright (c) 2002, 2003 Peter Dimov
5 //
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
12
13 #include <boost/assert.hpp>
14 #include <new>
15 #include <cstdlib>
16
17 int const m = 2; // m * sizeof(int) must be aligned appropriately
18
19 // magic values to mark heap blocks with
20
21 int const allocated_scalar = 0x1234560C;
22 int const allocated_array = 0x1234560A;
23 int const adopted_scalar = 0x0567890C;
24 int const adopted_array = 0x0567890A;
25 int const deleted = 0x498769DE;
26
27 using namespace std; // for compilers where things aren't in std
28
29 // operator new
30
31 static new_handler get_new_handler()
32 {
33 new_handler p = set_new_handler(0);
34 set_new_handler(p);
35 return p;
36 }
37
38 static void * allocate(size_t n, int mark)
39 {
40 int * pm;
41
42 for(;;)
43 {
44 pm = static_cast<int*>(malloc(n + m * sizeof(int)));
45
46 if(pm != 0) break;
47
48 if(new_handler pnh = get_new_handler())
49 {
50 pnh();
51 }
52 else
53 {
54 return 0;
55 }
56 }
57
58 *pm = mark;
59
60 return pm + m;
61 }
62
63 void * operator new(size_t n) throw(bad_alloc)
64 {
65 void * p = allocate(n, allocated_scalar);
66
67 #if !defined(BOOST_NO_EXCEPTIONS)
68
69 if(p == 0) throw bad_alloc();
70
71 #endif
72
73 return p;
74 }
75
76 #if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
77
78 void * operator new(size_t n, nothrow_t const &) throw()
79 {
80 return allocate(n, allocated_scalar);
81 }
82
83 #endif
84
85 void * operator new[](size_t n) throw(bad_alloc)
86 {
87 void * p = allocate(n, allocated_array);
88
89 #if !defined(BOOST_NO_EXCEPTIONS)
90
91 if(p == 0) throw bad_alloc();
92
93 #endif
94
95 return p;
96 }
97
98 #if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
99
100 void * operator new[](size_t n, nothrow_t const &) throw()
101 {
102 return allocate(n, allocated_array);
103 }
104
105 #endif
106
107 // debug hooks
108
109 namespace boost
110 {
111
112 void sp_scalar_constructor_hook(void * p)
113 {
114 if(p == 0) return;
115
116 int * pm = static_cast<int*>(p);
117 pm -= m;
118
119 BOOST_ASSERT(*pm != adopted_scalar); // second smart pointer to the same address
120 BOOST_ASSERT(*pm != allocated_array); // allocated with new[]
121 BOOST_ASSERT(*pm == allocated_scalar); // not allocated with new
122
123 *pm = adopted_scalar;
124 }
125
126 void sp_scalar_constructor_hook(void * px, std::size_t, void *)
127 {
128 sp_scalar_constructor_hook(px);
129 }
130
131 void sp_scalar_destructor_hook(void * p)
132 {
133 if(p == 0) return;
134
135 int * pm = static_cast<int*>(p);
136 pm -= m;
137
138 BOOST_ASSERT(*pm == adopted_scalar); // attempt to destroy nonmanaged block
139
140 *pm = allocated_scalar;
141 }
142
143 void sp_scalar_destructor_hook(void * px, std::size_t, void *)
144 {
145 sp_scalar_destructor_hook(px);
146 }
147
148 // It is not possible to handle the array hooks in a portable manner.
149 // The implementation typically reserves a bit of storage for the number
150 // of objects in the array, and the argument of the array hook isn't
151 // equal to the return value of operator new[].
152
153 void sp_array_constructor_hook(void * /* p */)
154 {
155 /*
156 if(p == 0) return;
157
158 // adjust p depending on the implementation
159
160 int * pm = static_cast<int*>(p);
161 pm -= m;
162
163 BOOST_ASSERT(*pm != adopted_array); // second smart array pointer to the same address
164 BOOST_ASSERT(*pm != allocated_scalar); // allocated with new
165 BOOST_ASSERT(*pm == allocated_array); // not allocated with new[]
166
167 *pm = adopted_array;
168 */
169 }
170
171 void sp_array_destructor_hook(void * /* p */)
172 {
173 /*
174 if(p == 0) return;
175
176 // adjust p depending on the implementation
177
178 int * pm = static_cast<int*>(p);
179 pm -= m;
180
181 BOOST_ASSERT(*pm == adopted_array); // attempt to destroy nonmanaged block
182
183 *pm = allocated_array;
184 */
185 }
186
187 } // namespace boost
188
189 // operator delete
190
191 void operator delete(void * p) throw()
192 {
193 if(p == 0) return;
194
195 int * pm = static_cast<int*>(p);
196 pm -= m;
197
198 BOOST_ASSERT(*pm != deleted); // double delete
199 BOOST_ASSERT(*pm != adopted_scalar); // delete p.get();
200 BOOST_ASSERT(*pm != allocated_array); // allocated with new[]
201 BOOST_ASSERT(*pm == allocated_scalar); // not allocated with new
202
203 *pm = deleted;
204
205 free(pm);
206 }
207
208 #if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
209
210 void operator delete(void * p, nothrow_t const &) throw()
211 {
212 ::operator delete(p);
213 }
214
215 #endif
216
217 void operator delete[](void * p) throw()
218 {
219 if(p == 0) return;
220
221 int * pm = static_cast<int*>(p);
222 pm -= m;
223
224 BOOST_ASSERT(*pm != deleted); // double delete
225 BOOST_ASSERT(*pm != adopted_scalar); // delete p.get();
226 BOOST_ASSERT(*pm != allocated_scalar); // allocated with new
227 BOOST_ASSERT(*pm == allocated_array); // not allocated with new[]
228
229 *pm = deleted;
230
231 free(pm);
232 }
233
234 #if !defined(__BORLANDC__) || (__BORLANDC__ > 0x551)
235
236 void operator delete[](void * p, nothrow_t const &) throw()
237 {
238 ::operator delete[](p);
239 }
240
241 #endif
242
243 #endif // defined(BOOST_SP_ENABLE_DEBUG_HOOKS)