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>
ret = notmuch_database_open (path, mode, &database);
notmuch_rb_status_raise (ret);
ret = notmuch_database_open (path, mode, &database);
notmuch_rb_status_raise (ret);
- DATA_PTR (self) = database;
+ DATA_PTR (self) = notmuch_rb_object_create (database);
extern const rb_data_type_t notmuch_rb_message_type;
extern const rb_data_type_t notmuch_rb_tags_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))) { \
do { \
(ptr) = rb_check_typeddata ((obj), (type)); \
if (RB_UNLIKELY (!(ptr))) { \
+#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) \
#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), ¬much_rb_database_type, (ptr))
#define Data_Get_Notmuch_Database(obj, ptr) \
Data_Get_Notmuch_Object ((obj), ¬much_rb_database_type, (ptr))
#define Data_Get_Notmuch_Tags(obj, ptr) \
Data_Get_Notmuch_Object ((obj), ¬much_rb_tags_type, (ptr))
#define Data_Get_Notmuch_Tags(obj, ptr) \
Data_Get_Notmuch_Object ((obj), ¬much_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)
{
static inline notmuch_status_t
notmuch_rb_object_destroy (VALUE rb_object, const rb_data_type_t *type)
{
+ notmuch_rb_object_t *rb_wrapper;
- 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 */
/* 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;
DATA_PTR (rb_object) = NULL;
return ret;
const rb_data_type_t notmuch_rb_object_type = {
.wrap_struct_name = "notmuch_object",
const rb_data_type_t notmuch_rb_object_type = {
.wrap_struct_name = "notmuch_object",
+ .function = {
+ .dfree = notmuch_rb_object_free,
+ },
};
#define define_type(id) \
};
#define define_type(id) \
.wrap_struct_name = "notmuch_" #id, \
.parent = ¬much_rb_object_type, \
.data = ¬much_ ## id ## _destroy, \
.wrap_struct_name = "notmuch_" #id, \
.parent = ¬much_rb_object_type, \
.data = ¬much_ ## id ## _destroy, \
+ .function = { \
+ .dfree = notmuch_rb_object_free, \
+ }, \
}
define_type (database);
}
define_type (database);