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.

119 lines
4.0 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 2001-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: HandleTable.h
  6. * Content: Handle Table Header File
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 07/21/2001 masonb Created
  12. *
  13. ***************************************************************************/
  14. #ifndef __HANDLETABLE_H__
  15. #define __HANDLETABLE_H__
  16. /***************************************************************************
  17. *
  18. * USAGE NOTES:
  19. *
  20. * - This is a generic handle table. It allows you to pass in a piece of
  21. * data and get a random handle back that can later be used to refer to
  22. * that data.
  23. * - The handle values of 0 and 0xFFFFFFFF (INVALID_HANDLE_VALUE) are
  24. * guaranteed never to be returned and therefore always represent an
  25. * invalid value.
  26. * - The associated data is returned by Destroy so it is rarely necessary to
  27. * do a Find immediately followed by a Destroy.
  28. * - Using Find without first calling Lock can be un-safe. Consider the
  29. * situation where you call Find without having called Lock and some
  30. * other thread comes behind and calls Destroy. You now are holding
  31. * data from the call to Find that the other thread may have freed. It
  32. * is best to call Lock, then Find, place your own reference on the data
  33. * and then call Unlock.
  34. *
  35. * IMPLEMENTATION NOTES:
  36. *
  37. * - Each slot in the handle table is guaranteed uniqueness for 256 uses.
  38. * - The handle table can hold a maximum of 16,777,214 (0xFFFFFE) entries.
  39. *
  40. ***************************************************************************/
  41. //**********************************************************************
  42. // Constant definitions
  43. //**********************************************************************
  44. //**********************************************************************
  45. // Macro definitions
  46. //**********************************************************************
  47. //**********************************************************************
  48. // Structure definitions
  49. //**********************************************************************
  50. //**********************************************************************
  51. // Variable definitions
  52. //**********************************************************************
  53. //**********************************************************************
  54. // Function prototypes
  55. //**********************************************************************
  56. //**********************************************************************
  57. // Class prototypes
  58. //**********************************************************************
  59. // class for Handle Table
  60. class CHandleTable
  61. {
  62. public:
  63. CHandleTable();
  64. ~CHandleTable();
  65. void Lock( void );
  66. void Unlock( void );
  67. HRESULT Initialize( void );
  68. void Deinitialize( void );
  69. HRESULT Create( PVOID const pvData, DPNHANDLE *const pHandle );
  70. HRESULT Destroy( const DPNHANDLE handle, PVOID *const ppvData );
  71. HRESULT Find( const DPNHANDLE handle, PVOID *const ppvData );
  72. #ifdef DPNBUILD_PREALLOCATEDMEMORYMODEL
  73. HRESULT SetTableSize( const DWORD dwNumEntries );
  74. #endif // DPNBUILD_PREALLOCATEDMEMORYMODEL
  75. private:
  76. struct _HANDLETABLE_ENTRY
  77. {
  78. BYTE bVersion; // This is incremented on each usage of a particular slot
  79. union
  80. {
  81. PVOID pvData; // This will contain the data associated with a handle
  82. DWORD dwIndex; // For a free slot, this points to the next free slot
  83. } Entry;
  84. };
  85. #ifndef DPNBUILD_PREALLOCATEDMEMORYMODEL
  86. HRESULT GrowTable( void );
  87. #endif // ! DPNBUILD_PREALLOCATEDMEMORYMODEL
  88. DWORD m_dwNumEntries;
  89. DWORD m_dwNumFreeEntries;
  90. DWORD m_dwFirstFreeEntry;
  91. DWORD m_dwLastFreeEntry;
  92. _HANDLETABLE_ENTRY* m_pTable;
  93. #ifndef DPNBUILD_ONLYONETHREAD
  94. DNCRITICAL_SECTION m_cs;
  95. #endif // !DPNBUILD_ONLYONETHREAD
  96. DEBUG_ONLY(BOOL m_fInitialized);
  97. };
  98. #endif // __HANDLETABLE_H__