/* We already checked this is a memfd... */
g_assert_se (fstat (fd, &buf) == 0);
+ if (buf.st_size == 0)
+ {
+ g_assert_se (close (fd) == 0);
+
+ return g_bytes_new (NULL, 0);
+ }
+
bytes = g_bytes_allocate (sizeof (GBytesData), fd, buf.st_size);
- bytes->data = mmap (NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
+ bytes->data = mmap (NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (bytes->data == MAP_FAILED)
/* this is similar to malloc() failing, so do the same... */
g_error ("mmap() on memfd failed: %s\n", g_strerror (errno));
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: