Re: ffi.sizeof and variable length structs (VLS)

  • From: Laurent Deniau <Laurent.Deniau@xxxxxxx>
  • To: "luajit@xxxxxxxxxxxxx" <luajit@xxxxxxxxxxxxx>
  • Date: Fri, 25 May 2018 10:01:56 +0000

Hi Mike,

On 25 May 2018, at 11:50, Mike Pall <mikelj-1805@xxxxxxx> wrote:

Konstantin Stefanov wrote:
[...] This will not break exisitng semantics [...]

No, it _would_ break semantics, because the remaining arguments to
ffi.new() are considered initializers. Silently adding a VLA nelem
parameter for the 'a[]' case would change the interpretation of
the ffi.new() arguments.

As you've shown, in C you have to go through some contortions to
allocate a struct with a pseudo-VLA 'a[]'. You can certainly call
malloc() from the FFI, too. If you want that, then go for it.

The true VLA syntax 'a[?]' is an FFI extension for more comfort.
It's incompatible in many ways. sizeof() is just the most visible
difference.

You seem to be complaining that you want raw C declarations, but
the comfort of the FFI extension. Pick one.

The distinction between 'a[]' and 'a[?]' has been given a lot of
thought in the design phase of the FFI. It's intentional and it
will stay.

I agree.

On the other hand, I do not catch the reason why ffi.offsetof is JITed and not 
ffi.sizeof for VLA (according to your wiki pages). Is there any difference 
between a ffi.sizeof(struct s, N) of a VLA and the equivalent formula (at least 
in C) of ffi.offsetof(struct s, a) + N*ffi.sizeof(type_of_a) which should be 
JITed?

Best,
Laurent.

Other related posts: