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.

189 lines
4.4 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. buffer.h
  5. Abstract:
  6. Definition of BUFFER class.
  7. Author:
  8. Vlad Sadovsky (vlads) 26-Jan-1997
  9. Revision History:
  10. 26-Jan-1997 VladS created
  11. --*/
  12. #ifndef _INC_BUFFER
  13. #define _INC_BUFFER
  14. extern "C"
  15. {
  16. # include <windows.h>
  17. };
  18. /*************************************************************************
  19. NAME: BUFFER (buf)
  20. SYNOPSIS: A resizable object which lives in the application heap.
  21. Upon construction, the buffer takes a requested size in
  22. bytes; it allocates storage sufficient to hold that size.
  23. The client can later change this size with Resize, Trim,
  24. and FillOut. QuerySize returns the current size of
  25. the buffer; QueryPtr returns a pointer to its storage.
  26. Note that a buffer may have size 0, in which case it
  27. keeps no allocated storage.
  28. INTERFACE: BUFFER() - Constructor, naming initial size in bytes
  29. QuerySize() - return size in bytes
  30. QueryPtr() - return pointer to data buffer
  31. Resize() - resize the object to the given number
  32. of bytes. Returns TRUE if the resize was
  33. successful; otherwise returns FALSE (use
  34. GetLastError for error code)
  35. Trim() - force block to occupy no more storage
  36. than the client has requested.
  37. PARENT:
  38. **************************************************************************/
  39. class BUFFER
  40. {
  41. private:
  42. BYTE * _pb; // pointer to storage
  43. UINT _cb; // size of storage, as requested by client
  44. inline VOID VerifyState() const;
  45. UINT QueryActualSize();
  46. dllexp BOOL GetNewStorage( UINT cbRequested );
  47. BOOL ReallocStorage( UINT cbNewlyRequested );
  48. public:
  49. dllexp BUFFER( UINT cbRequested = 0 )
  50. {
  51. _pb = NULL;
  52. _cb = 0;
  53. if ( cbRequested != 0 )
  54. {
  55. GetNewStorage(cbRequested);
  56. }
  57. }
  58. dllexp ~BUFFER()
  59. {
  60. if ( _pb )
  61. {
  62. ::LocalFree( (HANDLE) _pb );
  63. }
  64. }
  65. dllexp VOID * QueryPtr() const
  66. { return _pb; }
  67. dllexp UINT QuerySize() const
  68. { return _cb; }
  69. //
  70. // If a resize is needed, added cbSlop to it
  71. //
  72. dllexp BOOL Resize( UINT cbNewReqestedSize,
  73. UINT cbSlop = 0);
  74. // The following method deals with the difference between the
  75. // actual memory size and the requested size. These methods are
  76. // intended to be used when optimization is key.
  77. // Trim reallocates the buffer so that the actual space allocated is
  78. // minimally more than the size requested
  79. //
  80. dllexp VOID Trim();
  81. };
  82. //
  83. // This class is a single item in a chain of buffers
  84. //
  85. class BUFFER_CHAIN_ITEM : public BUFFER
  86. {
  87. friend class BUFFER_CHAIN;
  88. public:
  89. dllexp BUFFER_CHAIN_ITEM( UINT cbReq = 0 )
  90. : BUFFER( cbReq ),
  91. _cbUsed( 0 )
  92. { _ListEntry.Flink = NULL; }
  93. dllexp ~BUFFER_CHAIN_ITEM()
  94. { if ( _ListEntry.Flink )
  95. RemoveEntryList( &_ListEntry );
  96. }
  97. dllexp DWORD QueryUsed( VOID ) const
  98. { return _cbUsed; }
  99. dllexp VOID SetUsed( DWORD cbUsed )
  100. { _cbUsed = cbUsed; }
  101. private:
  102. LIST_ENTRY _ListEntry;
  103. DWORD _cbUsed; // Bytes of valid data in this buffer
  104. };
  105. class BUFFER_CHAIN
  106. {
  107. public:
  108. dllexp BUFFER_CHAIN()
  109. { InitializeListHead( &_ListHead ); }
  110. dllexp ~BUFFER_CHAIN()
  111. { DeleteChain(); }
  112. dllexp BOOL AppendBuffer( BUFFER_CHAIN_ITEM * pBCI );
  113. //
  114. // Returns total number of bytes freed by deleting all of the buffer
  115. // chain items
  116. //
  117. dllexp DWORD DeleteChain();
  118. //
  119. // Enums buffer chain. Pass pBCI as NULL on first call, pass return
  120. // value till NULL on subsequent calls
  121. //
  122. dllexp BUFFER_CHAIN_ITEM * NextBuffer( BUFFER_CHAIN_ITEM * pBCI );
  123. //
  124. // Gives back total number of bytes allocated by chain (includes unused
  125. // bytes)
  126. //
  127. dllexp DWORD CalcTotalSize( BOOL fUsed = FALSE ) const;
  128. private:
  129. LIST_ENTRY _ListHead;
  130. };
  131. #endif /* _INC_BUFFER */