Source code of Windows XP (NT5)
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.

161 lines
4.5 KiB

  1. /***
  2. *handler.cpp - defines C++ setHandler routine
  3. *
  4. * Copyright (c) 1990-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines C++ setHandler routine.
  8. *
  9. *Revision History:
  10. * 05-07-90 WAJ Initial version.
  11. * 08-30-90 WAJ new now takes unsigned ints.
  12. * 08-08-91 JCR call _halloc/_hfree, not halloc/hfree
  13. * 08-13-91 KRS Change new.hxx to new.h. Fix copyright.
  14. * 08-13-91 JCR ANSI-compatible _set_new_handler names
  15. * 10-30-91 JCR Split new, delete, and handler into seperate sources
  16. * 11-13-91 JCR 32-bit version
  17. * 06-15-92 KRS Remove per-thread handler for multi-thread libs
  18. * 03-02-94 SKS Add _query_new_handler(), remove commented out
  19. * per-thread thread handler version of _set_new_h code.
  20. * 04-01-94 GJF Made declaration of _pnhHeap conditional on ndef
  21. * DLL_FOR_WIN32S.
  22. * 05-03-94 CFW Add set_new_handler.
  23. * 06-03-94 SKS Remove set_new_hander -- it does NOT conform to ANSI
  24. * C++ working standard. We may implement it later.
  25. * 09-21-94 SKS Fix typo: no leading _ on "DLL_FOR_WIN32S"
  26. * 02-01-95 GJF Merged in common\handler.cxx (used for Mac).
  27. * 02-01-95 GJF Comment out _query_new_handler for Mac builds
  28. * (temporary).
  29. * 04-13-95 CFW Add set_new_handler stub (asserts on non-NULL handler).
  30. * 05-08-95 CFW Official ANSI C++ new handler added.
  31. * 06-23-95 CFW ANSI new handler removed from build.
  32. * 12-28-95 JWM set_new_handler() moved to setnewh.cpp for granularity.
  33. * 10-02-96 GJF In _callnewh, use a local to hold the value of _pnhHeap
  34. * instead of asserting _HEAP_LOCK.
  35. * 09-22-97 JWM "OBSOLETE" warning removed from _set_new_handler().
  36. * 05-13-99 PML Remove Win32s
  37. *
  38. *******************************************************************************/
  39. #include <stddef.h>
  40. #include <internal.h>
  41. #include <cruntime.h>
  42. #include <mtdll.h>
  43. #include <process.h>
  44. #include <new.h>
  45. #include <dbgint.h>
  46. /* pointer to old-style C++ new handler */
  47. _PNH _pnhHeap;
  48. /***
  49. *_PNH _set_new_handler(_PNH pnh) - set the new handler
  50. *
  51. *Purpose:
  52. * _set_new_handler is different from the ANSI C++ working standard definition
  53. * of set_new_handler. Therefore it has a leading underscore in its name.
  54. *
  55. *Entry:
  56. * Pointer to the new handler to be installed.
  57. *
  58. *Return:
  59. * Previous new handler
  60. *
  61. *******************************************************************************/
  62. _PNH __cdecl _set_new_handler(
  63. _PNH pnh
  64. )
  65. {
  66. _PNH pnhOld;
  67. /* lock the heap */
  68. _mlock(_HEAP_LOCK);
  69. pnhOld = _pnhHeap;
  70. _pnhHeap = pnh;
  71. /* unlock the heap */
  72. _munlock(_HEAP_LOCK);
  73. return(pnhOld);
  74. }
  75. /***
  76. *_PNH _query_new_handler(void) - query value of new handler
  77. *
  78. *Purpose:
  79. * Obtain current new handler value.
  80. *
  81. *Entry:
  82. * None
  83. *
  84. * WARNING: This function is OBSOLETE. Use _query_new_ansi_handler instead.
  85. *
  86. *Return:
  87. * Currently installed new handler
  88. *
  89. *******************************************************************************/
  90. _PNH __cdecl _query_new_handler (
  91. void
  92. )
  93. {
  94. return _pnhHeap;
  95. }
  96. /***
  97. *int _callnewh - call the appropriate new handler
  98. *
  99. *Purpose:
  100. * Call the appropriate new handler.
  101. *
  102. *Entry:
  103. * None
  104. *
  105. *Return:
  106. * 1 for success
  107. * 0 for failure
  108. * may throw bad_alloc
  109. *
  110. *******************************************************************************/
  111. extern "C" int __cdecl _callnewh(size_t size)
  112. {
  113. #ifdef ANSI_NEW_HANDLER
  114. /*
  115. * if ANSI C++ new handler is activated but not installed, throw exception
  116. */
  117. #ifdef _MT
  118. _ptiddata ptd;
  119. ptd = _getptd();
  120. if (ptd->_newh == NULL)
  121. throw bad_alloc();
  122. #else
  123. if (_defnewh == NULL)
  124. throw bad_alloc();
  125. #endif
  126. /*
  127. * if ANSI C++ new handler activated and installed, call it
  128. * if it returns, the new handler was successful, retry allocation
  129. */
  130. #ifdef _MT
  131. if (ptd->_newh != _NO_ANSI_NEW_HANDLER)
  132. (*(ptd->_newh))();
  133. #else
  134. if (_defnewh != _NO_ANSI_NEW_HANDLER)
  135. (*_defnewh)();
  136. #endif
  137. /* ANSI C++ new handler is inactivated, call old handler if installed */
  138. else
  139. #endif /* ANSI_NEW_HANDLER */
  140. {
  141. _PNH pnh = _pnhHeap;
  142. if ( (pnh == NULL) || ((*pnh)(size) == 0) )
  143. return 0;
  144. }
  145. return 1;
  146. }