Blame SOURCES/0001-gi-Include-missing-glib-bits.patch

eed85c
From 9d7d64617c59e99bd2aced57009213c8a99e676e Mon Sep 17 00:00:00 2001
eed85c
From: =?UTF-8?q?Florian=20M=C3=BCllner?= <fmuellner@gnome.org>
eed85c
Date: Wed, 29 May 2019 19:44:55 +0200
eed85c
Subject: [PATCH] gi: Include missing glib bits
eed85c
eed85c
---
eed85c
 gi/garcbox.c       | 387 ++++++++++++++++++++++++++++++++++
eed85c
 gi/grcbox.c        | 506 +++++++++++++++++++++++++++++++++++++++++++++
eed85c
 gi/grcbox.h        |  96 +++++++++
eed85c
 gi/grcboxprivate.h |  78 +++++++
eed85c
 gi/grefcount.c     | 289 ++++++++++++++++++++++++++
eed85c
 gi/grefcount.h     | 134 ++++++++++++
eed85c
 gi/wrapperutils.h  |   2 +
eed85c
 gjs-srcs.mk        |   6 +
eed85c
 8 files changed, 1498 insertions(+)
eed85c
 create mode 100644 gi/garcbox.c
eed85c
 create mode 100644 gi/grcbox.c
eed85c
 create mode 100644 gi/grcbox.h
eed85c
 create mode 100644 gi/grcboxprivate.h
eed85c
 create mode 100644 gi/grefcount.c
eed85c
 create mode 100644 gi/grefcount.h
