const guchar *data;
gsize size;
- vector.gbytes = g_bytes_new_take_zero_copy_fd (item->memfd.fd);
+ vector.gbytes = g_bytes_new_take_zero_copy_fd_size (item->memfd.fd, item->memfd.size);
data = g_bytes_get_data (vector.gbytes, &size);
- g_assert (item->memfd.start + item->memfd.size <= size);
+ g_assert (item->memfd.size == size);
vector.data.pointer = data + item->memfd.start;
vector.size = item->memfd.size;
*
* Returns: (transfer full): a new #GBytes
*
- * Since: 2.44
*/
#ifdef G_OS_UNIX
GBytes *
GBytesData *bytes;
struct stat buf;
- g_return_val_if_fail_se (g_unix_fd_ensure_zero_copy_safe (fd), NULL);
-
/* We already checked this is a memfd... */
g_assert_se (fstat (fd, &buf) == 0);
+ bytes = g_bytes_new_take_zero_copy_fd_size(fd, buf.st_size);
+
if (buf.st_size == 0)
{
g_assert_se (close (fd) == 0);
+ }
+
+ return (GBytes *) bytes;
+}
+GBytes *
+g_bytes_new_take_zero_copy_fd_size (gint fd, gsize size)
+{
+ GBytesData *bytes;
+
+ g_return_val_if_fail_se (g_unix_fd_ensure_zero_copy_safe (fd), NULL);
+
+ /* We already checked this is a memfd... */
+ if (size == 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_PRIVATE, fd, 0);
+ bytes = g_bytes_allocate (sizeof (GBytesData), fd, size);
+ bytes->data = mmap (NULL, 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));