2003-08-19 15:19:38 -04:00
|
|
|
/* Alloca() simulation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kernel.h"
|
|
|
|
|
|
|
|
#ifndef USE_ALLOCA
|
|
|
|
|
|
|
|
extern char *malloc();
|
|
|
|
|
|
|
|
MEM_NODE *Mem_List;
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
char *Mem_Alloc (unsigned int size) {
|
2003-08-19 15:19:38 -04:00
|
|
|
char *ret;
|
|
|
|
|
|
|
|
Disable_Interrupts;
|
|
|
|
if ((ret = malloc (size)) == 0)
|
|
|
|
Fatal_Error ("not enough memory to malloc %u bytes", size);
|
|
|
|
Enable_Interrupts;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Free_Mem_Nodes (MEM_NODE *first) {
|
2003-08-19 15:19:38 -04:00
|
|
|
MEM_NODE *p;
|
|
|
|
|
|
|
|
Disable_Interrupts;
|
|
|
|
while (p = first) {
|
|
|
|
first = first->next;
|
|
|
|
if (--p->refcnt == 0)
|
|
|
|
free ((char *)p);
|
|
|
|
}
|
|
|
|
Enable_Interrupts;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Save_Mem_Nodes (Object cont) {
|
|
|
|
unsigned int sum = 0;
|
2003-08-19 15:19:38 -04:00
|
|
|
char *s;
|
|
|
|
MEM_NODE *p;
|
|
|
|
Object str;
|
|
|
|
GC_Node;
|
|
|
|
|
|
|
|
CONTROL(cont)->memlist = Mem_List;
|
|
|
|
for (p = Mem_List; p; p = p->next)
|
|
|
|
sum += p->len;
|
|
|
|
GC_Link (cont);
|
|
|
|
str = Make_String ((char *)0, sum);
|
|
|
|
CONTROL(cont)->memsave = str;
|
|
|
|
GC_Unlink;
|
|
|
|
for (p = Mem_List, s = STRING(str)->data; p; s += p->len, p = p->next) {
|
2003-08-19 15:24:23 -04:00
|
|
|
memcpy (s, p+1, p->len);
|
2003-08-19 15:19:38 -04:00
|
|
|
p->refcnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Restore_Mem_Nodes (Object cont) {
|
2003-08-19 15:19:38 -04:00
|
|
|
MEM_NODE *p;
|
|
|
|
char *s;
|
|
|
|
Object str;
|
|
|
|
|
|
|
|
Free_Mem_Nodes (Mem_List);
|
|
|
|
Mem_List = CONTROL(cont)->memlist;
|
|
|
|
str = CONTROL(cont)->memsave;
|
|
|
|
for (p = Mem_List, s = STRING(str)->data; p; s += p->len, p = p->next) {
|
|
|
|
p->refcnt++;
|
2003-08-19 15:24:23 -04:00
|
|
|
memcpy (p+1, s, p->len);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object Save_GC_Nodes () {
|
|
|
|
Object vec;
|
2003-08-19 15:24:23 -04:00
|
|
|
register unsigned int sum = 0, i = 0, n;
|
2003-08-19 15:19:38 -04:00
|
|
|
register GCNODE *p;
|
|
|
|
|
|
|
|
for (p = GC_List; p; p = p->next)
|
|
|
|
sum += p->gclen <= 0 ? 1 : p->gclen-1;
|
|
|
|
vec = Make_Vector (sum, Null);
|
|
|
|
for (p = GC_List; p; p = p->next, i += n) {
|
|
|
|
n = p->gclen <= 0 ? 1 : p->gclen-1;
|
2003-08-19 15:24:23 -04:00
|
|
|
memcpy (&(VECTOR(vec)->data[i]), p->gcobj, n * sizeof (Object));
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
2003-08-19 15:24:23 -04:00
|
|
|
Restore_GC_Nodes (Object vec) {
|
|
|
|
register int i = 0, n;
|
2003-08-19 15:19:38 -04:00
|
|
|
register GCNODE *p;
|
|
|
|
|
|
|
|
for (p = GC_List; p; p = p->next, i += n) {
|
|
|
|
n = p->gclen <= 0 ? 1 : p->gclen-1;
|
2003-08-19 15:24:23 -04:00
|
|
|
memcpy (p->gcobj, &(VECTOR(vec)->data[i]), n * sizeof (Object));
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|