eed85c
eed85c
diff --git a/gi/garcbox.c b/gi/garcbox.c
eed85c
new file mode 100644
eed85c
index 00000000..bee75073
eed85c
--- /dev/null
eed85c
+++ b/gi/garcbox.c
eed85c
@@ -0,0 +1,387 @@
eed85c
+/* garcbox.c: Atomically reference counted data
eed85c
+ *
eed85c
+ * Copyright 2018  Emmanuele Bassi
eed85c
+ *
eed85c
+ * This library is free software; you can redistribute it and/or
eed85c
+ * modify it under the terms of the GNU Lesser General Public
eed85c
+ * License as published by the Free Software Foundation; either
eed85c
+ * version 2.1 of the License, or (at your option) any later version.
eed85c
+ *
eed85c
+ * This library is distributed in the hope that it will be useful,
eed85c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
eed85c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eed85c
+ * Lesser General Public License for more details.
eed85c
+ *
eed85c
+ * You should have received a copy of the GNU Lesser General Public
eed85c
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
eed85c
+ */
eed85c
+
eed85c
+#include "config.h"
eed85c
+
eed85c
+#include "grcboxprivate.h"
eed85c
+
eed85c
+#if 0
eed85c
+#include "gmessages.h"
eed85c
+#endif
eed85c
+#include "grefcount.h"
eed85c
+
eed85c
+#ifdef ENABLE_VALGRIND
eed85c
+#include "valgrind.h"
eed85c
+#endif
eed85c
+
eed85c
+#if 0
eed85c
+#include "glib_trace.h"
eed85c
+#else
eed85c
+#include "gjs_gi_trace.h"
eed85c
+#endif
eed85c
+
eed85c
+#include <string.h>
eed85c
+
eed85c
+#define G_ARC_BOX(p)            (GArcBox *) (((char *) (p)) - G_ARC_BOX_SIZE)
eed85c
+
eed85c
+/**
eed85c
+ * SECTION:arcbox
eed85c
+ * @Title: Atomically reference counted data
eed85c
+ * @Short_description: Allocated memory with atomic reference counting semantics
eed85c
+ *
eed85c
+ * An "atomically reference counted box", or "ArcBox", is an opaque wrapper
eed85c
+ * data type that is guaranteed to be as big as the size of a given data type,
eed85c
+ * and which augments the given data type with thread safe reference counting
eed85c
+ * semantics for its memory management.
eed85c
+ *
eed85c
+ * ArcBox is useful if you have a plain old data type, like a structure
eed85c
+ * typically placed on the stack, and you wish to provide additional API
eed85c
+ * to use it on the heap; or if you want to implement a new type to be
eed85c
+ * passed around by reference without necessarily implementing copy/free
eed85c
+ * semantics or your own reference counting.
eed85c
+ *
eed85c
+ * The typical use is:
eed85c
+ *
eed85c
+ * |[
eed85c
+ * typedef struct {
eed85c
+ *   char *name;
eed85c
+ *   char *address;
eed85c
+ *   char *city;
eed85c
+ *   char *state;
eed85c
+ *   int age;
eed85c
+ * } Person;
eed85c
+ *
eed85c
+ * Person *
eed85c
+ * person_new (void)
eed85c
+ * {
eed85c
+ *   return g_atomic_rc_box_new0 (Person);
eed85c
+ * }
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * Every time you wish to acquire a reference on the memory, you should
eed85c
+ * call g_atomic_rc_box_acquire(); similarly, when you wish to release a reference
eed85c
+ * you should call g_atomic_rc_box_release():
eed85c
+ *
eed85c
+ * |[
eed85c
+ * // Add a Person to the Database; the Database acquires ownership
eed85c
+ * // of the Person instance
eed85c
+ * void
eed85c
+ * add_person_to_database (Database *db, Person *p)
eed85c
+ * {
eed85c
+ *   db->persons = g_list_prepend (db->persons, g_atomic_rc_box_acquire (p));
eed85c
+ * }
eed85c
+ *
eed85c
+ * // Removes a Person from the Database; the reference acquired by
eed85c
+ * // add_person_to_database() is released here
eed85c
+ * void
eed85c
+ * remove_person_from_database (Database *db, Person *p)
eed85c
+ * {
eed85c
+ *   db->persons = g_list_remove (db->persons, p);
eed85c
+ *   g_atomic_rc_box_release (p);
eed85c
+ * }
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * If you have additional memory allocated inside the structure, you can
eed85c
+ * use g_atomic_rc_box_release_full(), which takes a function pointer, which
eed85c
+ * will be called if the reference released was the last:
eed85c
+ *
eed85c
+ * |[
eed85c
+ * void
eed85c
+ * person_clear (Person *p)
eed85c
+ * {
eed85c
+ *   g_free (p->name);
eed85c
+ *   g_free (p->address);
eed85c
+ *   g_free (p->city);
eed85c
+ *   g_free (p->state);
eed85c
+ * }
eed85c
+ *
eed85c
+ * void
eed85c
+ * remove_person_from_database (Database *db, Person *p)
eed85c
+ * {
eed85c
+ *   db->persons = g_list_remove (db->persons, p);
eed85c
+ *   g_atomic_rc_box_release_full (p, (GDestroyNotify) person_clear);
eed85c
+ * }
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * If you wish to transfer the ownership of a reference counted data
eed85c
+ * type without increasing the reference count, you can use g_steal_pointer():
eed85c
+ *
eed85c
+ * |[
eed85c
+ *   Person *p = g_atomic_rc_box_new (Person);
eed85c
+ *
eed85c
+ *   fill_person_details (p);
eed85c
+ *
eed85c
+ *   add_person_to_database (db, g_steal_pointer (&p);;
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * ## Thread safety
eed85c
+ *
eed85c
+ * The reference counting operations on data allocated using g_atomic_rc_box_alloc(),
eed85c
+ * g_atomic_rc_box_new(), and g_atomic_rc_box_dup() are guaranteed to be atomic, and thus
eed85c
+ * can be safely be performed by different threads. It is important to note that
eed85c
+ * only the reference acquisition and release are atomic; changes to the content
eed85c
+ * of the data are your responsibility.
eed85c
+ *
eed85c
+ * ## Automatic pointer clean up
eed85c
+ *
eed85c
+ * If you want to add g_autoptr() support to your plain old data type through
eed85c
+ * reference counting, you can use the G_DEFINE_AUTOPTR_CLEANUP_FUNC() and
eed85c
+ * g_atomic_rc_box_release():
eed85c
+ *
eed85c
+ * |[
eed85c
+ * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, g_atomic_rc_box_release)
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * If you need to clear the contents of the data, you will need to use an
eed85c
+ * ancillary function that calls g_rc_box_release_full():
eed85c
+ *
eed85c
+ * |[
eed85c
+ * static void
eed85c
+ * my_data_struct_release (MyDataStruct *data)
eed85c
+ * {
eed85c
+ *   // my_data_struct_clear() is defined elsewhere
eed85c
+ *   g_atomic_rc_box_release_full (data, (GDestroyNotify) my_data_struct_clear);
eed85c
+ * }
eed85c
+ *
eed85c
+ * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, my_data_struct_release)
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * Since: 2.58.
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_alloc:
eed85c
+ * @block_size: the size of the allocation, must be greater than 0
eed85c
+ *
eed85c
+ * Allocates @block_size bytes of memory, and adds atomic
eed85c
+ * reference counting semantics to it.
eed85c
+ *
eed85c
+ * The data will be freed when its reference count drops to
eed85c
+ * zero.
eed85c
+ *
eed85c
+ * The allocated data is guaranteed to be suitably aligned for any
eed85c
+ * built-in type.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated memory
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+g_atomic_rc_box_alloc (gsize block_size)
eed85c
+{
eed85c
+  g_return_val_if_fail (block_size > 0, NULL);
eed85c
+
eed85c
+  return g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, TRUE, FALSE);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_alloc0:
eed85c
+ * @block_size: the size of the allocation, must be greater than 0
eed85c
+ *
eed85c
+ * Allocates @block_size bytes of memory, and adds atomic
eed85c
+ * referenc counting semantics to it.
eed85c
+ *
eed85c
+ * The contents of the returned data is set to zero.
eed85c
+ *
eed85c
+ * The data will be freed when its reference count drops to
eed85c
+ * zero.
eed85c
+ *
eed85c
+ * The allocated data is guaranteed to be suitably aligned for any
eed85c
+ * built-in type.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated memory
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+g_atomic_rc_box_alloc0 (gsize block_size)
eed85c
+{
eed85c
+  g_return_val_if_fail (block_size > 0, NULL);
eed85c
+
eed85c
+  return g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, TRUE, TRUE);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_new:
eed85c
+ * @type: the type to allocate, typically a structure name
eed85c
+ *
eed85c
+ * A convenience macro to allocate atomically reference counted
eed85c
+ * data with the size of the given @type.
eed85c
+ *
eed85c
+ * This macro calls g_atomic_rc_box_alloc() with `sizeof (@type)` and
eed85c
+ * casts the returned pointer to a pointer of the given @type,
eed85c
+ * avoiding a type cast in the source code.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated
eed85c
+ *   memory, cast to a pointer for the given @type
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_new0:
eed85c
+ * @type: the type to allocate, typically a structure name
eed85c
+ *
eed85c
+ * A convenience macro to allocate atomically reference counted
eed85c
+ * data with the size of the given @type, and set its contents
eed85c
+ * to zero.
eed85c
+ *
eed85c
+ * This macro calls g_atomic_rc_box_alloc0() with `sizeof (@type)` and
eed85c
+ * casts the returned pointer to a pointer of the given @type,
eed85c
+ * avoiding a type cast in the source code.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated
eed85c
+ *   memory, cast to a pointer for the given @type
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_dup:
eed85c
+ * @block_size: the number of bytes to copy, must be greater than 0
eed85c
+ * @mem_block: (not nullable): the memory to copy
eed85c
+ *
eed85c
+ * Allocates a new block of data with atomit reference counting
eed85c
+ * semantics, and copies @block_size bytes of @mem_block
eed85c
+ * into it.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated
eed85c
+ *   memory
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+(g_atomic_rc_box_dup) (gsize         block_size,
eed85c
+                       gconstpointer mem_block)
eed85c
+{
eed85c
+  gpointer res;
eed85c
+
eed85c
+  g_return_val_if_fail (block_size > 0, NULL);
eed85c
+  g_return_val_if_fail (mem_block != NULL, NULL);
eed85c
+
eed85c
+  res = g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, TRUE, FALSE);
eed85c
+  memcpy (res, mem_block, block_size);
eed85c
+
eed85c
+  return res;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_acquire:
eed85c
+ * @mem_block: (not nullable): a pointer to reference counted data
eed85c
+ *
eed85c
+ * Atomically acquires a reference on the data pointed by @mem_block.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the data,
eed85c
+ *   with its reference count increased
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+(g_atomic_rc_box_acquire) (gpointer mem_block)
eed85c
+{
eed85c
+  GArcBox *real_box = G_ARC_BOX (mem_block);
eed85c
+
eed85c
+  g_return_val_if_fail (mem_block != NULL, NULL);
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, NULL);
eed85c
+#endif
eed85c
+
eed85c
+  g_atomic_ref_count_inc (&real_box->ref_count);
eed85c
+
eed85c
+  TRACE (GLIB_RCBOX_ACQUIRE (mem_block, 1));
eed85c
+
eed85c
+  return mem_block;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_release:
eed85c
+ * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
eed85c
+ *
eed85c
+ * Atomically releases a reference on the data pointed by @mem_block.
eed85c
+ *
eed85c
+ * If the reference was the last one, it will free the
eed85c
+ * resources allocated for @mem_block.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+g_atomic_rc_box_release (gpointer mem_block)
eed85c
+{
eed85c
+  g_atomic_rc_box_release_full (mem_block, NULL);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_release_full:
eed85c
+ * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
eed85c
+ * @clear_func: (not nullable): a function to call when clearing the data
eed85c
+ *
eed85c
+ * Atomically releases a reference on the data pointed by @mem_block.
eed85c
+ *
eed85c
+ * If the reference was the last one, it will call @clear_func
eed85c
+ * to clear the contents of @mem_block, and then will free the
eed85c
+ * resources allocated for @mem_block.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+g_atomic_rc_box_release_full (gpointer       mem_block,
eed85c
+                              GDestroyNotify clear_func)
eed85c
+{
eed85c
+  GArcBox *real_box = G_ARC_BOX (mem_block);
eed85c
+
eed85c
+  g_return_if_fail (mem_block != NULL);
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  g_return_if_fail (real_box->magic == G_BOX_MAGIC);
eed85c
+#endif
eed85c
+
eed85c
+  if (g_atomic_ref_count_dec (&real_box->ref_count))
eed85c
+    {
eed85c
+      char *real_mem = (char *) real_box - real_box->private_offset;
eed85c
+
eed85c
+      TRACE (GLIB_RCBOX_RELEASE (mem_block, 1));
eed85c
+
eed85c
+      if (clear_func != NULL)
eed85c
+        clear_func (mem_block);
eed85c
+
eed85c
+      TRACE (GLIB_RCBOX_FREE (mem_block));
eed85c
+      g_free (real_mem);
eed85c
+    }
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_rc_box_get_size:
eed85c
+ * @mem_block: (not nullable): a pointer to reference counted data
eed85c
+ *
eed85c
+ * Retrieves the size of the reference counted data pointed by @mem_block.
eed85c
+ *
eed85c
+ * Returns: the size of the data, in bytes
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gsize
eed85c
+g_atomic_rc_box_get_size (gpointer mem_block)
eed85c
+{
eed85c
+  GArcBox *real_box = G_ARC_BOX (mem_block);
eed85c
+
eed85c
+  g_return_val_if_fail (mem_block != NULL, 0);
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, 0);
eed85c
+#endif
eed85c
+
eed85c
+  return real_box->mem_size;
eed85c
+}
eed85c
diff --git a/gi/grcbox.c b/gi/grcbox.c
eed85c
new file mode 100644
eed85c
index 00000000..4f76a2b5
eed85c
--- /dev/null
eed85c
+++ b/gi/grcbox.c
eed85c
@@ -0,0 +1,506 @@
eed85c
+/* grcbox.c: Reference counted data
eed85c
+ *
eed85c
+ * Copyright 2018  Emmanuele Bassi
eed85c
+ *
eed85c
+ * This library is free software; you can redistribute it and/or
eed85c
+ * modify it under the terms of the GNU Lesser General Public
eed85c
+ * License as published by the Free Software Foundation; either
eed85c
+ * version 2.1 of the License, or (at your option) any later version.
eed85c
+ *
eed85c
+ * This library is distributed in the hope that it will be useful,
eed85c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
eed85c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eed85c
+ * Lesser General Public License for more details.
eed85c
+ *
eed85c
+ * You should have received a copy of the GNU Lesser General Public
eed85c
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
eed85c
+ */
eed85c
+
eed85c
+#include "config.h"
eed85c
+
eed85c
+#include "grcboxprivate.h"
eed85c
+
eed85c
+#include "grefcount.h"
eed85c
+#if 0
eed85c
+#include "gmessages.h"
eed85c
+#include "gtestutils.h"
eed85c
+
eed85c
+#include "glib_trace.h"
eed85c
+#else
eed85c
+#include "gjs_gi_trace.h"
eed85c
+#endif
eed85c
+
eed85c
+#ifdef ENABLE_VALGRIND
eed85c
+#include "valgrind.h"
eed85c
+#endif
eed85c
+
eed85c
+#include <string.h>
eed85c
+
eed85c
+/**
eed85c
+ * SECTION:rcbox
eed85c
+ * @Title: Reference counted data
eed85c
+ * @Short_description: Allocated memory with reference counting semantics
eed85c
+ *
eed85c
+ * A "reference counted box", or "RcBox", is an opaque wrapper data type
eed85c
+ * that is guaranteed to be as big as the size of a given data type, and
eed85c
+ * which augments the given data type with reference counting semantics
eed85c
+ * for its memory management.
eed85c
+ *
eed85c
+ * RcBox is useful if you have a plain old data type, like a structure
eed85c
+ * typically placed on the stack, and you wish to provide additional API
eed85c
+ * to use it on the heap; or if you want to implement a new type to be
eed85c
+ * passed around by reference without necessarily implementing copy/free
eed85c
+ * semantics or your own reference counting.
eed85c
+ *
eed85c
+ * The typical use is:
eed85c
+ *
eed85c
+ * |[
eed85c
+ * typedef struct {
eed85c
+ *   char *name;
eed85c
+ *   char *address;
eed85c
+ *   char *city;
eed85c
+ *   char *state;
eed85c
+ *   int age;
eed85c
+ * } Person;
eed85c
+ *
eed85c
+ * Person *
eed85c
+ * person_new (void)
eed85c
+ * {
eed85c
+ *   return g_rc_box_new0 (Person);
eed85c
+ * }
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * Every time you wish to acquire a reference on the memory, you should
eed85c
+ * call g_rc_box_acquire(); similarly, when you wish to release a reference
eed85c
+ * you should call g_rc_box_release():
eed85c
+ *
eed85c
+ * |[
eed85c
+ * // Add a Person to the Database; the Database acquires ownership
eed85c
+ * // of the Person instance
eed85c
+ * void
eed85c
+ * add_person_to_database (Database *db, Person *p)
eed85c
+ * {
eed85c
+ *   db->persons = g_list_prepend (db->persons, g_rc_box_acquire (p));
eed85c
+ * }
eed85c
+ *
eed85c
+ * // Removes a Person from the Database; the reference acquired by
eed85c
+ * // add_person_to_database() is released here
eed85c
+ * void
eed85c
+ * remove_person_from_database (Database *db, Person *p)
eed85c
+ * {
eed85c
+ *   db->persons = g_list_remove (db->persons, p);
eed85c
+ *   g_rc_box_release (p);
eed85c
+ * }
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * If you have additional memory allocated inside the structure, you can
eed85c
+ * use g_rc_box_release_full(), which takes a function pointer, which
eed85c
+ * will be called if the reference released was the last:
eed85c
+ *
eed85c
+ * |[
eed85c
+ * void
eed85c
+ * person_clear (Person *p)
eed85c
+ * {
eed85c
+ *   g_free (p->name);
eed85c
+ *   g_free (p->address);
eed85c
+ *   g_free (p->city);
eed85c
+ *   g_free (p->state);
eed85c
+ * }
eed85c
+ *
eed85c
+ * void
eed85c
+ * remove_person_from_database (Database *db, Person *p)
eed85c
+ * {
eed85c
+ *   db->persons = g_list_remove (db->persons, p);
eed85c
+ *   g_rc_box_release_full (p, (GDestroyNotify) person_clear);
eed85c
+ * }
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * If you wish to transfer the ownership of a reference counted data
eed85c
+ * type without increasing the reference count, you can use g_steal_pointer():
eed85c
+ *
eed85c
+ * |[
eed85c
+ *   Person *p = g_rc_box_new (Person);
eed85c
+ *
eed85c
+ *   // fill_person_details() is defined elsewhere
eed85c
+ *   fill_person_details (p);
eed85c
+ *
eed85c
+ *   // add_person_to_database_no_ref() is defined elsewhere; it adds
eed85c
+ *   // a Person to the Database without taking a reference
eed85c
+ *   add_person_to_database_no_ref (db, g_steal_pointer (&p);;
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * ## Thread safety
eed85c
+ *
eed85c
+ * The reference counting operations on data allocated using g_rc_box_alloc(),
eed85c
+ * g_rc_box_new(), and g_rc_box_dup() are not thread safe; it is your code's
eed85c
+ * responsibility to ensure that references are acquired are released on the
eed85c
+ * same thread.
eed85c
+ *
eed85c
+ * If you need thread safe reference counting, see the [atomic reference counted
eed85c
+ * data][arcbox] API.
eed85c
+ *
eed85c
+ * ## Automatic pointer clean up
eed85c
+ *
eed85c
+ * If you want to add g_autoptr() support to your plain old data type through
eed85c
+ * reference counting, you can use the G_DEFINE_AUTOPTR_CLEANUP_FUNC() and
eed85c
+ * g_rc_box_release():
eed85c
+ *
eed85c
+ * |[
eed85c
+ * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, g_rc_box_release)
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * If you need to clear the contents of the data, you will need to use an
eed85c
+ * ancillary function that calls g_rc_box_release_full():
eed85c
+ *
eed85c
+ * |[
eed85c
+ * static void
eed85c
+ * my_data_struct_release (MyDataStruct *data)
eed85c
+ * {
eed85c
+ *   // my_data_struct_clear() is defined elsewhere
eed85c
+ *   g_rc_box_release_full (data, (GDestroyNotify) my_data_struct_clear);
eed85c
+ * }
eed85c
+ *
eed85c
+ * G_DEFINE_AUTOPTR_CLEANUP_FUNC (MyDataStruct, my_data_struct_release)
eed85c
+ * ]|
eed85c
+ *
eed85c
+ * Since: 2.58.
eed85c
+ */
eed85c
+
eed85c
+/* We use the same alignment as GTypeInstance and GNU libc's malloc */
eed85c
+#define ALIGN_STRUCT(offset)    ((offset + (STRUCT_ALIGNMENT - 1)) & -STRUCT_ALIGNMENT)
eed85c
+
eed85c
+#define G_RC_BOX(p)             (GRcBox *) (((char *) (p)) - G_RC_BOX_SIZE)
eed85c
+
eed85c
+gpointer
eed85c
+g_rc_box_alloc_full (gsize    block_size,
eed85c
+                     gsize    alignment,
eed85c
+                     gboolean atomic,
eed85c
+                     gboolean clear)
eed85c
+{
eed85c
+  /* We don't do an (atomic ? G_ARC_BOX_SIZE : G_RC_BOX_SIZE) check, here
eed85c
+   * because we have a static assertion that sizeof(GArcBox) == sizeof(GRcBox)
eed85c
+   * inside grcboxprivate.h, and we don't want the compiler to unnecessarily
eed85c
+   * warn about both branches of the conditional yielding identical results
eed85c
+   */
eed85c
+  gsize private_size = G_ARC_BOX_SIZE;
eed85c
+  gsize private_offset = 0;
eed85c
+  gsize real_size;
eed85c
+  char *allocated;
eed85c
+
eed85c
+  g_assert (alignment != 0);
eed85c
+
eed85c
+  /* We need to ensure that the private data is aligned */
eed85c
+  if (private_size % alignment != 0)
eed85c
+    {
eed85c
+      private_offset = private_size % alignment;
eed85c
+      private_size += (alignment - private_offset);
eed85c
+    }
eed85c
+
eed85c
+  g_assert (block_size < (G_MAXSIZE - private_size));
eed85c
+  real_size = private_size + block_size;
eed85c
+
eed85c
+  /* The real allocated size must be a multiple of @alignment, to
eed85c
+   * maintain the alignment of block_size
eed85c
+   */
eed85c
+  if (real_size % alignment != 0)
eed85c
+    {
eed85c
+      gsize offset = real_size % alignment;
eed85c
+      g_assert (real_size < (G_MAXSIZE - (alignment - offset)));
eed85c
+      real_size += (alignment - offset);
eed85c
+    }
eed85c
+
eed85c
+#ifdef ENABLE_VALGRIND
eed85c
+  if (RUNNING_ON_VALGRIND)
eed85c
+    {
eed85c
+      /* When running under Valgrind we massage the memory allocation
eed85c
+       * to include a pointer at the tail end of the block; the pointer
eed85c
+       * is then set to the start of the block. This trick allows
eed85c
+       * Valgrind to keep track of the over-allocation and not be
eed85c
+       * confused when passing the pointer around
eed85c
+       */
eed85c
+      g_assert (private_size < (G_MAXSIZE - ALIGN_STRUCT (1)));
eed85c
+      private_size += ALIGN_STRUCT (1);
eed85c
+
eed85c
+      if (clear)
eed85c
+        allocated = g_malloc0 (real_size + sizeof (gpointer));
eed85c
+      else
eed85c
+        allocated = g_malloc (real_size + sizeof (gpointer));
eed85c
+
eed85c
+      *(gpointer *) (allocated + private_size + block_size) = allocated + ALIGN_STRUCT (1);
eed85c
+
eed85c
+      VALGRIND_MALLOCLIKE_BLOCK (allocated + private_size, block_size + sizeof (gpointer), 0, TRUE);
eed85c
+      VALGRIND_MALLOCLIKE_BLOCK (allocated + ALIGN_STRUCT (1), private_size - ALIGN_STRUCT (1), 0, TRUE);
eed85c
+    }
eed85c
+  else
eed85c
+#endif /* ENABLE_VALGRIND */
eed85c
+    {
eed85c
+      if (clear)
eed85c
+        allocated = g_malloc0 (real_size);
eed85c
+      else
eed85c
+        allocated = g_malloc (real_size);
eed85c
+    }
eed85c
+
eed85c
+  if (atomic)
eed85c
+    {
eed85c
+      /* We leave the alignment padding at the top of the allocation,
eed85c
+       * so we have an in memory layout of:
eed85c
+       *
eed85c
+       *  |[ offset ][ sizeof(GArcBox) ]||[ block_size ]|
eed85c
+       */
eed85c
+      GArcBox *real_box = (GArcBox *) (allocated + private_offset);
eed85c
+      /* Store the real size */
eed85c
+      real_box->mem_size = block_size;
eed85c
+      /* Store the alignment offset, to be used when freeing the
eed85c
+       * allocated block
eed85c
+       */
eed85c
+      real_box->private_offset = private_offset;
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+      real_box->magic = G_BOX_MAGIC;
eed85c
+#endif
eed85c
+      g_atomic_ref_count_init (&real_box->ref_count);
eed85c
+    }
eed85c
+  else
eed85c
+    {
eed85c
+      /* We leave the alignment padding at the top of the allocation,
eed85c
+       * so we have an in memory layout of:
eed85c
+       *
eed85c
+       *  |[ offset ][ sizeof(GRcBox) ]||[ block_size ]|
eed85c
+       */
eed85c
+      GRcBox *real_box = (GRcBox *) (allocated + private_offset);
eed85c
+      /* Store the real size */
eed85c
+      real_box->mem_size = block_size;
eed85c
+      /* Store the alignment offset, to be used when freeing the
eed85c
+       * allocated block
eed85c
+       */
eed85c
+      real_box->private_offset = private_offset;
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+      real_box->magic = G_BOX_MAGIC;
eed85c
+#endif
eed85c
+      g_ref_count_init (&real_box->ref_count);
eed85c
+    }
eed85c
+
eed85c
+  TRACE (GLIB_RCBOX_ALLOC (allocated, block_size, atomic, clear));
eed85c
+
eed85c
+  return allocated + private_size;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_alloc:
eed85c
+ * @block_size: the size of the allocation, must be greater than 0
eed85c
+ *
eed85c
+ * Allocates @block_size bytes of memory, and adds reference
eed85c
+ * counting semantics to it.
eed85c
+ *
eed85c
+ * The data will be freed when its reference count drops to
eed85c
+ * zero.
eed85c
+ *
eed85c
+ * The allocated data is guaranteed to be suitably aligned for any
eed85c
+ * built-in type.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated memory
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+g_rc_box_alloc (gsize block_size)
eed85c
+{
eed85c
+  g_return_val_if_fail (block_size > 0, NULL);
eed85c
+
eed85c
+  return g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, FALSE, FALSE);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_alloc0:
eed85c
+ * @block_size: the size of the allocation, must be greater than 0
eed85c
+ *
eed85c
+ * Allocates @block_size bytes of memory, and adds reference
eed85c
+ * counting semantics to it.
eed85c
+ *
eed85c
+ * The contents of the returned data is set to zero.
eed85c
+ *
eed85c
+ * The data will be freed when its reference count drops to
eed85c
+ * zero.
eed85c
+ *
eed85c
+ * The allocated data is guaranteed to be suitably aligned for any
eed85c
+ * built-in type.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated memory
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+g_rc_box_alloc0 (gsize block_size)
eed85c
+{
eed85c
+  g_return_val_if_fail (block_size > 0, NULL);
eed85c
+
eed85c
+  return g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, FALSE, TRUE);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_new:
eed85c
+ * @type: the type to allocate, typically a structure name
eed85c
+ *
eed85c
+ * A convenience macro to allocate reference counted data with
eed85c
+ * the size of the given @type.
eed85c
+ *
eed85c
+ * This macro calls g_rc_box_alloc() with `sizeof (@type)` and
eed85c
+ * casts the returned pointer to a pointer of the given @type,
eed85c
+ * avoiding a type cast in the source code.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the
eed85c
+ *   allocated memory, cast to a pointer for the given @type
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_new0:
eed85c
+ * @type: the type to allocate, typically a structure name
eed85c
+ *
eed85c
+ * A convenience macro to allocate reference counted data with
eed85c
+ * the size of the given @type, and set its contents to zero.
eed85c
+ *
eed85c
+ * This macro calls g_rc_box_alloc0() with `sizeof (@type)` and
eed85c
+ * casts the returned pointer to a pointer of the given @type,
eed85c
+ * avoiding a type cast in the source code.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the
eed85c
+ *   allocated memory, cast to a pointer for the given @type
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_dup:
eed85c
+ * @block_size: the number of bytes to copy, must be greater than 0
eed85c
+ * @mem_block: (not nullable): the memory to copy
eed85c
+ *
eed85c
+ * Allocates a new block of data with reference counting
eed85c
+ * semantics, and copies @block_size bytes of @mem_block
eed85c
+ * into it.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the allocated
eed85c
+ *   memory
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+(g_rc_box_dup) (gsize         block_size,
eed85c
+                gconstpointer mem_block)
eed85c
+{
eed85c
+  gpointer res;
eed85c
+
eed85c
+  g_return_val_if_fail (block_size > 0, NULL);
eed85c
+  g_return_val_if_fail (mem_block != NULL, NULL);
eed85c
+
eed85c
+  res = g_rc_box_alloc_full (block_size, STRUCT_ALIGNMENT, FALSE, FALSE);
eed85c
+  memcpy (res, mem_block, block_size);
eed85c
+
eed85c
+  return res;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_acquire:
eed85c
+ * @mem_block: (not nullable): a pointer to reference counted data
eed85c
+ *
eed85c
+ * Acquires a reference on the data pointed by @mem_block.
eed85c
+ *
eed85c
+ * Returns: (transfer full) (not nullable): a pointer to the data,
eed85c
+ *   with its reference count increased
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gpointer
eed85c
+(g_rc_box_acquire) (gpointer mem_block)
eed85c
+{
eed85c
+  GRcBox *real_box = G_RC_BOX (mem_block);
eed85c
+
eed85c
+  g_return_val_if_fail (mem_block != NULL, NULL);
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, NULL);
eed85c
+#endif
eed85c
+
eed85c
+  g_ref_count_inc (&real_box->ref_count);
eed85c
+
eed85c
+  TRACE (GLIB_RCBOX_ACQUIRE (mem_block, 0));
eed85c
+
eed85c
+  return mem_block;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_release:
eed85c
+ * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
eed85c
+ *
eed85c
+ * Releases a reference on the data pointed by @mem_block.
eed85c
+ *
eed85c
+ * If the reference was the last one, it will free the
eed85c
+ * resources allocated for @mem_block.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+g_rc_box_release (gpointer mem_block)
eed85c
+{
eed85c
+  g_rc_box_release_full (mem_block, NULL);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_release_full:
eed85c
+ * @mem_block: (transfer full) (not nullable): a pointer to reference counted data
eed85c
+ * @clear_func: (not nullable): a function to call when clearing the data
eed85c
+ *
eed85c
+ * Releases a reference on the data pointed by @mem_block.
eed85c
+ *
eed85c
+ * If the reference was the last one, it will call @clear_func
eed85c
+ * to clear the contents of @mem_block, and then will free the
eed85c
+ * resources allocated for @mem_block.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+g_rc_box_release_full (gpointer       mem_block,
eed85c
+                       GDestroyNotify clear_func)
eed85c
+{
eed85c
+  GRcBox *real_box = G_RC_BOX (mem_block);
eed85c
+
eed85c
+  g_return_if_fail (mem_block != NULL);
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  g_return_if_fail (real_box->magic == G_BOX_MAGIC);
eed85c
+#endif
eed85c
+
eed85c
+  if (g_ref_count_dec (&real_box->ref_count))
eed85c
+    {
eed85c
+      char *real_mem = (char *) real_box - real_box->private_offset;
eed85c
+
eed85c
+      TRACE (GLIB_RCBOX_RELEASE (mem_block, 0));
eed85c
+
eed85c
+      if (clear_func != NULL)
eed85c
+        clear_func (mem_block);
eed85c
+
eed85c
+      TRACE (GLIB_RCBOX_FREE (mem_block));
eed85c
+      g_free (real_mem);
eed85c
+    }
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_rc_box_get_size:
eed85c
+ * @mem_block: (not nullable): a pointer to reference counted data
eed85c
+ *
eed85c
+ * Retrieves the size of the reference counted data pointed by @mem_block.
eed85c
+ *
eed85c
+ * Returns: the size of the data, in bytes
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gsize
eed85c
+g_rc_box_get_size (gpointer mem_block)
eed85c
+{
eed85c
+  GRcBox *real_box = G_RC_BOX (mem_block);
eed85c
+
eed85c
+  g_return_val_if_fail (mem_block != NULL, 0);
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, 0);
eed85c
+#endif
eed85c
+
eed85c
+  return real_box->mem_size;
eed85c
+}
eed85c
diff --git a/gi/grcbox.h b/gi/grcbox.h
eed85c
new file mode 100644
eed85c
index 00000000..7c2f89f5
eed85c
--- /dev/null
eed85c
+++ b/gi/grcbox.h
eed85c
@@ -0,0 +1,96 @@
eed85c
+/* grcbox.h: Reference counted data
eed85c
+ *
eed85c
+ * Copyright 2018  Emmanuele Bassi
eed85c
+ *
eed85c
+ * This library is free software; you can redistribute it and/or
eed85c
+ * modify it under the terms of the GNU Lesser General Public
eed85c
+ * License as published by the Free Software Foundation; either
eed85c
+ * version 2.1 of the License, or (at your option) any later version.
eed85c
+ *
eed85c
+ * This library is distributed in the hope that it will be useful,
eed85c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
eed85c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eed85c
+ * Lesser General Public License for more details.
eed85c
+ *
eed85c
+ * You should have received a copy of the GNU Lesser General Public
eed85c
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
eed85c
+ */
eed85c
+
eed85c
+#pragma once
eed85c
+
eed85c
+#if 0
eed85c
+#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
eed85c
+#error "Only <glib.h> can be included directly."
eed85c
+#endif
eed85c
+
eed85c
+#include <glib/gmem.h>
eed85c
+#else
eed85c
+#include <glib.h>
eed85c
+
eed85c
+#ifndef GLIB_AVAILABLE_IN_2_58
eed85c
+#define GLIB_AVAILABLE_IN_2_58
eed85c
+#endif
eed85c
+#endif
eed85c
+
eed85c
+G_BEGIN_DECLS
eed85c
+
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_rc_box_alloc                  (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_rc_box_alloc0                 (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_rc_box_dup                    (gsize           block_size,
eed85c
+                                                 gconstpointer   mem_block) G_GNUC_ALLOC_SIZE(1);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_rc_box_acquire                (gpointer        mem_block);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_rc_box_release                (gpointer        mem_block);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_rc_box_release_full           (gpointer        mem_block,
eed85c
+                                                 GDestroyNotify  clear_func);
eed85c
+
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gsize           g_rc_box_get_size               (gpointer        mem_block);
eed85c
+
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_atomic_rc_box_alloc           (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_atomic_rc_box_alloc0          (gsize           block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_atomic_rc_box_dup             (gsize           block_size,
eed85c
+                                                 gconstpointer   mem_block) G_GNUC_ALLOC_SIZE(1);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gpointer        g_atomic_rc_box_acquire         (gpointer        mem_block);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_atomic_rc_box_release         (gpointer        mem_block);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_atomic_rc_box_release_full    (gpointer        mem_block,
eed85c
+                                                 GDestroyNotify  clear_func);
eed85c
+
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gsize           g_atomic_rc_box_get_size        (gpointer        mem_block);
eed85c
+
eed85c
+#define g_rc_box_new(type) \
eed85c
+  ((type *) g_rc_box_alloc (sizeof (type)))
eed85c
+#define g_rc_box_new0(type) \
eed85c
+  ((type *) g_rc_box_alloc0 (sizeof (type)))
eed85c
+#define g_atomic_rc_box_new(type) \
eed85c
+  ((type *) g_atomic_rc_box_alloc (sizeof (type)))
eed85c
+#define g_atomic_rc_box_new0(type) \
eed85c
+  ((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
eed85c
+
eed85c
+#ifdef g_has_typeof
eed85c
+/* Type check to avoid assigning references to different types */
eed85c
+# define g_rc_box_acquire(mem_block) \
eed85c
+  ((__typeof__(mem_block)) (g_rc_box_acquire) (mem_block))
eed85c
+# define g_atomic_rc_box_acquire(mem_block) \
eed85c
+  ((__typeof__(mem_block)) (g_atomic_rc_box_acquire) (mem_block))
eed85c
+
eed85c
+/* Type check to avoid duplicating data to different types */
eed85c
+# define g_rc_box_dup(block_size,mem_block) \
eed85c
+  ((__typeof__(mem_block)) (g_rc_box_dup) (block_size,mem_block))
eed85c
+# define g_atomic_rc_box_dup(block_size,mem_block) \
eed85c
+  ((__typeof__(mem_block)) (g_atomic_rc_box_dup) (block_size,mem_block))
eed85c
+#endif
eed85c
+
eed85c
+G_END_DECLS
eed85c
diff --git a/gi/grcboxprivate.h b/gi/grcboxprivate.h
eed85c
new file mode 100644
eed85c
index 00000000..7ed94ab8
eed85c
--- /dev/null
eed85c
+++ b/gi/grcboxprivate.h
eed85c
@@ -0,0 +1,78 @@
eed85c
+/* grcboxprivate.h: Reference counted data
eed85c
+ *
eed85c
+ * Copyright 2018  Emmanuele Bassi
eed85c
+ *
eed85c
+ * This library is free software; you can redistribute it and/or
eed85c
+ * modify it under the terms of the GNU Lesser General Public
eed85c
+ * License as published by the Free Software Foundation; either
eed85c
+ * version 2.1 of the License, or (at your option) any later version.
eed85c
+ *
eed85c
+ * This library is distributed in the hope that it will be useful,
eed85c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
eed85c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eed85c
+ * Lesser General Public License for more details.
eed85c
+ *
eed85c
+ * You should have received a copy of the GNU Lesser General Public
eed85c
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
eed85c
+ */
eed85c
+
eed85c
+#pragma once
eed85c
+
eed85c
+#if 0
eed85c
+#include "gtypes.h"
eed85c
+#else
eed85c
+#include <glib.h>
eed85c
+
eed85c
+typedef gint            grefcount;
eed85c
+typedef volatile gint   gatomicrefcount;
eed85c
+#endif
eed85c
+#include "grcbox.h"
eed85c
+
eed85c
+G_BEGIN_DECLS
eed85c
+
eed85c
+typedef struct {
eed85c
+  grefcount ref_count;
eed85c
+
eed85c
+  gsize mem_size;
eed85c
+  gsize private_offset;
eed85c
+
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  /* A "magic" number, used to perform additional integrity
eed85c
+   * checks on the allocated data
eed85c
+   */
eed85c
+  guint32 magic;
eed85c
+#endif
eed85c
+} GRcBox;
eed85c
+
eed85c
+typedef struct {
eed85c
+  gatomicrefcount ref_count;
eed85c
+
eed85c
+  gsize mem_size;
eed85c
+  gsize private_offset;
eed85c
+
eed85c
+#ifndef G_DISABLE_ASSERT
eed85c
+  guint32 magic;
eed85c
+#endif
eed85c
+} GArcBox;
eed85c
+
eed85c
+#define G_BOX_MAGIC             0x44ae2bf0
eed85c
+
eed85c
+/* Keep the two refcounted boxes identical in size */
eed85c
+G_STATIC_ASSERT (sizeof (GRcBox) == sizeof (GArcBox));
eed85c
+
eed85c
+/* This is the default alignment we use when allocating the
eed85c
+ * refcounted memory blocks; it's similar to the alignment
eed85c
+ * guaranteed by the malloc() in GNU's libc and by the GSlice
eed85c
+ * allocator
eed85c
+ */
eed85c
+#define STRUCT_ALIGNMENT (2 * sizeof (gsize))
eed85c
+
eed85c
+#define G_RC_BOX_SIZE sizeof (GRcBox)
eed85c
+#define G_ARC_BOX_SIZE sizeof (GArcBox)
eed85c
+
eed85c
+gpointer        g_rc_box_alloc_full     (gsize    block_size,
eed85c
+                                         gsize    alignment,
eed85c
+                                         gboolean atomic,
eed85c
+                                         gboolean clear);
eed85c
+
eed85c
+G_END_DECLS
eed85c
diff --git a/gi/grefcount.c b/gi/grefcount.c
eed85c
new file mode 100644
eed85c
index 00000000..2bc7e9d7
eed85c
--- /dev/null
eed85c
+++ b/gi/grefcount.c
eed85c
@@ -0,0 +1,289 @@
eed85c
+/* grefcount.c: Reference counting
eed85c
+ *
eed85c
+ * Copyright 2018  Emmanuele Bassi
eed85c
+ *
eed85c
+ * This library is free software; you can redistribute it and/or
eed85c
+ * modify it under the terms of the GNU Lesser General Public
eed85c
+ * License as published by the Free Software Foundation; either
eed85c
+ * version 2.1 of the License, or (at your option) any later version.
eed85c
+ *
eed85c
+ * This library is distributed in the hope that it will be useful,
eed85c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
eed85c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eed85c
+ * Lesser General Public License for more details.
eed85c
+ *
eed85c
+ * You should have received a copy of the GNU Lesser General Public
eed85c
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * SECTION:refcount
eed85c
+ * @Title: Reference counting
eed85c
+ * @Short_description: Reference counting types and functions
eed85c
+ *
eed85c
+ * Reference counting is a garbage collection mechanism that is based on
eed85c
+ * assigning a counter to a data type, or any memory area; the counter is
eed85c
+ * increased whenever a new reference to that data type is acquired, and
eed85c
+ * decreased whenever the reference is released. Once the last reference
eed85c
+ * is released, the resources associated to that data type are freed.
eed85c
+ *
eed85c
+ * GLib uses reference counting in many of its data types, and provides
eed85c
+ * the #grefcount and #gatomicrefcount types to implement safe and atomic
eed85c
+ * reference counting semantics in new data types.
eed85c
+ *
eed85c
+ * It is important to note that #grefcount and #gatomicrefcount should be
eed85c
+ * considered completely opaque types; you should always use the provided
eed85c
+ * API to increase and decrease the counters, and you should never check
eed85c
+ * their content directly, or compare their content with other values.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+#include "config.h"
eed85c
+
eed85c
+#include "grefcount.h"
eed85c
+
eed85c
+#if 0
eed85c
+#include "gatomic.h"
eed85c
+#include "gmessages.h"
eed85c
+#else
eed85c
+#include <glib.h>
eed85c
+#endif
eed85c
+
eed85c
+/**
eed85c
+ * grefcount:
eed85c
+ *
eed85c
+ * A type for implementing non-atomic reference count semantics.
eed85c
+ *
eed85c
+ * Use g_ref_count_init() to initialize it; g_ref_count_inc() to
eed85c
+ * increase the counter, and g_ref_count_dec() to decrease it.
eed85c
+ *
eed85c
+ * It is safe to use #grefcount only if you're expecting to operate
eed85c
+ * on the reference counter from a single thread. It is entirely up
eed85c
+ * to you to ensure that all reference count changes happen in the
eed85c
+ * same thread.
eed85c
+ *
eed85c
+ * See also: #gatomicrefcount
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * gatomicrefcount:
eed85c
+ *
eed85c
+ * A type for implementing atomic reference count semantics.
eed85c
+ *
eed85c
+ * Use g_atomic_ref_count_init() to initialize it; g_atomic_ref_count_inc()
eed85c
+ * to increase the counter, and g_atomic_ref_count_dec() to decrease it.
eed85c
+ *
eed85c
+ * It is safe to use #gatomicrefcount if you're expecting to operate on the
eed85c
+ * reference counter from multiple threads.
eed85c
+ *
eed85c
+ * See also: #grefcount
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+
eed85c
+/**
eed85c
+ * g_ref_count_init:
eed85c
+ * @rc: the address of a reference count variable
eed85c
+ *
eed85c
+ * Initializes a reference count variable.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+(g_ref_count_init) (grefcount *rc)
eed85c
+{
eed85c
+  g_return_if_fail (rc != NULL);
eed85c
+
eed85c
+  /* Non-atomic refcounting is implemented using the negative range
eed85c
+   * of signed integers:
eed85c
+   *
eed85c
+   * G_MININT                 Z¯< 0 > Z⁺                G_MAXINT
eed85c
+   * |----------------------------|----------------------------|
eed85c
+   *
eed85c
+   * Acquiring a reference moves us towards MININT, and releasing a
eed85c
+   * reference moves us towards 0.
eed85c
+   */
eed85c
+  *rc = -1;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_ref_count_inc:
eed85c
+ * @rc: the address of a reference count variable
eed85c
+ *
eed85c
+ * Increases the reference count.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+(g_ref_count_inc) (grefcount *rc)
eed85c
+{
eed85c
+  grefcount rrc;
eed85c
+
eed85c
+  g_return_if_fail (rc != NULL);
eed85c
+
eed85c
+  rrc = *rc;
eed85c
+
eed85c
+  g_return_if_fail (rrc < 0);
eed85c
+
eed85c
+  /* Check for saturation */
eed85c
+  if (rrc == G_MININT)
eed85c
+    {
eed85c
+      g_critical ("Reference count %p has reached saturation", rc);
eed85c
+      return;
eed85c
+    }
eed85c
+
eed85c
+  rrc -= 1;
eed85c
+
eed85c
+  *rc = rrc;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_ref_count_dec:
eed85c
+ * @rc: the address of a reference count variable
eed85c
+ *
eed85c
+ * Decreases the reference count.
eed85c
+ *
eed85c
+ * Returns: %TRUE if the reference count reached 0, and %FALSE otherwise
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gboolean
eed85c
+(g_ref_count_dec) (grefcount *rc)
eed85c
+{
eed85c
+  grefcount rrc;
eed85c
+
eed85c
+  g_return_val_if_fail (rc != NULL, FALSE);
eed85c
+
eed85c
+  rrc = *rc;
eed85c
+
eed85c
+  g_return_val_if_fail (rrc < 0, FALSE);
eed85c
+
eed85c
+  rrc += 1;
eed85c
+  if (rrc == 0)
eed85c
+    return TRUE;
eed85c
+
eed85c
+  *rc = rrc;
eed85c
+
eed85c
+  return FALSE;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_ref_count_compare:
eed85c
+ * @rc: the address of a reference count variable
eed85c
+ * @val: the value to compare
eed85c
+ *
eed85c
+ * Compares the current value of @rc with @val.
eed85c
+ *
eed85c
+ * Returns: %TRUE if the reference count is the same
eed85c
+ *   as the given value
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gboolean
eed85c
+(g_ref_count_compare) (grefcount *rc,
eed85c
+                       gint       val)
eed85c
+{
eed85c
+  grefcount rrc;
eed85c
+
eed85c
+  g_return_val_if_fail (rc != NULL, FALSE);
eed85c
+  g_return_val_if_fail (val >= 0, FALSE);
eed85c
+
eed85c
+  rrc = *rc;
eed85c
+
eed85c
+  if (val == G_MAXINT)
eed85c
+    return rrc == G_MININT;
eed85c
+
eed85c
+  return rrc == -val;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_ref_count_init:
eed85c
+ * @arc: the address of an atomic reference count variable
eed85c
+ *
eed85c
+ * Initializes a reference count variable.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+(g_atomic_ref_count_init) (gatomicrefcount *arc)
eed85c
+{
eed85c
+  g_return_if_fail (arc != NULL);
eed85c
+
eed85c
+  /* Atomic refcounting is implemented using the positive range
eed85c
+   * of signed integers:
eed85c
+   *
eed85c
+   * G_MININT                 Z¯< 0 > Z⁺                G_MAXINT
eed85c
+   * |----------------------------|----------------------------|
eed85c
+   *
eed85c
+   * Acquiring a reference moves us towards MAXINT, and releasing a
eed85c
+   * reference moves us towards 0.
eed85c
+   */
eed85c
+  *arc = 1;
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_ref_count_inc:
eed85c
+ * @arc: the address of an atomic reference count variable
eed85c
+ *
eed85c
+ * Atomically increases the reference count.
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+void
eed85c
+(g_atomic_ref_count_inc) (gatomicrefcount *arc)
eed85c
+{
eed85c
+  g_return_if_fail (arc != NULL);
eed85c
+  g_return_if_fail (g_atomic_int_get (arc) > 0);
eed85c
+
eed85c
+  if (g_atomic_int_get (arc) == G_MAXINT)
eed85c
+    {
eed85c
+      g_critical ("Reference count has reached saturation");
eed85c
+      return;
eed85c
+    }
eed85c
+
eed85c
+  g_atomic_int_inc (arc);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_ref_count_dec:
eed85c
+ * @arc: the address of an atomic reference count variable
eed85c
+ *
eed85c
+ * Atomically decreases the reference count.
eed85c
+ *
eed85c
+ * Returns: %TRUE if the reference count reached 0, and %FALSE otherwise
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gboolean
eed85c
+(g_atomic_ref_count_dec) (gatomicrefcount *arc)
eed85c
+{
eed85c
+  g_return_val_if_fail (arc != NULL, FALSE);
eed85c
+  g_return_val_if_fail (g_atomic_int_get (arc) > 0, FALSE);
eed85c
+
eed85c
+  return g_atomic_int_dec_and_test (arc);
eed85c
+}
eed85c
+
eed85c
+/**
eed85c
+ * g_atomic_ref_count_compare:
eed85c
+ * @arc: the address of an atomic reference count variable
eed85c
+ * @val: the value to compare
eed85c
+ *
eed85c
+ * Atomically compares the current value of @arc with @val.
eed85c
+ *
eed85c
+ * Returns: %TRUE if the reference count is the same
eed85c
+ *   as the given value
eed85c
+ *
eed85c
+ * Since: 2.58
eed85c
+ */
eed85c
+gboolean
eed85c
+(g_atomic_ref_count_compare) (gatomicrefcount *arc,
eed85c
+                              gint             val)
eed85c
+{
eed85c
+  g_return_val_if_fail (arc != NULL, FALSE);
eed85c
+  g_return_val_if_fail (val >= 0, FALSE);
eed85c
+
eed85c
+  return g_atomic_int_get (arc) == val;
eed85c
+}
eed85c
diff --git a/gi/grefcount.h b/gi/grefcount.h
eed85c
new file mode 100644
eed85c
index 00000000..d2f78037
eed85c
--- /dev/null
eed85c
+++ b/gi/grefcount.h
eed85c
@@ -0,0 +1,134 @@
eed85c
+/* grefcount.h: Reference counting
eed85c
+ *
eed85c
+ * Copyright 2018  Emmanuele Bassi
eed85c
+ *
eed85c
+ * This library is free software; you can redistribute it and/or
eed85c
+ * modify it under the terms of the GNU Lesser General Public
eed85c
+ * License as published by the Free Software Foundation; either
eed85c
+ * version 2.1 of the License, or (at your option) any later version.
eed85c
+ *
eed85c
+ * This library is distributed in the hope that it will be useful,
eed85c
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
eed85c
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
eed85c
+ * Lesser General Public License for more details.
eed85c
+ *
eed85c
+ * You should have received a copy of the GNU Lesser General Public
eed85c
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
eed85c
+ */
eed85c
+
eed85c
+#ifndef __GREFCOUNT_H__
eed85c
+#define __GREFCOUNT_H__
eed85c
+
eed85c
+#if 0
eed85c
+#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
eed85c
+#error "Only <glib.h> can be included directly."
eed85c
+#endif
eed85c
+
eed85c
+#include <glib/gatomic.h>
eed85c
+#include <glib/gtypes.h>
eed85c
+#else
eed85c
+#include <glib.h>
eed85c
+
eed85c
+#ifndef GLIB_AVAILABLE_IN_2_58
eed85c
+#define GLIB_AVAILABLE_IN_2_58
eed85c
+#endif
eed85c
+
eed85c
+typedef gint            grefcount;
eed85c
+typedef volatile gint   gatomicrefcount;
eed85c
+#endif
eed85c
+
eed85c
+G_BEGIN_DECLS
eed85c
+
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_ref_count_init                (grefcount       *rc);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_ref_count_inc                 (grefcount       *rc);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gboolean        g_ref_count_dec                 (grefcount       *rc);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gboolean        g_ref_count_compare             (grefcount       *rc,
eed85c
+                                                 gint             val);
eed85c
+
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_atomic_ref_count_init         (gatomicrefcount *arc);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+void            g_atomic_ref_count_inc          (gatomicrefcount *arc);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gboolean        g_atomic_ref_count_dec          (gatomicrefcount *arc);
eed85c
+GLIB_AVAILABLE_IN_2_58
eed85c
+gboolean        g_atomic_ref_count_compare      (gatomicrefcount *arc,
eed85c
+                                                 gint             val);
eed85c
+
eed85c
+/* On GCC we can use __extension__ to inline the API without using
eed85c
+ * ancillary functions; we only do this when disabling checks, as
eed85c
+ * it disables warnings when saturating the reference counters
eed85c
+ */
eed85c
+#if defined(__GNUC__) && defined(G_DISABLE_CHECKS)
eed85c
+
eed85c
+# define g_ref_count_init(rc) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ *(rc) : 1); \
eed85c
+    *(rc) = -1; \
eed85c
+  }))
eed85c
+
eed85c
+# define g_ref_count_inc(rc) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ *(rc) : 1); \
eed85c
+    if (*(rc) == G_MININT) ; else { \
eed85c
+      *(rc) -= 1; \
eed85c
+    } \
eed85c
+  }))
eed85c
+
eed85c
+# define g_ref_count_dec(rc) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
eed85c
+    grefcount __rc = *(rc); \
eed85c
+    __rc += 1; \
eed85c
+    if (__rc == 0) ; else { \
eed85c
+      *(rc) = __rc; \
eed85c
+    } \
eed85c
+    (gboolean) (__rc == 0); \
eed85c
+  }))
eed85c
+
eed85c
+# define g_ref_count_compare(rc,val) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (grefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ (val) : 1); \
eed85c
+    (gboolean) (*(rc) == -(val)); \
eed85c
+  }))
eed85c
+
eed85c
+# define g_atomic_ref_count_init(rc) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ *(rc) : 1); \
eed85c
+    *(rc) = 1; \
eed85c
+  }))
eed85c
+
eed85c
+# define g_atomic_ref_count_inc(rc) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ *(rc) : 1); \
eed85c
+    (void) (g_atomic_int_get (rc) == G_MAXINT ? 0 : g_atomic_int_inc ((rc))); \
eed85c
+  }))
eed85c
+
eed85c
+# define g_atomic_ref_count_dec(rc) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ *(rc) : 1); \
eed85c
+    g_atomic_int_dec_and_test ((rc)); \
eed85c
+  }))
eed85c
+
eed85c
+# define g_atomic_ref_count_compare(rc,val) \
eed85c
+  (G_GNUC_EXTENSION ({ \
eed85c
+    G_STATIC_ASSERT (sizeof *(rc) == sizeof (gatomicrefcount)); \
eed85c
+    (void) (0 ? *(rc) ^ (val) : 1); \
eed85c
+    (gboolean) (g_atomic_int_get (rc) == (val)); \
eed85c
+  }))
eed85c
+
eed85c
+#endif /* __GNUC__ && G_DISABLE_CHECKS */
eed85c
+
eed85c
+G_END_DECLS
eed85c
+
eed85c
+#endif /* __GREFCOUNT_H__ */
eed85c
diff --git a/gi/wrapperutils.h b/gi/wrapperutils.h
eed85c
index 5fa4ae2f..4a67ea87 100644
eed85c
--- a/gi/wrapperutils.h
eed85c
+++ b/gi/wrapperutils.h
eed85c
@@ -31,6 +31,8 @@
eed85c
 #include "gjs/macros.h"
eed85c
 #include "util/log.h"
eed85c
 
eed85c
+#include "gi/grcbox.h"
eed85c
+
eed85c
 G_BEGIN_DECLS
eed85c
 
eed85c
 GJS_JSAPI_RETURN_CONVENTION
eed85c
diff --git a/gjs-srcs.mk b/gjs-srcs.mk
eed85c
index dcdc4387..a66f4788 100644
eed85c
--- a/gjs-srcs.mk
eed85c
+++ b/gjs-srcs.mk
eed85c
@@ -13,6 +13,12 @@ gjs_public_headers =		\
eed85c
 # public
eed85c
 
eed85c
 gjs_srcs =				\
eed85c
+	gi/garcbox.c			\
eed85c
+	gi/grcbox.c			\
eed85c
+	gi/grcbox.h			\
eed85c
+	gi/grcboxprivate.h		\
eed85c
+	gi/grefcount.c			\
eed85c
+	gi/grefcount.h			\
eed85c
 	gi/arg.cpp			\
eed85c
 	gi/arg.h			\
eed85c
 	gi/boxed.cpp			\
eed85c
-- 
eed85c
2.21.0
eed85c