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

"Hoehle, Joerg-Cyril" <[email protected]> Thu, 18 Apr 2002 18:31:18 +0200
Newsgroups gmane.lisp.uffi.general
Message-ID <[email protected]>
Hi,

Kevin Rosenberg answered:
> > (malloc 'c-string "abc") does in fact two allocations:
> > 1. one for the pointer, since char* IS a pointer
> > 2. one for the buffer contents
> 
> True.

I'd like to expand on this.

What does
(uffi:with-foreign-object (o '(* (* (:array :unsigned-char 256))))
  ...)
allocate or create?

Especially important: how much memory does it allocate?

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.
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.

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.

How does UFFI behave?

Thanks for your help,
	Jorg Hohle.