1 /*
2  * Copyright (c) 1999
3  * Boris Fomitchev
4  *
5  * This material is provided "as is", with absolutely no warranty expressed
6  * or implied. Any use is at your own risk.
7  *
8  * Permission to use or copy this software for any purpose is hereby granted
9  * without fee, provided the above notices are retained on all copies.
10  * Permission to modify the code and to distribute modified code is granted,
11  * provided the above notices are retained, and a notice that the code was
12  * modified is included with the above copyright notice.
13  *
14  */
15 
16 #ifndef _STLP_INTERNAL_NEW
17 #define _STLP_INTERNAL_NEW
18 
19 #ifndef _STLP_INTERNAL_CSTDDEF
20 // size_t
21 #  include <stl/_cstddef.h>
22 #endif
23 
24 #if defined (__BORLANDC__) && (__BORLANDC__ < 0x580)
25 // new.h uses ::malloc ;(
26 #  include _STLP_NATIVE_CPP_C_HEADER(cstdlib)
27 using _STLP_VENDOR_CSTD::malloc;
28 #endif
29 
30 #if !defined (_STLP_NO_NEW_NEW_HEADER)
31 #  if defined (_STLP_BROKEN_BAD_ALLOC_CLASS)
32 #    define bad_alloc _STLP_NULLIFIED_BROKEN_BAD_ALLOC_CLASS
33 #    define nothrow_t _STLP_NULLIFIED_BROKEN_BAD_NOTHROW_T_CLASS
34 #    define nothrow _STLP_NULLIFIED_BROKEN_BAD_NOTHROW
35 #  endif
36 
37 // eMbedded Visual C++ .NET unfortunately uses _INC_NEW for both <new.h> and <new>
38 // we undefine the symbol to get the stuff in the SDK's <new>
39 #  if defined (_STLP_WCE_NET) && defined (_INC_NEW)
40 #    undef _INC_NEW
41 #  endif
42 
43 #  if defined (new)
44 /* STLport cannot replace native Std library new header if new is a macro,
45  * please define new macro after <new> header inclusion.
46  */
47 #    error Cannot include native new header as new is a macro.
48 #  endif
49 
50 #  include _STLP_NATIVE_CPP_RUNTIME_HEADER(new)
51 
52 #  if defined (_STLP_BROKEN_BAD_ALLOC_CLASS)
53 #    undef bad_alloc
54 #    undef nothrow_t
55 #    undef nothrow
56 #    undef _STLP_NULLIFIED_BROKEN_BAD_ALLOC_CLASS
57 #    undef _STLP_NULLIFIED_BROKEN_BAD_NOTHROW_T_CLASS
58 #    undef _STLP_NULLIFIED_BROKEN_BAD_NOTHROW
59 #  endif
60 #else
61 #  include <new.h>
62 #endif
63 
64 #if defined (_STLP_NO_BAD_ALLOC) && !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
65 #  define _STLP_NEW_DONT_THROW_BAD_ALLOC 1
66 #endif
67 
68 #if defined (_STLP_USE_EXCEPTIONS) && defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
69 
70 #  ifndef _STLP_INTERNAL_EXCEPTION
71 #    include <stl/_exception.h>
72 #  endif
73 
74 _STLP_BEGIN_NAMESPACE
75 
76 #  if defined (_STLP_NO_BAD_ALLOC)
77 struct nothrow_t {};
78 #    define nothrow nothrow_t()
79 #  endif
80 
81 /*
82  * STLport own bad_alloc exception to be used if the native C++ library
83  * do not define it or when the new operator do not throw it to avoid
84  * a useless library dependency.
85  */
86 class bad_alloc : public exception {
87 public:
bad_alloc()88   bad_alloc () _STLP_NOTHROW_INHERENTLY { }
bad_alloc(const bad_alloc &)89   bad_alloc(const bad_alloc&) _STLP_NOTHROW_INHERENTLY { }
90   bad_alloc& operator=(const bad_alloc&) _STLP_NOTHROW_INHERENTLY {return *this;}
~bad_alloc()91   ~bad_alloc () _STLP_NOTHROW_INHERENTLY { }
what()92   const char* what() const _STLP_NOTHROW_INHERENTLY { return "bad alloc"; }
93 };
94 
95 _STLP_END_NAMESPACE
96 
97 #endif /* _STLP_USE_EXCEPTIONS && (_STLP_NO_BAD_ALLOC || _STLP_NEW_DONT_THROW_BAD_ALLOC) */
98 
99 #if defined (_STLP_RTTI_BUG)
100 _STLP_BEGIN_NAMESPACE
101 
__stl_new(size_t __n)102 inline void* _STLP_CALL __stl_new(size_t __n)
103 { return ::malloc(__n); }
104 
__stl_delete(void * __p)105 inline void _STLP_CALL __stl_delete(void* __p)
106 { ::free(__p); }
107 _STLP_END_NAMESPACE
108 
109 #else /* _STLP_RTTI_BUG */
110 
111 #  if defined (_STLP_USE_OWN_NAMESPACE)
112 
113 _STLP_BEGIN_NAMESPACE
114 
115 #    if !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC)
116 using _STLP_VENDOR_EXCEPT_STD::bad_alloc;
117 #    endif
118 
119 #    if !defined (_STLP_NO_BAD_ALLOC)
120 using _STLP_VENDOR_EXCEPT_STD::nothrow_t;
121 using _STLP_VENDOR_EXCEPT_STD::nothrow;
122 #      if defined (_STLP_GLOBAL_NEW_HANDLER)
123 using ::new_handler;
124 using ::set_new_handler;
125 #      else
126 using _STLP_VENDOR_EXCEPT_STD::new_handler;
127 using _STLP_VENDOR_EXCEPT_STD::set_new_handler;
128 #      endif
129 #    endif /* !_STLP_NO_BAD_ALLOC */
130 
131 _STLP_END_NAMESPACE
132 #  endif /* _STLP_USE_OWN_NAMESPACE */
133 
134 #  if defined (_STLP_USE_EXCEPTIONS) && \
135      (defined (_STLP_NO_NEW_NEW_HEADER) || defined (_STLP_NEW_DONT_THROW_BAD_ALLOC))
136 #    define _STLP_CHECK_NULL_ALLOC(__x) void* __y = __x; if (__y == 0) { _STLP_THROW(_STLP_STD::bad_alloc()); } return __y
137 #  else
138 #    define _STLP_CHECK_NULL_ALLOC(__x) return __x
139 #  endif
140 
141 _STLP_BEGIN_NAMESPACE
142 
143 #  if ((defined (__IBMCPP__) || defined (__OS400__) || defined (__xlC__) || defined (qTidyHeap)) && defined (__DEBUG_ALLOC__))
144 inline void* _STLP_CALL __stl_new(size_t __n)   { _STLP_CHECK_NULL_ALLOC(::operator _STLP_NEW(__n, __FILE__, __LINE__)); }
145 inline void  _STLP_CALL __stl_delete(void* __p) { ::operator delete(__p, __FILE__, __LINE__); }
146 #  else
147 inline void* _STLP_CALL __stl_new(size_t __n)   { _STLP_CHECK_NULL_ALLOC(::operator _STLP_NEW(__n)); }
148 inline void  _STLP_CALL __stl_delete(void* __p) { ::operator delete(__p); }
149 #  endif
150 _STLP_END_NAMESPACE
151 
152 #endif /* _STLP_RTTI_BUG */
153 
154 #if defined(__SYMBIAN32__)
155 
156 #  if defined(__WINSCW__)
new(unsigned int s,const std::nothrow_t &)157 inline void* operator new(unsigned int s, const std::nothrow_t &) throw()
158 {
159 	return ::operator new(s);
160 }
throw()161 inline void* operator new[](unsigned int s, const std::nothrow_t &) throw()
162 {
163 	return ::operator new[](s);
164 }
delete(void * p,const std::nothrow_t &)165 inline void operator delete(void* p, const std::nothrow_t &) throw()
166 {
167 	return ::operator delete(p);
168 }
throw()169 inline void operator delete[](void* p, const std::nothrow_t &) throw()
170 {
171 	return ::operator delete[](p);
172 }
173 #  endif
174 
175 template<typename T>
_CHECK_PTR(T * p)176 inline T* _CHECK_PTR(T* p)
177 {
178 	if (!p)
179 		throw std::bad_alloc();
180 	return p;
181 }
182 
183 #else
184 
185 #  define _CHECK_PTR
186 
187 #endif
188 
189 
190 #endif /* _STLP_INTERNAL_NEW */
191 
192 
193 /*
194  * Local Variables:
195  * mode:C++
196  * End:
197  */
198