Signed-off-by: Jean Privat <jean@pryen.org>
+#ifdef DEBUG
+# include <assert.h>
+#else
+# define assert(x) while(0)
+#endif
+void Nit_gc_init(void) {
heapActive = malloc(sizeof(heap));
heapInactive = malloc(sizeof(heap));
heapActive = malloc(sizeof(heap));
heapInactive = malloc(sizeof(heap));
GC_List_Init(&staticObjects);
}
GC_List_Init(&staticObjects);
}
-val_t GC_evacuation(obj_t object){
+val_t GC_evacuation(obj_t object) {
bigint size;
bigint objectSize;
val_t newAdress;
bigint size;
bigint objectSize;
val_t newAdress;
BOX_struct box;
assert(ISOBJ(object) && !ISNULL(object));
BOX_struct box;
assert(ISOBJ(object) && !ISNULL(object));
- if ( GET_MARKBIT(object) != (bigint)0) {
+ if (GET_MARKBIT(object) != (bigint)0) {
newAdress = REMOVE_MARKBIT((bigint)((object)->vft));
newAdress = REMOVE_MARKBIT((bigint)((object)->vft));
newAdress = (val_t)evacuationPointer;
if (OBJ_IS_ARRAY(object)) {
newAdress = (val_t)evacuationPointer;
if (OBJ_IS_ARRAY(object)) {
- array = (Nit_NativeArray ) object;
+ array = (Nit_NativeArray)object;
size = sizeof(struct Nit_NativeArray) + ((array->size - 1) * sizeof(val_t));
size = sizeof(struct Nit_NativeArray) + ((array->size - 1) * sizeof(val_t));
- memcpy((void *)evacuationPointer, (array), size);
+ memcpy(evacuationPointer, (array), size);
(array)->vft = (classtable_elt_t*)evacuationPointer;
(array)->vft = (classtable_elt_t*)evacuationPointer;
- }else if(IS_BOX(object)){
+ } else if (IS_BOX(object)) {
box = (BOX_struct)object;
size = sizeof(struct TBOX_struct);
box = (BOX_struct)object;
size = sizeof(struct TBOX_struct);
- memcpy((void *)evacuationPointer, object, size);
+ memcpy(evacuationPointer, object, size);
box->vft = (classtable_elt_t*)evacuationPointer;
box->vft = (classtable_elt_t*)evacuationPointer;
objectSize = (bigint)((object)[0].vft[1].i);
size = (objectSize) * sizeof(val_t);
objectSize = (bigint)((object)[0].vft[1].i);
size = (objectSize) * sizeof(val_t);
- memcpy((void *)evacuationPointer, object, size);
+ memcpy(evacuationPointer, object, size);
(object)[0].vft = (classtable_elt_t*)evacuationPointer;
}
SET_MARKBIT(object);
(object)[0].vft = (classtable_elt_t*)evacuationPointer;
}
SET_MARKBIT(object);
+void GC_scavenging(void) {
obj_t object = (obj_t)scavengingPointer;
obj_t object = (obj_t)scavengingPointer;
- obj_t * object2 = (obj_t *)&scavengingPointer;
int size;
int i;
obj_t referencedObject;
bigint objectSize;
int size;
int i;
obj_t referencedObject;
bigint objectSize;
- Nit_NativeArray * array;
+ Nit_NativeArray *array;
size = sizeof(struct TBOX_struct);
size = sizeof(struct TBOX_struct);
array = (Nit_NativeArray*)&scavengingPointer;
if (OBJ_IS_ARRAY((obj_t)*array)) {
size = sizeof(struct Nit_NativeArray) + (((*array)->size - 1) * sizeof(val_t));
array = (Nit_NativeArray*)&scavengingPointer;
if (OBJ_IS_ARRAY((obj_t)*array)) {
size = sizeof(struct Nit_NativeArray) + (((*array)->size - 1) * sizeof(val_t));
(*array)->val[i] = (bigint)GC_evacuation(referencedObject);
}
}
(*array)->val[i] = (bigint)GC_evacuation(referencedObject);
}
}
objectSize = (bigint)((object)->vft[1].i);
size = (objectSize) * sizeof(val_t);
for (i = 2; i < objectSize; i++) {
objectSize = (bigint)((object)->vft[1].i);
size = (objectSize) * sizeof(val_t);
for (i = 2; i < objectSize; i++) {
scavengingPointer += size;
}
scavengingPointer += size;
}
-void GC_collect(){
- val_t ** pointers;
- val_t * pointer;
+void GC_collect(void) {
+ val_t **pointers;
+ val_t *pointer;
- struct trace_t * frame = tracehead;
- GC_static_object * staticObject = staticObjects.top;
+ struct trace_t *frame = tracehead;
+ GC_static_object *staticObject = staticObjects.top;
evacuationPointer = heapInactive->heapPointer;
scavengingPointer = heapInactive->heapPointer;
for (i = 0; i < staticObjects.size; i++) {
object = *(val_t*)(staticObject->pointer);
evacuationPointer = heapInactive->heapPointer;
scavengingPointer = heapInactive->heapPointer;
for (i = 0; i < staticObjects.size; i++) {
object = *(val_t*)(staticObject->pointer);
- if(!ISNULL(object) && ISOBJ(object)){
+ if (!ISNULL(object) && ISOBJ(object)) {
*(staticObject->pointer) = (val_t)GC_evacuation((obj_t)object);
}
staticObject = staticObject->next;
*(staticObject->pointer) = (val_t)GC_evacuation((obj_t)object);
}
staticObject = staticObject->next;
pointers = frame->REG_pointer;
for (j = 0; j < frame->REG_size; j++) {
object = (val_t)(pointers[j]);
pointers = frame->REG_pointer;
for (j = 0; j < frame->REG_size; j++) {
object = (val_t)(pointers[j]);
- if(!ISNULL(object) && ISOBJ(object)){
+ if (!ISNULL(object) && ISOBJ(object)) {
pointers[j] = (val_t*)GC_evacuation((obj_t)object);
}
}
if (frame == frame->prev) break;
frame = frame->prev;
}
pointers[j] = (val_t*)GC_evacuation((obj_t)object);
}
}
if (frame == frame->prev) break;
frame = frame->prev;
}
- while ( evacuationPointer != scavengingPointer) {
+ while (evacuationPointer != scavengingPointer) {
GC_scavenging();
}
/* pour tests seulement, pas necessaire */
GC_scavenging();
}
/* pour tests seulement, pas necessaire */
- memset((void *)heapActive->heapPointer, 0, heapActive->size);
+ memset(heapActive->heapPointer, 0, heapActive->size);
allocationPointer = evacuationPointer;
/* inverse le tas actif et le tas inactif */
allocationPointer = evacuationPointer;
/* inverse le tas actif et le tas inactif */
heapActive = heapInactive;
heapInactive = tempPointer;
heapActive = heapInactive;
heapInactive = tempPointer;
- heapActiveUsedSize = (int)allocationPointer - (int)heapActive->heapPointer;
+ heapActiveUsedSize = allocationPointer - heapActive->heapPointer;
-void GC_set_heap_size(size_t newHeapSize){
+void GC_set_heap_size(size_t newHeapSize) {
free(heapInactive->heapPointer);
heapInactive->heapPointer = malloc(newHeapSize);
free(heapInactive->heapPointer);
heapInactive->heapPointer = malloc(newHeapSize);
- if(heapInactive->heapPointer == NULL) {
+ if (heapInactive->heapPointer == NULL) {
exit(1);
}
heapInactive->size = newHeapSize;
memset(heapInactive->heapPointer, 0, newHeapSize);
}
exit(1);
}
heapInactive->size = newHeapSize;
memset(heapInactive->heapPointer, 0, newHeapSize);
}
-void GC_detect_memory_needs( size_t size ) {
- if ( size > (heapActive->size - heapActiveUsedSize)) {
+void GC_detect_memory_needs(size_t size) {
+ if (size > (heapActive->size - heapActiveUsedSize)) {
- if (heapActive->size - heapActiveUsedSize > heapActive->size / 2 && heapActive->size * 3 / 4 > HEAP_ACTIVE_SIZE_MIN){
+ if (heapActive->size - heapActiveUsedSize > heapActive->size / 2 && heapActive->size * 3 / 4 > HEAP_ACTIVE_SIZE_MIN) {
GC_set_heap_size(heapActive->size * 3 / 4);
GC_collect();
GC_set_heap_size(heapActive->size);
}
}
GC_set_heap_size(heapActive->size * 3 / 4);
GC_collect();
GC_set_heap_size(heapActive->size);
}
}
- if ( size > (heapActive->size - heapActiveUsedSize)) {
+ if (size > (heapActive->size - heapActiveUsedSize)) {
int try_size = heapInactive->size * 2;
int try_size = heapInactive->size * 2;
- while (size > (try_size - heapActiveUsedSize)){
+ while (size > (try_size - heapActiveUsedSize)) {
try_size *= 2;
}
GC_set_heap_size(try_size);
try_size *= 2;
}
GC_set_heap_size(try_size);
-void *Nit_gc_malloc( size_t size ) {
+void *Nit_gc_malloc(size_t size) {
char *result;
GC_detect_memory_needs(size);
char *result;
GC_detect_memory_needs(size);
-void GC_add_static_object(val_t * pointer){
+void GC_add_static_object(val_t *pointer) {
GC_List_Push(&staticObjects, pointer);
}
GC_List_Push(&staticObjects, pointer);
}
#include "gc_static_objects_list.h"
/* Markbit manipulations */
#include "gc_static_objects_list.h"
/* Markbit manipulations */
-#define GET_MARKBIT(x) (((val_t)(x[0].vft)) & 1)
+#define GET_MARKBIT(x) (((val_t)((x)[0].vft)) & 1)
#define SET_MARKBIT(x) ((x)->vft = (void*)(((bigint)((x)->vft)) | 1))
#define SET_MARKBIT(x) ((x)->vft = (void*)(((bigint)((x)->vft)) | 1))
-#define REMOVE_MARKBIT(x) (x ^ 1)
+#define REMOVE_MARKBIT(x) ((x) ^ 1)
#define HEAP_ACTIVE_SIZE_MIN 3000
typedef struct heap {
char *heapPointer;
unsigned long size;
#define HEAP_ACTIVE_SIZE_MIN 3000
typedef struct heap {
char *heapPointer;
unsigned long size;
typedef struct TBOX_struct {
typedef struct TBOX_struct {
- const classtable_elt_t * vft;
- char * val;
+ const classtable_elt_t *vft;
+ char *val;
-heap* heapActive;
-heap* heapInactive;
+heap *heapActive;
+heap *heapInactive;
char *allocationPointer;
char *evacuationPointer;
char *scavengingPointer;
char *allocationPointer;
char *evacuationPointer;
char *scavengingPointer;
val_t GC_evacuation(obj_t object);
val_t GC_evacuation(obj_t object);
+void GC_scavenging(void);
-void* Nit_gc_malloc( size_t size );
+void* Nit_gc_malloc(size_t size);
-void GC_add_static_object(val_t * pointer);
+void GC_add_static_object(val_t *pointer);
#include "gc_static_objects_list.h"
#include "gc_static_objects_list.h"
-void GC_List_Init (GC_List * list){
+void GC_List_Init(GC_List *list) {
list->top = NULL;
list->size = 0;
}
list->top = NULL;
list->size = 0;
}
-int GC_List_Push (GC_List * list, val_t *pointer){
+int GC_List_Push(GC_List *list, val_t *pointer) {
GC_static_object *newElement;
GC_static_object *newElement;
- if ((newElement = (GC_static_object *) malloc (sizeof (GC_static_object))) == NULL)
+ newElement = (GC_static_object*)malloc(sizeof(GC_static_object));
+ if (newElement == NULL)
return -1;
newElement->pointer = pointer;
return -1;
newElement->pointer = pointer;
#include "nit_common.h"
typedef struct GC_static_object {
#include "nit_common.h"
typedef struct GC_static_object {
struct GC_static_object *next;
struct GC_static_object *next;
+typedef struct GC_List {
GC_static_object *top;
int size;
GC_static_object *top;
int size;
-void GC_List_Init (GC_List *list);
-int GC_List_Push (GC_List *list, val_t *pointer);
+void GC_List_Init(GC_List *list);
+int GC_List_Push(GC_List *list, val_t *pointer);