OVR_NewOverride.inl 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /************************************************************************************
  2. PublicHeader: OVR_Kernel.h
  3. Filename : OVR_NewOverride.inl
  4. Content : New override for LibOVR Allocator
  5. Created : April 7, 2015
  6. Authors : Paul Pedriana, Chris Taylor
  7. Copyright : Copyright 2015 Oculus VR, LLC All Rights reserved.
  8. Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
  9. you may not use the Oculus VR Rift SDK except in compliance with the License,
  10. which is provided at the time of installation or download, or which
  11. otherwise accompanies this software in either electronic or hard copy form.
  12. You may obtain a copy of the License at
  13. http://www.oculusvr.com/licenses/LICENSE-3.2
  14. Unless required by applicable law or agreed to in writing, the Oculus VR SDK
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. ************************************************************************************/
  20. #ifndef OVR_NewOverride_inl
  21. #define OVR_NewOverride_inl
  22. #include "OVR_Allocator.h"
  23. #include <new>
  24. #if defined(_MSC_VER)
  25. #pragma warning(push, 0)
  26. #include <malloc.h>
  27. #include <crtdbg.h>
  28. #include <math.h> // Work around VS header bug by #including math.h then intrin.h.
  29. #if (_MSC_VER >= 1500)
  30. #include <intrin.h>
  31. #endif
  32. #pragma warning(pop)
  33. #if (_MSC_VER >= 1600) // VS2010+
  34. #pragma warning(disable : 4986) // 'operator delete[]': exception specification does not match previous declaration.
  35. #endif
  36. #endif
  37. #if defined(_MSC_VER)
  38. #define OVR_THROW_SPEC_NEW(X) __pragma(warning(push)) __pragma(warning(disable: 4987)) _THROWS(X) __pragma(warning(pop))
  39. #define OVR_THROW_SPEC_NEW_NONE() _THROW0()
  40. #define OVR_THROW_SPEC_DELETE_NONE() _THROW0()
  41. #else
  42. #define OVR_THROW_SPEC_NEW(x) throw(x)
  43. #define OVR_THROW_SPEC_NEW_NONE() throw()
  44. #define OVR_THROW_SPEC_DELETE_NONE() throw()
  45. #endif
  46. // Add common decorators here as neeeded.
  47. #define OVR_NEW_OVERRIDE_INLINE
  48. OVR_NEW_OVERRIDE_INLINE void* operator new(size_t size) OVR_THROW_SPEC_NEW(std::bad_alloc)
  49. {
  50. void* p = OVR_ALLOC(size);
  51. #if !defined(OVR_CPP_NO_EXCEPTIONS)
  52. if(!p)
  53. throw std::bad_alloc();
  54. #endif
  55. return p;
  56. }
  57. OVR_NEW_OVERRIDE_INLINE void* operator new[](size_t size) OVR_THROW_SPEC_NEW(std::bad_alloc)
  58. {
  59. void* p = OVR_ALLOC(size);
  60. #if !defined(OVR_CPP_NO_EXCEPTIONS)
  61. if(!p)
  62. throw std::bad_alloc();
  63. #endif
  64. return p;
  65. }
  66. OVR_NEW_OVERRIDE_INLINE void* operator new(size_t size, std::nothrow_t&) OVR_THROW_SPEC_NEW_NONE()
  67. {
  68. return OVR_ALLOC(size);
  69. }
  70. OVR_NEW_OVERRIDE_INLINE void* operator new[](size_t size, std::nothrow_t&) OVR_THROW_SPEC_NEW_NONE()
  71. {
  72. return OVR_ALLOC(size);
  73. }
  74. OVR_NEW_OVERRIDE_INLINE void* operator new(size_t size, const std::nothrow_t&) OVR_THROW_SPEC_NEW_NONE()
  75. {
  76. return OVR_ALLOC(size);
  77. }
  78. OVR_NEW_OVERRIDE_INLINE void* operator new[](size_t size, const std::nothrow_t&) OVR_THROW_SPEC_NEW_NONE()
  79. {
  80. return OVR_ALLOC(size);
  81. }
  82. OVR_NEW_OVERRIDE_INLINE void operator delete(void* p) OVR_THROW_SPEC_DELETE_NONE()
  83. {
  84. OVR_FREE(p);
  85. }
  86. OVR_NEW_OVERRIDE_INLINE void operator delete[](void* p) OVR_THROW_SPEC_DELETE_NONE()
  87. {
  88. OVR_FREE(p);
  89. }
  90. OVR_NEW_OVERRIDE_INLINE void operator delete(void* p, std::nothrow_t&) OVR_THROW_SPEC_DELETE_NONE()
  91. {
  92. OVR_FREE(p);
  93. }
  94. OVR_NEW_OVERRIDE_INLINE void operator delete[](void* p, std::nothrow_t&) OVR_THROW_SPEC_DELETE_NONE()
  95. {
  96. OVR_FREE(p);
  97. }
  98. OVR_NEW_OVERRIDE_INLINE void operator delete(void* p, const std::nothrow_t&) OVR_THROW_SPEC_DELETE_NONE()
  99. {
  100. OVR_FREE(p);
  101. }
  102. OVR_NEW_OVERRIDE_INLINE void operator delete[](void* p, const std::nothrow_t&) OVR_THROW_SPEC_DELETE_NONE()
  103. {
  104. OVR_FREE(p);
  105. }
  106. // The following new/delete overrides are required under VC++ because it defines the following operator new versions of its own.
  107. #if defined(_MSC_VER)
  108. OVR_NEW_OVERRIDE_INLINE void* operator new(size_t n, const char* /*fileName*/, int /*line*/)
  109. {
  110. return ::operator new(n);
  111. }
  112. OVR_NEW_OVERRIDE_INLINE void* operator new[](size_t n, const char* /*fileName*/, int /*line*/)
  113. {
  114. return ::operator new[](n);
  115. }
  116. OVR_NEW_OVERRIDE_INLINE void operator delete(void* p, const char* /*fileName*/, int /*line*/)
  117. {
  118. ::operator delete(p);
  119. }
  120. OVR_NEW_OVERRIDE_INLINE void operator delete[](void* p, const char* /*fileName*/, int /*line*/)
  121. {
  122. ::operator delete[](p);
  123. }
  124. OVR_NEW_OVERRIDE_INLINE void* operator new(size_t n, int /*debug*/, const char* /*fileName*/, int /*line*/)
  125. {
  126. return ::operator new (n);
  127. }
  128. OVR_NEW_OVERRIDE_INLINE void* operator new[](size_t n, int /*debug*/, const char* /*fileName*/, int /*line*/)
  129. {
  130. return ::operator new[](n);
  131. }
  132. OVR_NEW_OVERRIDE_INLINE void operator delete(void* p, int /*debug*/, const char* /*fileName*/, int /*line*/)
  133. {
  134. ::operator delete(p);
  135. }
  136. OVR_NEW_OVERRIDE_INLINE void operator delete[](void* p, int /*debug*/, const char* /*fileName*/, int /*line*/)
  137. {
  138. ::operator delete[](p);
  139. }
  140. #endif
  141. #endif // OVR_NewOverride_inl