Test new weak-box and finalizer functions. Includes value-printer.
This commit is contained in:
parent
7bd6e616ff
commit
f9f3cae062
152
gc_test.c
152
gc_test.c
|
|
@ -15,9 +15,13 @@ void out_of_memory(void)
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void print_value(value_t v);
|
||||||
|
static inline void comma(void) { fputs(", ", stdout); }
|
||||||
|
static inline void nl(void) { putchar('\n'); }
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
gc_root_t list_root;
|
gc_root_t list_root, tmp_root;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
|
||||||
gc_init(1024, 256*1024*1024);
|
gc_init(1024, 256*1024*1024);
|
||||||
|
|
@ -25,7 +29,60 @@ int main(int argc, char **argv)
|
||||||
srand((unsigned int)time(NULL));
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
register_gc_root(&list_root, NIL);
|
register_gc_root(&list_root, NIL);
|
||||||
|
register_gc_root(&tmp_root, NIL);
|
||||||
|
|
||||||
|
tmp_root.value = cons(make_fixnum(1), cons(make_fixnum(2), NIL));
|
||||||
|
list_root.value = make_weak_box(tmp_root.value);
|
||||||
|
|
||||||
|
register_finalizer(tmp_root.value, make_fixnum(10));
|
||||||
|
|
||||||
|
print_value(list_root.value); comma();
|
||||||
|
{
|
||||||
|
value_t v, f;
|
||||||
|
|
||||||
|
get_next_finalizer(&v, &f);
|
||||||
|
|
||||||
|
print_value(v); comma();
|
||||||
|
print_value(f); nl(); nl();
|
||||||
|
}
|
||||||
|
|
||||||
|
collect_garbage(0);
|
||||||
|
|
||||||
|
print_value(list_root.value); comma();
|
||||||
|
{
|
||||||
|
value_t v, f;
|
||||||
|
|
||||||
|
get_next_finalizer(&v, &f);
|
||||||
|
|
||||||
|
print_value(v); comma();
|
||||||
|
print_value(f); nl(); nl();
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp_root.value = NIL;
|
||||||
|
|
||||||
|
print_value(list_root.value); comma();
|
||||||
|
{
|
||||||
|
value_t v, f;
|
||||||
|
|
||||||
|
get_next_finalizer(&v, &f);
|
||||||
|
|
||||||
|
print_value(v); comma();
|
||||||
|
print_value(f); nl(); nl();
|
||||||
|
}
|
||||||
|
|
||||||
|
collect_garbage(0);
|
||||||
|
|
||||||
|
print_value(list_root.value); comma();
|
||||||
|
{
|
||||||
|
value_t v, f;
|
||||||
|
|
||||||
|
get_next_finalizer(&v, &f);
|
||||||
|
|
||||||
|
print_value(v); comma();
|
||||||
|
print_value(f); nl(); nl();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if 0
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
int r = rand() & 0x3fff;
|
int r = rand() & 0x3fff;
|
||||||
|
|
@ -80,10 +137,103 @@ int main(int argc, char **argv)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
unregister_gc_root(&list_root);
|
unregister_gc_root(&list_root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void print_value(value_t v)
|
||||||
|
{
|
||||||
|
if (v == NIL)
|
||||||
|
{
|
||||||
|
fputs("nil", stdout);
|
||||||
|
}
|
||||||
|
else if (v == FALSE_VALUE)
|
||||||
|
{
|
||||||
|
fputs("#f", stdout);
|
||||||
|
}
|
||||||
|
else if (v == TRUE_VALUE)
|
||||||
|
{
|
||||||
|
fputs("#t", stdout);
|
||||||
|
}
|
||||||
|
else if (is_fixnum(v))
|
||||||
|
{
|
||||||
|
printf("%d", (int)get_fixnum(v));
|
||||||
|
}
|
||||||
|
else if (is_box(v))
|
||||||
|
{
|
||||||
|
fputs("#&", stdout);
|
||||||
|
print_value(_get_box(v)->value);
|
||||||
|
}
|
||||||
|
else if (is_pair(v))
|
||||||
|
{
|
||||||
|
putchar('(');
|
||||||
|
|
||||||
|
print_value(_get_pair(v)->car);
|
||||||
|
v = _get_pair(v)->cdr;
|
||||||
|
|
||||||
|
while (is_pair(v))
|
||||||
|
{
|
||||||
|
putchar(' ');
|
||||||
|
print_value(_get_pair(v)->car);
|
||||||
|
v = _get_pair(v)->cdr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (v != NIL)
|
||||||
|
{
|
||||||
|
fputs(" . ", stdout);
|
||||||
|
print_value(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
putchar(')');
|
||||||
|
}
|
||||||
|
else if (is_vector(v))
|
||||||
|
{
|
||||||
|
fputs("#(", stdout);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < _get_vector(v)->size; ++i)
|
||||||
|
{
|
||||||
|
if (i != 0) putchar(' ');
|
||||||
|
print_value(_get_vector(v)->elements[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
putchar(')');
|
||||||
|
}
|
||||||
|
else if (is_byte_string(v))
|
||||||
|
{
|
||||||
|
fputs("#B(", stdout);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < _get_byte_string(v)->size; ++i)
|
||||||
|
{
|
||||||
|
if (i != 0) putchar(' ');
|
||||||
|
printf("%d", (int)_get_byte_string(v)->bytes[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
putchar(')');
|
||||||
|
}
|
||||||
|
else if (is_struct(v))
|
||||||
|
{
|
||||||
|
fputs("#S(", stdout);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < _get_struct(v)->nslots; ++i)
|
||||||
|
{
|
||||||
|
if (i != 0) putchar(' ');
|
||||||
|
print_value(_get_struct(v)->slots[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
putchar(')');
|
||||||
|
}
|
||||||
|
else if (is_weak_box(v))
|
||||||
|
{
|
||||||
|
fputs("#W&", stdout);
|
||||||
|
print_value(_get_weak_box(v)->value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fputs("#<unknown>", stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* vim:set sw=2 expandtab: */
|
/* vim:set sw=2 expandtab: */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue