From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id 0CObFmw7oGD85wAAgWs5BA (envelope-from ) for ; Sat, 15 May 2021 23:21:48 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id iA56Emw7oGAZDAAA1q6Kng (envelope-from ) for ; Sat, 15 May 2021 21:21:48 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id AD1D9B700 for ; Sat, 15 May 2021 23:21:47 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 6033F2906F; Sat, 15 May 2021 17:21:24 -0400 (EDT) Received: from mail-ot1-x32c.google.com (mail-ot1-x32c.google.com [IPv6:2607:f8b0:4864:20::32c]) by mail.notmuchmail.org (Postfix) with ESMTPS id 1D40529050 for ; Sat, 15 May 2021 17:21:17 -0400 (EDT) Received: by mail-ot1-x32c.google.com with SMTP id u25-20020a0568302319b02902ac3d54c25eso2382227ote.1 for ; Sat, 15 May 2021 14:21:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rl8Ocm1E0PbKGX7e3CZ2uAWZ4v0ZvuMS2BRkUlAE0ns=; b=Yk5Y570nox1b8jkgQ2t8o+C/lEiBe6yWQADxPJHWI3RzqxNc7//WoyHqepyS9PPOk4 Z2wq9URgRC0BENzIMeM21X6O1JdtwUkQNrE9E/s5FxYXV6VcWC0izcsm/8PppHxQf1KC RRoo6f6i8L3nT+i5TNH8qEAAW8gscqNIvs7LFi2lAy5Y/txThK5kE37YEgeYnFaVReJU KwFwtF69tvODkNvRP3oKWP/tG75KPqkuvEqEU9LqyY+wV6BVPt7tDIaQLlnAoSmO5Ncq eAbMjU9LFIJzbax13QbNxPpxSLYPj7+LfHz5B9RkbIMenEd44BjXUtHteb1g1E8ZiSzE CaDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rl8Ocm1E0PbKGX7e3CZ2uAWZ4v0ZvuMS2BRkUlAE0ns=; b=AJVHoc8HOGaLq071ys/OAa01iV1BgSk+zn9oAUdYY5uBpzrJ0RGYFRXy3i2bT3ze2m FiPXSmxkoJwdFJLj7G8Xb2mxi5P000+QdcslmKnNW40FJ/8k058KciHX+UO6psh82YEq 7N8fptq/KNu28F0z/pXXPvoOEboyfWUZkDzaJwmPEybVnh9PSM9I9nemMPk1EJ1uHfKB 3vssXaehsRdfnH7p1eSZy85ynsQmT4HhhzI7y/FvxTAtjgoPsFchN1HMxx/GeZcPmKXt x38HZG62qKU+553l+ZcA70NuLcWdtBa3GV2qzNm+SW6XziheGbNjxHkkR52hJyNU69+m DKTA== X-Gm-Message-State: AOAM5333zfek7n8O8orYW0JzWOg+Ntz9P6XWIN8khn9jrrbDNaAVDOI/ 4VrbrxreEyHJ4wIIb41w+teCK+L1g5azuA== X-Google-Smtp-Source: ABdhPJy6rXPmmKkq3C/wsabwAJOzJM+pF3JUyzybKfQd8zfvU765+MTQMeJACRz2dHt14x77lSCI+Q== X-Received: by 2002:a9d:68d8:: with SMTP id i24mr43031267oto.347.1621113676254; Sat, 15 May 2021 14:21:16 -0700 (PDT) Received: from localhost (fixed-187-189-165-231.totalplay.net. [187.189.165.231]) by smtp.gmail.com with ESMTPSA id 35sm2199811oth.49.2021.05.15.14.21.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 May 2021 14:21:15 -0700 (PDT) From: Felipe Contreras To: notmuch@notmuchmail.org Subject: [PATCH v3 7/7] ruby: new notmuch_rb_object_destroy() helper Date: Sat, 15 May 2021 16:21:03 -0500 Message-Id: <20210515212103.1001295-8-felipe.contreras@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210515212103.1001295-1-felipe.contreras@gmail.com> References: <20210515212103.1001295-1-felipe.contreras@gmail.com> MIME-Version: 1.0 Message-ID-Hash: DVW5JSYMY2O2MRGIRB7LAO5ZMBETAPDG X-Message-ID-Hash: DVW5JSYMY2O2MRGIRB7LAO5ZMBETAPDG X-MailFrom: felipe.contreras@gmail.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header CC: Ali Polatel , Austin Clements , Tomi Ollila , Ludovic LANGE , Daniel Kahn Gillmor X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1621113707; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post:dkim-signature; bh=x+ZUJt+zAY8xJak3kF6ROpsS4S+fTxmQdb0y7Xc8Of0=; b=PtieIgm2MQ445b9yS2cnPxfc0KnT4NZlL1lE3jQdVPZTyPyufwUIuCL5GXEyJ2dBtNqfeG X5XY4DOpqVgq4L2ueY6JibgRJsGE5/hgGQiVlAl1lzmOgNgl0sqIXvagOE1/K3YdgepfT/ IoVVkLxbhZcbRi3DjGCe9llDnDM9aoQka2TgdvGfHPCjn5OiVEQOQPBD88zQmnbpz47re8 cJV23CqovQdVY65CqSt2/afvA39s8TyqEM0cFGLFEMW+h+zJD7OUfPoZleAm/fIzmCpYNM C+HZKy9EJEbAWgCSI3VXwuPuKYOrqTCUbg0IigBqdw1ptP/6I/Z1ks2NFlRy/A== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1621113707; a=rsa-sha256; cv=none; b=Dzk1may5k0Dd7JP3VNovQrbSe/Y2DbS7rOWGd73MVGLYRmFa91Umj/ydOZW7syXTCAf8jA KFBfSLRjxtjOeQVMKLzPEckkB9Pw7ihtIqyl+IVMizrykTIBP5ULJMhh/NTcpZqBaUKPeo lODdHfU8cYalLbFYME6mUViI6Rsir9Zf/zAJ0MtoMBbV28UwzoI6Drcl0TnKH6j6rIuu+w jCGtwHPn6jQepNIB9HGMz+VsC27LqFkkFVh6+z4ktQUNXv6u+byCu5wUdrFC8S1T3K3VZX AzBkfHYXWUpJ4H7iK4dWxLrFadvMfChj+FX8JFB4JP8PWKLp87LzwvFv7+OhfA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=Yk5Y570n; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -0.94 Authentication-Results: aspmx1.migadu.com; dkim=fail ("body hash did not verify") header.d=gmail.com header.s=20161025 header.b=Yk5Y570n; dmarc=fail reason="SPF not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: AD1D9B700 X-Spam-Score: -0.94 X-Migadu-Scanner: scn0.migadu.com X-TUID: LRY6B+81s846 The struct used to store the types (rb_data_type_t) contains a "data" field where we can store whatever we want. I use that field to store a pointer to the corresponding destroy function. For example notmuch_rb_database_type contains a pointer to notmuch_database_destroy. I cast that pointer as a notmuch_status_t (func*)(void *) and call that function passing the internal object (e.g. notmuch_database_t). Using the rb_data_type_t data we can call the correct notmuch destroy function. Therefore this: ret = ((notmuch_status_t (*)(void *)) type->data) (nm_object); Is effectively the same as this: ret = notmuch_database_destroy (database); The advantage of doing it this way is that much less code is necesary since each rb_data_type_t has the corresponding destroy function stored in it. Signed-off-by: Felipe Contreras --- bindings/ruby/database.c | 6 +----- bindings/ruby/defs.h | 15 +++++++++++++++ bindings/ruby/directory.c | 7 +------ bindings/ruby/filenames.c | 7 +------ bindings/ruby/init.c | 1 + bindings/ruby/message.c | 7 +------ bindings/ruby/messages.c | 7 +------ bindings/ruby/query.c | 7 +------ bindings/ruby/tags.c | 7 +------ bindings/ruby/thread.c | 7 +------ bindings/ruby/threads.c | 7 +------ 11 files changed, 25 insertions(+), 53 deletions(-) diff --git a/bindings/ruby/database.c b/bindings/ruby/database.c index 4ecc8f78..bb993d86 100644 --- a/bindings/ruby/database.c +++ b/bindings/ruby/database.c @@ -114,11 +114,7 @@ VALUE notmuch_rb_database_close (VALUE self) { notmuch_status_t ret; - notmuch_database_t *db; - - Data_Get_Notmuch_Database (self, db); - ret = notmuch_database_destroy (db); - DATA_PTR (self) = NULL; + ret = notmuch_rb_object_destroy (self, ¬much_rb_database_type); notmuch_rb_status_raise (ret); return Qnil; diff --git a/bindings/ruby/defs.h b/bindings/ruby/defs.h index fa7b9515..9860ee17 100644 --- a/bindings/ruby/defs.h +++ b/bindings/ruby/defs.h @@ -105,6 +105,21 @@ extern const rb_data_type_t notmuch_rb_tags_type; #define Data_Get_Notmuch_Tags(obj, ptr) \ Data_Get_Notmuch_Object ((obj), ¬much_rb_tags_type, (ptr)) +static inline notmuch_status_t +notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type) +{ + void *nm_object; + notmuch_status_t ret; + + Data_Get_Notmuch_Object (rb_object, type, nm_object); + + /* Call the corresponding notmuch_*_destroy function */ + ret = ((notmuch_status_t (*)(void *)) type->data) (nm_object); + DATA_PTR (rb_object) = NULL; + + return ret; +} + /* status.c */ void notmuch_rb_status_raise (notmuch_status_t status); diff --git a/bindings/ruby/directory.c b/bindings/ruby/directory.c index 17d60d1d..910f0a99 100644 --- a/bindings/ruby/directory.c +++ b/bindings/ruby/directory.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_directory_destroy (VALUE self) { - notmuch_directory_t *dir; - - Data_Get_Notmuch_Directory (self, dir); - - notmuch_directory_destroy (dir); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_directory_type); return Qnil; } diff --git a/bindings/ruby/filenames.c b/bindings/ruby/filenames.c index 656c58e6..0dec1952 100644 --- a/bindings/ruby/filenames.c +++ b/bindings/ruby/filenames.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_filenames_destroy (VALUE self) { - notmuch_filenames_t *fnames; - - Data_Get_Notmuch_FileNames (self, fnames); - - notmuch_filenames_destroy (fnames); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_filenames_type); return Qnil; } diff --git a/bindings/ruby/init.c b/bindings/ruby/init.c index a9f863eb..62515eca 100644 --- a/bindings/ruby/init.c +++ b/bindings/ruby/init.c @@ -54,6 +54,7 @@ const rb_data_type_t notmuch_rb_object_type = { const rb_data_type_t notmuch_rb_ ## id ## _type = { \ .wrap_struct_name = "notmuch_" #id, \ .parent = ¬much_rb_object_type, \ + .data = ¬much_ ## id ## _destroy, \ } define_type (database); diff --git a/bindings/ruby/message.c b/bindings/ruby/message.c index b3aed604..f45c95cc 100644 --- a/bindings/ruby/message.c +++ b/bindings/ruby/message.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_message_destroy (VALUE self) { - notmuch_message_t *message; - - Data_Get_Notmuch_Message (self, message); - - notmuch_message_destroy (message); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_message_type); return Qnil; } diff --git a/bindings/ruby/messages.c b/bindings/ruby/messages.c index e04f3af1..ca5b10d0 100644 --- a/bindings/ruby/messages.c +++ b/bindings/ruby/messages.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_messages_destroy (VALUE self) { - notmuch_messages_t *messages; - - Data_Get_Notmuch_Messages (self, messages); - - notmuch_messages_destroy (messages); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_messages_type); return Qnil; } diff --git a/bindings/ruby/query.c b/bindings/ruby/query.c index 79727d6a..3ec98c6c 100644 --- a/bindings/ruby/query.c +++ b/bindings/ruby/query.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_query_destroy (VALUE self) { - notmuch_query_t *query; - - Data_Get_Notmuch_Query (self, query); - - notmuch_query_destroy (query); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_query_type); return Qnil; } diff --git a/bindings/ruby/tags.c b/bindings/ruby/tags.c index db8b4cfc..2af85e36 100644 --- a/bindings/ruby/tags.c +++ b/bindings/ruby/tags.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_tags_destroy (VALUE self) { - notmuch_tags_t *tags; - - Data_Get_Notmuch_Tags (self, tags); - - notmuch_tags_destroy (tags); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_tags_type); return Qnil; } diff --git a/bindings/ruby/thread.c b/bindings/ruby/thread.c index f6bf7849..7cb2a3dc 100644 --- a/bindings/ruby/thread.c +++ b/bindings/ruby/thread.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_thread_destroy (VALUE self) { - notmuch_thread_t *thread; - - Data_Get_Notmuch_Thread (self, thread); - - notmuch_thread_destroy (thread); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_thread_type); return Qnil; } diff --git a/bindings/ruby/threads.c b/bindings/ruby/threads.c index d809b571..50280260 100644 --- a/bindings/ruby/threads.c +++ b/bindings/ruby/threads.c @@ -28,12 +28,7 @@ VALUE notmuch_rb_threads_destroy (VALUE self) { - notmuch_threads_t *threads; - - Data_Get_Notmuch_Threads (self, threads); - - notmuch_threads_destroy (threads); - DATA_PTR (self) = NULL; + notmuch_rb_object_destroy (self, ¬much_rb_threads_type); return Qnil; } -- 2.31.1