[elephant-devel] SBCL make-alien limits array size details

Ian Eslick eslick at csail.mit.edu
Wed Feb 14 16:16:11 UTC 2007


 From current SBCL CVS.

   216 (defmacro make-alien (type &optional size &environment env)
   217   #!+sb-doc
   218   "Allocate an alien of type TYPE and return an alien pointer  
to it. If SIZE
   219 is supplied, how it is interpreted depends on TYPE. If TYPE is  
an array type,
   220 SIZE is used as the first dimension for the allocated array.  
If TYPE is not an
   221 array, then SIZE is the number of elements to allocate. The  
memory is
   222 allocated using ``malloc'', so it can be passed to foreign  
functions which use
   223 ``free''."
   224   (let ((alien-type (if (alien-type-p type)
   225                         type
   226                         (parse-alien-type type env))))
   227     (multiple-value-bind (size-expr element-type)
   228         (if (alien-array-type-p alien-type)
   229             (let ((dims (alien-array-type-dimensions alien- 
type)))
   230               (cond
   231                 (size
   232                  (unless dims
   233                    (error
   234                     "cannot override the size of zero- 
dimensional arrays"))
   235                  (when (constantp size)
   236                    (setf alien-type (copy-alien-array-type  
alien-type))
   237                    (setf (alien-array-type-dimensions alien-type)
   238                          (cons (constant-form-value size) (cdr  
dims)))))
   239                 (dims
   240                  (setf size (car dims)))
   241                 (t
   242                  (setf size 1)))
   243               (values `(* ,size ,@(cdr dims))
   244                       (alien-array-type-element-type alien- 
type)))
   245             (values (or size 1) alien-type))
   246       (let ((bits (alien-type-bits element-type))
   247             (alignment (alien-type-alignment element-type)))
   248         (unless bits
   249           (error "The size of ~S is unknown."
   250                  (unparse-alien-type element-type)))
   251         (unless alignment
   252           (error "The alignment of ~S is unknown."
   253                  (unparse-alien-type element-type)))
   254         ;; This is the one place where the %SAP-ALIEN note is  
quite
   255         ;; undesirable, in most uses of MAKE-ALIEN the %SAP-ALIEN
   256         ;; cannot be optimized away.
   257         `(locally (declare (muffle-conditions compiler-note))

Notice that the # of bits of the primitive is multiplied by the total  
# of elements in the
call to %make-alien

   258            (%sap-alien (%make-alien (* ,(align-offset bits  
alignment)
   259                                        ,size-expr))
   260                        ',(make-alien-pointer-type :to alien- 
type)))))))
   261
   262 ;;; Allocate a block of memory at least BITS bits long and  
return a
   263 ;;; system area pointer to it.
   264 #!-sb-fluid (declaim (inline %make-alien))
   265 (defun %make-alien (bits)
   266   (declare (type index bits))
   267   (alien-funcall (extern-alien "malloc"
   268                                (function system-area-pointer  
unsigned))
   269                  (ash (the index (+ bits 7)) -3)))

Not sure what type index is, but based on the error it must be a  
fixnum.  However the + will
fail as bits is not a fixnum.

Just FYI...

Ian




More information about the elephant-devel mailing list