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.

267 lines
6.7 KiB

  1. /***************************************************************************
  2. Name : MEMUTIL.C
  3. Comment : Mem mgmnt and utilty functions
  4. Revision Log
  5. Date Name Description
  6. -------- ----- ---------------------------------------------------------
  7. ***************************************************************************/
  8. #include "prep.h"
  9. #ifdef WIN32_LEAN_AND_MEAN // for timeBeginPeriod and timeEndPeriod
  10. # include <mmsystem.h>
  11. #endif
  12. #include "glbproto.h"
  13. #define SZMOD "Memory: "
  14. void MyAllocInit(PThrdGlbl pTG)
  15. {
  16. pTG->uCount=0;
  17. pTG->uUsed=0;
  18. }
  19. LPBUFFER MyAllocBuf(PThrdGlbl pTG, LONG sSize)
  20. {
  21. LPBUFFER lpbf;
  22. BG_CHK(sSize > 0);
  23. if(pTG->uCount >= STATICBUFCOUNT)
  24. {
  25. (MyDebugPrint(pTG, LOG_ERR, "<<ERROR>> Already alloced %d bufs\r\n", pTG->uCount));
  26. BG_CHK(FALSE);
  27. return NULL;
  28. }
  29. else if(pTG->uUsed+sSize > STATICBUFSIZE)
  30. {
  31. (MyDebugPrint(pTG, LOG_ERR, "<<ERROR>> Already alloced %d bytes out of %d. Want %d\r\n", pTG->uUsed, STATICBUFSIZE, sSize));
  32. BG_CHK(FALSE);
  33. return NULL;
  34. }
  35. // init header
  36. // pTG->bfStaticBuf[pTG->uCount].lpbdBufData = NULL;
  37. // Initialize fields
  38. // pTG->bfStaticBuf[pTG->uCount].lpbdBufData->header.uRefCount = 1;
  39. pTG->bfStaticBuf[pTG->uCount].lpbCurPtr = pTG->bfStaticBuf[pTG->uCount].lpbBegData =
  40. pTG->bfStaticBuf[pTG->uCount].lpbBegBuf =
  41. pTG->bStaticBufData + pTG->uUsed;
  42. pTG->bfStaticBuf[pTG->uCount].wLengthBuf = (USHORT) sSize;
  43. pTG->uUsed += (USHORT) sSize;
  44. // pTG->bfStaticBuf[pTG->uCount].lpbfNextBuf = NULL;
  45. // pTG->bfStaticBuf[pTG->uCount].uReadOnly = FALSE;
  46. pTG->bfStaticBuf[pTG->uCount].wLengthData = 0;
  47. pTG->bfStaticBuf[pTG->uCount].dwMetaData = 0;
  48. lpbf = &(pTG->bfStaticBuf[pTG->uCount++]);
  49. // wsprintf(szTemp, "%d %d %x %08lx %08lx %08lx\r\n", sSize, pTG->uCount, pTG->uUsed, lpbf, lpbf->lpbBegBuf, lpbf->lpbBegData);
  50. // OutputDebugStr(szTemp);
  51. return lpbf;
  52. }
  53. BOOL MyFreeBuf(PThrdGlbl pTG, LPBUFFER lpbf)
  54. {
  55. if(pTG->uCount==0 || lpbf!= &(pTG->bfStaticBuf[pTG->uCount-1]))
  56. {
  57. (MyDebugPrint(pTG, LOG_ERR, "<<ERROR>> Not alloced or out-of-order free. Count=%d lpbf=%08lx bf=%08lx\r\n",
  58. pTG->uCount, lpbf, (LPBUFFER)&pTG->bfStaticBuf));
  59. BG_CHK(FALSE);
  60. return FALSE;
  61. }
  62. pTG->uCount--;
  63. BG_CHK(lpbf->lpbBegBuf == pTG->bStaticBufData+pTG->uUsed-lpbf->wLengthBuf);
  64. pTG->uUsed -= lpbf->wLengthBuf;
  65. return TRUE;
  66. }
  67. LPVOID IFMemAlloc (UINT fuAlloc, LONG lAllocSize, LPWORD lpwActualSize)
  68. {
  69. DWORD dwSize;
  70. LPVOID lpv = NULL;
  71. // Find the required size
  72. if (lAllocSize > 0) {
  73. dwSize = (DWORD)lAllocSize;
  74. }
  75. else {
  76. if (lAllocSize == RAW_DATA_SIZE)
  77. dwSize=16000;
  78. else if (lAllocSize == COMPRESS_DATA_SIZE)
  79. dwSize=2000;
  80. else if (lAllocSize == SMALL_HEADER_SIZE)
  81. dwSize = sizeof(BUFFER);
  82. else
  83. return NULL;
  84. }
  85. lpv = MemAlloc(dwSize);
  86. if (lpwActualSize) {
  87. if (lpv) {
  88. *lpwActualSize = (UINT) dwSize;
  89. }
  90. else {
  91. *lpwActualSize = 0;
  92. }
  93. }
  94. return lpv;
  95. }
  96. BOOL IFMemFree (LPVOID lpvMem)
  97. {
  98. MemFree(lpvMem);
  99. return 1;
  100. }
  101. ////////////////////////////////////////////////////////
  102. // from utils\runtime\buffers.c
  103. ////////////////////////////////////////////////////////
  104. typedef struct _BUFFERDATA {
  105. // Private portion
  106. struct {
  107. UINT uRefCount;
  108. UINT uFiller; // maintain alignment
  109. } header;
  110. // public portion
  111. BYTE rgbData[]; // Actual data
  112. } BUFFERDATA;
  113. // signature
  114. #define BUFFER_SIGNATURE 4656 // decimal so that it can be used in
  115. LPBUFFER IFBufAlloc (LONG lBufSize)
  116. {
  117. register LPBUFFER lpbf;
  118. WORD wActualSize;
  119. // First do resource management to decide whether this job can be
  120. // allocated these extra resources or not.
  121. // If we reach this point we can try and allocate the buffer header
  122. if (!(lpbf = (LPBUFFER) IFMemAlloc (0, SMALL_HEADER_SIZE, &wActualSize))) {
  123. return NULL;
  124. }
  125. if (lBufSize) {
  126. // We need to allocate the data portion too ...
  127. // increase the data size by 4 if he is explicitly asking
  128. // for some amount to make space for ref count etc
  129. if (lBufSize > 0) {
  130. // sizeof evaluates at compile time so its OK
  131. // that lpbdBufData is Null !!
  132. lBufSize += sizeof(lpbf->lpbdBufData->header);
  133. }
  134. if (!(lpbf->lpbdBufData = (BUFFERDATA FAR*) IFMemAlloc (0, lBufSize, &wActualSize))) {
  135. // Free everything and return error ..
  136. IFMemFree (lpbf);
  137. return NULL;
  138. }
  139. // Initialize fields
  140. lpbf->lpbdBufData->header.uRefCount = 1;
  141. lpbf->lpbCurPtr = lpbf->lpbBegData = lpbf->lpbBegBuf
  142. = lpbf->lpbdBufData->rgbData;
  143. // take out space for uRefCount & wFiller
  144. lpbf->wLengthBuf = wActualSize - sizeof(lpbf->lpbdBufData->header);
  145. }
  146. else {
  147. // No data - buffer will contain metadata only
  148. lpbf->lpbdBufData = NULL;
  149. lpbf->lpbCurPtr = lpbf->lpbBegData = lpbf->lpbBegBuf = NULL;
  150. lpbf->wLengthBuf = 0;
  151. }
  152. // Now initialize all the rest of the common fields
  153. lpbf->lpbfNextBuf = NULL;
  154. lpbf->fReadOnly = FALSE;
  155. lpbf->wLengthData = 0;
  156. lpbf->dwMetaData = 0;
  157. #ifdef VALIDATE
  158. lpbf->sentinel = BUFFER_SIGNATURE;
  159. #endif
  160. return lpbf;
  161. }
  162. BOOL IFBufFree (LPBUFFER lpbf)
  163. {
  164. register BOOL bRetVal;
  165. if (lpbf->lpbdBufData && (!--lpbf->lpbdBufData->header.uRefCount)) {
  166. // both header and data need to be freed
  167. bRetVal = IFMemFree(lpbf->lpbdBufData);
  168. bRetVal = bRetVal && IFMemFree(lpbf);
  169. }
  170. else {
  171. // only the buffer header needs to be freed. Either the buffer only
  172. // contained metadata, or the data is still being referenced.
  173. bRetVal = IFMemFree(lpbf);
  174. }
  175. return bRetVal;
  176. }