xref: /reactos/sdk/include/c++/stlport/stl/_new.h (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * Copyright (c) 1999
3*c2c66affSColin Finck  * Boris Fomitchev
4*c2c66affSColin Finck  *
5*c2c66affSColin Finck  * This material is provided "as is", with absolutely no warranty expressed
6*c2c66affSColin Finck  * or implied. Any use is at your own risk.
7*c2c66affSColin Finck  *
8*c2c66affSColin Finck  * Permission to use or copy this software for any purpose is hereby granted
9*c2c66affSColin Finck  * without fee, provided the above notices are retained on all copies.
10*c2c66affSColin Finck  * Permission to modify the code and to distribute modified code is granted,
11*c2c66affSColin Finck  * provided the above notices are retained, and a notice that the code was
12*c2c66affSColin Finck  * modified is included with the above copyright notice.
13*c2c66affSColin Finck  *
14*c2c66affSColin Finck  */
15*c2c66affSColin Finck 
16*c2c66affSColin Finck #ifndef _STLP_INTERNAL_NEW
17*c2c66affSColin Finck #define _STLP_INTERNAL_NEW
18*c2c66affSColin Finck 
19*c2c66affSColin Finck #ifndef _STLP_INTERNAL_CSTDDEF
20*c2c66affSColin Finck // size_t
21*c2c66affSColin Finck #  include <stl/_cstddef.h>
22*c2c66affSColin Finck #endif
23*c2c66affSColin Finck 
24*c2c66affSColin Finck #if defined (__BORLANDC__) && (__BORLANDC__ < 0x570)
25*c2c66affSColin Finck // new.h uses ::malloc ;(
26*c2c66affSColin Finck #  include _STLP_NATIVE_CPP_C_HEADER(cstdlib)
27*c2c66affSColin Finck using _STLP_VENDOR_CSTD::malloc;
28*c2c66affSColin Finck #endif
29*c2c66affSColin Finck 
30*c2c66affSColin Finck #if !defined (_STLP_NO_NEW_NEW_HEADER)
31*c2c66affSColin Finck // eMbedded Visual C++ .NET unfortunately uses _INC_NEW for both <new.h> and <new>
32*c2c66affSColin Finck // we undefine the symbol to get the stuff in the SDK's <new>
33*c2c66affSColin Finck #  if defined (_STLP_WCE_NET) && defined (_INC_NEW)
34*c2c66affSColin Finck #    undef _INC_NEW
35*c2c66affSColin Finck #  endif
36*c2c66affSColin Finck 
37*c2c66affSColin Finck #  if defined (new)
38*c2c66affSColin Finck /* STLport cannot replace native Std library new header if new is a macro,
39*c2c66affSColin Finck  * please define new macro after <new> header inclusion.
40*c2c66affSColin Finck  */
41*c2c66affSColin Finck #    error Cannot include native new header as new is a macro.
42*c2c66affSColin Finck #  endif
43*c2c66affSColin Finck 
44*c2c66affSColin Finck #  if defined (_STLP_HAS_INCLUDE_NEXT)
45*c2c66affSColin Finck #    include_next <new>
46*c2c66affSColin Finck #  else
47*c2c66affSColin Finck #    include _STLP_NATIVE_CPP_RUNTIME_HEADER(new)
48*c2c66affSColin Finck #  endif
49*c2c66affSColin Finck #else
50*c2c66affSColin Finck #  include <new.h>
51*c2c66affSColin Finck #endif
52*c2c66affSColin Finck 
53*c2c66affSColin Finck #if defined (_STLP_NO_BAD_ALLOC) && !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
54*c2c66affSColin Finck #  define _STLP_NEW_DONT_THROW_BAD_ALLOC 1
55*c2c66affSColin Finck #endif
56*c2c66affSColin Finck 
57*c2c66affSColin Finck #if defined (_STLP_USE_EXCEPTIONS) && defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
58*c2c66affSColin Finck 
59*c2c66affSColin Finck #  ifndef _STLP_INTERNAL_EXCEPTION
60*c2c66affSColin Finck #    include <stl/_exception.h>
61*c2c66affSColin Finck #  endif
62*c2c66affSColin Finck 
63*c2c66affSColin Finck _STLP_BEGIN_NAMESPACE
64*c2c66affSColin Finck 
65*c2c66affSColin Finck #  if defined (_STLP_NO_BAD_ALLOC)
66*c2c66affSColin Finck struct nothrow_t {};
67*c2c66affSColin Finck #    define nothrow nothrow_t()
68*c2c66affSColin Finck #  endif
69*c2c66affSColin Finck 
70*c2c66affSColin Finck /*
71*c2c66affSColin Finck  * STLport own bad_alloc exception to be used if the native C++ library
72*c2c66affSColin Finck  * do not define it or when the new operator do not throw it to avoid
73*c2c66affSColin Finck  * a useless library dependency.
74*c2c66affSColin Finck  */
75*c2c66affSColin Finck class bad_alloc : public exception {
76*c2c66affSColin Finck public:
bad_alloc()77*c2c66affSColin Finck   bad_alloc () _STLP_NOTHROW_INHERENTLY { }
bad_alloc(const bad_alloc &)78*c2c66affSColin Finck   bad_alloc(const bad_alloc&) _STLP_NOTHROW_INHERENTLY { }
79*c2c66affSColin Finck   bad_alloc& operator=(const bad_alloc&) _STLP_NOTHROW_INHERENTLY {return *this;}
~bad_alloc()80*c2c66affSColin Finck   ~bad_alloc () _STLP_NOTHROW_INHERENTLY { }
what()81*c2c66affSColin Finck   const char* what() const _STLP_NOTHROW_INHERENTLY { return "bad alloc"; }
82*c2c66affSColin Finck };
83*c2c66affSColin Finck 
84*c2c66affSColin Finck _STLP_END_NAMESPACE
85*c2c66affSColin Finck 
86*c2c66affSColin Finck #endif /* _STLP_USE_EXCEPTIONS && (_STLP_NO_BAD_ALLOC || _STLP_NEW_DONT_THROW_BAD_ALLOC) */
87*c2c66affSColin Finck 
88*c2c66affSColin Finck #if defined (_STLP_USE_OWN_NAMESPACE)
89*c2c66affSColin Finck 
90*c2c66affSColin Finck _STLP_BEGIN_NAMESPACE
91*c2c66affSColin Finck 
92*c2c66affSColin Finck #  if !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
93*c2c66affSColin Finck using _STLP_VENDOR_EXCEPT_STD::bad_alloc;
94*c2c66affSColin Finck #  endif
95*c2c66affSColin Finck 
96*c2c66affSColin Finck #  if !defined (_STLP_NO_BAD_ALLOC)
97*c2c66affSColin Finck using _STLP_VENDOR_EXCEPT_STD::nothrow_t;
98*c2c66affSColin Finck using _STLP_VENDOR_EXCEPT_STD::nothrow;
99*c2c66affSColin Finck #    if defined (_STLP_GLOBAL_NEW_HANDLER)
100*c2c66affSColin Finck using ::new_handler;
101*c2c66affSColin Finck using ::set_new_handler;
102*c2c66affSColin Finck #    else
103*c2c66affSColin Finck using _STLP_VENDOR_EXCEPT_STD::new_handler;
104*c2c66affSColin Finck using _STLP_VENDOR_EXCEPT_STD::set_new_handler;
105*c2c66affSColin Finck #    endif
106*c2c66affSColin Finck #  endif /* !_STLP_NO_BAD_ALLOC */
107*c2c66affSColin Finck 
108*c2c66affSColin Finck _STLP_END_NAMESPACE
109*c2c66affSColin Finck #endif /* _STLP_USE_OWN_NAMESPACE */
110*c2c66affSColin Finck 
111*c2c66affSColin Finck #ifndef _STLP_THROW_BAD_ALLOC
112*c2c66affSColin Finck #  if !defined (_STLP_USE_EXCEPTIONS)
113*c2c66affSColin Finck #    ifndef _STLP_INTERNAL_CSTDIO
114*c2c66affSColin Finck #      include <stl/_cstdio.h>
115*c2c66affSColin Finck #    endif
116*c2c66affSColin Finck #    define _STLP_THROW_BAD_ALLOC puts("out of memory\n"); exit(1)
117*c2c66affSColin Finck #  else
118*c2c66affSColin Finck #    define _STLP_THROW_BAD_ALLOC _STLP_THROW(_STLP_STD::bad_alloc())
119*c2c66affSColin Finck #  endif
120*c2c66affSColin Finck #endif
121*c2c66affSColin Finck 
122*c2c66affSColin Finck #if defined (_STLP_NO_NEW_NEW_HEADER) || defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
123*c2c66affSColin Finck #  define _STLP_CHECK_NULL_ALLOC(__x) void* __y = __x; if (__y == 0) { _STLP_THROW_BAD_ALLOC; } return __y
124*c2c66affSColin Finck #else
125*c2c66affSColin Finck #  define _STLP_CHECK_NULL_ALLOC(__x) return __x
126*c2c66affSColin Finck #endif
127*c2c66affSColin Finck 
128*c2c66affSColin Finck _STLP_BEGIN_NAMESPACE
129*c2c66affSColin Finck 
130*c2c66affSColin Finck #if ((defined (__IBMCPP__) || defined (__OS400__) || defined (__xlC__) || defined (qTidyHeap)) && defined (_STLP_DEBUG_ALLOC))
__stl_new(size_t __n)131*c2c66affSColin Finck inline void* _STLP_CALL __stl_new(size_t __n)   { _STLP_CHECK_NULL_ALLOC(::operator new(__n, __FILE__, __LINE__)); }
__stl_delete(void * __p)132*c2c66affSColin Finck inline void  _STLP_CALL __stl_delete(void* __p) { ::operator delete(__p, __FILE__, __LINE__); }
133*c2c66affSColin Finck #else
134*c2c66affSColin Finck inline void* _STLP_CALL __stl_new(size_t __n)   { _STLP_CHECK_NULL_ALLOC(::operator new(__n)); }
135*c2c66affSColin Finck inline void  _STLP_CALL __stl_delete(void* __p) { ::operator delete(__p); }
136*c2c66affSColin Finck #endif
137*c2c66affSColin Finck _STLP_END_NAMESPACE
138*c2c66affSColin Finck 
139*c2c66affSColin Finck #endif /* _STLP_INTERNAL_NEW */
140*c2c66affSColin Finck 
141*c2c66affSColin Finck /*
142*c2c66affSColin Finck  * Local Variables:
143*c2c66affSColin Finck  * mode:C++
144*c2c66affSColin Finck  * End:
145*c2c66affSColin Finck  */
146