_new.h 4.2 KB

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