Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
memory.cpp
Go to the documentation of this file.
1
30#define _RWSTD_LIB_SRC
31
32#include <rw/_error.h>
33#include <rw/_mutex.h>
34
35#include <new>
36#include <stdlib.h>
37
38#include <rw/_defs.h>
39
40
42
43
44_RWSTD_EXPORT void*
45__rw_allocate (_RWSTD_SIZE_T nbytes, int /* = 0 */)
46{
47 void *ptr = 0;
48
49 _TRY {
50
51#ifndef _RWSTD_NO_NEW_OFLOW_SAFE
52
53 ptr = ::operator new (nbytes);
54
55#else // if defined (_RWSTD_NO_NEW_OFLOW_SAFE)
56
57 // operator new() doesn't check for argument overflow,
58 // check before calling it and fail if `n' looks too big
59
60 if (nbytes < (_RWSTD_SIZE_T)-1 - 256)
61 ptr = ::operator new (nbytes);
62
63#endif // _RWSTD_NO_NEW_OFLOW_SAFE
64
65 }
66 _CATCH (...) {
67 // prevent bad_alloc raised by the compiler
68 }
69
70 // fix broken ::operator new ()
71 if (0 == ptr)
72 _RW::__rw_throw (_RWSTD_ERROR_FIRST + 3 /* == bad_alloc */);
73
74 return ptr;
75}
76
77
79__rw_deallocate (void *p, _RWSTD_SIZE_T, int /* = 0 */)
80{
81 ::operator delete (p);
82}
83
84
85} // namespace __rw
86
87
88#if defined _RWSTD_NEW_INCLUDED
89
90// define operator new and delete and bad_alloc members only if our own
91// implementation's <new> header (as opposed to the compiler's native C++
92// Standard library) has been included; SunPro always #includes its own,
93// regardless of compiler options
94
95
96// The following operators are replaceable; provide definitions if they're
97// missing in the compiler support library, and _RWSTD_NO_EXT_OPERATOR_NEW
98// is not defined (i.e., not in strict ansi mode). This means that they will
99// no longer be replaceable, but at least they will be callable without the
100// user providing the definitions
101//
102#ifndef _RWSTD_NO_EXT_OPERATOR_NEW
103
104# ifdef _RWSTD_NO_OPERATOR_NEW_NOTHROW
105
106_RWSTD_EXPORT void*
107operator new (_RWSTD_SIZE_T __size, const _STD::nothrow_t&) _NEW_THROWS (())
108{
109 return malloc (__size);
110}
111
112# endif // _RWSTD_NO_OPERATOR_NEW_NOTHROW
113
114
115# if defined (_RWSTD_NO_OPERATOR_DELETE_NOTHROW) \
116 && !defined (_RWSTD_NO_PLACEMENT_DELETE)
117
118_RWSTD_EXPORT void
119operator delete (void* __ptr, const _STD::nothrow_t&) _NEW_THROWS (())
120{
121 free (__ptr);
122}
123
124# endif // _RWSTD_NO_OPERATOR_DELETE_NOTHROW && _RWSTD_NO_PLACEMENT_DELETE
125
126# ifdef _RWSTD_NO_OPERATOR_NEW_ARRAY
127
128_RWSTD_EXPORT void*
129operator new[] (_RWSTD_SIZE_T __size) _NEW_THROWS ((_RWSTD_BAD_ALLOC))
130{
131 return ::operator new (__size);
132}
133
134# endif // _RWSTD_NO_OPERATOR_NEW_ARRAY
135
136# ifdef _RWSTD_NO_OPERATOR_NEW_ARRAY_NOTHROW
137
138_RWSTD_EXPORT void*
139operator new[] (_RWSTD_SIZE_T __size, const _STD::nothrow_t&) _NEW_THROWS (())
140{
141 _TRY {
142 return ::operator new (__size);
143 }
144 _CATCH (...) {
145 void *__nil = 0; // prevent a g++ 2.95.2 warning
146 return __nil; // about operator new returning 0
147 }
148}
149
150# endif // _RWSTD_NO_OPERATOR_NEW_ARRAY_NOTHROW
151
152
153# ifdef _RWSTD_NO_OPERATOR_DELETE_ARRAY
154
155_RWSTD_EXPORT void
156operator delete[] (void* __ptr) _NEW_THROWS (())
157{
158 ::operator delete (__ptr);
159}
160
161# endif // _RWSTD_NO_OPERATOR_DELETE_ARRAY
162
163
164# if defined (_RWSTD_NO_OPERATOR_DELETE_ARRAY_NOTHROW) \
165 && !defined (_RWSTD_NO_PLACEMENT_DELETE)
166
167_RWSTD_EXPORT void
168operator delete[] (void* __ptr, const _STD::nothrow_t&) _NEW_THROWS (())
169{
170 ::operator delete[] (__ptr); // ::operator delete[] doesn't throw
171}
172
173# endif // _RWSTD_NO_OPERATOR_DELETE_NOTHROW && _RWSTD_NO_PLACEMENT_DELETE
174
175#endif // _RWSTD_NO_EXT_OPERATOR_NEW
176
177//**************************************************************************
178
179#ifdef _MSC_VER
180
181typedef int (*__rw_new_handler_t)(_RWSTD_SIZE_T);
182
183_RWSTD_DLLIMPORT __rw_new_handler_t _set_new_handler (__rw_new_handler_t);
184
186
187static _STD::new_handler __rw_new_handler /* = 0 */;
188
189static int __rw_new_handler_imp (_RWSTD_SIZE_T)
190{
191 _RWSTD_ASSERT (0 != __rw_new_handler);
192
193 __rw_new_handler ();
194
195 return 1;
196}
197
198} // namespace __rw
199
200
202
203
204// 18.4.2.3
207{
208 // MSVC's set_new_handler(h) calls assert(h == 0)
209 // use the _set_new_handler() interface instead
210
212
213 new_handler save = _RW::__rw_new_handler;
214
215 _RW::__rw_new_handler = handler;
216
217 _set_new_handler (_RW::__rw_new_handler_imp);
218
219 return save;
220}
221
222} // namespace std
223
224#endif // _MSC_VER
225
226//**************************************************************************
227
228
230
231// define std::nothrow only if the symbol doesn't appear
232// in the compiler support library to prevent linker errors
233// (with gcc -fno-honor-std, don't define if global version exists)
234// +----------+-------------------+--------------------+----------------+
235// | define? | _NO_STD_NOTHROW_T | _NO_GLOBAL_NOTHROW | _NO_HONOR_STD |
236// +----------+-------------------+--------------------+----------------+
237// | yes | defined | defined | defined |
238// +----------+-------------------+--------------------+----------------+
239// | yes | defined | defined | !defined |
240// +----------+-------------------+--------------------+----------------+
241// | no | defined | !defined | defined |
242// +----------+-------------------+--------------------+----------------+
243// | no | defined | !defined | !defined |
244// +----------+-------------------+--------------------+----------------+
245// | no | !defined | defined | defined |
246// +----------+-------------------+--------------------+----------------+
247// | no | !defined | defined | !defined |
248// +----------+-------------------+--------------------+----------------+
249// | no | !defined | !defined | defined |
250// +----------+-------------------+--------------------+----------------+
251// | no | !defined | !defined | !defined |
252// +----------+-------------------+--------------------+----------------+
253
254#if defined (_RWSTD_NO_STD_NOTHROW) \
255 && ( defined (_RWSTD_NO_GLOBAL_NOTHROW) \
256 || !defined (_RWSTD_NO_HONOR_STD))
257
258# if __HP_aCC != 33000 && __HP_aCC != 33100
259
260// HP aCC 3.30 fails to emit data symbols for extern consts
261_RWSTD_EXPORT extern const nothrow_t nothrow = nothrow_t ();
262
263# endif // __HP_aCC
264
265#endif // _RWSTD_NO_STD_NOTHROW && _RWSTD_NO_GLOBAL_NOTHROW || ...
266
267} // namespace std
268
269//**************************************************************************
270
271// class bad_alloc members
272
273# if !defined (_RWSTD_NO_STD_BAD_ALLOC) \
274 || !defined (_RWSTD_NO_RUNTIME_IN_STD)
275
277
278# endif // !_RWSTD_NO_STD_BAD_ALLOC || !_RWSTD_NO_RUNTIME_IN_STD ...
279
280
281# ifdef _RWSTD_NO_BAD_ALLOC_DEFAULT_CTOR
282
283bad_alloc::
284bad_alloc () _THROWS (())
285 : exception ()
286{
287 // empty
288}
289
290# endif // _RWSTD_NO_BAD_ALLOC_DEFAULT_CTOR
291
292# ifdef _RWSTD_NO_BAD_ALLOC_COPY_CTOR
293
294bad_alloc::
295bad_alloc (const bad_alloc &rhs) _THROWS (())
296 : exception (rhs)
297{
298 // empty
299}
300
301# endif // _RWSTD_NO_BAD_ALLOC_COPY_CTOR
302
303# ifdef _RWSTD_NO_BAD_ALLOC_DTOR
304# if !__INTEL_COMPILER || __INTEL_COMPILER > 800
305
306/* virtual */ bad_alloc::
307~bad_alloc () _THROWS (())
308{
309 // empty
310}
311# endif // Intel C++ > 8.0
312# endif // _RWSTD_NO_BAD_ALLOC_DTOR
313
314# ifdef _RWSTD_NO_BAD_ALLOC_ASSIGNMENT
315
316bad_alloc& bad_alloc::
317operator= (const bad_alloc &rhs) _THROWS (())
318{
319 exception::operator= (rhs);
320
321 return *this;
322}
323
324# endif // _RWSTD_NO_BAD_ALLOC_ASSIGNMENT
325
326# ifdef _RWSTD_NO_BAD_ALLOC_WHAT
327
328# if !__INTEL_COMPILER || __INTEL_COMPILER > 800
329
330/* virtual */ const char* bad_alloc::
331what () const _THROWS (())
332{
334}
335
336# endif // Intel C++ > 8.0
337# endif // _RWSTD_NO_BAD_ALLOC_WHAT
338
339# if !defined (_RWSTD_NO_STD_BAD_ALLOC) \
340 || !defined (_RWSTD_NO_RUNTIME_IN_STD)
341
342} // namespace std
343
344# endif // !_RWSTD_NO_STD_BAD_ALLOC || !_RWSTD_NO_RUNTIME_IN_STD ...
345#endif // _RWSTD_NEW_INCLUDED
#define rhs
Definition 17.names.cpp:536
#define ptr
Definition 17.names.cpp:525
#define save
Definition 17.names.cpp:538
#define p
Definition 17.names.cpp:132
void handler()
const nothrow_t nothrow
void(* new_handler)()
#define _THROWS()
int set_new_handler()
#define _RWSTD_DLLIMPORT
Definition _config.h:391
#define _RWSTD_EXPORT
Definition _defs.h:485
#define _TRY
Definition _defs.h:850
#define _RWSTD_NAMESPACE(name)
Definition _defs.h:567
#define _RWSTD_ASSERT(ignore)
Definition _defs.h:1108
#define _NEW_THROWS(args)
Definition _defs.h:1160
#define _CATCH(ex)
Definition _defs.h:851
#define _RWSTD_MT_STATIC_GUARD(name)
Definition _defs.h:696
#define _RWSTD_ERROR_BAD_ALLOC
Definition _error.h:144
#define _RWSTD_ERROR_FIRST
Definition _error.h:78
Definition json.h:5363
#define _RWSTD_BAD_ALLOC
Definition rw_new.h:141