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.

155 lines
5.3 KiB

  1. /*==========================================================================
  2. *
  3. * Copyright (C) 1998 Microsoft Corporation. All Rights Reserved.
  4. *
  5. * File: RCBuffer.cpp
  6. * Content: RefCount Buffers
  7. *
  8. *
  9. * History:
  10. * Date By Reason
  11. * ==== == ======
  12. * 01/12/00 mjn Created
  13. * 01/31/00 mjn Allow user defined Alloc and Free
  14. ***************************************************************************/
  15. #include "dncmni.h"
  16. #include "RCBuffer.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. //**********************************************************************
  29. // Variable definitions
  30. //**********************************************************************
  31. //**********************************************************************
  32. // Function prototypes
  33. //**********************************************************************
  34. //**********************************************************************
  35. // Function definitions
  36. //**********************************************************************
  37. #undef DPF_MODNAME
  38. #define DPF_MODNAME "CRefCountBuffer::Initialize"
  39. HRESULT CRefCountBuffer::Initialize(CFixedPool* pFPRefCountBuffer,
  40. PFNALLOC_REFCOUNT_BUFFER pfnAlloc,
  41. PFNFREE_REFCOUNT_BUFFER pfnFree,
  42. const DWORD dwBufferSize)
  43. {
  44. DNASSERT(pFPRefCountBuffer != NULL);
  45. DNASSERT((pfnAlloc == NULL && pfnFree == NULL && dwBufferSize == 0) || (pfnAlloc != NULL && pfnFree != NULL && dwBufferSize != 0));
  46. m_pFPOOLRefCountBuffer = pFPRefCountBuffer;
  47. if (dwBufferSize)
  48. {
  49. DNASSERT(pfnAlloc);
  50. DNASSERT(pfnFree);
  51. m_pfnAlloc = pfnAlloc;
  52. m_pfnFree = pfnFree;
  53. m_dnBufferDesc.pBufferData = static_cast<BYTE*>((pfnAlloc)(m_pvContext,dwBufferSize));
  54. if (m_dnBufferDesc.pBufferData == NULL)
  55. {
  56. return(DPNERR_OUTOFMEMORY);
  57. }
  58. m_dnBufferDesc.dwBufferSize = dwBufferSize;
  59. }
  60. DPFX(DPFPREP, 5,"[%p] Initialize RefCountBuffer pPool[%p], pfnAlloc[%p], pfnFree[%p], m_pvContext[%p], dwSize[%d], pBufferData[%p]", this, pFPRefCountBuffer, pfnAlloc, pfnFree, m_pvContext, dwBufferSize, m_dnBufferDesc.pBufferData);
  61. return(DPN_OK);
  62. }
  63. #undef DPF_MODNAME
  64. #define DPF_MODNAME "CRefCountBuffer::SetBufferDesc"
  65. HRESULT CRefCountBuffer::SetBufferDesc( BYTE *const pBufferData, const DWORD dwBufferSize, PFNFREE_REFCOUNT_BUFFER pfnFree, void *const pvSpecialFree)
  66. {
  67. DPFX(DPFPREP, 5,"[%p] Set BufferDesc refcount[%d], pPool[%p], pfnAlloc[%p], pfnFree[%p], m_pvContext[%p], dwSize[%d], pBufferData[%p], pvSpecialFree[%p]", this, m_lRefCount, m_pFPOOLRefCountBuffer, m_pfnAlloc, pfnFree, m_pvContext, dwBufferSize, pBufferData, pvSpecialFree);
  68. DNASSERT(m_lRefCount > 0);
  69. // Don't allow overwriting a previous bufferdesc
  70. DNASSERT(m_dnBufferDesc.dwBufferSize == 0);
  71. DNASSERT(m_dnBufferDesc.pBufferData == 0);
  72. DNASSERT(pfnFree);
  73. m_dnBufferDesc.dwBufferSize = dwBufferSize;
  74. m_dnBufferDesc.pBufferData = pBufferData;
  75. m_pfnFree = pfnFree;
  76. m_pvSpecialFree = pvSpecialFree;
  77. return(DPN_OK);
  78. };
  79. #undef DPF_MODNAME
  80. #define DPF_MODNAME "CRefCountBuffer::FPMInitialize"
  81. void CRefCountBuffer::FPMInitialize( void* pvItem, void* pvContext )
  82. {
  83. DPFX(DPFPREP, 5,"[%p] Get RefCountBuffer from pool", pvItem);
  84. CRefCountBuffer* pRCBuffer = (CRefCountBuffer*)pvItem;
  85. pRCBuffer->m_lRefCount = 1;
  86. pRCBuffer->m_pvContext = pvContext;
  87. pRCBuffer->m_pvSpecialFree = NULL;
  88. pRCBuffer->m_dnBufferDesc.dwBufferSize = 0;
  89. pRCBuffer->m_dnBufferDesc.pBufferData = NULL;
  90. };
  91. #undef DPF_MODNAME
  92. #define DPF_MODNAME "CRefCountBuffer::AddRef"
  93. void CRefCountBuffer::AddRef()
  94. {
  95. DPFX(DPFPREP, 5,"[%p] AddRef RefCountBuffer refcount[%d], pPool[%p], pfnAlloc[%p], pfnFree[%p], pvContext[%p], dwSize[%d], pBufferData[%p]", this, m_lRefCount + 1, m_pFPOOLRefCountBuffer, m_pfnAlloc, m_pfnFree, m_pvContext, m_dnBufferDesc.dwBufferSize, m_dnBufferDesc.pBufferData);
  96. DNASSERT(m_lRefCount >= 0);
  97. DNInterlockedIncrement( &m_lRefCount );
  98. };
  99. #undef DPF_MODNAME
  100. #define DPF_MODNAME "CRefCountBuffer::Release"
  101. void CRefCountBuffer::Release( void )
  102. {
  103. DPFX(DPFPREP, 5,"[%p] Release RefCountBuffer refcount[%d], pPool[%p], pfnAlloc[%p], pfnFree[%p], pvContext[%p], dwSize[%d], pBufferData[%p]", this, m_lRefCount - 1, m_pFPOOLRefCountBuffer, m_pfnAlloc, m_pfnFree, m_pvContext, m_dnBufferDesc.dwBufferSize, m_dnBufferDesc.pBufferData);
  104. DNASSERT(m_lRefCount > 0);
  105. if ( DNInterlockedDecrement( &m_lRefCount ) == 0 )
  106. {
  107. if (m_dnBufferDesc.pBufferData)
  108. {
  109. if (m_pvSpecialFree)
  110. {
  111. (*m_pfnFree)(m_pvContext,m_pvSpecialFree);
  112. }
  113. else
  114. {
  115. (*m_pfnFree)(m_pvContext,m_dnBufferDesc.pBufferData);
  116. }
  117. m_dnBufferDesc.pBufferData = NULL;
  118. m_dnBufferDesc.dwBufferSize = 0;
  119. m_pvSpecialFree = NULL;
  120. }
  121. m_pFPOOLRefCountBuffer->Release( this );
  122. }
  123. }