gc: New debug flag to log oject creation/destruction

This commit is contained in:
Laria 2022-08-12 14:50:28 +02:00
parent 42fe4f6b33
commit 0474a85c6f

View file

@ -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