unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: Felipe Contreras <felipe.contreras@gmail.com>
To: notmuch@notmuchmail.org
Cc: Tomi Ollila <tomi.ollila@iki.fi>
Subject: [PATCH 1/2] ruby: create an actual wrapper struct
Date: Mon, 17 May 2021 14:39:14 -0500	[thread overview]
Message-ID: <20210517193915.1220114-2-felipe.contreras@gmail.com> (raw)
In-Reply-To: <20210517193915.1220114-1-felipe.contreras@gmail.com>

Currently Ruby data points directly to a notmuch object (e.g.
notmuch_database_t), since we don't need any extra data that is fine.

However, in the next commit we will need extra data, therefore we create
a new struct notmuch_rb_object_t wrapper which contains nothing but a
pointer to the current pointer (e.g. notmuch_database_t).

This struct is tied to the Ruby object, and is freed when the Ruby
object is freed by the garbage collector.

We do nothing with this wrapper, so no functionality should be changed.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
---
 bindings/ruby/database.c |  2 +-
 bindings/ruby/defs.h     | 39 ++++++++++++++++++++++++++++++++++-----
 bindings/ruby/init.c     |  6 ++++++
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c
index bb993d86..66100de2 100644
--- a/bindings/ruby/database.c
+++ b/bindings/ruby/database.c
@@ -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) = database;
+    DATA_PTR (self) = notmuch_rb_object_create (database);
 
     return self;
 }
diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h
index 9860ee17..1413eb72 100644
--- a/bindings/ruby/defs.h
+++ b/bindings/ruby/defs.h
@@ -66,7 +66,7 @@ extern const rb_data_type_t notmuch_rb_messages_type;
 extern const rb_data_type_t notmuch_rb_message_type;
 extern const rb_data_type_t notmuch_rb_tags_type;
 
-#define Data_Get_Notmuch_Object(obj, type, ptr)					    \
+#define Data_Get_Notmuch_Rb_Object(obj, type, ptr)		    		    \
     do {									    \
 	(ptr) = rb_check_typeddata ((obj), (type));				    \
 	if (RB_UNLIKELY (!(ptr))) {						    \
@@ -75,8 +75,15 @@ extern const rb_data_type_t notmuch_rb_tags_type;
 	}									    \
     } while (0)
 
+#define Data_Get_Notmuch_Object(obj, type, ptr)			\
+    do {							\
+	notmuch_rb_object_t *rb_wrapper;			\
+	Data_Get_Notmuch_Rb_Object ((obj), (type), rb_wrapper);	\
+	(ptr) = rb_wrapper->nm_object;				\
+    } while (0)
+
 #define Data_Wrap_Notmuch_Object(klass, type, ptr) \
-    TypedData_Wrap_Struct ((klass), (type), (ptr))
+    TypedData_Wrap_Struct ((klass), (type), notmuch_rb_object_create ((ptr)))
 
 #define Data_Get_Notmuch_Database(obj, ptr) \
     Data_Get_Notmuch_Object ((obj), &notmuch_rb_database_type, (ptr))
@@ -105,16 +112,38 @@ extern const rb_data_type_t notmuch_rb_tags_type;
 #define Data_Get_Notmuch_Tags(obj, ptr) \
     Data_Get_Notmuch_Object ((obj), &notmuch_rb_tags_type, (ptr))
 
+typedef struct {
+    void *nm_object;
+} notmuch_rb_object_t;
+
+static inline void *
+notmuch_rb_object_create (void *nm_object)
+{
+    notmuch_rb_object_t *rb_wrapper = malloc (sizeof (*rb_wrapper));
+    if (RB_UNLIKELY (!rb_wrapper))
+	return NULL;
+
+    rb_wrapper->nm_object = nm_object;
+    return rb_wrapper;
+}
+
+static inline void
+notmuch_rb_object_free (void *rb_wrapper)
+{
+    free (rb_wrapper);
+}
+
 static inline notmuch_status_t
 notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
 {
-    void *nm_object;
+    notmuch_rb_object_t *rb_wrapper;
     notmuch_status_t ret;
 
-    Data_Get_Notmuch_Object (rb_object, type, nm_object);
+    Data_Get_Notmuch_Rb_Object (rb_object, type, rb_wrapper);
 
     /* Call the corresponding notmuch_*_destroy function */
-    ret = ((notmuch_status_t (*)(void *)) type->data) (nm_object);
+    ret = ((notmuch_status_t (*)(void *)) type->data) (rb_wrapper->nm_object);
+    notmuch_rb_object_free (rb_wrapper);
     DATA_PTR (rb_object) = NULL;
 
     return ret;
diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c
index 62515eca..831f7695 100644
--- a/bindings/ruby/init.c
+++ b/bindings/ruby/init.c
@@ -48,6 +48,9 @@ ID ID_db_mode;
 
 const rb_data_type_t notmuch_rb_object_type = {
     .wrap_struct_name = "notmuch_object",
+    .function = {
+	.dfree = notmuch_rb_object_free,
+    },
 };
 
 #define define_type(id) \
@@ -55,6 +58,9 @@ const rb_data_type_t notmuch_rb_object_type = {
 	.wrap_struct_name = "notmuch_" #id, \
 	.parent = &notmuch_rb_object_type, \
 	.data = &notmuch_ ## id ## _destroy, \
+	.function = { \
+	    .dfree = notmuch_rb_object_free, \
+	}, \
     }
 
 define_type (database);
-- 
2.31.1

  reply	other threads:[~2021-05-17 19:39 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-17 19:39 [PATCH 0/2] ruby: enable garbage collection Felipe Contreras
2021-05-17 19:39 ` Felipe Contreras [this message]
2021-07-18 22:58   ` [PATCH 1/2] ruby: create an actual wrapper struct David Bremner
2021-05-17 19:39 ` [PATCH 2/2] ruby: enable garbage collection using talloc Felipe Contreras
2021-06-11  0:46   ` David Bremner
2021-06-11  8:54     ` David Bremner
2021-06-26 19:07       ` Felipe Contreras
2021-06-26 19:54         ` David Bremner
2021-06-26 20:05           ` Felipe Contreras
2021-06-26 18:56     ` Felipe Contreras
2021-06-26 19:56       ` David Bremner
2021-05-22 10:49 ` [PATCH 0/2] ruby: enable garbage collection David Bremner
2021-05-28  3:10   ` Felipe Contreras
2021-06-26 20:07 ` [PATCH] perf-test: add ruby test Felipe Contreras
2021-07-18 22:54   ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210517193915.1220114-2-felipe.contreras@gmail.com \
    --to=felipe.contreras@gmail.com \
    --cc=notmuch@notmuchmail.org \
    --cc=tomi.ollila@iki.fi \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).