diff --git a/gc_boehm.c b/gc_boehm.c index 0ff55ad..e0b0a11 100644 --- a/gc_boehm.c +++ b/gc_boehm.c @@ -429,6 +429,7 @@ static id allocate_class(Class cls, size_t extra) return obj; } +static void free_object(id obj) {} id objc_allocate_object(Class cls, int extra) { return class_createInstance(cls, extra); @@ -714,6 +715,7 @@ static void debug_free(void *ptr) PRIVATE struct gc_ops gc_ops_boehm = { .allocate_class = allocate_class, + .free_object = free_object, .malloc = debug_malloc, .free = debug_free, }; diff --git a/gc_none.c b/gc_none.c index c9c8796..ab6fd37 100644 --- a/gc_none.c +++ b/gc_none.c @@ -11,6 +11,11 @@ static id allocate_class(Class cls, size_t extraBytes) return (id)(addr + 1); } +static void free_object(id obj) +{ + free((void*)(((intptr_t)obj) - 1)); +} + static void *alloc(size_t size) { return calloc(size, 1); @@ -19,6 +24,7 @@ static void *alloc(size_t size) PRIVATE struct gc_ops gc_ops_none = { .allocate_class = allocate_class, + .free_object = free_object, .malloc = alloc, .free = free }; diff --git a/gc_ops.h b/gc_ops.h index 45ce0f0..5bd35a3 100644 --- a/gc_ops.h +++ b/gc_ops.h @@ -12,6 +12,10 @@ struct gc_ops * Allocates enough space for a class, followed by some extra bytes. */ id (*allocate_class)(Class, size_t); + /** + * Frees an object. + */ + void (*free_object)(id); /** * Allocates some memory that can be used to store pointers. This must be * used instead of malloc() for internal data structures that will store diff --git a/runtime.c b/runtime.c index 076361f..0001b39 100644 --- a/runtime.c +++ b/runtime.c @@ -358,8 +358,11 @@ id object_copy(id obj, size_t size) id object_dispose(id obj) { - call_cxx_destruct(obj); - gc->free(obj); + if (isGCEnabled) + { + call_cxx_destruct(obj); + gc->free_object(obj); + } return nil; }