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.

106 lines
3.3 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1995-2002 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: fixedpool.h
  6. * Content: fixed size pool manager
  7. *
  8. * History:
  9. * Date By Reason
  10. * ====== == ======
  11. * 07-21-2001 masonb Created
  12. ***************************************************************************/
  13. #ifndef _FIXEDPOOL_H_
  14. #define _FIXEDPOOL_H_
  15. #include "CallStack.h"
  16. #include "dnslist.h"
  17. /***************************************************************************
  18. *
  19. * USAGE NOTES:
  20. *
  21. * - This is a generic fixed pool. It allows to reuse items once you have
  22. * allocated them so that you can save the time normally used allocating
  23. * and freeing.
  24. * - This pool may be used with classes, but you should be aware that the
  25. * class' constructor and destructor will not be called. This pool also
  26. * will not work on classes that inherit from a class that has virtual
  27. * functions that are not pure virtuals (ie interfaces are okay).
  28. *
  29. * IMPLEMENTATION NOTES:
  30. *
  31. * - This pool is non-invasive. In other words it does not utilize any of
  32. * the memory space alloted to the item itself to maintain its state.
  33. * - The pool can hold a maximum of sizeof(WORD) = 65535 items due to its
  34. * reliance on SLISTs.
  35. * - An element will be on either the Available or InUse queue. The InUse
  36. * queue is used only in debug for reporting memory leaks.
  37. *
  38. ***************************************************************************/
  39. typedef BOOL (*FN_BLOCKALLOC)(void * pvItem, void * pvContext);
  40. typedef VOID (*FN_BLOCKGET)(void * pvItem, void * pvContext);
  41. typedef VOID (*FN_BLOCKRELEASE)(void * pvItem);
  42. typedef VOID (*FN_BLOCKDEALLOC)(void *pvItem);
  43. class CFixedPool
  44. {
  45. public:
  46. struct FIXED_POOL_ITEM
  47. {
  48. DNSLIST_ENTRY slist; // Link to other elements
  49. #ifdef DBG
  50. CFixedPool* pThisPool; // This is used to ensure that items are returned to the correct pool (debug builds only)
  51. CCallStack callstack; // size=12 pointers
  52. #else // !DBG
  53. VOID* pAlignPad; // To stay heap aligned we need an even number of pointers (SLIST is one)
  54. #endif // DBG
  55. };
  56. BOOL Initialize(DWORD dwElementSize, // size of blocks in pool
  57. FN_BLOCKALLOC pfnBlockAlloc, // fn called for each new alloc
  58. FN_BLOCKGET pfnBlockGet, // fn called each time block used
  59. FN_BLOCKRELEASE pfnBlockRelease, // fn called each time block released
  60. FN_BLOCKDEALLOC pfnBlockDeAlloc // fn called before releasing mem
  61. );
  62. VOID DeInitialize();
  63. #ifdef DPNBUILD_PREALLOCATEDMEMORYMODEL
  64. DWORD Preallocate( DWORD dwCount, PVOID pvContext );
  65. #endif // DPNBUILD_PREALLOCATEDMEMORYMODEL
  66. VOID* Get( PVOID pvContext = NULL );
  67. VOID Release(VOID* pvItem);
  68. DWORD GetInUseCount();
  69. private:
  70. FN_BLOCKALLOC m_pfnBlockAlloc;
  71. FN_BLOCKGET m_pfnBlockGet;
  72. FN_BLOCKRELEASE m_pfnBlockRelease;
  73. FN_BLOCKDEALLOC m_pfnBlockDeAlloc;
  74. DWORD m_dwItemSize;
  75. DNSLIST_HEADER m_slAvailableElements;
  76. BOOL m_fInitialized;
  77. #ifdef DBG
  78. void DumpLeaks();
  79. DNSLIST_ENTRY* m_pInUseElements;
  80. #ifndef DPNBUILD_ONLYONETHREAD
  81. DNCRITICAL_SECTION m_csInUse;
  82. #endif // !DPNBUILD_ONLYONETHREAD
  83. LONG m_lAllocated;
  84. #endif // DBG
  85. LONG m_lInUse;
  86. };
  87. #endif // _FIXEDPOOL_H_