Re: Making FFI callbacks call free() automatically when it's collected

  • From: Johnson Lin <arch.jslin@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 20 Jul 2012 12:57:28 +0800

2012/7/20 William Adams <william_a_adams@xxxxxxx>:
>
> I did the pointer thing like the following, to try and be somewhat size 
> conscious:
>
>
>
>
> function PointerToString(instance)
>  if ffi.abi("64bit") then
>   return string.format("0x%016x", tonumber(ffi.cast("int64_t", ffi.cast("void 
> *", instance))))
>  elseif ffi.abi("32bit") then
>   return string.format("0x%08x", tonumber(ffi.cast("int32_t", ffi.cast("void 
> *", instance))))
>  end
>
>  return nil
> end
>
>

Hello, thanks for the snippet. :)

It might be better to write that in this way:

local pointer_conv
if ffi.abi(xxx) then
  pointer_conv = function ... end
elseif ffi.abi(ooo) then
  pointer_conv = function ... end
end

to make it a little bit more "compile-time thingy."

I am also not sure about the need of converting to strings, for using
it in the table keys. Does this have any advantages over using Lua
numbers as keys?

best,
Johnson

Other related posts: