Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.8 KiB

  1. #ifndef DOTHROW_H
  2. #define DOTHROW_H
  3. #include <new>
  4. #include <corex.h>
  5. struct dothrow_t
  6. {
  7. static void raise_bad_alloc();
  8. static void raise_lock_failure();
  9. };
  10. struct wminothrow_t
  11. {
  12. static void raise_bad_alloc(){};
  13. static void raise_lock_failure(){};
  14. };
  15. extern const dothrow_t dothrow;
  16. extern const wminothrow_t wminothrow;
  17. typedef wminothrow_t NOTHROW;
  18. typedef dothrow_t DOTHROW;
  19. /*
  20. #if _MSC_VER > 1400
  21. void * _cdecl operator new[](size_t size, const dothrow_t& ex_spec)
  22. {
  23. void * p = ::operator new[](size);
  24. if (p) return p;
  25. dothrow_t::raise_bad_alloc();
  26. return p;
  27. };
  28. void _cdecl operator delete[](void * p, const dothrow_t&)
  29. {
  30. ::operator delete[](p);
  31. };
  32. #endif
  33. */
  34. inline void * _cdecl operator new(size_t size,const dothrow_t& ex_spec)
  35. {
  36. void * p = operator new(size);
  37. if (p) return p;
  38. dothrow_t::raise_bad_alloc();
  39. return p;
  40. };
  41. inline void _cdecl operator delete(void * p, const dothrow_t&)
  42. {
  43. operator delete(p);
  44. };
  45. // TEMPLATE CLASS allocator
  46. template<class Ty>
  47. class throw_allocator {
  48. public:
  49. typedef size_t size_type;
  50. typedef ptrdiff_t difference_type;
  51. typedef Ty *pointer;
  52. typedef const Ty *const_pointer;
  53. typedef Ty & reference;
  54. typedef const Ty & const_reference;
  55. typedef Ty value_type;
  56. pointer address(reference X) const
  57. {return (&X); }
  58. const_pointer address(const_reference X) const
  59. {return (&X); }
  60. pointer allocate(size_type N, const void *)
  61. {return (Allocate((difference_type)N, (pointer)0)); }
  62. char *_Charalloc(size_type N)
  63. {return (Allocate((difference_type)N,
  64. (char *)0)); }
  65. void deallocate(void *P, size_type)
  66. {operator delete(P,dothrow); }
  67. void construct(pointer P, const Ty& V)
  68. {Construct(P, V); }
  69. void destroy(pointer P)
  70. {Destroy(P); }
  71. size_t max_size() const
  72. {size_t N = (size_t)(-1) / sizeof (Ty);
  73. return (0 < N ? N : 1); }
  74. // TEMPLATE FUNCTION _Construct
  75. template<class T1, class T2> inline
  76. void Construct(T1 *P, const T2& V)
  77. {new ((void *)P) T1(V); }
  78. // TEMPLATE FUNCTION _Destroy
  79. template<class Ty> inline
  80. void Destroy(Ty *P)
  81. {_DESTRUCTOR(Ty, P); }
  82. inline void Destroy(char *)
  83. {}
  84. inline void Destroy(wchar_t *)
  85. {}
  86. // TEMPLATE FUNCTION _Allocate
  87. template<class Ty> inline
  88. Ty *Allocate(ptrdiff_t N, Ty *)
  89. {if (N < 0)
  90. N = 0;
  91. return ((Ty *)operator new(
  92. (size_t)N * sizeof (Ty),dothrow)); }
  93. };
  94. template<class Ty, class U> inline
  95. bool operator==(const throw_allocator<Ty>&, const throw_allocator<U>&)
  96. {return (true); }
  97. template<class Ty, class U> inline
  98. bool operator!=(const throw_allocator<Ty>&, const throw_allocator<U>&)
  99. {return (false); }
  100. // CLASS allocator<void>
  101. template<> class _CRTIMP throw_allocator<void> {
  102. public:
  103. typedef void Ty;
  104. typedef Ty *pointer;
  105. typedef const Ty *const_pointer;
  106. typedef Ty value_type;
  107. };
  108. #endif