diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt
index dd851bc9e..b73ba1cc0 100644
--- a/docs/reference/glib/glib-sections.txt
+++ b/docs/reference/glib/glib-sections.txt
@@ -3477,6 +3477,7 @@ g_rc_box_dup
g_rc_box_acquire
g_rc_box_release
g_rc_box_release_full
+g_rc_box_get_size
@@ -3489,6 +3490,7 @@ g_arc_box_dup
g_arc_box_acquire
g_arc_box_release
g_arc_box_release_full
+g_arc_box_get_size
diff --git a/glib/garcbox.c b/glib/garcbox.c
index 01e0f1c56..b1581134c 100644
--- a/glib/garcbox.c
+++ b/glib/garcbox.c
@@ -354,3 +354,26 @@ g_arc_box_release_full (gpointer mem_block,
g_free (real_box);
}
}
+
+/**
+ * g_arc_box_get_size:
+ * @mem_block: (not nullable): a pointer to reference counted data
+ *
+ * Retrieves the size of the reference counted data pointed by @mem_block.
+ *
+ * Returns: the size of the data
+ *
+ * Since: 2.58
+ */
+gsize
+g_arc_box_get_size (gpointer mem_block)
+{
+ GArcBox *real_box = G_ARC_BOX (mem_block);
+
+ g_return_val_if_fail (mem_block != NULL, 0);
+#ifndef G_DISABLE_ASSERT
+ g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, 0);
+#endif
+
+ return real_box->mem_size;
+}
diff --git a/glib/grcbox.c b/glib/grcbox.c
index d09c3b7fa..5cceb87b7 100644
--- a/glib/grcbox.c
+++ b/glib/grcbox.c
@@ -424,3 +424,26 @@ g_rc_box_release_full (gpointer mem_block,
g_free (real_box);
}
}
+
+/**
+ * g_rc_box_get_size:
+ * @mem_block: (not nullable): a pointer to reference counted data
+ *
+ * Retrieves the size of the reference counted data pointed by @mem_block.
+ *
+ * Returns: the size of the data
+ *
+ * Since: 2.58
+ */
+gsize
+g_rc_box_get_size (gpointer mem_block)
+{
+ GRcBox *real_box = G_RC_BOX (mem_block);
+
+ g_return_val_if_fail (mem_block != NULL, 0);
+#ifndef G_DISABLE_ASSERT
+ g_return_val_if_fail (real_box->magic == G_BOX_MAGIC, 0);
+#endif
+
+ return real_box->mem_size;
+}
diff --git a/glib/grcbox.h b/glib/grcbox.h
index 3f364d330..a8ecce356 100644
--- a/glib/grcbox.h
+++ b/glib/grcbox.h
@@ -41,6 +41,9 @@ GLIB_AVAILABLE_IN_2_58
void g_rc_box_release_full (gpointer mem_block,
GDestroyNotify clear_func);
+GLIB_AVAILABLE_IN_2_58
+gsize g_rc_box_get_size (gpointer mem_block);
+
GLIB_AVAILABLE_IN_2_58
gpointer g_arc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
GLIB_AVAILABLE_IN_2_58
@@ -56,6 +59,9 @@ GLIB_AVAILABLE_IN_2_58
void g_arc_box_release_full (gpointer mem_block,
GDestroyNotify clear_func);
+GLIB_AVAILABLE_IN_2_58
+gsize g_arc_box_get_size (gpointer mem_block);
+
#define g_rc_box_new(type) \
((type *) g_rc_box_alloc (sizeof (type)))
#define g_rc_box_new0(type) \
diff --git a/glib/tests/rcbox.c b/glib/tests/rcbox.c
index 4a33876bc..48271a5c8 100644
--- a/glib/tests/rcbox.c
+++ b/glib/tests/rcbox.c
@@ -31,6 +31,7 @@ test_rcbox_new (void)
Point *a = g_rc_box_new (Point);
g_assert_nonnull (a);
+ g_assert_cmpuint (g_rc_box_get_size (a), ==, sizeof (Point));
g_rc_box_release (a);