Reference counted garbage collection

Published September 23, 2011
Advertisement
Bit too tired to write this up in detail, but I've got an interesting system working in the VM now where, as per ApochPiQ's advice last post, I'm now using reference counted garbage collection. A buffer is also tagged to say whether it points to a list of allocated buffers and if so, when it is freed it decrements the references of its members recursively, freeing them if needed.

I've implemented arbitrary dimensional arrays in the compiler and got everything working at run-time using a Copy On Write system that utilises the reference counting built into the VM. For what it is worth:

[source]

char[][][] c={ { "hello","world" },{ "hi","there" } };
char[][][] d=c;

out c[0][1],"\n";
out c[1][1],"\n";
out d[0][1],"\n";
out d[1][1],"\n";

c[0][1]="paul";
d[1][1]="froob";

out c[0][1],"\n";
out c[1][1],"\n";
out d[0][1],"\n";
out d[1][1],"\n";
[/source]

Compiles to (deep breath):

[source]

setrxi 2
push
setrxi 4
push
muli
pop
alloc
setrxi 1
setbt
movar
push
movar
push
setrxi 2
push
setrxi 4
push
muli
pop
alloc
setrxi 1
setbt
movar
push
movar
push
setrxi string1
push
axstm 8
get
movra
pop
put
pop
addax 4
movar
push
setrxi string2
push
axstm 8
get
movra
pop
put
pop
axstm 4
get
movra
incref
axstm 8
get
movra
pop
put
pop
addax 4
movar
push
setrxi 2
push
setrxi 4
push
muli
pop
alloc
setrxi 1
setbt
movar
push
movar
push
setrxi string3
push
axstm 8
get
movra
pop
put
pop
addax 4
movar
push
setrxi string4
push
axstm 8
get
movra
pop
put
pop
axstm 4
get
movra
incref
axstm 8
get
movra
pop
put
pop
axstm 4
get
movra
incref
axstm 4
get
push
axstm 4
get
movra
incref
axstm 8
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 8
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 8
get
movra
detach
movar
axstm 8
put
setrxi string6
push
axstm 12
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
push
get
movra
decref
pop
movra
pop
put
push
pop
gc
axstm 4
get
movra
detach
movar
axstm 4
put
setrxi string7
push
axstm 8
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
push
get
movra
decref
pop
movra
pop
put
push
pop
gc
axstm 8
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 8
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 0
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
axstm 4
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
setrxi 1
push
axstm 4
get
push
axstm 12
get
movra
pop
chkbi 4
setrxi 4
push
muli
addi
pop
movra
get
push
pop
outs
setrxi string5
push
pop
outs
pop
movra
decref
pop
movra
decref
gc
exit

-1 1 0 string5: 10b
-1 5 0 string7: "froob"
-1 5 0 string1: "hello"
-1 2 0 string3: "hi"
-1 4 0 string6: "paul"
-1 5 0 string4: "there"
-1 5 0 string2: "world"
[/source]

And correctly outputs what you would expect if deep copies of the arrays were being made. In fact the two arrays point to the same memory until the c[0][1]="paul" line, at which point a clone is created which c points to. When d[1][1]="froob" is executed, the same happens but as d only now has one reference, the cloning is skipped.

My god this stuff is hard to get right and to debug when it goes wrong. Wish my project was proceeding at the pace that Deyja's VM/compiler project was, but then I've written lots of these before and really wanted to see if I could get this nonsense to work.

This is an example of the VM running in debug mode, just to give you an idea of what I am staring at while going mad trying to work out what is broken.

[source]
Om 1.0 Copyright(C)2011 Aardvajk Software
REGISTERS:
pc : 5
ir : setrxi (11)
st : 1402
rx.i: 2
rx.f: 2.8026e-045
ax : 2004270470
cx : -1
ALLOCATED BLOCKS:
FREE BLOCKS:
2426: 1024
MEMORY SIZE: 3450
REGISTERS:
pc : 6
ir : push (21)
st : 1406
rx.i: 2
rx.f: 2.8026e-045
ax : 2004270470
cx : -1
ALLOCATED BLOCKS:
FREE BLOCKS:
2426: 1024
MEMORY SIZE: 3450
REGISTERS:
pc : 11
ir : setrxi (11)
st : 1406
rx.i: 4
rx.f: 5.60519e-045
ax : 2004270470
cx : -1
ALLOCATED BLOCKS:
FREE BLOCKS:
2426: 1024
MEMORY SIZE: 3450
ALLOCATED BLOCKS:
STACK:
1402: 2 (2)
1403: 0
1404: 0
1405: 0
[/source]

Need to think about array resizing next really. Not much point having dynamic arrays in a language if they, er, aren't.
0 likes 4 comments

Comments

Deyja
Part of the reason I'm moving so fast is that I'm writing the virtual machine in C#. My stack is a stack of Objects, my registers are Objects, and my 'classes' are an array of objects. I pay a performance penalty but I don't have to deal with figuring out the size of objects on the stack or do any sort of memory management.
September 24, 2011 04:07 AM
Aardvajk
Yeah, I think these are very different projects, although it's kind of fun that there are two VM/compiler projects running at a roughly similar development level in journal-land at the same time.

Mine is a complete curiosity. Being written purely for the masochistic joy of seeing if I can make this work.
September 24, 2011 07:56 AM
Deyja
Have you implemented virtual functions yet?
September 24, 2011 12:19 PM
Aardvajk
Hell no, not even implemented normal functions yet. :)
September 24, 2011 02:39 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement