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.

194 lines
3.9 KiB

  1. #include <mvopsys.h>
  2. #include <orkin.h>
  3. #include <mem.h>
  4. #include "bfnew.h"
  5. #if defined(_DEBUG)
  6. static char * s_aszModule = __FILE__; /* For error report */
  7. #endif
  8. #ifndef MAXWORD
  9. #define MAXWORD ((WORD)0xffff)
  10. #endif
  11. /***************************************************************************
  12. *
  13. - Name DynBufferAlloc
  14. -
  15. * Purpose
  16. * Creates a new buffer.
  17. *
  18. * Arguments
  19. * int cIncr: Minimum increment size for this buffer.
  20. *
  21. * Returns
  22. * Pointer to a new buffer. Returns pNil on OOM.
  23. *
  24. * +++
  25. *
  26. * Notes
  27. *
  28. ***************************************************************************/
  29. LPBF DynBufferAlloc( DWORD cIncr )
  30. {
  31. LPBF pbf;
  32. HANDLE hnd;
  33. if ((hnd = _GLOBALALLOC ((GMEM_MOVEABLE|GMEM_ZEROINIT), sizeof (BF) )) == NULL)
  34. return NULL;
  35. pbf = (LPBF)_GLOBALLOCK (hnd);
  36. pbf->hnd = hnd;
  37. pbf->cIncr = cIncr;
  38. pbf->cbMax = cIncr;
  39. pbf->cbSize = 0;
  40. if ((pbf->hBuf = _GLOBALALLOC (GMEM_MOVEABLE | GMEM_ZEROINIT,
  41. (LONG)cIncr)) == 0) {
  42. _GLOBALUNLOCK( hnd);
  43. _GLOBALFREE (hnd);
  44. return NULL;
  45. }
  46. pbf->qBuffer = _GLOBALLOCK (pbf->hBuf);
  47. return pbf;
  48. }
  49. // inserts the given data at the byte position given by the argument.
  50. // returns a pointer to the buffer at that byte position if successful.
  51. LPBYTE DynBufferInsert(LPBF lpbf, DWORD lib, LPBYTE qbData, SHORT cbData)
  52. {
  53. LPBYTE lpbInsert;
  54. //LPBYTE lpbCur;
  55. int cbSizeOld = lpbf->cbSize;
  56. // grow the buffer by cbData bytes
  57. if ((cbData == 0) || !DynBufferEnsureAdd(lpbf, cbData))
  58. return NULL;
  59. assert( lpbf->cbMax >= lpbf->cbSize + cbData );
  60. // note that we set these vars here, since DynBufferEnsure may cause a resize
  61. lpbInsert = (LPBYTE)(lpbf->qBuffer) + lib;
  62. //lpbCur = (LPBYTE)(lpbf->qBuffer) + cbSizeOld; // first byte of expanded buffer
  63. // shift the existing data down if necessary
  64. //if (lpbCur != lpbInsert)
  65. MEMMOVE(lpbInsert + cbData, lpbInsert, cbSizeOld - lib);
  66. // bring in new stuff if necessary
  67. if (qbData)
  68. MEMCPY(lpbInsert, qbData, cbData);
  69. lpbf->cbSize += cbData;
  70. return lpbInsert;
  71. }
  72. /***************************************************************************
  73. *
  74. - Name DynBufferAppend
  75. -
  76. * Purpose
  77. * Adds data to a buffer.
  78. *
  79. * Arguments
  80. * PBF -- pointer to buffer.
  81. * QV -- pointer to data to copy.
  82. * WORD -- amount of data to copy.
  83. *
  84. * Returns
  85. * rcOutOfMemory if we run out of memory
  86. * rcFailure if we try to add more than 64K bytes of data to the
  87. * buffer
  88. * rcSuccess otherwise.
  89. *
  90. * +++
  91. *
  92. * Notes
  93. *
  94. ***************************************************************************/
  95. LPBYTE DynBufferAppend(LPBF lpbf, LPBYTE qData, DWORD cb )
  96. {
  97. if (!DynBufferEnsureAdd(lpbf, cb))
  98. return NULL;
  99. assert( lpbf->cbMax >= lpbf->cbSize + cb );
  100. if (qData)
  101. MEMCPY ((LPBYTE)lpbf->qBuffer + lpbf->cbSize, qData, cb);
  102. lpbf->cbSize += cb;
  103. return((LPBYTE)(lpbf->qBuffer));
  104. }
  105. // Ensures that there is space in the buffer to
  106. // add <w> bytes
  107. BOOL InternalEnsureAdd(LPBF lpbf, DWORD w)
  108. {
  109. #ifndef _WIN32
  110. if (lpbf->cbSize > 0xffff - w)
  111. return FALSE;
  112. #endif
  113. if (w > lpbf->cIncr)
  114. lpbf->cbMax += w;
  115. else
  116. lpbf->cbMax += lpbf->cIncr;
  117. _GLOBALUNLOCK (lpbf->hBuf);
  118. if ((lpbf->hBuf =_GLOBALREALLOC (lpbf->hBuf,
  119. (LONG)lpbf->cbMax, GMEM_MOVEABLE | GMEM_ZEROINIT)) == 0)
  120. {
  121. lpbf->qBuffer = NULL;
  122. return FALSE;
  123. }
  124. lpbf->qBuffer = _GLOBALLOCK (lpbf->hBuf);
  125. return TRUE;
  126. }
  127. /***************************************************************************
  128. *
  129. - Name DynBufferFree
  130. -
  131. * Purpose
  132. * Frees an allocated buffer.
  133. *
  134. * Arguments
  135. * Pointer to buffer.
  136. *
  137. * Returns
  138. * nothing.
  139. *
  140. * +++
  141. *
  142. * Notes
  143. * See also EmptyPbf, which empties the buffer but leaves it available
  144. * for re-use.
  145. *
  146. ***************************************************************************/
  147. VOID DynBufferFree( LPBF pbf )
  148. {
  149. HANDLE hnd;
  150. if (pbf == 0)
  151. return;
  152. if (hnd = pbf->hBuf)
  153. {
  154. _GLOBALUNLOCK (hnd);
  155. _GLOBALFREE(hnd);
  156. }
  157. if (hnd = pbf->hnd)
  158. {
  159. _GLOBALUNLOCK(hnd);
  160. _GLOBALFREE (hnd);
  161. }
  162. }