2 * Copyright © 2009, 2010 Codethink Limited
3 * Copyright © 2011 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the licence, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Ryan Lortie <desrt@desrt.ca>
19 * Stef Walter <stefw@collabora.co.uk>
26 #include <glib/garray.h>
27 #include <glib/gstrfuncs.h>
28 #include <glib/gatomic.h>
29 #include <glib/gslice.h>
30 #include <glib/gtestutils.h>
31 #include <glib/gmem.h>
32 #include <glib/gmessages.h>
37 #include <sys/types.h>
40 #include "glib-unix.h"
47 * A simple refcounted data type representing an immutable sequence of zero or
48 * more bytes from an unspecified origin.
50 * The purpose of a #GBytes is to keep the memory region that it holds
51 * alive for as long as anyone holds a reference to the bytes. When
52 * the last reference count is dropped, the memory is released. Multiple
53 * unrelated callers can use byte data in the #GBytes without coordinating
54 * their activities, resting assured that the byte data will not change or
55 * move while they hold a reference.
57 * A #GBytes can come from many different origins that may have
58 * different procedures for freeing the memory region. Examples are
59 * memory from g_malloc(), from memory slices, from a #GMappedFile or
60 * memory from other allocators.
62 * #GBytes work well as keys in #GHashTable. Use g_bytes_equal() and
63 * g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
64 * #GBytes can also be used as keys in a #GTree by passing the g_bytes_compare()
65 * function to g_tree_new().
67 * The data pointed to by this bytes must not be modified. For a mutable
68 * array of bytes see #GByteArray. Use g_bytes_unref_to_array() to create a
69 * mutable array for a #GBytes sequence. To create an immutable #GBytes from
70 * a mutable #GByteArray, use the g_byte_array_free_to_bytes() function.
85 #if GLIB_SIZEOF_SIZE_T == 4
92 /* important: the ->data field of GBytesInline should always be 'nicely
95 G_STATIC_ASSERT (G_STRUCT_OFFSET (GBytesInline, data) % (2 * sizeof (gpointer)) == 0);
96 G_STATIC_ASSERT (G_STRUCT_OFFSET (GBytesInline, data) % 8 == 0);
108 GBytesData data_bytes;
110 GDestroyNotify notify;
114 #define G_BYTES_TYPE_INLINE (-1)
115 #define G_BYTES_TYPE_STATIC (-2)
116 #define G_BYTES_TYPE_FREE (-3)
117 #define G_BYTES_TYPE_NOTIFY (-4)
119 /* All bytes are either inline or subtypes of GBytesData */
120 #define G_BYTES_IS_INLINE(bytes) ((bytes)->type_or_fd == G_BYTES_TYPE_INLINE)
121 #define G_BYTES_IS_DATA(bytes) (!G_BYTES_IS_INLINE(bytes))
123 /* More specific subtypes of GBytesData */
124 #define G_BYTES_IS_STATIC(bytes) ((bytes)->type_or_fd == G_BYTES_TYPE_STATIC)
125 #define G_BYTES_IS_FREE(bytes) ((bytes)->type_or_fd == G_BYTES_TYPE_FREE)
126 #define G_BYTES_IS_NOTIFY(bytes) ((bytes)->type_or_fd == G_BYTES_TYPE_NOTIFY)
128 /* we have a memfd if type_or_fd >= 0 */
129 #define G_BYTES_IS_MEMFD(bytes) ((bytes)->type_or_fd >= 0)
132 g_bytes_allocate (guint struct_size,
138 bytes = g_slice_alloc (struct_size);
139 bytes->size = data_size;
140 bytes->ref_count = 1;
141 bytes->type_or_fd = type_or_fd;
148 * @data: (transfer none) (array length=size) (element-type guint8) (allow-none):
149 * the data to be used for the bytes
150 * @size: the size of @data
152 * Creates a new #GBytes from @data.
154 * @data is copied. If @size is 0, @data may be %NULL.
156 * Returns: (transfer full): a new #GBytes
161 g_bytes_new (gconstpointer data,
166 g_return_val_if_fail (data != NULL || size == 0, NULL);
168 bytes = g_bytes_allocate (G_STRUCT_OFFSET (GBytesInline, data[size]), G_BYTES_TYPE_INLINE, size);
169 memcpy (bytes->data, data, size);
171 return (GBytes *) bytes;
175 * g_bytes_new_take_zero_copy_fd:
176 * @fd: a file descriptor capable of being zero-copy-safe
178 * Creates a new #GBytes from @fd.
180 * @fd must be capable of being made zero-copy-safe. In concrete terms,
181 * this means that a call to g_unix_fd_ensure_zero_copy_safe() on @fd
182 * will succeed. This call will be made before returning.
184 * This call consumes @fd, transferring ownership to the returned
187 * Returns: (transfer full): a new #GBytes
193 g_bytes_new_take_zero_copy_fd (gint fd)
198 g_return_val_if_fail_se (g_unix_fd_ensure_zero_copy_safe (fd), NULL);
200 /* We already checked this is a memfd... */
201 g_assert_se (fstat (fd, &buf) == 0);
203 bytes = g_bytes_allocate (sizeof (GBytesData), fd, buf.st_size);
204 bytes->data = mmap (NULL, buf.st_size, PROT_READ, MAP_SHARED, fd, 0);
205 if (bytes->data == MAP_FAILED)
206 /* this is similar to malloc() failing, so do the same... */
207 g_error ("mmap() on memfd failed: %s\n", g_strerror (errno));
209 return (GBytes *) bytes;
211 #endif /* G_OS_UNIX */
215 * @data: (transfer full) (array length=size) (element-type guint8) (allow-none):
216 the data to be used for the bytes
217 * @size: the size of @data
219 * Creates a new #GBytes from @data.
221 * After this call, @data belongs to the bytes and may no longer be
222 * modified by the caller. g_free() will be called on @data when the
223 * bytes is no longer in use. Because of this @data must have been created by
224 * a call to g_malloc(), g_malloc0() or g_realloc() or by one of the many
225 * functions that wrap these calls (such as g_new(), g_strdup(), etc).
227 * For creating #GBytes with memory from other allocators, see
228 * g_bytes_new_with_free_func().
230 * @data may be %NULL if @size is 0.
232 * Returns: (transfer full): a new #GBytes
237 g_bytes_new_take (gpointer data,
242 bytes = g_bytes_allocate (sizeof (GBytesNotify), G_BYTES_TYPE_FREE, size);
245 return (GBytes *) bytes;
249 * g_bytes_new_static: (skip)
250 * @data: (transfer full) (array length=size) (element-type guint8) (allow-none):
251 the data to be used for the bytes
252 * @size: the size of @data
254 * Creates a new #GBytes from static data.
256 * @data must be static (ie: never modified or freed). It may be %NULL if @size
259 * Returns: (transfer full): a new #GBytes
264 g_bytes_new_static (gconstpointer data,
269 g_return_val_if_fail (data != NULL || size == 0, NULL);
271 bytes = g_bytes_allocate (sizeof (GBytesData), G_BYTES_TYPE_STATIC, size);
272 bytes->data = (gpointer) data;
274 return (GBytes *) bytes;
278 * g_bytes_new_with_free_func:
279 * @data: (array length=size) (allow-none): the data to be used for the bytes
280 * @size: the size of @data
281 * @free_func: the function to call to release the data
282 * @user_data: data to pass to @free_func
284 * Creates a #GBytes from @data.
286 * When the last reference is dropped, @free_func will be called with the
287 * @user_data argument.
289 * @data must not be modified after this call is made until @free_func has
290 * been called to indicate that the bytes is no longer in use.
292 * @data may be %NULL if @size is 0.
294 * Returns: (transfer full): a new #GBytes
299 g_bytes_new_with_free_func (gconstpointer data,
301 GDestroyNotify free_func,
307 return g_bytes_new_static (data, size);
309 bytes = g_bytes_allocate (sizeof (GBytesNotify), G_BYTES_TYPE_NOTIFY, size);
310 bytes->data_bytes.data = (gpointer) data;
311 bytes->notify = free_func;
312 bytes->user_data = user_data;
314 return (GBytes *) bytes;
318 * g_bytes_new_from_bytes:
320 * @offset: offset which subsection starts at
321 * @length: length of subsection
323 * Creates a #GBytes which is a subsection of another #GBytes. The @offset +
324 * @length may not be longer than the size of @bytes.
326 * A reference to @bytes will be held by the newly created #GBytes until
327 * the byte data is no longer needed.
329 * Returns: (transfer full): a new #GBytes
334 g_bytes_new_from_bytes (GBytes *bytes,
338 /* Note that length may be 0. */
339 g_return_val_if_fail (bytes != NULL, NULL);
340 g_return_val_if_fail (offset <= bytes->size, NULL);
341 g_return_val_if_fail (offset + length <= bytes->size, NULL);
343 return g_bytes_new_with_free_func ((gchar *) g_bytes_get_data (bytes, NULL) + offset, length,
344 (GDestroyNotify)g_bytes_unref, g_bytes_ref (bytes));
350 * @size: (out) (allow-none): location to return size of byte data
352 * Get the byte data in the #GBytes. This data should not be modified.
354 * This function will always return the same pointer for a given #GBytes.
356 * %NULL may be returned if @size is 0. This is not guaranteed, as the #GBytes
357 * may represent an empty string with @data non-%NULL and @size as 0. %NULL will
358 * not be returned if @size is non-zero.
360 * Returns: (transfer none) (array length=size) (type guint8) (allow-none): a pointer to the
361 * byte data, or %NULL
366 g_bytes_get_data (GBytes *bytes,
369 g_return_val_if_fail (bytes != NULL, NULL);
374 if (G_BYTES_IS_DATA (bytes))
376 GBytesData *data_bytes = (GBytesData *) bytes;
378 return data_bytes->data;
380 else if (G_BYTES_IS_INLINE (bytes))
382 GBytesInline *inline_bytes = (GBytesInline *) bytes;
384 return inline_bytes->data;
387 g_assert_not_reached ();
394 * Get the size of the byte data in the #GBytes.
396 * This function will always return the same value for a given #GBytes.
403 g_bytes_get_size (GBytes *bytes)
405 g_return_val_if_fail (bytes != NULL, 0);
410 * g_bytes_get_zero_copy_fd:
413 * Gets the zero-copy fd from a #GBytes, if it has one.
415 * Returns -1 if @bytes was not created from a zero-copy fd.
417 * A #GBytes created with a zero-copy fd may have been internally
418 * converted into another type of #GBytes for any reason at all. This
419 * function may therefore return -1 at any time, even for a #GBytes that
420 * was created with g_bytes_new_take_zero_copy_fd().
422 * The returned file descriptor belongs to @bytes. Do not close it.
424 * Returns: a file descriptor, or -1
429 g_bytes_get_zero_copy_fd (GBytes *bytes)
431 g_return_val_if_fail (bytes != NULL, -1);
433 if (G_BYTES_IS_MEMFD (bytes))
434 return bytes->type_or_fd;
443 * Increase the reference count on @bytes.
445 * Returns: the #GBytes
450 g_bytes_ref (GBytes *bytes)
452 g_return_val_if_fail (bytes != NULL, NULL);
454 g_atomic_int_inc (&bytes->ref_count);
461 * @bytes: (allow-none): a #GBytes
463 * Releases a reference on @bytes. This may result in the bytes being
469 g_bytes_unref (GBytes *bytes)
474 if (g_atomic_int_dec_and_test (&bytes->ref_count))
476 switch (bytes->type_or_fd)
478 case G_BYTES_TYPE_STATIC:
479 /* data does not need to be freed */
480 g_slice_free (GBytesData, (GBytesData *) bytes);
483 case G_BYTES_TYPE_INLINE:
484 /* data will be freed along with struct */
485 g_slice_free1 (G_STRUCT_OFFSET (GBytesInline, data[bytes->size]), bytes);
488 case G_BYTES_TYPE_FREE:
490 GBytesData *data_bytes = (GBytesData *) bytes;
492 g_free (data_bytes->data);
494 g_slice_free (GBytesData, data_bytes);
498 case G_BYTES_TYPE_NOTIFY:
500 GBytesNotify *notify_bytes = (GBytesNotify *) bytes;
502 /* We don't create GBytesNotify if callback was NULL */
503 (* notify_bytes->notify) (notify_bytes->user_data);
505 g_slice_free (GBytesNotify, notify_bytes);
511 GBytesData *data_bytes = (GBytesData *) bytes;
513 g_assert (bytes->type_or_fd >= 0);
515 g_assert_se (munmap (data_bytes->data, bytes->size) == 0);
516 g_assert_se (close (bytes->type_or_fd) == 0);
518 g_slice_free (GBytesData, data_bytes);
527 * @bytes1: (type GLib.Bytes): a pointer to a #GBytes
528 * @bytes2: (type GLib.Bytes): a pointer to a #GBytes to compare with @bytes1
530 * Compares the two #GBytes values being pointed to and returns
531 * %TRUE if they are equal.
533 * This function can be passed to g_hash_table_new() as the @key_equal_func
534 * parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
536 * Returns: %TRUE if the two keys match.
541 g_bytes_equal (gconstpointer bytes1,
542 gconstpointer bytes2)
544 gconstpointer d1, d2;
547 g_return_val_if_fail (bytes1 != NULL, FALSE);
548 g_return_val_if_fail (bytes2 != NULL, FALSE);
550 d1 = g_bytes_get_data ((GBytes *) bytes1, &s1);
551 d2 = g_bytes_get_data ((GBytes *) bytes2, &s2);
559 return memcmp (d1, d2, s1) == 0;
564 * @bytes: (type GLib.Bytes): a pointer to a #GBytes key
566 * Creates an integer hash code for the byte data in the #GBytes.
568 * This function can be passed to g_hash_table_new() as the @key_hash_func
569 * parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
571 * Returns: a hash value corresponding to the key.
576 g_bytes_hash (gconstpointer bytes)
583 g_return_val_if_fail (bytes != NULL, 0);
585 data = g_bytes_get_data ((GBytes *) bytes, &size);
589 h = (h << 5) + h + *(data++);
596 * @bytes1: (type GLib.Bytes): a pointer to a #GBytes
597 * @bytes2: (type GLib.Bytes): a pointer to a #GBytes to compare with @bytes1
599 * Compares the two #GBytes values.
601 * This function can be used to sort GBytes instances in lexographical order.
603 * Returns: a negative value if bytes2 is lesser, a positive value if bytes2 is
604 * greater, and zero if bytes2 is equal to bytes1
609 g_bytes_compare (gconstpointer bytes1,
610 gconstpointer bytes2)
612 gconstpointer d1, d2;
616 g_return_val_if_fail (bytes1 != NULL, 0);
617 g_return_val_if_fail (bytes2 != NULL, 0);
619 d1 = g_bytes_get_data ((GBytes *) bytes1, &s1);
620 d2 = g_bytes_get_data ((GBytes *) bytes2, &s2);
622 ret = memcmp (d1, d2, MIN (s1, s2));
623 if (ret == 0 && s1 != s2)
624 ret = s1 < s2 ? -1 : 1;
630 * g_bytes_unref_to_data:
631 * @bytes: (transfer full): a #GBytes
632 * @size: location to place the length of the returned data
634 * Unreferences the bytes, and returns a pointer the same byte data
637 * As an optimization, the byte data is returned without copying if this was
638 * the last reference to bytes and bytes was created with g_bytes_new(),
639 * g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the
642 * Returns: (transfer full): a pointer to the same byte data, which should
643 * be freed with g_free()
648 g_bytes_unref_to_data (GBytes *bytes,
653 g_return_val_if_fail (bytes != NULL, NULL);
654 g_return_val_if_fail (size != NULL, NULL);
658 * Optimal path: if this is was the last reference, then we can return
659 * the data from this GBytes without copying.
661 if (G_BYTES_IS_FREE(bytes) && g_atomic_int_get (&bytes->ref_count) == 1)
663 GBytesData *data_bytes = (GBytesData *) bytes;
665 result = data_bytes->data;
668 g_slice_free (GBytesData, data_bytes);
674 data = g_bytes_get_data (bytes, size);
675 result = g_memdup (data, *size);
676 g_bytes_unref (bytes);
683 * g_bytes_unref_to_array:
684 * @bytes: (transfer full): a #GBytes
686 * Unreferences the bytes, and returns a new mutable #GByteArray containing
687 * the same byte data.
689 * As an optimization, the byte data is transferred to the array without copying
690 * if this was the last reference to bytes and bytes was created with
691 * g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all
692 * other cases the data is copied.
694 * Returns: (transfer full): a new mutable #GByteArray containing the same byte data
699 g_bytes_unref_to_array (GBytes *bytes)
704 g_return_val_if_fail (bytes != NULL, NULL);
706 data = g_bytes_unref_to_data (bytes, &size);
707 return g_byte_array_new_take (data, size);