7 /* Memory management routines */
10 static void compandtake(word, word *, word *, word *, bool);
11 static void sinsert(word);
13 static void compandtake();
14 static void sinsert();
18 int compactify_allowed=1;
19 #define space 400 /* words */
22 void request(app, ah, am)
26 bool wascompactified, found;
28 if (app >= MAXAPPT) errsignal(RTEMEMOV);
29 wascompactified = FALSE;
31 if( compactify_allowed && thisp->force_compactification ){
33 thisp->force_compactification=FALSE;
37 if (thisp->freeitem != 0) /* reserve dictionary item */
39 *ah = thisp->freeitem;
40 thisp->freeitem = M[ *ah ];
44 *ah = thisp->lastitem-2;
45 if (*ah <= thisp->lastused + space) /* cannot take free item */
47 if( compactify_allowed )
48 if( !wascompactified ) compactify(),wascompactified=TRUE;
51 thisp->force_compactification=TRUE;
52 *ah = thisp->lastitem-2;
53 if (*ah <= thisp->lastused) errsignal(RTEMEMOV);
56 thisp->lastitem = *ah;
57 M[ *ah+1 ] = 0; /* clear mark */
58 } /* now we have a free dict. item */
61 if (app == 2 && thisp->headk2 != 0) /* special case app=2 */
64 thisp->headk2 = M[ *am+SHORTLINK ];
68 word t1 = thisp->headk;
71 while (t1 != thisp->lower && !found)
74 if (t2 == app) found = TRUE;
76 if (t2-app >= 2) found = TRUE;
80 t1 = M[ t1+LONGLINK ];
84 t5 = M[ t1+SHORTLINK ];
85 if (t5 != 0) M[ t5+LONGLINK ] = M[ t1+LONGLINK ];
86 else t5 = M[ t1+LONGLINK ];
87 if (t4 == 0) thisp->headk = t5; else M[ t4+LONGLINK ] = t5;
89 if (t2 > app) /* at least two extra words */
97 if ( thisp->lastitem - thisp->lastused > app + space )
99 *am = thisp->lastused+1;
100 thisp->lastused += app;
104 M[ *ah ] = thisp->freeitem; /* return dictionary item */
105 thisp->freeitem = *ah;
106 if( compactify_allowed )
107 if( !wascompactified ) compactify();
110 thisp->force_compactification=TRUE;
111 *ah = thisp->lastitem-2; /* reserve dictionary item */
112 thisp->lastitem = *ah;
113 M[ *ah+1 ] = 0; /* clear mark */
114 if ( thisp->lastitem - thisp->lastused > app ) {
115 *am = thisp->lastused+1;
116 thisp->lastused += app;
124 for (t2 = *am+1; t2 < *am+app; t2++ ) M[ t2 ] = 0;
130 static void sinsert(am) /* Dispose of a memory item. */
135 t1 = M[ am ]; /* appetite */
136 if (t1 == 2) /* a special list should be used */
138 M[ am+SHORTLINK ] = thisp->headk2;
145 while (TRUE) /* look for a proper place */
147 t3 = M[ t2 ]; /* appetite */
148 if (t1 == t3) /* an entry with matching appetite */
150 M[ am+SHORTLINK ] = M[ t2+SHORTLINK ];
151 M[ t2+SHORTLINK ] = am;
157 M[ am+LONGLINK ] = t2;
158 M[ am+SHORTLINK ] = 0;
159 if (t4 == 0) thisp->headk = am;
160 else M[ t4+LONGLINK ] = am;
166 t2 = M[ t2+LONGLINK ];
173 void disp(virt) /* Simple kill. */
178 t1 = M[ virt->addr+1 ];
179 if (t1 == virt->mark) /* not none */
181 t1++; /* advance mark */
182 t2 = M[ virt->addr ]; /* am */
183 M[ virt->addr+1 ] = t1;
184 if (t1 != MAXMARKER) /* mark still usable */
186 M[ virt->addr ] = thisp->freeitem;
187 thisp->freeitem = virt->addr;
188 } /* now dictionary item is released */
189 if (t2+M[ t2 ]-1 == thisp->lastused) /* on the boundary */
190 thisp->lastused = t2-1;
196 word memavail() /* Compute available memory size */
200 avail = thisp->lastitem-thisp->lastused-1; /* contiguos memory */
201 t1 = thisp->headk2; /* go through killed 2 list */
205 t1 = M[ t1+SHORTLINK ];
208 while (t1 != thisp->lower) /* go through killed object list */
214 t2 = M[ t2+SHORTLINK ];
216 t1 = M[ t1+LONGLINK ];
218 t1 = thisp->freeitem; /* go through free item list */