]> git.cworth.org Git - notmuch/commitdiff
ruby: enable garbage collection using talloc
authorFelipe Contreras <felipe.contreras@gmail.com>
Mon, 17 May 2021 19:39:15 +0000 (14:39 -0500)
committerDavid Bremner <david@tethera.net>
Sun, 18 Jul 2021 20:08:53 +0000 (17:08 -0300)
We basically steal all the objects from their notmuch parents, therefore
they are completely under Ruby's gc control.

The order at which these objects are freed does not matter any more,
because destroying the database does not destroy all the children
objects, since they belong to Ruby now.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
bindings/ruby/database.c
bindings/ruby/defs.h
bindings/ruby/extconf.rb

index 0159aaac57ca0413132352d6afe42b4bc485cab6..1cf4e4e27c170e45f2e332b3e2d5e61c93646a9d 100644 (file)
@@ -81,7 +81,7 @@ notmuch_rb_database_initialize (int argc, VALUE *argv, VALUE self)
        ret = notmuch_database_open (path, mode, &database);
     notmuch_rb_status_raise (ret);
 
-    DATA_PTR (self) = notmuch_rb_object_create (database);
+    DATA_PTR (self) = notmuch_rb_object_create (database, "notmuch_rb_database");
 
     return self;
 }
index 7ddb5df5ae0df3ef02f1cc52a67cf432374dc643..0f39f2d0880efdfff365a0322987b50550bdea67 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <notmuch.h>
 #include <ruby.h>
+#include <talloc.h>
 
 extern VALUE notmuch_rb_cDatabase;
 extern VALUE notmuch_rb_cDirectory;
@@ -83,7 +84,7 @@ extern const rb_data_type_t notmuch_rb_tags_type;
     } while (0)
 
 #define Data_Wrap_Notmuch_Object(klass, type, ptr) \
-    TypedData_Wrap_Struct ((klass), (type), notmuch_rb_object_create ((ptr)))
+    TypedData_Wrap_Struct ((klass), (type), notmuch_rb_object_create ((ptr), "notmuch_rb_object: " __location__))
 
 #define Data_Get_Notmuch_Database(obj, ptr) \
     Data_Get_Notmuch_Object ((obj), &notmuch_rb_database_type, (ptr))
@@ -117,20 +118,22 @@ typedef struct {
 } notmuch_rb_object_t;
 
 static inline void *
-notmuch_rb_object_create (void *nm_object)
+notmuch_rb_object_create (void *nm_object, const char *name)
 {
-    notmuch_rb_object_t *rb_wrapper = malloc (sizeof (*rb_wrapper));
+    notmuch_rb_object_t *rb_wrapper = talloc_named_const (NULL, sizeof (*rb_wrapper), name);
+
     if (RB_UNLIKELY (!rb_wrapper))
        return NULL;
 
     rb_wrapper->nm_object = nm_object;
+    talloc_steal (rb_wrapper, nm_object);
     return rb_wrapper;
 }
 
 static inline void
 notmuch_rb_object_free (void *rb_wrapper)
 {
-    free (rb_wrapper);
+    talloc_free (rb_wrapper);
 }
 
 static inline notmuch_status_t
index 161de5a2c7c52bb6d5130ee6db3616c36acdba1d..d914537cff1bbb502e8dd85c1f9e764eed2d1b55 100644 (file)
@@ -19,6 +19,7 @@ if not ENV['LIBNOTMUCH']
 end
 
 $LOCAL_LIBS += ENV['LIBNOTMUCH']
+$LIBS += " -ltalloc"
 
 # Create Makefile
 dir_config('notmuch')