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