<GBytes>

<GBytes>

Description

A simple refcounted 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 g_malloc(), from memory slices, from a GMappedFile or memory from other allocators.

GBytes work well as keys in GHashTable. Use g_bytes_equal() and g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full(). GBytes can also be used as keys in a GTree by passing the g_bytes_compare() function to g_tree_new().

The data pointed to by this bytes must not be modified. For a mutable array of bytes see GByteArray. Use g_bytes_unref_to_array() to create a mutable array for a GBytes sequence. To create an immutable GBytes from a mutable GByteArray, use the g_byte_array_free_to_bytes() function.

Functions

compare

(define-values (%return) (bytes:compare self bytes2))

Compares the two GBytes values.

This function can be used to sort GBytes instances 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 bytes1 has a smaller value at that position it is considered less, otherwise greater than bytes2.

Parameters

bytes1

a pointer to a GBytes

Passed as self

bytes2

a pointer to a GBytes to compare with bytes1

Passed as bytes2

equal?

(define-values (%return) (bytes:equal? self bytes2))

Compares the two GBytes values being pointed to and returns TRUE if they are equal.

This function can be passed to g_hash_table_new() as the key_equal_func parameter, when using non-NULL GBytes pointers as keys in a GHashTable.

Parameters

bytes1

a pointer to a GBytes

Passed as self

bytes2

a pointer to a GBytes to compare with bytes1

Passed as bytes2

get-data

(define-values (%return size) (bytes:get-data self))

Get the byte data in the GBytes. This data should not be modified.

This function will always return the same pointer for a given GBytes.

NULL may be returned if size is 0. This is not guaranteed, as the GBytes may represent an empty string with data non-NULL and size as 0. NULL will not be returned if size is non-zero.

Parameters

bytes

a GBytes

Passed as self

size

location to return size of byte data

Inferred from %return

get-region

(define-values
  (%return)
  (bytes:get-region self element-size offset n-elements))

Undocumented

Parameters

bytes

Passed as self

element_size

Passed as element-size

offset

Passed as offset

n_elements

Passed as n-elements

get-size

(define-values (%return) (bytes:get-size self))

Get the size of the byte data in the GBytes.

This function will always return the same value for a given GBytes.

Parameters

bytes

a GBytes

Passed as self

hash

(define-values (%return) (bytes:hash self))

Creates an integer hash code for the byte data in the GBytes.

This function can be passed to g_hash_table_new() as the key_hash_func parameter, when using non-NULL GBytes pointers as keys in a GHashTable.

Parameters

bytes

a pointer to a GBytes key

Passed as self

new-from-bytes

(define-values (%return) (bytes:new-from-bytes self offset length))

Creates a GBytes 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 created GBytes until 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 another GBytes, then the resulting GBytes will reference the same GBytes instead of bytes. This allows consumers to simplify the usage of GBytes when asynchronously writing to streams.

Parameters

bytes

a GBytes

Passed as self

offset

offset which subsection starts at

Passed as offset

length

length of subsection

Passed as length

ref

(define-values (%return) (bytes:ref self))

Increase the reference count on bytes.

Parameters

bytes

a GBytes

Passed as self

unref

(define-values () (bytes:unref self))

Releases a reference on bytes. This may result in the bytes being freed. If bytes is NULL, it will return immediately.

Parameters

bytes

a GBytes

Passed as self

unref-to-array

(define-values (%return) (bytes:unref-to-array self))

Unreferences the bytes, and returns a new mutable GByteArray 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 g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the data is copied.

Parameters

bytes

a GBytes

Passed as self

unref-to-data

(define-values (%return size) (bytes:unref-to-data self))

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 g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the data is copied.

Parameters

bytes

a GBytes

Passed as self

size

location to place the length of the returned data

Inferred from %return

bytes:new-take

(define-values (%return) (bytes:new-take data))

Undocumented

Parameters

data

Passed as data

size

Inferred from data

bytes:new

(define-values (%return) (bytes:new data))

Undocumented

Parameters

data

Passed as data

size

Inferred from data