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.

152 lines
2.1 KiB

  1. #include "precomp.h"
  2. ///////////////////////////////////////////////////////
  3. //
  4. // Public methods
  5. //
  6. BufferPool::BufferPool ( void )
  7. {
  8. InitializeCriticalSection (&m_CritSect);
  9. _Construct ();
  10. }
  11. BufferPool::~BufferPool ( void )
  12. {
  13. _Destruct ();
  14. DeleteCriticalSection (&m_CritSect);
  15. }
  16. HRESULT BufferPool::Initialize ( UINT uBuf, ULONG cbSizeBuf )
  17. {
  18. HRESULT hr = DPR_SUCCESS;
  19. PBYTE *ppb;
  20. DEBUGMSG (ZONE_VERBOSE, ("BufPool::Initialize: enter.\r\n"));
  21. EnterCriticalSection (&m_CritSect);
  22. if (m_fInitialized)
  23. {
  24. hr = DPR_CANT_INITIALIZE_AGAIN;
  25. goto MyExit;
  26. }
  27. m_cBufFree = uBuf;
  28. m_cbSizeBuf = (cbSizeBuf + 3) & (~3); // round up to 4
  29. m_pAlloc = m_pBufFree = LocalAlloc (LMEM_FIXED, m_cBufFree * m_cbSizeBuf);
  30. if (m_pAlloc == NULL)
  31. {
  32. hr = DPR_OUT_OF_MEMORY;
  33. goto MyExit;
  34. }
  35. ppb = (PBYTE *) m_pBufFree;
  36. while (-- uBuf)
  37. {
  38. ppb = (PBYTE *) (*ppb = (PBYTE) ppb + m_cbSizeBuf);
  39. }
  40. *ppb = NULL;
  41. MyExit:
  42. if (hr == DPR_SUCCESS) m_fInitialized = TRUE;
  43. LeaveCriticalSection (&m_CritSect);
  44. DEBUGMSG (ZONE_VERBOSE, ("BufPool::Initialize: exit, hr=0x%lX\r\n", hr));
  45. return hr;
  46. }
  47. PVOID BufferPool::GetBuffer ( void )
  48. {
  49. PVOID p = NULL;
  50. EnterCriticalSection (&m_CritSect);
  51. if (m_fInitialized)
  52. {
  53. p = m_pBufFree;
  54. if (m_pBufFree)
  55. {
  56. m_pBufFree = (PVOID) *((PBYTE *) m_pBufFree);
  57. }
  58. }
  59. LeaveCriticalSection (&m_CritSect);
  60. return p;
  61. }
  62. void BufferPool::ReturnBuffer ( PVOID p )
  63. {
  64. EnterCriticalSection (&m_CritSect);
  65. if (m_fInitialized)
  66. {
  67. *((PVOID *) p) = m_pBufFree;
  68. m_pBufFree = p;
  69. }
  70. LeaveCriticalSection (&m_CritSect);
  71. }
  72. ULONG BufferPool::GetMaxBufferSize ( void )
  73. {
  74. return m_fInitialized ? m_cbSizeBuf : 0;
  75. }
  76. void BufferPool::Release ( void )
  77. {
  78. _Destruct ();
  79. }
  80. ///////////////////////////////////////////////////////
  81. //
  82. // Private methods
  83. //
  84. void BufferPool::_Construct ( void )
  85. {
  86. m_fInitialized = FALSE;
  87. m_cbSizeBuf = 0;
  88. m_cBufAlloc = 0;
  89. m_cBufFree = 0;
  90. m_pAlloc = NULL;
  91. m_pBufFree = NULL;
  92. }
  93. void BufferPool::_Destruct ( void )
  94. {
  95. if (m_fInitialized)
  96. {
  97. if (m_pAlloc)
  98. {
  99. LocalFree (m_pAlloc);
  100. m_pAlloc = NULL;
  101. }
  102. m_fInitialized = FALSE;
  103. }
  104. }