~swisschili/bluejay

16156be5a740f7b4a075d9f15363b4fef9834043 — swissChili 2 years ago 8056031 master
Fix memory leaks revealed by GC segments
4 files changed, 5 insertions(+), 19 deletions(-)

M src/lisp/Jmk2
M src/lisp/compiler.dasc
M src/lisp/gc.c
M src/lisp/main.c
M src/lisp/Jmk2 => src/lisp/Jmk2 +1 -1
@@ 36,7 36,7 @@ rule repl [pwd]/lisp {

rule valgrind [pwd]/lisp {
	log VALGRIND "lisp test-gc.lisp"
	shell "LISP_LIBRARY_PATH=$::lisp_libpath valgrind --track-origins=yes --leak-check=full ./lisp test-gc.lisp"
	shell "LISP_LIBRARY_PATH=$::lisp_libpath valgrind --track-origins=yes --leak-check=full --show-leak-kinds=all ./lisp test-gc.lisp"
}

srcs main.c lisp.c compiler.c lib/std.c plat/linux.c istream.c gc.c \

M src/lisp/compiler.dasc => src/lisp/compiler.dasc +3 -0
@@ 554,6 554,9 @@ value_t eval(struct environment *env, value_t form)

	value_t (*f)() = link_program(&d);

	dasm_free(&d);
	free(args);

	gc_prepare_call(0);
	value_t val = f();


M src/lisp/gc.c => src/lisp/gc.c +0 -18
@@ 69,15 69,6 @@ void _mark(value_t value, unsigned int *marked)
		void *pointer = (void *)(value & ~HEAP_MASK);
		struct alloc *alloc = pointer - sizeof(struct alloc);

		if (!pointer)
		{
			// TODO: Where are these coming from? Maybe this is a C
			// stack variable that we are interpreting as beign in
			// Lisp stack space on accident?
			fprintf(stderr, "lisp:gc:warning: value %x is a null pointer\n", value);
			return;
		}

		// Only recursively mark if this hasn't been marked yet. i.e. prevent
		// marking circular references twice
		if (alloc->mark != gc_mark)


@@ 86,9 77,6 @@ void _mark(value_t value, unsigned int *marked)

			alloc->mark = gc_mark;

			// printf("[ GC ] val =");
			// printval(alloc_to_value(alloc), 2);

			switch (alloc->type_tag)
			{
			case CONS_TAG: {


@@ 132,7 120,6 @@ void _sweep()
		}
		else
		{
			fprintf(stderr, "[ GC ] freeing: %p\n", a);
			// Free and remove from allocation list
			struct alloc *p = a->prev, *n = a->next;
			del_alloc(a);


@@ 207,17 194,12 @@ void _do_gc(unsigned int esp, unsigned int ebp)
		value_t *args = seg->seg_end + 4;
		for (int i = 0; i < seg->nargs; i++)
		{
			fprintf(stderr, "Marking arg %d\n", i);

			// mark arguments
			_mark(args[i], &num_marked);
		}

		for (int i = 0; i < seg->nretained; i++)
		{
			fprintf(stderr, "Marking retained %d\n", i);
			printval(seg->retained[i], 0);

			_mark(seg->retained[i], &num_marked);
		}


M src/lisp/main.c => src/lisp/main.c +1 -0
@@ 39,4 39,5 @@ done:
	free_all();
	if (env)
		del_env(env);
	gc_pop_segment();
}