Source code of Windows XP (NT5)
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.

171 lines
4.5 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1998 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: RCBuff.h
  6. * Content: RefCount Buffers
  7. *
  8. * History:
  9. * Date By Reason
  10. * ==== == ======
  11. * 01/12/00 mjn Created
  12. * 01/15/00 mjn Added GetBufferAddress and GetBufferSize
  13. * 01/31/00 mjn Allow user defined Alloc and Free
  14. ***************************************************************************/
  15. #ifndef __RCBUFF_H__
  16. #define __RCBUFF_H__
  17. #undef DPF_SUBCOMP
  18. #define DPF_SUBCOMP DN_SUBCOMP_COMMON
  19. //**********************************************************************
  20. // Constant definitions
  21. //**********************************************************************
  22. //**********************************************************************
  23. // Macro definitions
  24. //**********************************************************************
  25. //**********************************************************************
  26. // Structure definitions
  27. //**********************************************************************
  28. typedef PVOID (*PFNALLOC_REFCOUNT_BUFFER)(void *const,const DWORD);
  29. typedef void (*PFNFREE_REFCOUNT_BUFFER)(void *const,void *const);
  30. template< class CRefCountBuffer > class CLockedContextClassFixedPool;
  31. //**********************************************************************
  32. // Variable definitions
  33. //**********************************************************************
  34. //**********************************************************************
  35. // Function prototypes
  36. //**********************************************************************
  37. PVOID RefCountBufferDefaultAlloc(void *const pv,const DWORD dwSize);
  38. void RefCountBufferDefaultFree(void *const pv,void *const pvBuffer);
  39. //**********************************************************************
  40. // Class prototypes
  41. //**********************************************************************
  42. // class for RefCount buffer
  43. class CRefCountBuffer
  44. {
  45. public:
  46. CRefCountBuffer() { }; // Constructor
  47. ~CRefCountBuffer() { }; // Destructor
  48. #undef DPF_MODNAME
  49. #define DPF_MODNAME "FPMAlloc"
  50. BOOL FPMAlloc( void *const pvContext )
  51. {
  52. m_pvContext = pvContext;
  53. return(TRUE);
  54. };
  55. #undef DPF_MODNAME
  56. #define DPF_MODNAME "FPMInitialize"
  57. BOOL FPMInitialize( void *const pvContext )
  58. {
  59. // DNASSERT(pvContext == m_pvContext);
  60. m_lRefCount = 1;
  61. m_pvContext = pvContext;
  62. m_dnBufferDesc.dwBufferSize = 0;
  63. m_dnBufferDesc.pBufferData = NULL;
  64. return(TRUE);
  65. };
  66. #undef DPF_MODNAME
  67. #define DPF_MODNAME "FPMRelease"
  68. void FPMRelease( void *const pvContext ) { };
  69. #undef DPF_MODNAME
  70. #define DPF_MODNAME "FPMDealloc"
  71. void FPMDealloc( void *const pvContext ) { };
  72. HRESULT Initialize( CLockedContextClassFixedPool <CRefCountBuffer> *pFPRefCountBuffer,
  73. PFNALLOC_REFCOUNT_BUFFER pfnAlloc,
  74. PFNFREE_REFCOUNT_BUFFER pfnFree,
  75. void *const pvContext,
  76. const DWORD dwBufferSize);
  77. #undef DPF_MODNAME
  78. #define DPF_MODNAME "ReturnSelfToPool"
  79. void ReturnSelfToPool()
  80. {
  81. DNASSERT(m_lRefCount == 0);
  82. m_pFPOOLRefCountBuffer->Release( this );
  83. };
  84. #undef DPF_MODNAME
  85. #define DPF_MODNAME "AddRef"
  86. void AddRef()
  87. {
  88. DNASSERT(m_lRefCount >= 0);
  89. InterlockedIncrement( &m_lRefCount );
  90. };
  91. void Release();
  92. #undef DPF_MODNAME
  93. #define DPF_MODNAME "SetBufferDesc"
  94. HRESULT SetBufferDesc( BYTE *const pBufferData,
  95. const DWORD dwBufferSize,
  96. PFNFREE_REFCOUNT_BUFFER pfnFree,
  97. void *const pvContext)
  98. {
  99. DNASSERT(m_lRefCount > 0);
  100. if (m_dnBufferDesc.dwBufferSize || m_dnBufferDesc.pBufferData)
  101. return(DPNERR_INVALIDPARAM);
  102. m_dnBufferDesc.dwBufferSize = dwBufferSize;
  103. m_dnBufferDesc.pBufferData = pBufferData;
  104. m_pfnFree = pfnFree;
  105. m_pvContext = pvContext;
  106. return(DPN_OK);
  107. };
  108. #undef DPF_MODNAME
  109. #define DPF_MODNAME "BufferDescAddress"
  110. DPN_BUFFER_DESC *BufferDescAddress()
  111. {
  112. return(&m_dnBufferDesc);
  113. };
  114. #undef DPF_MODNAME
  115. #define DPF_MODNAME "GetBufferAddress"
  116. BYTE *GetBufferAddress()
  117. {
  118. return(m_dnBufferDesc.pBufferData);
  119. };
  120. #undef DPF_MODNAME
  121. #define DPF_MODNAME "GetBufferSize"
  122. DWORD GetBufferSize()
  123. {
  124. return(m_dnBufferDesc.dwBufferSize);
  125. };
  126. private:
  127. LONG m_lRefCount;
  128. DPN_BUFFER_DESC m_dnBufferDesc; // Buffer
  129. CLockedContextClassFixedPool< CRefCountBuffer > *m_pFPOOLRefCountBuffer; // source FP of RefCountBuffers
  130. PFNFREE_REFCOUNT_BUFFER m_pfnFree; // Function to free buffer when released
  131. PFNALLOC_REFCOUNT_BUFFER m_pfnAlloc;
  132. PVOID m_pvContext; // Context provided to free buffer call
  133. };
  134. #undef DPF_SUBCOMP
  135. #undef DPF_MODNAME
  136. #endif // __RCBUFF_H__