Commit f2e1c77e authored by Juergen Nickelsen's avatar Juergen Nickelsen
Browse files

first step to garbage collection: protect objects from the sweep

by putting them on a protect stack
parent 85130616
#2345678901234567890123456789012345678901234567890123456789012345678901234567890
# 1 2 3 4 5 6 7 8
HEADERS = objects.h hashmap.h cbasics.h xmemory.h printer.h reader.h signals.h \
strbuf.h functions.h eval.h names.h builtins.h io.h repl.h
strbuf.h functions.h eval.h names.h builtins.h io.h repl.h gc.h
SOURCES = main.c hashmap.c xmemory.c objects.c printer.c reader.c signals.c \
strbuf.c vectors.c xdump.c eval.c builtins.c io.c repl.c
strbuf.c vectors.c xdump.c eval.c builtins.c io.c repl.c gc.c
OBJECTS = $(subst .c,.o,$(SOURCES))
HOBJECTS = objects.o xmemory.o xdump.o strbuf.o
CFLAGS = -g# -O4 -DNDEBUG
......
#include "cbasics.h"
#include <stdlib.h>
#include "objects.h"
#include "xmemory.h"
#include "gc.h"
typedef struct GCPROT {
obp_t *obpp;
struct GCPROT *next;
} *gcp_t;
gcp_t gc_prot_root;
void gc_start_protect(void)
{
gc_protect(0);
}
void gc_protect(obp_t *obpp)
{
gcp_t newp = xmalloc(sizeof(struct GCPROT), "gc protect entry");
newp->obpp = obpp;
newp->next = gc_prot_root;
gc_prot_root = newp;
}
void gc_unprotect(void)
{
while (gc_prot_root) {
gcp_t entry = gc_prot_root;
gc_prot_root = gc_prot_root->next;
obp_t *obpp = entry->obpp;
free(entry);
if (!obpp) {
break;
}
}
}
#ifndef __GC_H_INC
#define __GC_H_INC
#include "cbasics.h"
void gc_start_protect(void);
void gc_protect(obp_t *obpp);
void gc_unprotect(void);
#define protect(obvar) gc_protect(&obvar)
#define protect1(ob1) (gc_start_protect(), protect(ob1))
#define protect2(ob1, ob2) \
(protect1(ob1), protect(ob2))
#define protect3(ob1, ob2, ob3) \
(protect2(ob1, ob2), protect(ob3))
#define protect4(ob1, ob2, ob3, ob4) \
(protect3(ob1, ob2, ob3), protect(ob4))
#define protect5(ob1, ob2, ob3, ob4, ob5) \
(protect4(ob1, ob2, ob3, ob4), protect(ob5))
#define protect6(ob1, ob2, ob3, ob4, ob5, ob6) \
(protect5(ob1, ob2, ob3, ob4, ob5), protect(ob6))
#endif /* __GC_H_INC */
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment