From 25b35c2031088fb40fd37ace2ac7e1556b3662ae Mon Sep 17 00:00:00 2001 From: Ryan Lortie Date: Tue, 2 Dec 2014 14:29:04 -0500 Subject: [PATCH] GBytes: add g_bytes_get_zero_copy_fd() Add a way to get the zero-copy fd back out of a GBytes that was created from one. --- docs/reference/glib/glib-sections.txt | 1 + glib/gbytes.c | 29 +++++++++++++++++++++++++++++ glib/gbytes.h | 3 +++ 3 files changed, 33 insertions(+) diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 49ea6e5..d13a5ae 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -2479,6 +2479,7 @@ g_bytes_new_with_free_func g_bytes_new_from_bytes g_bytes_get_data g_bytes_get_size +g_bytes_get_zero_copy_fd g_bytes_hash g_bytes_equal g_bytes_compare diff --git a/glib/gbytes.c b/glib/gbytes.c index 27f56eb..266e257 100644 --- a/glib/gbytes.c +++ b/glib/gbytes.c @@ -406,6 +406,35 @@ g_bytes_get_size (GBytes *bytes) return bytes->size; } +/** + * g_bytes_get_zero_copy_fd: + * @bytes: a #GBytes + * + * Gets the zero-copy fd from a #GBytes, if it has one. + * + * Returns -1 if @bytes was not created from a zero-copy fd. + * + * A #GBytes created with a zero-copy fd may have been internally + * converted into another type of #GBytes for any reason at all. This + * function may therefore return -1 at any time, even for a #GBytes that + * was created with g_bytes_new_take_zero_copy_fd(). + * + * The returned file descriptor belongs to @bytes. Do not close it. + * + * Returns: a file descriptor, or -1 + * + * Since: 2.44 + */ +gint +g_bytes_get_zero_copy_fd (GBytes *bytes) +{ + g_return_val_if_fail (bytes != NULL, -1); + + if (G_BYTES_IS_MEMFD (bytes)) + return bytes->type_or_fd; + else + return -1; +} /** * g_bytes_ref: diff --git a/glib/gbytes.h b/glib/gbytes.h index 459f95e..16f38aa 100644 --- a/glib/gbytes.h +++ b/glib/gbytes.h @@ -66,6 +66,9 @@ gconstpointer g_bytes_get_data (GBytes *bytes, GLIB_AVAILABLE_IN_ALL gsize g_bytes_get_size (GBytes *bytes); +GLIB_AVAILABLE_IN_2_44 +gint g_bytes_get_zero_copy_fd (GBytes *bytes); + GLIB_AVAILABLE_IN_ALL GBytes * g_bytes_ref (GBytes *bytes); -- 2.7.4