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__ < 0x570) 25// new.h uses ::malloc ;( 26# include _STLP_NATIVE_CPP_C_HEADER(cstdlib) 27using _STLP_VENDOR_CSTD::malloc; 28#endif 29 30#if !defined (_STLP_NO_NEW_NEW_HEADER) 31// eMbedded Visual C++ .NET unfortunately uses _INC_NEW for both <new.h> and <new> 32// we undefine the symbol to get the stuff in the SDK's <new> 33# if defined (_STLP_WCE_NET) && defined (_INC_NEW) 34# undef _INC_NEW 35# endif 36 37# if defined (new) 38/* STLport cannot replace native Std library new header if new is a macro, 39 * please define new macro after <new> header inclusion. 40 */ 41# error Cannot include native new header as new is a macro. 42# endif 43 44# if defined (_STLP_HAS_INCLUDE_NEXT) 45# include_next <new> 46# else 47# include _STLP_NATIVE_CPP_RUNTIME_HEADER(new) 48# endif 49#else 50# include <new.h> 51#endif 52 53#if defined (_STLP_NO_BAD_ALLOC) && !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC) 54# define _STLP_NEW_DONT_THROW_BAD_ALLOC 1 55#endif 56 57#if defined (_STLP_USE_EXCEPTIONS) && defined (_STLP_NEW_DONT_THROW_BAD_ALLOC) 58 59# ifndef _STLP_INTERNAL_EXCEPTION 60# include <stl/_exception.h> 61# endif 62 63_STLP_BEGIN_NAMESPACE 64 65# if defined (_STLP_NO_BAD_ALLOC) 66struct nothrow_t {}; 67# define nothrow nothrow_t() 68# endif 69 70/* 71 * STLport own bad_alloc exception to be used if the native C++ library 72 * do not define it or when the new operator do not throw it to avoid 73 * a useless library dependency. 74 */ 75class bad_alloc : public exception { 76public: 77 bad_alloc () _STLP_NOTHROW_INHERENTLY { } 78 bad_alloc(const bad_alloc&) _STLP_NOTHROW_INHERENTLY { } 79 bad_alloc& operator=(const bad_alloc&) _STLP_NOTHROW_INHERENTLY {return *this;} 80 ~bad_alloc () _STLP_NOTHROW_INHERENTLY { } 81 const char* what() const _STLP_NOTHROW_INHERENTLY { return "bad alloc"; } 82}; 83 84_STLP_END_NAMESPACE 85 86#endif /* _STLP_USE_EXCEPTIONS && (_STLP_NO_BAD_ALLOC || _STLP_NEW_DONT_THROW_BAD_ALLOC) */ 87 88#if defined (_STLP_USE_OWN_NAMESPACE) 89 90_STLP_BEGIN_NAMESPACE 91 92# if !defined (_STLP_NEW_DONT_THROW_BAD_ALLOC) 93using _STLP_VENDOR_EXCEPT_STD::bad_alloc; 94# endif 95 96# if !defined (_STLP_NO_BAD_ALLOC) 97using _STLP_VENDOR_EXCEPT_STD::nothrow_t; 98using _STLP_VENDOR_EXCEPT_STD::nothrow; 99# if defined (_STLP_GLOBAL_NEW_HANDLER) 100using ::new_handler; 101using ::set_new_handler; 102# else 103using _STLP_VENDOR_EXCEPT_STD::new_handler; 104using _STLP_VENDOR_EXCEPT_STD::set_new_handler; 105# endif 106# endif /* !_STLP_NO_BAD_ALLOC */ 107 108_STLP_END_NAMESPACE 109#endif /* _STLP_USE_OWN_NAMESPACE */ 110 111#ifndef _STLP_THROW_BAD_ALLOC 112# if !defined (_STLP_USE_EXCEPTIONS) 113# ifndef _STLP_INTERNAL_CSTDIO 114# include <stl/_cstdio.h> 115# endif 116# define _STLP_THROW_BAD_ALLOC puts("out of memory\n"); abort() 117# else 118# define _STLP_THROW_BAD_ALLOC _STLP_THROW(_STLP_STD::bad_alloc()) 119# endif 120#endif 121 122#if defined (_STLP_NO_NEW_NEW_HEADER) || defined (_STLP_NEW_DONT_THROW_BAD_ALLOC) 123# define _STLP_CHECK_NULL_ALLOC(__x) void* __y = __x; if (__y == 0) { _STLP_THROW_BAD_ALLOC; } return __y 124#else 125# define _STLP_CHECK_NULL_ALLOC(__x) return __x 126#endif 127 128_STLP_BEGIN_NAMESPACE 129 130#if ((defined (__IBMCPP__) || defined (__OS400__) || defined (__xlC__) || defined (qTidyHeap)) && defined (_STLP_DEBUG_ALLOC)) 131inline void* _STLP_CALL __stl_new(size_t __n) { _STLP_CHECK_NULL_ALLOC(::operator new(__n, __FILE__, __LINE__)); } 132inline void _STLP_CALL __stl_delete(void* __p) { ::operator delete(__p, __FILE__, __LINE__); } 133#else 134inline void* _STLP_CALL __stl_new(size_t __n) { _STLP_CHECK_NULL_ALLOC(::operator new(__n)); } 135inline void _STLP_CALL __stl_delete(void* __p) { ::operator delete(__p); } 136#endif 137_STLP_END_NAMESPACE 138 139#endif /* _STLP_INTERNAL_NEW */ 140 141/* 142 * Local Variables: 143 * mode:C++ 144 * End: 145 */ 146