2005-05-01 11:48:36 +00:00
|
|
|
#include "SchemeObject.h"
|
2005-05-02 02:33:44 +00:00
|
|
|
#include "defs.h"
|
2005-05-06 23:25:06 +00:00
|
|
|
//#include "debug.h"
|
2005-05-02 02:33:44 +00:00
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
SchemeObject []maybe_garbage, not_garbage, not_garbage_end, wait_list, roots, queue_pos;
|
2005-05-02 02:33:44 +00:00
|
|
|
BOOL markstate;
|
|
|
|
|
2005-05-06 23:25:06 +00:00
|
|
|
typedef enum {
|
|
|
|
GC_IDLE = 0,
|
|
|
|
GC_MARK = 1,
|
|
|
|
GC_SWEEP = 2
|
|
|
|
} gc_state_e;
|
|
|
|
|
|
|
|
gc_state_e gc_state;
|
|
|
|
integer checkpoint;
|
|
|
|
|
2005-05-08 11:17:44 +00:00
|
|
|
#define GC_AMOUNT 100
|
2005-05-01 11:48:36 +00:00
|
|
|
|
|
|
|
@implementation SchemeObject
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
+ (void) initialize
|
|
|
|
{
|
2011-01-14 03:07:40 +00:00
|
|
|
maybe_garbage = not_garbage = not_garbage_end = wait_list = roots = nil;
|
2005-05-02 02:33:44 +00:00
|
|
|
markstate = true;
|
2005-05-06 23:25:06 +00:00
|
|
|
gc_state = GC_IDLE;
|
|
|
|
checkpoint = 0;
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) initDummy
|
|
|
|
{
|
|
|
|
self = [super init];
|
2011-01-14 03:07:40 +00:00
|
|
|
prev = next = nil;
|
2005-05-02 02:33:44 +00:00
|
|
|
marked = markstate;
|
|
|
|
root = false;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2010-12-16 11:01:49 +00:00
|
|
|
+ (id) dummyObject
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2010-12-16 11:01:49 +00:00
|
|
|
return [[SchemeObject alloc] initDummy];
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) collect
|
|
|
|
{
|
2011-01-09 10:41:24 +00:00
|
|
|
local SchemeObject []cur;
|
2005-05-06 23:25:06 +00:00
|
|
|
local integer amount;
|
|
|
|
|
|
|
|
switch (gc_state) {
|
|
|
|
case GC_IDLE:
|
|
|
|
dprintf("GC: Starting collection...\n");
|
|
|
|
gc_state = GC_MARK;
|
|
|
|
not_garbage = not_garbage_end = [SchemeObject dummyObject];
|
|
|
|
for (cur = roots; cur; cur = cur.next) {
|
|
|
|
[cur markReachable];
|
|
|
|
}
|
|
|
|
queue_pos = not_garbage_end;
|
|
|
|
return;
|
|
|
|
case GC_MARK:
|
|
|
|
dprintf("GC: Marking...\n");
|
|
|
|
amount = 0;
|
|
|
|
while (queue_pos) {
|
|
|
|
dprintf("GC: marking queue: %s[%s]@%i\n",
|
|
|
|
[queue_pos description],
|
|
|
|
[queue_pos printForm],
|
|
|
|
(integer) queue_pos);
|
|
|
|
[queue_pos markReachable];
|
|
|
|
queue_pos = queue_pos.prev;
|
2005-05-08 03:44:18 +00:00
|
|
|
if (++amount >= GC_AMOUNT/2) return;
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
2005-05-08 03:44:18 +00:00
|
|
|
dprintf("MARKED: %i reachable objects\n", amount);
|
2005-05-06 23:25:06 +00:00
|
|
|
gc_state = GC_SWEEP;
|
|
|
|
queue_pos = maybe_garbage;
|
|
|
|
return;
|
|
|
|
case GC_SWEEP:
|
|
|
|
dprintf("GC: Sweeping...\n");
|
|
|
|
amount = 0;
|
|
|
|
while (queue_pos) {
|
|
|
|
dprintf("GC: freeing %s[%s]@%i...\n",
|
|
|
|
[queue_pos description],
|
|
|
|
[queue_pos printForm],
|
|
|
|
(integer) queue_pos);
|
|
|
|
[queue_pos release];
|
|
|
|
queue_pos = queue_pos.next;
|
2005-05-08 03:44:18 +00:00
|
|
|
//if (++amount == GC_AMOUNT) return;
|
|
|
|
}
|
2005-05-06 23:25:06 +00:00
|
|
|
maybe_garbage = not_garbage;
|
|
|
|
not_garbage_end.next = wait_list;
|
|
|
|
if (wait_list) {
|
|
|
|
wait_list.prev = not_garbage_end;
|
|
|
|
}
|
2011-01-14 03:07:40 +00:00
|
|
|
wait_list = nil;
|
|
|
|
not_garbage_end = nil;
|
|
|
|
not_garbage = nil;
|
2005-05-06 23:25:06 +00:00
|
|
|
markstate = !markstate;
|
|
|
|
gc_state = GC_IDLE;
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
2005-05-06 23:25:06 +00:00
|
|
|
}
|
|
|
|
|
2010-12-16 11:01:49 +00:00
|
|
|
+ (void) collectCheckPoint
|
|
|
|
{
|
|
|
|
if (checkpoint >= GC_AMOUNT)
|
|
|
|
{
|
|
|
|
[self collect];
|
|
|
|
checkpoint = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-06 23:25:06 +00:00
|
|
|
+ (void) finishCollecting
|
|
|
|
{
|
|
|
|
while (gc_state) {
|
|
|
|
[self collect];
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
2005-05-02 02:33:44 +00:00
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
|
|
|
self = [super init];
|
2005-05-08 03:44:18 +00:00
|
|
|
|
2005-05-06 23:25:06 +00:00
|
|
|
if (gc_state) {
|
|
|
|
if (wait_list) {
|
|
|
|
wait_list.prev = self;
|
|
|
|
}
|
|
|
|
next = wait_list;
|
|
|
|
wait_list = self;
|
|
|
|
marked = markstate;
|
|
|
|
dprintf("GC: During collect: %i\n", (integer) self);
|
|
|
|
} else {
|
|
|
|
if (maybe_garbage) {
|
|
|
|
maybe_garbage.prev = self;
|
|
|
|
}
|
|
|
|
next = maybe_garbage;
|
|
|
|
maybe_garbage = self;
|
|
|
|
marked = !markstate;
|
|
|
|
dprintf("GC: Not during collect: %i\n", (integer) self);
|
2005-05-02 02:33:44 +00:00
|
|
|
}
|
2005-05-06 23:25:06 +00:00
|
|
|
|
2011-01-14 03:07:40 +00:00
|
|
|
prev = nil;
|
2005-05-02 02:33:44 +00:00
|
|
|
root = false;
|
2005-05-08 03:44:18 +00:00
|
|
|
checkpoint++;
|
2005-05-02 02:33:44 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
- (void) mark
|
|
|
|
{
|
2005-05-02 02:33:44 +00:00
|
|
|
if (!root && marked != markstate) {
|
|
|
|
dprintf("GC: Marking %s[%s]@%i\n", [self description], [self printForm], (integer) self);
|
|
|
|
marked = markstate;
|
|
|
|
if (prev) {
|
|
|
|
prev.next = next;
|
|
|
|
} else {
|
|
|
|
maybe_garbage = next;
|
|
|
|
}
|
|
|
|
if (next) {
|
|
|
|
next.prev = prev;
|
|
|
|
}
|
|
|
|
if (not_garbage) {
|
|
|
|
not_garbage.prev = self;
|
|
|
|
}
|
|
|
|
next = not_garbage;
|
2011-01-14 03:07:40 +00:00
|
|
|
prev = nil;
|
2005-05-02 02:33:44 +00:00
|
|
|
not_garbage = self;
|
|
|
|
}
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) makeRootCell
|
2005-05-01 11:48:36 +00:00
|
|
|
{
|
2005-05-08 06:38:01 +00:00
|
|
|
if (root)
|
|
|
|
return;
|
2005-05-06 23:25:06 +00:00
|
|
|
if (gc_state) {
|
|
|
|
dprintf("Root cell made during collection!\n");
|
|
|
|
[SchemeObject finishCollecting];
|
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
if (prev) {
|
|
|
|
prev.next = next;
|
2005-05-01 11:48:36 +00:00
|
|
|
} else {
|
2005-05-02 02:33:44 +00:00
|
|
|
maybe_garbage = next;
|
|
|
|
}
|
|
|
|
if (next) {
|
|
|
|
next.prev = prev;
|
|
|
|
}
|
|
|
|
if (roots) {
|
|
|
|
roots.prev = self;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
2005-05-02 02:33:44 +00:00
|
|
|
next = roots;
|
2011-01-14 03:07:40 +00:00
|
|
|
prev = nil;
|
2005-05-02 02:33:44 +00:00
|
|
|
roots = self;
|
|
|
|
root = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) markReachable
|
|
|
|
{
|
|
|
|
return;
|
2005-05-01 11:48:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (string) printForm
|
|
|
|
{
|
|
|
|
return "<generic>";
|
|
|
|
}
|
|
|
|
|
2005-05-02 02:33:44 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
dprintf("Deallocing %s @ %i!\n", [self description], (integer) self);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
2005-05-02 04:58:22 +00:00
|
|
|
- (BOOL) isError
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (string) source
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) source: (string) s
|
|
|
|
{
|
|
|
|
source = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (integer) line
|
|
|
|
{
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) line: (integer) l
|
|
|
|
{
|
|
|
|
line = l;
|
|
|
|
}
|
|
|
|
|
2005-05-01 11:48:36 +00:00
|
|
|
@end
|