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.

86 lines
1.6 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997, Microsoft Corp. All rights reserved.
  4. //
  5. // FILE
  6. //
  7. // newop.cpp
  8. //
  9. // SYNOPSIS
  10. //
  11. // Implmentation of the global new and delete operators.
  12. //
  13. // MODIFICATION HISTORY
  14. //
  15. // 08/19/1997 Original version.
  16. //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. #ifndef _NEWOP_CPP_
  19. #define _NEWOP_CPP_
  20. #include <cstdlib>
  21. #include <new>
  22. //////////
  23. // CRT function that calls the new handler directly.
  24. //////////
  25. extern "C" int __cdecl _callnewh(size_t);
  26. //////////
  27. // Standard new operator.
  28. //////////
  29. void* __cdecl operator new(size_t size) throw (std::bad_alloc)
  30. {
  31. void* p;
  32. // Loop until either we get the memory or the new handler fails.
  33. while ((p = malloc(size)) == 0)
  34. {
  35. if (!_callnewh(size))
  36. {
  37. throw std::bad_alloc();
  38. }
  39. }
  40. return p;
  41. }
  42. //////////
  43. // "No throw" version of the new operator.
  44. //////////
  45. void* __cdecl operator new(size_t size, const std::nothrow_t&) throw ()
  46. {
  47. void* p;
  48. // Loop until either we get the memory or the new handler fails.
  49. while ((p = malloc(size)) == 0)
  50. {
  51. if (!_callnewh(size))
  52. {
  53. return 0;
  54. }
  55. }
  56. return p;
  57. }
  58. //////////
  59. // Delete operator.
  60. //////////
  61. void __cdecl operator delete(void *p) throw()
  62. {
  63. #ifdef DEBUG
  64. // Clobber the memory.
  65. if (p) memset(p, 0xA3, _msize(p));
  66. #endif
  67. // The MSVC free() handles null pointers, so we don't have to check.
  68. free(p);
  69. }
  70. #endif // _NEWOP_CPP_