GBytes: add g_bytes_get_zero_copy_fd()
authorRyan Lortie <desrt@desrt.ca>
Tue, 2 Dec 2014 19:29:04 +0000 (14:29 -0500)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Fri, 10 Jul 2015 09:47:42 +0000 (11:47 +0200)
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
glib/gbytes.c
glib/gbytes.h

index 49ea6e5..d13a5ae 100644 (file)
@@ -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
index 27f56eb..266e257 100644 (file)
@@ -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:
index 459f95e..16f38aa 100644 (file)
@@ -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);