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.

166 lines
4.9 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. * 12-12-01 BWT Switch to _getptd_noexit instead of getptd - it's ok to
  38. * throw bad_alloc instead of terminating.
  39. *
  40. *******************************************************************************/
  41. #include <stddef.h>
  42. #include <internal.h>
  43. #include <cruntime.h>
  44. #include <mtdll.h>
  45. #include <process.h>
  46. #include <new.h>
  47. #include <dbgint.h>
  48. /* pointer to old-style C++ new handler */
  49. _PNH _pnhHeap;
  50. /***
  51. *_PNH _set_new_handler(_PNH pnh) - set the new handler
  52. *
  53. *Purpose:
  54. * _set_new_handler is different from the ANSI C++ working standard definition
  55. * of set_new_handler. Therefore it has a leading underscore in its name.
  56. *
  57. *Entry:
  58. * Pointer to the new handler to be installed.
  59. *
  60. *Return:
  61. * Previous new handler
  62. *
  63. *******************************************************************************/
  64. _PNH __cdecl _set_new_handler(
  65. _PNH pnh
  66. )
  67. {
  68. _PNH pnhOld;
  69. /* lock the heap */
  70. _mlock(_HEAP_LOCK);
  71. pnhOld = _pnhHeap;
  72. _pnhHeap = pnh;
  73. /* unlock the heap */
  74. _munlock(_HEAP_LOCK);
  75. return(pnhOld);
  76. }
  77. /***
  78. *_PNH _query_new_handler(void) - query value of new handler
  79. *
  80. *Purpose:
  81. * Obtain current new handler value.
  82. *
  83. *Entry:
  84. * None
  85. *
  86. * WARNING: This function is OBSOLETE. Use _query_new_ansi_handler instead.
  87. *
  88. *Return:
  89. * Currently installed new handler
  90. *
  91. *******************************************************************************/
  92. _PNH __cdecl _query_new_handler (
  93. void
  94. )
  95. {
  96. return _pnhHeap;
  97. }
  98. /***
  99. *int _callnewh - call the appropriate new handler
  100. *
  101. *Purpose:
  102. * Call the appropriate new handler.
  103. *
  104. *Entry:
  105. * None
  106. *
  107. *Return:
  108. * 1 for success
  109. * 0 for failure
  110. * may throw bad_alloc
  111. *
  112. *******************************************************************************/
  113. extern "C" int __cdecl _callnewh(size_t size)
  114. {
  115. #ifdef ANSI_NEW_HANDLER
  116. /*
  117. * if ANSI C++ new handler is activated but not installed, throw exception
  118. */
  119. #ifdef _MT
  120. _ptiddata ptd;
  121. ptd = _getptd_noexit();
  122. if (!ptd)
  123. throw bad_alloc();
  124. if (ptd->_newh == NULL)
  125. throw bad_alloc();
  126. #else
  127. if (_defnewh == NULL)
  128. throw bad_alloc();
  129. #endif
  130. /*
  131. * if ANSI C++ new handler activated and installed, call it
  132. * if it returns, the new handler was successful, retry allocation
  133. */
  134. #ifdef _MT
  135. if (ptd->_newh != _NO_ANSI_NEW_HANDLER)
  136. (*(ptd->_newh))();
  137. #else
  138. if (_defnewh != _NO_ANSI_NEW_HANDLER)
  139. (*_defnewh)();
  140. #endif
  141. /* ANSI C++ new handler is inactivated, call old handler if installed */
  142. else
  143. #endif /* ANSI_NEW_HANDLER */
  144. {
  145. _PNH pnh = _pnhHeap;
  146. if ( (pnh == NULL) || ((*pnh)(size) == 0) )
  147. return 0;
  148. }
  149. return 1;
  150. }