add object finalizer

This commit is contained in:
Yuichi Nishiwaki 2013-10-13 19:00:39 +09:00
parent dc2f25ef2a
commit a5db43ba91
3 changed files with 58 additions and 24 deletions

28
include/picrin/irep.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef IREP_H__
#define IREP_H__
enum pic_instruction {
OP_PUSHNIL,
OP_PUSHI,
OP_PUSHUNDEF,
OP_GREF,
OP_GSET,
OP_CONS,
OP_ADD,
OP_STOP
};
struct pic_code {
enum pic_instruction insn;
union {
int i;
struct pic_pair *gvar;
} u;
};
struct pic_irep {
struct pic_code *code;
size_t clen, ccapa;
};
#endif

View File

@ -2,6 +2,7 @@
#include "picrin.h"
#include "picrin/gc.h"
#include "picrin/irep.h"
void
init_heap_page(struct heap_page *heap)
@ -180,6 +181,33 @@ gc_unmark(union header *p)
p->s.mark = PIC_GC_UNMARK;
}
static void
gc_finalize_object(pic_state *pic, struct pic_object *obj)
{
switch (obj->tt) {
case PIC_TT_SYMBOL: {
char *name;
name = ((struct pic_symbol *)obj)->name;
free(name);
break;
}
case PIC_TT_PAIR: {
break;
}
case PIC_TT_PROC: {
struct pic_proc *proc;
proc = (struct pic_proc *)obj;
/* free irep */
free(proc->u.irep->code);
free(proc->u.irep);
}
default:
pic_raise(pic, "logic flaw");
}
}
static void
gc_sweep_phase(pic_state *pic)
{
@ -193,6 +221,7 @@ gc_sweep_phase(pic_state *pic)
continue;
}
/* free! */
gc_finalize_object(pic, (struct pic_object *)(bp + 1));
if (bp + bp->s.size == p->s.ptr) {
bp->s.size += p->s.ptr->s.size;
bp->s.ptr = p->s.ptr->s.ptr;

View File

@ -2,30 +2,7 @@
#include <stdio.h>
#include "picrin.h"
enum pic_instruction {
OP_PUSHNIL,
OP_PUSHI,
OP_PUSHUNDEF,
OP_GREF,
OP_GSET,
OP_CONS,
OP_ADD,
OP_STOP
};
struct pic_code {
enum pic_instruction insn;
union {
int i;
struct pic_pair *gvar;
} u;
};
struct pic_irep {
struct pic_code *code;
size_t clen, ccapa;
};
#include "picrin/irep.h"
static pic_value
pic_assq(pic_state *pic, pic_value key, pic_value assoc)