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.

161 lines
4.0 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. //**********************************************************************
  38. // ------------------------------
  39. // CRefCountBuffer::Initialize
  40. //
  41. // Entry: CFixedPool <CRefCountBuffer> *pFPRefCountBuffer
  42. // const DWORD dwBufferSize
  43. //
  44. // Exit: Error Code: DN_OK
  45. // DNERR_OUTOFMEMORY
  46. // ------------------------------
  47. #undef DPF_MODNAME
  48. #define DPF_MODNAME "CRefCountBuffer::Initialize"
  49. HRESULT CRefCountBuffer::Initialize(CLockedContextClassFixedPool <CRefCountBuffer> *pFPRefCountBuffer,
  50. PFNALLOC_REFCOUNT_BUFFER pfnAlloc,
  51. PFNFREE_REFCOUNT_BUFFER pfnFree,
  52. void *const pvContext,
  53. const DWORD dwBufferSize)
  54. {
  55. DPFX(DPFPREP, 3,"Entered");
  56. DNASSERT(pFPRefCountBuffer != NULL);
  57. DNASSERT((pfnAlloc == NULL && pfnFree == NULL) || (pfnAlloc != NULL && pfnFree != NULL));
  58. m_pFPOOLRefCountBuffer = pFPRefCountBuffer;
  59. if (dwBufferSize)
  60. {
  61. if (pfnAlloc)
  62. {
  63. m_pfnAlloc = pfnAlloc;
  64. m_pfnFree = pfnFree;
  65. m_pvContext = pvContext;
  66. }
  67. else
  68. {
  69. m_pfnAlloc = RefCountBufferDefaultAlloc;
  70. m_pfnFree = RefCountBufferDefaultFree;
  71. }
  72. m_dnBufferDesc.pBufferData = static_cast<BYTE*>((pfnAlloc)(m_pvContext,dwBufferSize));
  73. if (m_dnBufferDesc.pBufferData == NULL)
  74. {
  75. return(DPNERR_OUTOFMEMORY);
  76. }
  77. m_dnBufferDesc.dwBufferSize = dwBufferSize;
  78. }
  79. return(DPN_OK);
  80. }
  81. /* REMOVE
  82. //**********************************************************************
  83. // ------------------------------
  84. // CRefCountBuffer::ReturnSelfToPool
  85. //
  86. // Entry: Nothing
  87. //
  88. // Exit: Nothing
  89. // ------------------------------
  90. #undef DPF_MODNAME
  91. #define DPF_MODNAME "CRefCountBuffer::ReturnSelfToPool"
  92. void CRefCountBuffer::ReturnSelfToPool( void )
  93. {
  94. DPFX(DPFPREP, 3,"Entered");
  95. DNASSERT(m_lRefCount == 0);
  96. m_pFPOOLRefCountBuffer->Release( this );
  97. }
  98. */
  99. //**********************************************************************
  100. // ------------------------------
  101. // CRefCountBuffer::Release
  102. //
  103. // Entry: Nothing
  104. //
  105. // Exit: Nothing
  106. // ------------------------------
  107. #undef DPF_MODNAME
  108. #define DPF_MODNAME "CRefCountBuffer::Release"
  109. void CRefCountBuffer::Release( void )
  110. {
  111. DNASSERT(m_lRefCount > 0);
  112. if ( InterlockedDecrement( &m_lRefCount ) == 0 )
  113. {
  114. if (m_pfnFree && m_dnBufferDesc.pBufferData)
  115. {
  116. (*m_pfnFree)(m_pvContext,m_dnBufferDesc.pBufferData);
  117. m_dnBufferDesc.pBufferData = NULL;
  118. m_dnBufferDesc.dwBufferSize = 0;
  119. }
  120. ReturnSelfToPool();
  121. }
  122. }
  123. PVOID RefCountBufferDefaultAlloc(void *const pv,const DWORD dwSize)
  124. {
  125. return(DNMalloc(dwSize));
  126. }
  127. void RefCountBufferDefaultFree(PVOID pv,PVOID pvBuffer)
  128. {
  129. DNFree(pvBuffer);
  130. }