2003-08-30 12:47:54 -04:00
|
|
|
/* stkmem.c: Alloca() simulation.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright 1990, 1991, 1992, 1993, 1994, 1995, Oliver Laumann, Berlin
|
2009-12-19 07:28:26 -05:00
|
|
|
* Copyright 2002, 2003 Sam Hocevar <sam@hocevar.net>, Paris
|
2003-08-30 12:47:54 -04:00
|
|
|
*
|
|
|
|
* This software was derived from Elk 1.2, which was Copyright 1987, 1988,
|
|
|
|
* 1989, Nixdorf Computer AG and TELES GmbH, Berlin (Elk 1.2 has been written
|
|
|
|
* by Oliver Laumann for TELES Telematic Services, Berlin, in a joint project
|
|
|
|
* between TELES and Nixdorf Microprocessor Engineering, Berlin).
|
|
|
|
*
|
|
|
|
* Oliver Laumann, TELES GmbH, Nixdorf Computer AG and Sam Hocevar, as co-
|
|
|
|
* owners or individual owners of copyright in this software, grant to any
|
|
|
|
* person or company a worldwide, royalty free, license to
|
|
|
|
*
|
|
|
|
* i) copy this software,
|
|
|
|
* ii) prepare derivative works based on this software,
|
|
|
|
* iii) distribute copies of this software or derivative works,
|
|
|
|
* iv) perform this software, or
|
|
|
|
* v) display this software,
|
|
|
|
*
|
|
|
|
* provided that this notice is not removed and that neither Oliver Laumann
|
|
|
|
* nor Teles nor Nixdorf are deemed to have made any representations as to
|
|
|
|
* the suitability of this software for any purpose nor are held responsible
|
|
|
|
* for any defects of this software.
|
|
|
|
*
|
|
|
|
* THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
|
2003-08-19 15:19:38 -04:00
|
|
|
*/
|
|
|
|
|
2003-09-06 07:25:29 -04:00
|
|
|
#include "config.h"
|
|
|
|
|
2003-08-19 15:19:38 -04:00
|
|
|
#include "kernel.h"
|
|
|
|
|
2003-09-09 11:18:55 -04:00
|
|
|
#ifndef HAVE_ALLOCA
|
2003-08-19 15:19:38 -04:00
|
|
|
|
|
|
|
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)
|
2003-09-02 04:12:11 -04:00
|
|
|
Fatal_Error ("not enough memory to malloc %u bytes", size);
|
2003-08-19 15:19:38 -04:00
|
|
|
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) {
|
2003-09-02 04:12:11 -04:00
|
|
|
first = first->next;
|
|
|
|
if (--p->refcnt == 0)
|
|
|
|
free ((char *)p);
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
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)
|
2003-09-02 04:12:11 -04:00
|
|
|
sum += p->len;
|
2003-08-19 15:19:38 -04:00
|
|
|
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-09-02 04:12:11 -04:00
|
|
|
memcpy (s, p+1, p->len);
|
|
|
|
p->refcnt++;
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2003-09-02 04:12:11 -04:00
|
|
|
p->refcnt++;
|
|
|
|
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)
|
2003-09-02 04:12:11 -04:00
|
|
|
sum += p->gclen <= 0 ? 1 : p->gclen-1;
|
2003-08-19 15:19:38 -04:00
|
|
|
vec = Make_Vector (sum, Null);
|
|
|
|
for (p = GC_List; p; p = p->next, i += n) {
|
2003-09-02 04:12:11 -04:00
|
|
|
n = p->gclen <= 0 ? 1 : p->gclen-1;
|
|
|
|
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) {
|
2003-09-02 04:12:11 -04:00
|
|
|
n = p->gclen <= 0 ? 1 : p->gclen-1;
|
|
|
|
memcpy (p->gcobj, &(VECTOR(vec)->data[i]), n * sizeof (Object));
|
2003-08-19 15:19:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|