opaque pointers and metatype

  • From: Cosmin Apreutesei <cosmin.apreutesei@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 2 Mar 2014 04:39:28 +0200

Hi,

What do you guys do when you have to use metatype on opaque pointers?

Take the following example:

   struct OpaqueStruct * handle;

The constructor and destructor for OpaqueStruct are defined as:

   void create(handle*);
   void destroy(handle*);

Other functions are defined as func(handle, ...) -- so without the "*".

To use metatype I have to write metatype("struct OpaqueStruct",
{__index = ...}), so that my cdata type is "OpaqueStruct*", aka
"handle".

The create/destroy wrappers thus are:

function create()
   h = ffi.new('handle[1]')
   create(h)
   return h[0]
end

function destroy()
   h = ffi.new('handle[1]')
   destroy(h)
end


Well, it seems ironic that I have to alloc memory to destroy a handle.
Am I going wrong with this approach? Are there better styles to wrap
these types of APIs? (incidentally, I'm illustrating the pthreads API)

Other related posts: