[Openmcl-devel] ccl:with-pointer-to-ivector for simple-arrays

Gary Byers gb at clozure.com
Fri Jun 5 12:39:27 UTC 2009


A multidimensional array in CCL is always (at least implicitly)
displaced.  Array displacement in CL can be transitive (an array A can
be displaced to some other array B which can be displaced to a third
array C ...); whatever's at the end of this chain of displacement in
CCL is always a simple one-dimensional array.  If a multidemensional
array in CCL is a SIMPLE-ARRAY, then the multidemensional array is
directly displaced to a simple one-dimensional array (the length of
the chain of transitive displacement is exactly 1.)

CL provides a function - ARRAY-DISPLACEMENT - which will portably
return the non-null value of the :DISPLACED-TO argument to MAKE-ARRAY
(or ADJUST-ARRAY), but which may return NIL if the displacement is
implicit (and which does return a first value of NIL in this case in
CCL.)

An internal function - CCL::ARRAY-DATA-AND-OFFSET - which is based on
something that someone proposed for inclusion in CL but was never
adopted - will follow any transitive displacement chain associated
with its array argument and return the underlying simple one-dimensional
array (and the cumulative DISPLACED-INDEX-OFFSET as a second value.)

? (let* ((a (make-array (list 2 3) :element-type '(unsigned-byte 8))))
     (dotimes (i 6) (setf (row-major-aref a i) i))
     (ccl::array-data-and-offset a)) 
#(0 1 2 3 4 5) ; A is implicitly and "directly" displaced to this vector
0 ; cumulative displacement is always 0 in this case.
? (type-of *)
(SIMPLE-ARRAY (UNSIGNED-BYTE 8) (6))

A simple one-dimensional array with an "immediate" (number or
character) element-type is called an IVECTOR (the term was used in Spice Lisp
~ 25 years ago), and it's meaningful use things like CCL:WITH-POINTER-TO-IVECTOR
on any IVECTOR (and it might be reasonable of CCL:WITH-POINTER-TO-IVECTOR handled
anything transitively displaced to an IVECTOR, or if something like it did.)






On Fri, 5 Jun 2009, Vebjorn Ljosa wrote:

> Hi
>
> I have been using ccl:with-pointer-to-ivector for some time to pass
> vector contents to foreign code. Now I would like to do the same with
> two- and three-dimensional simple-arrays of (unsigned-byte 8) and
> (unsigned-byte 16).  (The arrays are images in cl-png.)  I have found
> solutions for the other major CL implementations, but ClozureCL remains.
>
> Pretending that such arrays are laid out just like ivectors doesn't
> work:
>
> (let ((a (make-array (list 2 3) :element-type '(unsigned-byte 8))))
> 	 (dotimes (i 6)
> 	   (setf (row-major-aref a i) i))
> 	 (ccl::without-gcing
> 	   (ccl:with-macptrs ((p))
> 	     (ccl::%vect-data-to-macptr a p)
> 	     (loop
> 		for i below 6
> 		collect (cffi:mem-ref p :uint8 i)))))
> => (16 0 0 0 0 0)
>
> Could someone please explain how simple-arrays of unsigned-bytes are
> laid out in memory?  Is there a pointer that I need to follow to data
> somewhere else?
>
> Thanks,
> Vebjorn
>
>
> _______________________________________________
> Openmcl-devel mailing list
> Openmcl-devel at clozure.com
> http://clozure.com/mailman/listinfo/openmcl-devel
>
>



More information about the Openmcl-devel mailing list