summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Withnall <philip@tecnocode.co.uk>2023-04-24 12:13:16 +0000
committerPhilip Withnall <philip@tecnocode.co.uk>2023-04-24 12:13:16 +0000
commitfd493cacf26ed65ff307ca146e7060384c012d6f (patch)
treeeb715b626c572733c5821830e8296919a1df8145
parent28b1b9d9c3cd1a7cebe9eac53dc49941eb47298c (diff)
parente2e93056f2498405748f8ee2046f32e82db960be (diff)
downloadglib-fd493cacf26ed65ff307ca146e7060384c012d6f.tar.gz
Merge branch 'ebassi/refcount-init-macros' into 'main'
Add init macros for refcounting types See merge request GNOME/glib!3366
-rw-r--r--docs/reference/glib/glib-sections.txt.in2
-rw-r--r--glib/grefcount.h54
2 files changed, 56 insertions, 0 deletions
diff --git a/docs/reference/glib/glib-sections.txt.in b/docs/reference/glib/glib-sections.txt.in
index 3703198bd..a674f8320 100644
--- a/docs/reference/glib/glib-sections.txt.in
+++ b/docs/reference/glib/glib-sections.txt.in
@@ -3689,12 +3689,14 @@ g_ref_count_init
g_ref_count_inc
g_ref_count_dec
g_ref_count_compare
+G_REF_COUNT_INIT
<SUBSECTION>
gatomicrefcount
g_atomic_ref_count_init
g_atomic_ref_count_inc
g_atomic_ref_count_dec
g_atomic_ref_count_compare
+G_ATOMIC_REF_COUNT_INIT
</SECTION>
<SECTION>
diff --git a/glib/grefcount.h b/glib/grefcount.h
index 88fc716e7..53b96932e 100644
--- a/glib/grefcount.h
+++ b/glib/grefcount.h
@@ -50,6 +50,60 @@ GLIB_AVAILABLE_IN_2_58
gboolean g_atomic_ref_count_compare (gatomicrefcount *arc,
gint val);
+/**
+ * G_REF_COUNT_INIT:
+ *
+ * Evaluates to the initial reference count for `grefcount`.
+ *
+ * This macro is useful for initializing `grefcount` fields inside
+ * structures, for instance:
+ *
+ * |[<!-- language="C" -->
+ * typedef struct {
+ * grefcount ref_count;
+ * char *name;
+ * char *address;
+ * } Person;
+ *
+ * static const Person default_person = {
+ * .ref_count = G_REF_COUNT_INIT,
+ * .name = "Default name",
+ * .address = "Default address",
+ * };
+ * ]|
+ *
+ * Since: 2.78
+ */
+#define G_REF_COUNT_INIT -1 \
+ GLIB_AVAILABLE_MACRO_IN_2_78
+
+/**
+ * G_ATOMIC_REF_COUNT_INIT:
+ *
+ * Evaluates to the initial reference count for `gatomicrefcount`.
+ *
+ * This macro is useful for initializing `gatomicrefcount` fields inside
+ * structures, for instance:
+ *
+ * |[<!-- language="C" -->
+ * typedef struct {
+ * gatomicrefcount ref_count;
+ * char *name;
+ * char *address;
+ * } Person;
+ *
+ * static const Person default_person = {
+ * .ref_count = G_ATOMIC_REF_COUNT_INIT,
+ * .name = "Default name",
+ * .address = "Default address",
+ * };
+ * ]|
+ *
+ * Since: 2.78
+ */
+#define G_ATOMIC_REF_COUNT_INIT 1 \
+ GLIB_AVAILABLE_MACRO_IN_2_78
+
/* On GCC we can use __extension__ to inline the API without using
* ancillary functions; we only do this when disabling checks, as
* it disables warnings when saturating the reference counters