2019-05-10 11:34:20 +03:00
|
|
|
PROGRAMS = step0_repl step1_read_print step2_eval step3_env step4_if_fn_do \
|
|
|
|
step5_tco step6_file step7_quote step8_macros step9_try stepA_mal
|
vala: implement a garbage collector.
This fixes a lot of memory leakage due to me having relied on the Vala
reference-counting system to do my memory management.
The most obvious bad result of that design decision was the memory
leak from Mal.Nil pointing to itself as metadata. But fixing that
didn't solve the whole problem, because other kinds of reference cycle
are common.
In particular, the idiom `(def! FUNC (fn* (ARGS) BODY))`, for defining
a function in the most obvious way, would create a cycle of two
objects: from the outer environment in which FUNC is defined, to the
function object for FUNC itself, back to that same environment because
it was captured by FUNC.
And _either_ of those objects could end up being the only element of
the cycle referred to from the rest of the system: it could be the
environment, if nothing ever uses that function definition, or it
could be the function, if that function object is looked up and
returned from an outer function that was the last user of the
environment. So you can't break the cycle in the way that reference
counting systems would like you to, by making a well-chosen one of the
links weak: there's no universally right choice for which one it needs
to be.
So I've fixed it properly by writing a simple garbage collector. In
Vala's ref-counted environment, that works by being the only thing
allowed to hold an _owning_ reference to any derivative of GC.Object.
All the normal kinds of link between objects are now weak references;
each object provides a gc_traverse method which lists all the things
it links to; and when the garbage collector runs, it unlinks any
unwanted objects from its big linked list of all of them, causing the
one owned reference to each one to disappear.
Now the perf3 test can run without its memory usage gradually
increasing.
2019-05-12 12:05:34 +03:00
|
|
|
AUX1 = gc.vala types.vala reader.vala printer.vala
|
2019-05-10 11:34:20 +03:00
|
|
|
AUX3 = $(AUX1) env.vala
|
|
|
|
AUX4 = $(AUX3) core.vala
|
|
|
|
|
2019-05-12 12:05:23 +03:00
|
|
|
# Inhibit default make rules, in case they try to build from leftover .c files
|
|
|
|
.SUFFIXES:
|
|
|
|
|
2019-05-10 11:34:20 +03:00
|
|
|
all: $(PROGRAMS)
|
|
|
|
|
2019-05-13 11:14:52 +03:00
|
|
|
# You can define VFLAGS on the command line to add flags to the vala compiler.
|
|
|
|
# Some useful ones:
|
|
|
|
#
|
|
|
|
# -g annotate the output C with #line directives so that backtraces
|
|
|
|
# from gdb, sanitisers and valgrind will list Vala source locations
|
|
|
|
#
|
|
|
|
# -X -g -X -O0 compile the output C for sensible debugging
|
|
|
|
#
|
|
|
|
# -X -fsanitize=address link the output program against Address Sanitizer
|
|
|
|
#
|
|
|
|
# --save-temps don't automatically delete the C files after compiling
|
vala: implement a garbage collector.
This fixes a lot of memory leakage due to me having relied on the Vala
reference-counting system to do my memory management.
The most obvious bad result of that design decision was the memory
leak from Mal.Nil pointing to itself as metadata. But fixing that
didn't solve the whole problem, because other kinds of reference cycle
are common.
In particular, the idiom `(def! FUNC (fn* (ARGS) BODY))`, for defining
a function in the most obvious way, would create a cycle of two
objects: from the outer environment in which FUNC is defined, to the
function object for FUNC itself, back to that same environment because
it was captured by FUNC.
And _either_ of those objects could end up being the only element of
the cycle referred to from the rest of the system: it could be the
environment, if nothing ever uses that function definition, or it
could be the function, if that function object is looked up and
returned from an outer function that was the last user of the
environment. So you can't break the cycle in the way that reference
counting systems would like you to, by making a well-chosen one of the
links weak: there's no universally right choice for which one it needs
to be.
So I've fixed it properly by writing a simple garbage collector. In
Vala's ref-counted environment, that works by being the only thing
allowed to hold an _owning_ reference to any derivative of GC.Object.
All the normal kinds of link between objects are now weak references;
each object provides a gc_traverse method which lists all the things
it links to; and when the garbage collector runs, it unlinks any
unwanted objects from its big linked list of all of them, causing the
one owned reference to each one to disappear.
Now the perf3 test can run without its memory usage gradually
increasing.
2019-05-12 12:05:34 +03:00
|
|
|
#
|
|
|
|
# -D GC_STATS print statistics every time the garbage collector runs
|
|
|
|
#
|
|
|
|
# -D GC_DEBUG print full diagnostics from the garbage collector
|
|
|
|
#
|
|
|
|
# -D GC_ALWAYS make the garbage collector run at every opportunity
|
|
|
|
# (good for making occasional GC errors show up sooner)
|
2019-05-13 11:14:52 +03:00
|
|
|
|
2019-05-10 11:34:20 +03:00
|
|
|
$(PROGRAMS): %: %.vala
|
2019-05-13 11:14:52 +03:00
|
|
|
valac $(VFLAGS) -o $@ $^ $(DEFINES) --pkg readline -X -lreadline
|
2019-05-10 11:34:20 +03:00
|
|
|
|
|
|
|
step1_read_print step2_eval: override DEFINES += -D NO_ENV
|
|
|
|
|
|
|
|
step0_repl:
|
|
|
|
step1_read_print: $(AUX1)
|
|
|
|
step2_eval: $(AUX1)
|
|
|
|
step3_env: $(AUX3)
|
|
|
|
step4_if_fn_do: $(AUX4)
|
|
|
|
step5_tco: $(AUX4)
|
|
|
|
step6_file: $(AUX4)
|
|
|
|
step7_quote: $(AUX4)
|
|
|
|
step8_macros: $(AUX4)
|
|
|
|
step9_try: $(AUX4)
|
|
|
|
stepA_mal: $(AUX4)
|
|
|
|
|
|
|
|
clean: clean-c
|
|
|
|
rm -f $(PROGRAMS)
|
|
|
|
|
|
|
|
clean-c:
|
|
|
|
rm -f *.c *.h
|