9 * @brief Memory management routines
13 static void compandtake(word, word *, word *, word *, bool);
14 static void sinsert(word);
16 static void compandtake();
17 static void sinsert();
21 int compactify_allowed=1;
22 #define space 400 /* words */
25 void request(word app, word *ah, word *am)
28 bool wascompactified, found;
32 wascompactified = FALSE;
34 if (compactify_allowed && thisp->force_compactification) {
36 thisp->force_compactification = FALSE;
37 wascompactified = TRUE;
40 /* reserve dictionary item */
41 if (thisp->freeitem != 0) {
42 *ah = thisp->freeitem;
43 thisp->freeitem = M[*ah];
46 *ah = thisp->lastitem - 2;
47 /* cannot take free item */
48 if (*ah <= thisp->lastused + space) {
49 if (compactify_allowed) {
50 if(!wascompactified) {
52 wascompactified = TRUE;
55 thisp->force_compactification = TRUE;
57 *ah = thisp->lastitem - 2;
58 if (*ah <= thisp->lastused)
62 thisp->lastitem = *ah;
66 /* now we have a free dict. item */
68 /* special case app=2 */
69 if (app == 2 && thisp->headk2 != 0) {
71 thisp->headk2 = M[*am + SHORTLINK];
73 word t1 = thisp->headk;
76 while (t1 != thisp->lower && !found) {
80 else if (t2 - app >= 2)
84 t1 = M[t1 + LONGLINK];
88 t5 = M[t1 + SHORTLINK];
90 M[t5 + LONGLINK] = M[t1 + LONGLINK];
92 t5 = M[t1 + LONGLINK];
96 M[t4 + LONGLINK] = t5;
99 /* at least two extra words */
105 } else if (thisp->lastitem - thisp->lastused > app + space) {
106 *am = thisp->lastused+1;
107 thisp->lastused += app;
109 /* return dictionary item */
110 M[*ah] = thisp->freeitem;
111 thisp->freeitem = *ah;
112 if(compactify_allowed) {
113 if( !wascompactified )
116 thisp->force_compactification = TRUE;
118 /* reserve dictionary item */
119 *ah = thisp->lastitem-2;
120 thisp->lastitem = *ah;
123 if (thisp->lastitem - thisp->lastused > app) {
124 *am = thisp->lastused + 1;
125 thisp->lastused += app;
132 for (t2 = *am+1; t2 < *am + app; t2++)
138 /* Dispose of a memory item. */
139 static void sinsert(word am)
144 /* a special list should be used */
146 M[am + SHORTLINK] = thisp->headk2;
152 /* look for a proper place */
156 /* an entry with matching appetite */
158 M[am + SHORTLINK] = M[t2 + SHORTLINK];
159 M[t2 + SHORTLINK] = am;
163 M[am + LONGLINK] = t2;
164 M[am + SHORTLINK] = 0;
168 M[t4 + LONGLINK] = am;
173 t2 = M[t2 + LONGLINK];
182 void disp(virtaddr *virt)
186 t1 = M[virt->addr + 1];
188 if (t1 == virt->mark) {
193 M[virt->addr + 1] = t1;
194 /* mark still usable */
195 if (t1 != MAXMARKER) {
196 M[virt->addr] = thisp->freeitem;
197 thisp->freeitem = virt->addr;
200 /* now dictionary item is released */
202 /* on the boundary */
203 if (t2 + M[t2] - 1 == thisp->lastused)
204 thisp->lastused = t2 - 1;
211 * Compute available memory size
217 /* contiguos memory */
218 avail = thisp->lastitem-thisp->lastused - 1;
219 /* go through killed 2 list */
223 t1 = M[t1 + SHORTLINK];
226 /* go through killed object list */
227 while (t1 != thisp->lower) {
231 t2 = M[t2 + SHORTLINK];
233 t1 = M[t1 + LONGLINK];
235 /* go through free item list */
236 t1 = thisp->freeitem;