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.

188 lines
2.2 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. refb.cxx
  5. Abstract:
  6. Reference counting blob class
  7. Author:
  8. Philippe Choquier (phillich) 11-sep-1996
  9. --*/
  10. #include <windows.h>
  11. #include <refb.hxx>
  12. RefBlob::RefBlob(
  13. )
  14. /*++
  15. Description:
  16. Constructor for RefBlob
  17. Arguments:
  18. None
  19. Returns:
  20. Nothing
  21. --*/
  22. {
  23. m_lRef = 0;
  24. m_pvBlob = 0;
  25. m_dwSize = 0;
  26. m_pfnFree = NULL;
  27. }
  28. RefBlob::~RefBlob(
  29. )
  30. /*++
  31. Description:
  32. Destructor for RefBlob
  33. Arguments:
  34. None
  35. Returns:
  36. Nothing
  37. --*/
  38. {
  39. }
  40. BOOL
  41. RefBlob::Init(
  42. LPVOID pv,
  43. DWORD sz,
  44. PFN_FREE_BLOB pFn
  45. )
  46. /*++
  47. Description:
  48. Initialize a RefBlob
  49. ownership of buffer pointed to by pv is transferred
  50. to this object. buffer must have been allocated using
  51. LocalAlloc( LMEM_FIXED, )
  52. Arguments:
  53. pv - pointer to blob
  54. sz - size of blob
  55. pFn - ptr to function to call to free blob
  56. Returns:
  57. TRUE if success, otherwise FALSE
  58. --*/
  59. {
  60. m_pvBlob = pv;
  61. m_dwSize = sz;
  62. m_pfnFree = pFn;
  63. AddRef();
  64. return TRUE;
  65. }
  66. VOID
  67. RefBlob::AddRef(
  68. VOID
  69. )
  70. /*++
  71. Description:
  72. Add a reference to this object
  73. Arguments:
  74. None
  75. Returns:
  76. Nothing
  77. --*/
  78. {
  79. InterlockedIncrement( &m_lRef );
  80. }
  81. VOID
  82. RefBlob::Release(
  83. VOID
  84. )
  85. /*++
  86. Description:
  87. Remove a reference to this object
  88. When the reference count drops to zero
  89. the object is destroyed, blob memory freed
  90. Arguments:
  91. None
  92. Returns:
  93. Nothing
  94. --*/
  95. {
  96. if ( !InterlockedDecrement( &m_lRef ) )
  97. {
  98. if ( m_pfnFree )
  99. {
  100. (m_pfnFree)( m_pvBlob );
  101. }
  102. else
  103. {
  104. LocalFree( m_pvBlob );
  105. }
  106. delete this;
  107. }
  108. }
  109. LPVOID
  110. RefBlob::QueryPtr(
  111. )
  112. /*++
  113. Description:
  114. Returns a ptr to blob
  115. Arguments:
  116. None
  117. Returns:
  118. ptr to blob
  119. --*/
  120. {
  121. return m_pvBlob;
  122. }
  123. DWORD
  124. RefBlob::QuerySize(
  125. )
  126. /*++
  127. Description:
  128. Returns a blob size
  129. Arguments:
  130. None
  131. Returns:
  132. size of blob
  133. --*/
  134. {
  135. return m_dwSize;
  136. }