Package: glib

GBoxed glib:bytes

Declaration

(define-gboxed-opaque bytes "GBytes"
  :export t
  :type-initializer "g_bytes_get_type"
  :alloc (%bytes-new (cffi:null-pointer) 0))  

Details

The g:bytes structure is a simple refcounted data type representing an immutable sequence of zero or more bytes from an unspecified origin. The purpose of a g:bytes instance 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 g:bytes instance without coordinating their activities, resting assured that the byte data will not change or move while they hold a reference.

A g:bytes instance can come from many different origins that may have different procedures for freeing the memory region. Examples are memory from the g:malloc function.

Examples

Using a g:bytes instance for a Lisp string as byte data.
(multiple-value-bind (data len)
    (foreign-string-alloc "A test string.")
  (defvar bytes (g:bytes-new data len)))
=> BYTES
(g:bytes-size bytes)
=> 15
(g:bytes-data bytes)
=> #.(SB-SYS:INT-SAP #X55EBB2C1DB70)
=> 15
(foreign-string-to-lisp (g:bytes-data bytes))
=> "A test string."
=> 14    
Using a g:bytes instance to allocate the memory for a paintable with 110 x 80 pixels and 4 bytes per pixel.
(let* ((size (* 4 110 80))
       (data (cffi:foreign-alloc :uchar :count size :initial-element #xff))
       (bytes (g:bytes-new data size))
       (texture (gdk:memory-texture-new 110 70
                                        :B8G8R8A8-PREMULTIPLIED
                                        bytes
                                        (* 4 110)))
       (picture (gtk:picture-new-for-paintable texture)))
... )    
 

Returned by

2024-11-6