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.

147 lines
4.0 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1995 - 2000 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: templatepools.h
  6. * Content: Templated based pool objects derived from CFixedPool
  7. *
  8. * History:
  9. * Date By Reason
  10. * ====== == ======
  11. * 12-10-2001 simonpow Created
  12. ***************************************************************************/
  13. #ifndef __TEMPLATEPOOLS_H__
  14. #define __TEMPLATEPOOLS_H__
  15. /*
  16. * Usage Notes
  17. * There are three different pool types -
  18. * CObjectFixedPool, CDataBlockFixedPool, CMemBlockFixedPool
  19. * They all basically do the same thing, which is to provide a very thin wrapper
  20. * on CFixedPool, a wrapper which in turn imposes a certain usage policy.
  21. *
  22. * CObjectFixedPool ensures the templated object type is correctly constructed and
  23. * destructed on a pool alloc/dealloc. It forwards pool Init/Release calls
  24. * to FPMInitialize and FPMRelease methods on the templated object
  25. *
  26. * CDataBlockFixedPool doesn't provide any construction/destruction support, it
  27. * simply forwards the pool Init/Release calls to FPMInitialize and FPMRelease methods
  28. *
  29. * CMemBlockFixedPool doesn't provide any alloc/dealloc/init/release methods. It assumes
  30. * the user simply wants raw memory chunks
  31. */
  32. #include "fixedpool.h"
  33. //placement new operator. Used to construct objects at location provided
  34. //from CFixedPool base class
  35. inline void * operator new(size_t sz, void * v)
  36. { return v; };
  37. /*
  38. * CObjectFixedPool
  39. * Object allocated through this must provide a default constructor, a destructor,
  40. * a FPMInitialize method and an FPMRelease method
  41. */
  42. template <class T>
  43. class CObjectFixedPool : protected CFixedPool
  44. {
  45. public:
  46. CObjectFixedPool() : CFixedPool() {};
  47. ~CObjectFixedPool() {};
  48. BOOL Initialize()
  49. { return CFixedPool::Initialize(sizeof(T), FPMAlloc, FPMInitialize, FPMRelease, FPMDealloc); };
  50. void DeInitialize()
  51. { CFixedPool::DeInitialize(); };
  52. T * Get()
  53. { return (T* ) CFixedPool::Get(); };
  54. void Release(T * pItem)
  55. { CFixedPool::Release(pItem); };
  56. DWORD GetInUseCount()
  57. { return FixedPool::GetInUseCount(); };
  58. protected:
  59. static BOOL FPMAlloc(void * pvItem, void * pvContext)
  60. { new (pvItem) T(); return TRUE; };
  61. static void FPMInitialize(void * pvItem, void * pvContext)
  62. { ((T * ) pvItem)->FPMInitialize(); };
  63. static void FPMRelease(void * pvItem)
  64. { ((T * ) pvItem)->FPMRelease(); };
  65. static void FPMDealloc(void * pvItem)
  66. { ((T * ) pvItem)->~T(); };
  67. };
  68. /*
  69. * CDataBlockFixedPool
  70. * Object allocated through this must provide a FPMInitialize method and an
  71. * FPMRelease method. Their constructor/destructor will not be called
  72. */
  73. template <class T>
  74. class CDataBlockFixedPool : protected CFixedPool
  75. {
  76. public:
  77. CDataBlockFixedPool() : CFixedPool() {};
  78. ~CDataBlockFixedPool() {};
  79. BOOL Initialize()
  80. { return CFixedPool::Initialize(sizeof(T), NULL, FPMInitialize, FPMRelease, NULL); };
  81. void DeInitialize()
  82. { CFixedPool::DeInitialize(); };
  83. T * Get()
  84. { return (T* ) CFixedPool::Get(); };
  85. void Release(T * pItem)
  86. { CFixedPool::Release(pItem); };
  87. DWORD GetInUseCount()
  88. { return FixedPool::GetInUseCount(); };
  89. protected:
  90. static void FPMInitialize(void * pvItem, void * pvContext)
  91. { ((T * ) pvItem)->FPMInitialize(); };
  92. static void FPMRelease(void * pvItem)
  93. { ((T * ) pvItem)->FPMRelease(); };
  94. };
  95. /*
  96. * CMemBlockFixedPool
  97. * Object allocated through this will have no initialisation done
  98. */
  99. template <class T>
  100. class CMemBlockFixedPool : protected CFixedPool
  101. {
  102. public:
  103. CMemBlockFixedPool() : CFixedPool() {};
  104. ~CMemBlockFixedPool() {};
  105. BOOL Initialize()
  106. { return CFixedPool::Initialize(sizeof(T), NULL, NULL, NULL, NULL); };
  107. void DeInitialize()
  108. { CFixedPool::DeInitialize(); };
  109. T * Get()
  110. { return (T* ) CFixedPool::Get(); };
  111. void Release(T * pItem)
  112. { CFixedPool::Release(pItem); };
  113. DWORD GetInUseCount()
  114. { return FixedPool::GetInUseCount(); };
  115. };
  116. #endif //#ifndef __TEMPLATEPOOLS_H__