Basics
Guides
API Reference
Basics
Guides
API Reference
[31:7] extends: object
A simple reference counted data type representing an immutable sequence of
zero or more bytes from an unspecified origin. The purpose of a GBytes is
to keep the memory region that it holds alive for as long as anyone holds a
reference to the bytes. When the last reference count is dropped, the memory
is released. Multiple unrelated callers can use byte data in the GBytes
without coordinating their activities, resting assured that the byte data
will not change or move while they hold a reference. A GBytes can come from
many different origins that may have different procedures for freeing the
memory region. Examples are memory from [func@GLib.malloc], from memory
slices, from a [struct@GLib.MappedFile] or memory from other allocators.
GBytes work well as keys in [struct@GLib.HashTable]. Use
[method@GLib.Bytes.equal] and [method@GLib.Bytes.hash] as parameters to
[func@GLib.HashTable.new] or [func@GLib.HashTable.new_full]. GBytes can
also be used as keys in a [struct@GLib.Tree] by passing the
[method@GLib.Bytes.compare] function to [ctor@GLib.Tree.new]. The data
pointed to by this bytes must not be modified. For a mutable array of bytes
see [struct@GLib.ByteArray]. Use [method@GLib.Bytes.unref_to_array] to create
a mutable array for a GBytes sequence. To create an immutable GBytes from
a mutable [struct@GLib.ByteArray], use the
[func@GLib.ByteArray.free_to_bytes] function.
GBytes (data = null, size = null)
Creates a new [struct@GLib.Bytes] from @data. @data is copied. If @size is 0, @data may be
NULL. As an optimization, [ctor@GLib.Bytes.new] may avoid an extra allocation by copying the data within the resulting bytes structure if sufficiently small (since GLib 2.84).
data is the data to be used for the bytes.size is the size of @data.toNativeHandle (Source)
Normalizes a constructor argument into a raw pointer carrier. Accepts a raw NativeHandle, a raw NativeBuffer returned from
fn.call(...), another generated wrapper exposinghandle(), or null. Returns null when the argument carries no pointer.
Source is the raw handle, raw buffer, wrapper, or null.A raw pointer carrier or null when no pointer is present.getLib ()
Returns the opened native library for this generated wrapper.
The opened native library.handle ()
Returns the wrapped NativeHandle.
The wrapped NativeHandle.isNull ()
Returns true when the wrapped handle is null.
A bool.describe ()
Returns a small string for debugging generated wrappers.
A string.compare (object bytes2)
Compares the two [struct@GLib.Bytes] values. This function can be used to sort
GBytesinstances in lexicographical order. If @bytes1 and @bytes2 have different length but the shorter one is a prefix of the longer one then the shorter one is considered to be less than the longer one. Otherwise the first byte where both differ is used for comparison. If
has a smaller value at that position it is considered less, otherwise greater than @bytes2.bytes2 is a pointer to a [struct@GLib.Bytes] to compare with @bytes1.a negative value if @bytes1 is less than @bytes2, a positive value ifis greater than @bytes2, and zero if @bytes1 is equal toequal (object bytes2)
Compares the two [struct@GLib.Bytes] values being pointed to and returns
TRUEif they are equal. This function can be passed to [func@GLib.HashTable.new] as the @key_equal_func parameter, when using non-NULLGBytespointers as keys in a [struct@GLib.HashTable].
bytes2 is a pointer to a [struct@GLib.Bytes] to compare with @bytes1.TRUE if the two keys match..get_data ()
Get the byte data in the [struct@GLib.Bytes]. This data should not be modified. This function will always return the same pointer for a given
GBytes.NULLmay be returned if @size is 0. This is not guaranteed, as theGBytesmay represent an empty string with @data non-NULLand
as 0. NULL will not be returned if @size is non-zero.size is location to return size of byte data.a pointer to the byte data.get_region (int element_size, int offset, int n_elements)
Gets a pointer to a region in @bytes. The region starts at @offset many bytes from the start of the data and contains @n_elements many elements of @element_size size. @n_elements may be zero, but @element_size must always be non-zero. Ideally, @element_size is a static constant (eg:
sizeofa struct). This function does careful bounds checking (including checking for arithmetic overflows) and returns a non-NULLpointer if the specified region lies entirely within the @bytes. If the region is in some way out of range, or if an overflow has occurred, thenNULLis returned. Note: it is possible to have a valid zero-size region. In this case, the returned pointer will be equal to the base pointer of the data of @bytes, plus @offset. This will be non-NULLexcept for the case where @bytes itself was a zero-sized region. Since it is unlikely that you will be using this function to check for a zero-sized region in a zero-sized @bytes,NULLeffectively always means ‘error’.
element_size is a non-zero element size.offset is an offset to the start of the region within the @bytes.n_elements is the number of elements in the region.the requested region, or NULL in case of an error.get_size ()
Get the size of the byte data in the [struct@GLib.Bytes]. This function will always return the same value for a given
GBytes.
the size.hash ()
Creates an integer hash code for the byte data in the [struct@GLib.Bytes]. This function can be passed to [func@GLib.HashTable.new] as the @key_hash_func parameter, when using non-
NULLGBytespointers as keys in a [struct@GLib.HashTable].
a hash value corresponding to the key..ref ()
Increase the reference count on @bytes.
the [struct@GLib.Bytes].unref ()
Releases a reference on @bytes. This may result in the bytes being freed. If @bytes is
NULL, it will return immediately.
None.unref_to_array ()
Unreferences the bytes, and returns a new mutable [struct@GLib.ByteArray] containing the same byte data. As an optimization, the byte data is transferred to the array without copying if this was the last reference to @bytes and @bytes was created with [ctor@GLib.Bytes.new], [ctor@GLib.Bytes.new_take] or [func@GLib.ByteArray.free_to_bytes] and the buffer was larger than the size [struct@GLib.Bytes] may internalize within its allocation. In all other cases the data is copied. Do not use it if @bytes contains more than %G_MAXUINT bytes. [struct@GLib.ByteArray] stores the length of its data in
guint, which may be shorter thangsize, that @bytes is using.
a new mutable [struct@GLib.ByteArray] containing the same byte data.unref_to_data ()
Unreferences the bytes, and returns a pointer the same byte data contents. As an optimization, the byte data is returned without copying if this was the last reference to @bytes and @bytes was created with [ctor@GLib.Bytes.new], [ctor@GLib.Bytes.new_take] or [func@GLib.ByteArray.free_to_bytes] and the buffer was larger than the size [struct@GLib.Bytes] may internalize within its allocation. In all other cases the data is copied.
size is location to place the length of the returned data.a pointer to the same byte data, which should be freed with [func@GLib.free].[279:14] static extends: object
Alternate constructors for GBytes. Usage:
GBytesCtors.<name>(...). The primary constructor lives
directly on GBytes.
newFromBytes (object bytes, int offset, int length)
Creates a [struct@GLib.Bytes] which is a subsection of another
GBytes. The @offset + @length may not be longer than the size of @bytes. A reference to @bytes will be held by the newly createdGBytesuntil the byte data is no longer needed. Since 2.56, if @offset is 0 and @length matches the size of @bytes, then @bytes will be returned with the reference count incremented by 1. If @bytes is a slice of anotherGBytes, then the resultingGByteswill reference the sameGBytesinstead of @bytes. This allows consumers to simplify the usage ofGByteswhen asynchronously writing to streams.
bytes is a [struct@GLib.Bytes].offset is offset which subsection starts at.length is length of subsection.A new GBytes.newStatic (list data)
Creates a new [struct@GLib.Bytes] from static data. @data must be static (ie: never modified or freed). It may be
NULLif @size is 0.
data is the data to be used for the bytes.size is the size of @data.A new GBytes.newTake (list data)
Creates a new [struct@GLib.Bytes] from @data. After this call, @data belongs to the
GBytesand may no longer be modified by the caller. The memory of @data has to be dynamically allocated and will eventually be freed with [func@GLib.free]. For creatingGByteswith memory from other allocators, see [ctor@GLib.Bytes.new_with_free_func]. @data may beNULLif @size is 0.
data is the data to be used for the bytes.size is the size of @data.A new GBytes.newWithFreeFunc (list data, object free_func, user_data)
Creates a [struct@GLib.Bytes] from @data. When the last reference is dropped, @free_func will be called with the @user_data argument. @data must not be modified after this call is made until @free_func has been called to indicate that the bytes is no longer in use. @data may be
NULLif @size is 0.
data is the data to be used for the bytes.size is the size of @data.free_func is the function to call to release the data.user_data is data to pass to @free_func.A new GBytes.
Aussom
Write once. Embed everywhere.
Copyright 2026 Austin Lehman. All rights reserved.