Search code examples
gtkcommon-lispgdk

Pointers in Lisp?


I've started learning Lisp recently and wanted to write a program which uses gtk interface. I've installed lambda-gtk bindings (on CMUCL). I want to have putpixel/getpixel ability on a pixbuf. But I found that I'm unable to direct access memory. (or just don't know how)

Function (gdk:pixbuf-get-pixels pixbuf) returns me a number - memory addr, I guess. In C++ I can easily get to the pixel I need. Is there any way to write my own putpixel in Lisp?


Solution

  • In Lisp, modern and portable way to access C libraries and to do direct memory access is CFFI.

    You can use it like this:

    >(defparameter *p* (cffi:foreign-alloc :unsigned-char :count 10))
    ;; allocate 10 bytes
    *P*
    > (setf (cffi:mem-aref *p* :unsigned-char 0) 10)
    ;; access *p* as an array of bytes and set its 0th element to 10
    10
    > (cffi:mem-aref *p* :unsigned-char 0)
    ;; access *p* as an array of bytes and take its 0th element
    10
    > (cffi:make-pointer 123)
    ;; make a pointer that points to given address
    #.(SB-SYS:INT-SAP #X0000007B)