[erlang-questions] sizeof() FFI types

Raimo Niskanen <>
Thu Sep 13 09:30:23 CEST 2007


Regarding this suggestion being accepted as EEP 7...

I was a bit early adopting it. I thought it was very thought through,
and complete, but apparently it will benefit from more input from
erlang-questions to settle a bit.

Keep up working on the draft eep at http://muvara.org/crs4/erlang/ffi/
and we will see when it is appropriate to insert it in the Process.



Oh, and I have a suggestion about the actual ffi:call/2,3 functions.
Personally I would prefer an interface with tagged values:

    ffi:call(Port, {ReturnType,Function}, [TaggedVal]) ->
            {ReturnType,term()}
        ReturnType = type_tag()
        TaggedVal  = {type_tag(),Val}
        Val        = term()
        type_tag() = uchar|schar|...|pointer|size_t|ssize_t

e.g

    Pointer = {pointer,PointerVal} =
        ffi:call(Port, {pointer,malloc}, [{size_t,1024}]),
    void = ffi:call(Port, {void,free}, [Pointer]),

This would make the code more readable (resembling the C calls) IMHO,
and make the ffi values more self-contained; a return value can
directly be passed to another ffi function.



On Wed, Sep 12, 2007 at 10:54:41AM +0200, Alceste Scalas wrote:
> Il giorno mer, 12/09/2007 alle 09.21 +0200, Alceste Scalas ha scritto:
> >   * ffi:pointer_to_binary(Pointer, Size) would return a new binary()
> >     with a copy of Size bytes read from Pointer.
> 
> Well, one may even want to use the data inside the new binary --- and if
> it represents a struct or some other complex data type, one may need to
> know the sizes of the C basic types in order to perform binary matching.
> 
> This additional BIF may be useful:
> 
>       * ffi:sizeof(CType): return the number of bytes used by CType on
>         the current platform.  CType is one of the supported FFI type
>         atoms.
> 
> Summing up, this email and the previous one contain the following
> additions to the Erlang/OTP FFI proposal available on
> http://muvara.org/crs4/erlang/ffi/ :
> 
>       * 'cstring' FFI type
>       * ffi:cstring_to_binary/1 BIF
>       * ffi:pointer_to_binary/2 BIF
>       * ffi:sizeof/1 BIF
> 
> Based on your feedback, I'll add a patch and update the draft EEP.
> 
> Regards,
> 
> alceste
> -- 
> Alceste Scalas <>
> CRS4 - http://www.crs4.it/
> 
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB



More information about the erlang-questions mailing list