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.

118 lines
3.5 KiB

  1. /***
  2. *ehvecctr.cpp - EH-aware version of constructor iterator helper function
  3. *
  4. * Copyright (c) 1990-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * These functions are called when constructing and destructing arrays of
  8. * objects. Their purpose is to assure that constructed elements get
  9. * destructed if the constructor for one of the elements throws.
  10. *
  11. * Must be compiled using "-d1Binl" to be able to specify __thiscall
  12. * at the user level
  13. *
  14. *Revision History:
  15. * 10-11-93 JDR Module created
  16. * 05-09-94 BES Module adapted for CRT source conventions
  17. * 05-13-94 SKS Remove _CRTIMP modifier
  18. * 10-10-94 CFW Fix EH/SEH exception handling.
  19. * 10-17-94 BWT Disable code for PPC.
  20. * 11-09-94 CFW Back out 10-10-94 change.
  21. * 02-08-95 JWM Mac merge.
  22. * 04-14-95 JWM Re-fix EH/SEH exception handling.
  23. * 04-17-95 JWM Restore non-WIN32 behavior.
  24. * 05-17-99 PML Remove all Macintosh support.
  25. * 05-20-99 PML Turn off __thiscall for IA64.
  26. * 07-12-99 RDL Image relative fixes under CC_P7_SOFT25.
  27. * 03-15-00 PML Remove CC_P7_SOFT25, which is now on permanently.
  28. *
  29. ****/
  30. #include <cruntime.h>
  31. #include <eh.h>
  32. #if defined (_M_IX86)
  33. #define CALLTYPE __thiscall
  34. #else
  35. #define CALLTYPE __stdcall
  36. #endif
  37. #ifdef _WIN32
  38. void __stdcall __ArrayUnwind(
  39. void* ptr, // Pointer to array to destruct
  40. size_t size, // Size of each element (including padding)
  41. int count, // Number of elements in the array
  42. void(CALLTYPE *pDtor)(void*) // The destructor to call
  43. );
  44. void __stdcall __ehvec_ctor(
  45. void* ptr, // Pointer to array to destruct
  46. size_t size, // Size of each element (including padding)
  47. int count, // Number of elements in the array
  48. void(CALLTYPE *pCtor)(void*), // Constructor to call
  49. void(CALLTYPE *pDtor)(void*) // Destructor to call should exception be thrown
  50. ){
  51. int i; // Count of elements constructed
  52. int success = 0;
  53. __try
  54. {
  55. // Construct the elements of the array
  56. for( i = 0; i < count; i++ )
  57. {
  58. (*pCtor)( ptr );
  59. ptr = (char*)ptr + size;
  60. }
  61. success = 1;
  62. }
  63. __finally
  64. {
  65. if (!success)
  66. __ArrayUnwind(ptr, size, i, pDtor);
  67. }
  68. }
  69. #else
  70. void __stdcall __ehvec_ctor(
  71. void* ptr, // Pointer to array to destruct
  72. unsigned size, // Size of each element (including padding)
  73. int count, // Number of elements in the array
  74. void(CALLTYPE *pCtor)(void*), // Constructor to call
  75. void(CALLTYPE *pDtor)(void*) // Destructor to call should exception be thrown
  76. ){
  77. int i; // Count of elements constructed
  78. try
  79. {
  80. // Construct the elements of the array
  81. for( i = 0; i < count; i++ )
  82. {
  83. (*pCtor)( ptr );
  84. ptr = (char*)ptr + size;
  85. }
  86. }
  87. catch(...)
  88. {
  89. // If a constructor throws, unwind the portion of the array thus
  90. // far constructed.
  91. for( i--; i >= 0; i-- )
  92. {
  93. ptr = (char*)ptr - size;
  94. try {
  95. (*pDtor)(ptr);
  96. }
  97. catch(...) {
  98. // If the destructor threw during the unwind, quit
  99. terminate();
  100. }
  101. }
  102. // Propagate the exception to surrounding frames
  103. throw;
  104. }
  105. }
  106. #endif