aboutsummaryrefslogtreecommitdiffstats
path: root/vm/boehm-gc.c
blob: 23d80eaa2b074e8ebf1e6cf6eb05d51ee2d7e6e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "vm/gc.h"

#include "../boehmgc/include/gc.h"

#include <stdio.h>

static void *gc_out_of_memory(size_t nr)
{
	if (verbose_gc)
		fprintf(stderr, "[GC]\n");

	GC_gcollect();

	return NULL;	/* is this ok? */
}

static void gc_ignore_warnings(char *msg, GC_word arg)
{
}

static int
do_gc_register_finalizer(struct vm_object *object, finalizer_fn finalizer)
{
	GC_register_finalizer_no_order(object, (GC_finalization_proc) finalizer,
				       NULL, NULL, NULL);
	return 0;
}

static void *do_gc_malloc(size_t size)
{
	void *p;

	p = GC_malloc(size);
	if (!p)
		return NULL;

	memset(p, 0, size);

	return p;
}

static void *do_gc_malloc_noscan(size_t size)
{
	void *p;

	p = GC_malloc_atomic(size);
	if (!p)
		return NULL;

	memset(p, 0, size);

	return p;
}

static void *do_gc_malloc_uncollectable(size_t size)
{
	void *p;

	p = GC_malloc_uncollectable(size);
	if (!p)
		return NULL;

	memset(p, 0, size);

	return p;
}

static void do_gc_free(void *ptr)
{
	GC_free(ptr);
}

void gc_setup_boehm(void)
{
	gc_ops		= (struct gc_operations) {
		.gc_alloc		= do_gc_malloc,
		.gc_alloc_noscan	= do_gc_malloc_noscan,
		.vm_alloc		= do_gc_malloc_uncollectable,
		.vm_free		= do_gc_free,
		.gc_register_finalizer	= do_gc_register_finalizer
	};

	GC_set_warn_proc(gc_ignore_warnings);

	GC_oom_fn	= gc_out_of_memory;

	GC_quiet	= 1;

	GC_dont_gc	= dont_gc;

	GC_INIT();

	GC_set_max_heap_size(max_heap_size);
}