1.4 <GBytes>

Structure: <GBytes>

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 malloc, from memory slices, from a <GMappedFile> or memory from other allocators.

<GBytes> work well as keys in <GHashTable>. Use bytes:equal? and bytes:hash as parameters to GLib.HashTable.new or GLib.HashTable.new_full. <GBytes> can also be used as keys in a <GTree> by passing the bytes:compare function to GLib.Tree.new.

The data pointed to by this bytes must not be modified. For a mutable array of bytes see <GByteArray>. Use bytes:unref-to-array to create a mutable array for a <GBytes> sequence. To create an immutable <GBytes> from a mutable <GByteArray>, use the byte-array:free-to-bytes function.

Procedures

Method: bytes:compare self bytes2 ⇒ %return
Method: compare self bytes2 ⇒ %return

Compares the two <GBytes> values.

This function can be used to sort <GBytes> instances in lexicographical order.

If self 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 self has a smaller value at that position it is considered less, otherwise greater than bytes2.

self

Passed: a <GBytes>.

a pointer to a <GBytes>

bytes2

Passed: a <GBytes>.

a pointer to a <GBytes> to compare with self

%return

Returned: a 32-bit integer.

a negative value if self is less than bytes2, a positive value if self is greater than bytes2, and zero if self is equal to bytes2

Method: bytes:equal? self bytes2 ⇒ %return
Method: equal? self bytes2 ⇒ %return

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

This function can be passed to GLib.HashTable.new as the key-equal-func parameter, when using non-NULL <GBytes> pointers as keys in a <GHashTable>.

self

Passed: a <GBytes>.

a pointer to a <GBytes>

bytes2

Passed: a <GBytes>.

a pointer to a <GBytes> to compare with self

%return

Returned: a boolean.

TRUE if the two keys match.

Method: bytes:get-data self ⇒ %return
Method: get-data self ⇒ %return

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.

self

Passed: a <GBytes>.

a <GBytes>

%return

Returned: a bytevector, or #f.

a pointer to the byte data

size

Not returned: it is the length of %return.

location to return size of byte data

Method: bytes:get-region self element-size offset n-elements ⇒ %return
Method: get-region self element-size offset n-elements ⇒ %return

Gets a pointer to a region in self.

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: sizeof a struct).

This function does careful bounds checking (including checking for arithmetic overflows) and returns a non-NULL pointer if the specified region lies entirely within the self. If the region is in some way out of range, or if an overflow has occurred, then NULL is 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 self, plus offset. This will be non-NULL except for the case where self 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 self, NULL effectively always means ‘error’.

self

Passed: a <GBytes>.

a <GBytes>

element-size

Passed: an unsigned 64-bit integer.

a non-zero element size

offset

Passed: an unsigned 64-bit integer.

an offset to the start of the region within the self

n-elements

Passed: an unsigned 64-bit integer.

the number of elements in the region

%return

Returned: a foreign pointer, or #f.

the requested region, or NULL in case of an error

Method: bytes:get-size self ⇒ %return
Method: get-size self ⇒ %return

Get the size of the byte data in the <GBytes>.

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

self

Passed: a <GBytes>.

a <GBytes>

%return

Returned: an unsigned 64-bit integer.

the size

Method: bytes:hash self ⇒ %return
Method: hash self ⇒ %return

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

This function can be passed to GLib.HashTable.new as the key-hash-func parameter, when using non-NULL <GBytes> pointers as keys in a <GHashTable>.

self

Passed: a <GBytes>.

a pointer to a <GBytes> key

%return

Returned: an unsigned 32-bit integer.

a hash value corresponding to the key.

Procedure: bytes:new data ⇒ %return

Creates a new <GBytes> from data.

data is copied. If size is 0, data may be NULL.

As an optimization, bytes:new may avoid an extra allocation by copying the data within the resulting bytes structure if sufficiently small (since GLib 2.84).

data

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer, or #f.

the data to be used for the bytes

%return

Returned: a <GBytes>.

a new <GBytes>

size

Not passed: guile-gi gives the length of data.

the size of data

Procedure: bytes:new-from-bytes bytes offset length ⇒ %return

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.

bytes

Passed: a <GBytes>.

a <GBytes>

offset

Passed: an unsigned 64-bit integer.

offset which subsection starts at

length

Passed: an unsigned 64-bit integer.

length of subsection

%return

Returned: a <GBytes>.

a new <GBytes>

Procedure: bytes:new-take data ⇒ %return

Creates a new <GBytes> from data.

After this call, data belongs to the <GBytes> and may no longer be modified by the caller. The memory of data has to be dynamically allocated and will eventually be freed with byte-array:free.

For creating <GBytes> with memory from other allocators, see GLib.Bytes.new_with_free_func.

data may be NULL if size is 0.

data

Passed: a bytevector, or a list or vector, each an unsigned 8-bit integer, or #f.

the data to be used for the bytes

%return

Returned: a <GBytes>.

a new <GBytes>

size

Not passed: guile-gi gives the length of data.

the size of data

Method: bytes:ref self ⇒ %return
Method: ref self ⇒ %return

Increase the reference count on self.

self

Passed: a <GBytes>.

a <GBytes>

%return

Returned: a <GBytes>.

the <GBytes>

Method: bytes:unref self
Method: unref self

Releases a reference on self.

This may result in the bytes being freed. If self is NULL, it will return immediately.

self

Passed: a <GBytes>.

a <GBytes>

Method: bytes:unref-to-array self ⇒ %return
Method: unref-to-array self ⇒ %return

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 self and self was created with bytes:new, bytes:new-take or byte-array:free-to-bytes and the buffer was larger than the size <GBytes> may internalize within its allocation. In all other cases the data is copied.

Do not use it if self contains more than G_MAXUINT bytes. <GByteArray> stores the length of its data in guint, which may be shorter than gsize, that self is using.

self

Passed: a <GBytes>.

a <GBytes>

%return

Returned: a bytevector.

a new mutable <GByteArray> containing the same byte data

Method: bytes:unref-to-data self ⇒ %return
Method: unref-to-data self ⇒ %return

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 self and self was created with bytes:new, bytes:new-take or byte-array:free-to-bytes and the buffer was larger than the size <GBytes> may internalize within its allocation. In all other cases the data is copied.

self

Passed: a <GBytes>.

a <GBytes>

%return

Returned: a bytevector.

a pointer to the same byte data, which should be freed with byte-array:free

size

Not returned: it is the length of %return.

location to place the length of the returned data