Re: [snabb-devel] LuaJIT-friendly API and data structure design

  • From: Justin Cormack <justin@xxxxxxxxxxxxxxxxxxxxx>
  • To: snabb-devel@xxxxxxxxxxxxxxxx
  • Date: Thu, 19 Feb 2015 14:47:40 +0000

On 19 February 2015 at 13:54, Luke Gorrie <luke@xxxxxxxx> wrote:
> This pointer arithmetic can also lead to allocation, and it would be nice to
> avoid that. The approach that comes to mind is to require a reusable box
> (struct tcp *[1]) to be provided:
>
>     function tcp_header (packet, box)
>       box[0] = ffi.cast(tcp_t, packet.data + tcp_offset(packet.data))
>     end
>
> and maybe this is actually the basis for a general solution. I don't enjoy
> this specific "[0]" syntax so much but perhaps we could wrap it up in a Lua
> object nicely.
>
> (Again, Alex is already ahead of me on this track, and I am catching up from
> first principles.)

What I did for ljsyscall was to allow you to pass boxes to all
functions but to make them optional ie

function tcp_header (packet, box)
  box = box or ffi.new(tcp_t_1)
  box[0] = ffi.cast(tcp_t, packet.data + tcp_offset(packet.data))
  return box
end

(I have named types for the boxes as there are not many of them)

This way the user who doesnt care need to allocate a box, but the user
who wants to can, but the interface is still the same because it
returns the box anyway.

Justin

Other related posts:

  • » Re: [snabb-devel] LuaJIT-friendly API and data structure design - Justin Cormack