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.

112 lines
2.4 KiB

  1. /******************************************************************************
  2. Copyright (c) 1999-2000 Microsoft Corporation
  3. Module Name:
  4. StaticCrt.cpp
  5. Abstract:
  6. This file contains the implementation of various CRT functions, so we don't
  7. need to link against MSVCP60.DLL
  8. Revision History:
  9. Davide Massarenti (Dmassare) 05/29/2000
  10. created
  11. ******************************************************************************/
  12. #include <stdafx.h>
  13. // CLASS bad_alloc_nocrt
  14. class bad_alloc_nocrt
  15. {
  16. public:
  17. bad_alloc_nocrt() _THROW0() {}
  18. ~bad_alloc_nocrt() _THROW0() {}
  19. };
  20. void __cdecl operator delete(void *ptr)
  21. {
  22. free( ptr );
  23. }
  24. void *__cdecl operator new( size_t cb )
  25. {
  26. void *res = malloc( cb );
  27. if(!res) throw bad_alloc_nocrt();
  28. return res;
  29. }
  30. _STD_BEGIN
  31. void __cdecl _XlenNR() { throw "string too long"; } // report a length_error
  32. void __cdecl _XranNR() { throw "invalid string position"; } // report an out_of_range error
  33. _STD_END
  34. #ifdef _MT
  35. #include <xstddef>
  36. #include <windows.h>
  37. _STD_BEGIN
  38. static CRITICAL_SECTION _CritSec;
  39. static long _InitFlag = 0L;
  40. static void __cdecl _CleanUp()
  41. {
  42. long InitFlagValue;
  43. if ( InitFlagValue = InterlockedExchange( &_InitFlag, 3L ) == 2L )
  44. // Should be okay to delete critical section
  45. DeleteCriticalSection( &_CritSec );
  46. }
  47. _Lockit::_Lockit()
  48. {
  49. // Most common case - just enter the critical section
  50. if ( _InitFlag == 2L ) {
  51. EnterCriticalSection( &_CritSec );
  52. return;
  53. }
  54. // Critical section either needs to be initialized.
  55. if ( _InitFlag == 0L ) {
  56. long InitFlagVal;
  57. if ( (InitFlagVal = InterlockedExchange( &_InitFlag, 1L )) == 0L ) {
  58. InitializeCriticalSection( &_CritSec );
  59. atexit( _CleanUp );
  60. _InitFlag = 2L;
  61. }
  62. else if ( InitFlagVal == 2L )
  63. _InitFlag = 2L;
  64. }
  65. // If necessary, wait while another thread finishes initializing the
  66. // critical section
  67. while ( _InitFlag == 1L )
  68. Sleep( 1 );
  69. if ( _InitFlag == 2L )
  70. EnterCriticalSection( &_CritSec );
  71. }
  72. _Lockit::~_Lockit()
  73. {
  74. if ( _InitFlag == 2L )
  75. LeaveCriticalSection( &_CritSec );
  76. }
  77. _STD_END
  78. #endif