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.

110 lines
3.1 KiB

  1. /***
  2. *ehvccctr.cpp - EH-aware version of copy constructor iterator helper function
  3. *
  4. * Copyright (c) 2000-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. * 04-27-00 JJS File created
  16. *
  17. ****/
  18. #include <cruntime.h>
  19. #include <eh.h>
  20. #if defined (_M_IX86)
  21. #define CALLTYPE __thiscall
  22. #else
  23. #define CALLTYPE __stdcall
  24. #endif
  25. #ifdef _WIN32
  26. void __stdcall __ArrayUnwind(
  27. void* ptr, // Pointer to array to destruct
  28. size_t size, // Size of each element (including padding)
  29. int count, // Number of elements in the array
  30. void(CALLTYPE *pDtor)(void*) // The destructor to call
  31. );
  32. void __stdcall __ehvec_copy_ctor(
  33. void* dst, // Pointer to destination array
  34. void* src, // Pointer to source array
  35. size_t size, // Size of each element (including padding)
  36. int count, // Number of elements in the array
  37. void(CALLTYPE *pCopyCtor)(void*,void*), // Constructor to call
  38. void(CALLTYPE *pDtor)(void*) // Destructor to call should exception be thrown
  39. ){
  40. int i; // Count of elements constructed
  41. int success = 0;
  42. __try
  43. {
  44. // Construct the elements of the array
  45. for( i = 0; i < count; i++ )
  46. {
  47. (*pCopyCtor)( dst, src );
  48. dst = (char*)dst + size;
  49. src = (char*)src + size;
  50. }
  51. success = 1;
  52. }
  53. __finally
  54. {
  55. if (!success)
  56. __ArrayUnwind(dst, size, i, pDtor);
  57. }
  58. }
  59. #else
  60. void __stdcall __ehvec_copy_ctor(
  61. void* dst, // Pointer to destination array
  62. void* src, // Pointer to source array
  63. size_t size, // Size of each element (including padding)
  64. int count, // Number of elements in the array
  65. void(CALLTYPE *pCopyCtor)(void*, void*), // Constructor to call
  66. void(CALLTYPE *pDtor)(void*) // Destructor to call should exception be thrown
  67. ){
  68. int i; // Count of elements constructed
  69. try
  70. {
  71. // Construct the elements of the array
  72. for( i = 0; i < count; i++ )
  73. {
  74. (*pCopyCtor)( dst, src );
  75. dst = (char*)dst + size;
  76. src = (char*)src + size;
  77. }
  78. }
  79. catch(...)
  80. {
  81. // If a constructor throws, unwind the portion of the array thus
  82. // far constructed.
  83. for( i--; i >= 0; i-- )
  84. {
  85. dst = (char*)dst - size;
  86. try {
  87. (*pDtor)(dst);
  88. }
  89. catch(...) {
  90. // If the destructor threw during the unwind, quit
  91. terminate();
  92. }
  93. }
  94. // Propagate the exception to surrounding frames
  95. throw;
  96. }
  97. }
  98. #endif