Re: allocating (* (:array foo 256)): a complex structure or just a si ngle pointer?

Kevin Rosenberg <kevin-HJRc7zDS/[email protected]> Thu, 18 Apr 2002 16:33:21 -0600
Newsgroups gmane.lisp.uffi.general
Message-ID <[email protected]>
On Thu, Apr 18, 2002 at 06:31:18PM +0200, Hoehle, Joerg-Cyril wrote:
> What does
> (uffi:with-foreign-object (o '(* (* (:array :unsigned-char 256))))
>   ...)
> allocate or create?

A great questions. It is unspecified since it will be implementation
dependant. UFFI is mostly a macro wrapper around native FFI's.  For
allocating a foreign object, the type specifiers undergo simple
conversion to the native FFI's type specifiers. Doing a (macroexpand-1)
on this form will show that the conversion for the native FFI.

Now, I haven't tested to see what each implementation will do with this.
Reading through the documentation of the native FFI's, I haven't seen
an example like this of allocating a pointer to a sized array. I wouldn't be
surprised if different implementations handled this differently.

Two choices that I see, have UFFI deconstruct this and feed the components
to each implementation to ensure consistent behavior. Or, worse is better,
make the user do it.

> A simple mallocator() would say: I'm going to allocate no more than 4 bytes for the top-level pointer. Like in C, sizeof(char*)==4 (on many machines).
> 
> A complex recursive thing would do: Allocate a buffer for the array, then a pointer to this buffer, then a pointer to that pointer: 3 allocations, 256+4+4.
> 

> The latter is already available in CLISP's FFI, but only when given
> an initial value (i.e. an array in Lisp) -- BTW, a nice extension IMHO
> for UFFI.

Yes, that would be a nice extension.

> The former I have written for CLISP, but I consider it dangerous.
> 
> Especially,
> (uffi:with-foreign-object (o '(* (:array :unsigned-char 256)))
>   ... might not do what one may expect, may it?
> 
> With my simple allocator, it would just allocate a single pointer, initialized to NULL.

That may be exactly what most implementations may do as well. If you say
in CMUCL
(alien:with-alien (a '(* (c-call:array (alien:unsigned 8) 256)))
 ...
You may get just a single pointer.

> To make things clear:
> (uffi:with-foreign-object (name '(:array :unsigned-char 256))
>   ...) is *not* confusing me. This allocates 256 bytes.
> I've only problems with pointers to such arrays.

Yeah, the issue is knowing the intent of the user.

> How does UFFI behave?

It follows the behavior of the underlying implementations. So, it may
vary across implementations. This is *not* desirable!

-- 
Kevin Rosenberg
kevin-HJRc7zDS/[email protected]
signature.asc (application/pgp-signature, 232 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE8v0mwES7N8sSjgj4RAuVyAJ9ey05zdxUKKAHaslbc55tF3aQTHACfdtfW
o6cFvywgDSrhu/T2cYlThK8=
=mAkZ
-----END PGP SIGNATURE-----