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.

85 lines
1.9 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996 - 1999.
  5. //
  6. // File: falloc.hxx
  7. //
  8. // Contents: fast memory allocator
  9. //
  10. // History: 15-Mar-96 dlee Created.
  11. //
  12. //----------------------------------------------------------------------------
  13. #pragma once
  14. // alignment for all allocations
  15. const ULONG cbMemAlignment = 8;
  16. inline ULONG memAlignBlock( ULONG x )
  17. {
  18. return ( x + ( cbMemAlignment - 1 ) ) & ~( cbMemAlignment - 1 );
  19. }
  20. void * memAlloc( UINT ui );
  21. void memFree( void * p );
  22. UINT memSize( void const * p );
  23. BOOL memIsValidPointer( const void * p );
  24. void memUtilization();
  25. //+---------------------------------------------------------------------------
  26. //
  27. // Class: CMemMutex
  28. //
  29. // Purpose: Class for the global heap lock
  30. //
  31. // History: 25-Oct-96 dlee Created.
  32. //
  33. //----------------------------------------------------------------------------
  34. class CMemMutex
  35. {
  36. public:
  37. CMemMutex()
  38. {
  39. // two-phase construction to deal with exception on initialization
  40. memset( &_cs, 0, sizeof _cs );
  41. }
  42. ~CMemMutex()
  43. {
  44. DeleteCriticalSection( &_cs );
  45. }
  46. void Init()
  47. {
  48. // The high bit means the event is pre-allocated, so it won't
  49. // fail to be allocated while we're unwinding an exception.
  50. InitializeCriticalSectionAndSpinCount( &_cs, 0x80000500 );
  51. }
  52. void Enter()
  53. {
  54. ciAssert( !IsHeld() ); // valid, but would be wasteful
  55. EnterCriticalSection( &_cs );
  56. }
  57. void Leave()
  58. {
  59. ciAssert( IsHeld() ); // valid, but would be wasteful
  60. LeaveCriticalSection( &_cs );
  61. }
  62. BOOL IsHeld()
  63. {
  64. return ( LongToHandle( GetCurrentThreadId() ) == _cs.OwningThread );
  65. }
  66. private:
  67. CRITICAL_SECTION _cs;
  68. };