mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-11 06:47:37 +02:00
WIP
This commit is contained in:
parent
0eb6be21d4
commit
4beac74189
1 changed files with 12 additions and 14 deletions
26
gc.c
26
gc.c
|
@ -74,29 +74,27 @@ void *gc_try_alloc(gc_heap *h, size_t size)
|
||||||
void *gc_alloc(gc_heap *h, size_t size, int *heap_grown)
|
void *gc_alloc(gc_heap *h, size_t size, int *heap_grown)
|
||||||
{
|
{
|
||||||
void *result = NULL;
|
void *result = NULL;
|
||||||
size_t max_freed, sum_freed, total_size;
|
size_t max_freed = 0, sum_freed = 0, total_size;
|
||||||
// TODO: check return value, if null (could not alloc) then
|
// TODO: check return value, if null (could not alloc) then
|
||||||
// run a collection and check how much free space there is. if less
|
// run a collection and check how much free space there is. if less
|
||||||
// the allowed ratio, try growing heap.
|
// the allowed ratio, try growing heap.
|
||||||
// then try realloc. if cannot alloc now, then throw out of memory error
|
// then try realloc. if cannot alloc now, then throw out of memory error
|
||||||
size = gc_heap_align(size);
|
size = gc_heap_align(size);
|
||||||
//return gc_try_alloc(h, size);
|
|
||||||
result = gc_try_alloc(h, size);
|
result = gc_try_alloc(h, size);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
// TODO: may want to consider not doing this now, and implementing gc_collect as
|
// TODO: may want to consider not doing this now, and implementing gc_collect as
|
||||||
// part of the runtime, since we would have all of the roots, stack args,
|
// part of the runtime, since we would have all of the roots, stack args,
|
||||||
// etc available there.
|
// etc available there.
|
||||||
// max_freed = gc_collect(h); TODO: this does not work yet!
|
// max_freed = gc_collect(h); TODO: this does not work yet!
|
||||||
max_freed = 0;
|
//
|
||||||
|
// total_size = gc_heap_total_size(h);
|
||||||
total_size = gc_heap_total_size(h);
|
// if (((max_freed < size) ||
|
||||||
if (((max_freed < size) ||
|
// ((total_size > sum_freed) &&
|
||||||
((total_size > sum_freed) &&
|
// (total_size - sum_freed) > (total_size * 0.75))) // Grow ratio
|
||||||
(total_size - sum_freed) > (total_size * 0.75))) // Grow ratio
|
// && ((!h->max_size) || (total_size < h->max_size))) {
|
||||||
&& ((!h->max_size) || (total_size < h->max_size))) {
|
|
||||||
gc_grow_heap(h, size, 0);
|
gc_grow_heap(h, size, 0);
|
||||||
*heap_grown = 1;
|
*heap_grown = 1;
|
||||||
}
|
// }
|
||||||
result = gc_try_alloc(h, size);
|
result = gc_try_alloc(h, size);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
fprintf(stderr, "out of memory error allocating %d bytes\n", size);
|
fprintf(stderr, "out of memory error allocating %d bytes\n", size);
|
||||||
|
@ -104,7 +102,7 @@ max_freed = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#if GC_DEBUG_PRINTFS
|
#if GC_DEBUG_PRINTFS
|
||||||
fprintf(stdout, "alloc %p\n", result);
|
// fprintf(stdout, "alloc %p\n", result);
|
||||||
#endif
|
#endif
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -165,7 +163,7 @@ void gc_mark(gc_heap *h, object obj)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
#if GC_DEBUG_PRINTFS
|
#if GC_DEBUG_PRINTFS
|
||||||
fprintf(stdout, "gc_mark %p\n", obj);
|
// fprintf(stdout, "gc_mark %p\n", obj);
|
||||||
#endif
|
#endif
|
||||||
((list)obj)->hdr.mark = 1;
|
((list)obj)->hdr.mark = 1;
|
||||||
// TODO: mark heap saves (??)
|
// TODO: mark heap saves (??)
|
||||||
|
@ -234,7 +232,7 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr)
|
||||||
|
|
||||||
if (!mark(p)) {
|
if (!mark(p)) {
|
||||||
#if GC_DEBUG_PRINTFS
|
#if GC_DEBUG_PRINTFS
|
||||||
fprintf(stdout, "sweep: object is not marked %p\n", p);
|
// fprintf(stdout, "sweep: object is not marked %p\n", p);
|
||||||
#endif
|
#endif
|
||||||
// free p
|
// free p
|
||||||
sum_freed += size;
|
sum_freed += size;
|
||||||
|
@ -270,7 +268,7 @@ size_t gc_sweep(gc_heap *h, size_t *sum_freed_ptr)
|
||||||
max_freed = freed;
|
max_freed = freed;
|
||||||
} else {
|
} else {
|
||||||
#if GC_DEBUG_PRINTFS
|
#if GC_DEBUG_PRINTFS
|
||||||
fprintf(stdout, "sweep: object is marked %p\n", p);
|
// fprintf(stdout, "sweep: object is marked %p\n", p);
|
||||||
#endif
|
#endif
|
||||||
((list)p)->hdr.mark = 0;
|
((list)p)->hdr.mark = 0;
|
||||||
p = (object)(((char *)p) + size);
|
p = (object)(((char *)p) + size);
|
||||||
|
|
Loading…
Add table
Reference in a new issue