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.

192 lines
5.8 KiB

  1. /************************************************************/
  2. /* Windows Write, Copyright 1985-1992 Microsoft Corporation */
  3. /************************************************************/
  4. #define NOGDICAPMASKS
  5. #define NOVIRTUALKEYCODES
  6. #define NOWINMESSAGES
  7. #define NOWINSTYLES
  8. #define NOSYSMETRICS
  9. #define NOMENUS
  10. #define NOICON
  11. #define NOKEYSTATE
  12. #define NOSYSCOMMANDS
  13. #define NOSHOWWINDOW
  14. #define NOCTLMGR
  15. #define NOCLIPBOARD
  16. #define NOMSG
  17. #define NOGDI
  18. #define NOMB
  19. #define NOSOUND
  20. #define NOCOMM
  21. #define NOPEN
  22. #define NOBRUSH
  23. #define NOFONT
  24. #define NOWNDCLASS
  25. #include <windows.h>
  26. #include "mw.h"
  27. #ifdef OURHEAP
  28. /*
  29. heapInit.c - one routine to calculate the proper information for
  30. heap management.
  31. */
  32. #include "code.h"
  33. #include "heapDefs.h"
  34. #include "heapData.h"
  35. #include "str.h"
  36. #ifdef ENABLE
  37. #include "memDefs.h"
  38. #endif
  39. /* heap specific data */
  40. HH *phhMac; /* May change if grow heap */
  41. int cwHeapMac; /* " " " " " " " " */
  42. int *pHeapFirst; /* change if the finger table rgfgr expands */
  43. FGR *rgfgr; /* Declared as a pointer, but also used as an array. */
  44. FGR *pfgrMac; /* Initially equal to &rgfgr[ifgrInit]; */
  45. FGR *pfgrFree; /* Singly linked with a trailing NULL pointer. */
  46. HH *phhFree; /* May be NULL along the way. */
  47. ENV envMem;
  48. int fMemFailed;
  49. int cwInitStorage;
  50. FExpandFgrTbl()
  51. /* Will expand the finger table. This routines depends upon the fact
  52. that the CompactHeap routine moves the allocated blocks to the
  53. low end of memory. The new space from the finger table comes from
  54. the tail end of the (only) free block left after a compaction.
  55. The finger is expanded by at most 'cfgrNewMax' and at least 1.
  56. If there is no room to expand the finger table, then nothing is
  57. changed. To expand the table, several pointers and integers are
  58. decreamented to reflect the reallocation of the storage. Then
  59. we recalculate the memory totals so the user
  60. will have an acurate display of the percent free and total bytes
  61. available. The new fingers are linked so that the finger at the
  62. low end of the table is at the end of the list.
  63. (To expand the finger table there must be no fingers available.)
  64. */
  65. {
  66. FGR *pfgr;
  67. int cfgrNew = cfgrBlock;
  68. register HH *phhNew;
  69. #ifdef DEBUG
  70. if (pfgrFree != NULL)
  71. panicH(34);
  72. #endif
  73. if (!FCwInMem(cfgrNew + cwReqMin + 1))
  74. {
  75. /* couldn't get a block's worth - could we get one? */
  76. cfgrNew = 1;
  77. if (!FCwInMem(cfgrNew))
  78. /* no way to help this guy */
  79. return(FALSE);
  80. }
  81. phhNew = (HH *)pHeapFirst;
  82. if (phhNew->cw > 0 || !FHhFound(phhNew, cfgrNew))
  83. {
  84. /* we tried but failed to find an adequate free
  85. block at start of heap */
  86. CompactHeap();
  87. MoveFreeBlock(pHeapFirst);
  88. }
  89. if (!FPhhDoAssign(phhNew, cfgrNew))
  90. return(FALSE);
  91. /* we have a block which is FIRST in the heap - let's steal it */
  92. cfgrNew = phhNew->cw; /* in case it was more than we
  93. asked for */
  94. pHeapFirst = pHeapFirst + cfgrNew;
  95. pfgrMac += cfgrNew;
  96. cwHeapMac -= cfgrNew;
  97. /* do some initialization if pfgrFree is not NULL and you
  98. want the new fingers at the very end of the free finger list */
  99. for (pfgr = pfgrMac - cfgrNew; pfgr < pfgrMac; pfgr++)
  100. {
  101. *pfgr = (FGR)pfgrFree;
  102. pfgrFree = pfgr;
  103. }
  104. /* do we need this anymore? (cwInitStorage = cwHeapMac - cwHeapFree)
  105. cbTot = (cwHeapMac - cwInitStorage) * sizeof(int);
  106. cbTotQuotient = (cbTot>>1)/100;
  107. */
  108. return(TRUE);
  109. } /* End of FExpandFgrTbl () */
  110. CompactHeap()
  111. /* moves all allocated hunks */
  112. /* toward beginning of pHeapFirst. Free hunks */
  113. {
  114. HH *phh, *phhSrc, *phhDest; /* are combined into one hunk */
  115. FGR *pfgr;
  116. int cwActual;
  117. #ifdef DEBUG
  118. StoreCheck();
  119. #endif
  120. /* set up for compaction by placing cw of hunk in rgfgr and an
  121. index into rgfgr in the hunk */
  122. for (pfgr = rgfgr; pfgr < pfgrMac; pfgr++)
  123. {
  124. if (FPointsHeap(*pfgr))
  125. /* if rgfgr[ifgr] points to heap... */
  126. {
  127. phh = (HH *)(*pfgr + bhh);
  128. /* find header */
  129. *pfgr = (FGR)phh->cw;
  130. /* coerce so it fits, force the shift */
  131. phh->cw = (int)(((unsigned)pfgr - (unsigned)rgfgr)/2);
  132. }
  133. }
  134. /* now we have cw in rgfgr and ifgr in phh */
  135. phhSrc = (HH *) pHeapFirst;
  136. phhDest = phhSrc;
  137. while (phhSrc < phhMac)
  138. {
  139. if (phhSrc->cw < 0)
  140. /* free hunk, don't recopy */
  141. phhSrc = (HH *)((int *) phhSrc - phhSrc->cw);
  142. else
  143. {
  144. pfgr = &rgfgr[phhSrc->cw];
  145. /* find h */
  146. cwActual = phhSrc->cw = (int) *pfgr;
  147. /* restore cw */
  148. *pfgr = ((FGR) phhDest - bhh);
  149. /* update ha */
  150. blt((int *)phhSrc, (int *)phhDest, (unsigned)cwActual);
  151. /* unless ptrs are = */
  152. phhDest = (HH *) ((int *) phhDest + cwActual);
  153. phhSrc = (HH *) ((int *) phhSrc + cwActual);
  154. }
  155. }
  156. #ifdef DEBUG
  157. if ((int *)phhDest + cwHeapFree - cwHunkMin >= (int *)phhMac)
  158. panicH(35);
  159. #endif
  160. phhFree = phhDest;
  161. phhFree->cw = -cwHeapFree;
  162. phhFree->phhNext = phhFree->phhPrev = phhFree;
  163. #ifdef DEBUG
  164. StoreCheck();
  165. #endif
  166. }
  167. #endif /* NOT WINHEAP */
  168.