[cffi-devel] Re: foreign-alloc

Luis Oliveira luismbo at gmail.com
Wed Sep 7 05:54:49 UTC 2005


Martin Simmons <martin at lispworks.com> writes:
>   Luis>    CFFI> (foreign-alloc :int :initial-contents #(1 2 3))
>   Luis>    #<A Mac Pointer #x1022E0>
>   Luis>    CFFI> (loop for i from 0 below 3
>   Luis>                collect (mem-ref * :int i))
>   Luis>    (1 2 3)
>
>   Luis>    ;; Another note: what should should be in (mem-ref ** :int 3)?
>   Luis>    ;; Zero? Ie. Should these previous two calls to foreign-alloc
>   Luis>    ;; allocate 3*sizeof(int) or 4*sizeof(int) bytes? Lispworks seems to
>   Luis>    ;; do the latter.
>
> LispWorks allocates 3*sizeof(int), but the C heap manager might round this up
> to 4.  (mem-ref ** :int 3) should be expected to give a random result or a
> crash.

Yeah, that makes sense. My thoughts about 0 terminating such an array is
probably a symptom of a missing :array type.

>   Luis>    CFFI> (foreign-alloc :string :initial-element "foo")
>   Luis>    ;; I think this should do something similar to CLISP's
>   Luis>    ;; FFI:ALLOCATE-DEEP, and it should Just Work if we apply the :to-c
>   Luis>    ;; translator, which will allocate space for "foo".
>
> I think it depends on whether the :STRING type is an array type, a pointer
> type or some magic.

CFFI's :string is a pointer and the translator will allocate memory for
"foo", so will actually get a char** in this case, not a char*. Hmm..


>   Luis>    ;; A problem here is freeing this memory. Should we tell the user
>   Luis>    ;; he has to free the complex stuff inside by himself. Or should we
>   Luis>    ;; come up with something similar to CLISP's FFI:FOREIGN-FREE when
>   Luis>    ;; passed ":full t". (In this case CFFI:FOREIGN-FREE would have to
>   Luis>    ;; take an optional type or something to describe what needs
>   Luis>    ;; freeing.)
>
> Complex stuff is complex to free, so I would say that the user has to do it.

Well, I'm going with that. Experience will tell if this is needed or not.

Thanks for your feedback!


Meanwhile, I have implemented this and renamed foreign-alloc to
%foreign-alloc (and didn't export this). James, before I rename every
foreign-alloc in cffi-*.lisp do you think we should export
%foreign-alloc (and rename it to malloc) or not?


By the way, I remember now I found some bugs in Lispworks's FLI (I'm
using Lispworks 4.4.5 Personal Edition), will report them tomorrow with
some test cases.

-- 
Luis Oliveira
luismbo (@) gmail (.) com
Equipa Portuguesa do Translation Project
http://www2.iro.umontreal.ca/~pinard/po/registry.cgi?team=pt




More information about the cffi-devel mailing list