Sorry, because I'm still a beginner in CL and I still don't master macros, but maybe you want some code like this:
(let ((var (make-alien short 20)))
       (macroexpand-1 (list 'alien-size (cadadr (type-of var)) :bytes)))
;; => 2
;;      T
The problem is just because alien-size is a macro and not a function so I can't simply do (alien-size var :bytes).
I use cadadr because type-of on an alien value gives a type preceded with ALIEN symbol, and the next "head drop" is for the pointer type. If you eval (alien-size short :bytes) that gives you the number of bytes a foreign short type uses.

As of the second cuestion maybe you could use sb-alien:addr
(let ((var (make-alien short 20)))
       (list (addr (deref var 0)) (addr (deref var 1))))

Hope it helps you.

2013/12/26 Dmitriy Ivanov <>
Hello folks,

I wonder if a function similar to LispWorks fli:pointer-element-size exists
in SBCL. The function should return element size in bytes (or bits). If not,
how can I implement it?

For example,

(defvar *bar* (sb-alien:make-alien sb-alien:short 10))
                    :SAP #X003F4D28 :TYPE (* (SIGNED 16))>
(pointer-element-size *bar*) => 2   ; or 16

I have found the following in CFFI:

(defun %foreign-type-size (type-keyword)
  "Return the size in bytes of a foreign type."
  (/ (sb-alien-internals:alien-type-bits
       (convert-foreign-type type-keyword) nil)) 8))

But I have failed to extract a type descriptor from the alien-value
structure that is the value of *bar*.

I also need an analog of fli:incf-pointer (analog of  C  ++ptr) operation.
Its second parameter is optional and defaults to the size of the object the
pointer points to. The operator could be implemented via sb-sys:sap+ and
(setf sb-alien-internals:alien-value-sap) if the type were obtain somehow.

Dmitry Ivanov

Rapidly troubleshoot problems before they affect your business. Most IT
organizations don't have a clear picture of how application performance
affects their revenue. With AppDynamics, you get 100% visibility into your
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
Sbcl-help mailing list