From 0474a85c6fbf2e27b59b0e6909d6be919451fa87 Mon Sep 17 00:00:00 2001 From: Laria Carolin Chabowski Date: Fri, 12 Aug 2022 14:50:28 +0200 Subject: [PATCH] gc: New debug flag to log oject creation/destruction --- src/gc.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/gc.c b/src/gc.c index 67c1b15..67be04f 100644 --- a/src/gc.c +++ b/src/gc.c @@ -15,6 +15,7 @@ // #define GC_DEBUG_STATS 1 // #define GC_DEBUG_WIPE_RECLAIMED_OBJECTS 1 // #define GC_DEBUG_DUMP_GRAPH_ON_COLLECT 1 +// #define GC_DEBUG_LOG_NEW_AND_RECLAIM 1 struct gc_object { // Unlike most other tagged unions in apfl, the union is first here. @@ -159,12 +160,21 @@ new_object(struct gc *gc, enum gc_type type) return object; } -#define IMPL_NEW(t, name, type, field) \ - t * \ - name(struct gc *gc) \ - { \ - struct gc_object *object = new_object(gc, type); \ - return object == NULL ? NULL : &object->field; \ +static const char *type_to_string(enum gc_type); + +#ifdef GC_DEBUG_LOG_NEW_AND_RECLAIM +# define LOG_NEW_AND_RECLAIM(fmt, ...) fprintf(stderr, fmt, __VA_ARGS__) +#else +# define LOG_NEW_AND_RECLAIM(fmt, ...) +#endif + +#define IMPL_NEW(t, name, type, field) \ + t * \ + name(struct gc *gc) \ + { \ + struct gc_object *object = new_object(gc, type); \ + LOG_NEW_AND_RECLAIM("New %s object at %p\n", type_to_string(type), (void *)object); \ + return object == NULL ? NULL : &object->field; \ } IMPL_NEW(struct list_header, apfl_gc_new_list, GC_TYPE_LIST, list ) @@ -381,6 +391,7 @@ sweep(struct gc *gc) case GC_STATUS_FREE: break; case GC_STATUS_WHITE: + LOG_NEW_AND_RECLAIM("reclaiming %p of type %s\n", (void *)object, type_to_string(object->type)); deinit_object(gc, object); #ifdef GC_DEBUG_WIPE_RECLAIMED_OBJECTS