in reply to Wrapping C constructor with opaque pointer using Platypus

Hello there. Unfortunately I can't find the Alien::igraph, did you post that somewhere? No matter though.

From a cursory look at the igraph library, it seems igraph API expects you to manage the allocation of igraph_t structure on your own. Either a static or malloc'ed struct, but you have to allocate it somewhere. From this I presume you'd want a chunk (sizeof(igraph_t)) of opaque data in your perl object, not an opaque pointer. HtH.

Replies are listed 'Best First'.
Re^2: Wrapping C constructor with opaque pointer using Platypus
by pokki (Monk) on Feb 13, 2017 at 09:41 UTC

    Sorry for lateness, I was away on week-end.

    Hello there. Unfortunately I can't find the Alien::igraph, did you post that somewhere? No matter though.

    It's on a bitbucket repository only right now: https://bitbucket.org/fgabolde/alien-igraph

    From a cursory look at the igraph library, it seems igraph API expects you to manage the allocation of igraph_t structure on your own. Either a static or malloc'ed struct, but you have to allocate it somewhere. From this I presume you'd want a chunk (sizeof(igraph_t)) of opaque data in your perl object, not an opaque pointer. HtH.

    Like I said, I don't know much about C, but it does not look like the library expects me to allocate memory. From the API docs:

    igraph_t g; igraph_empty(&g, 0, 1);

    seems to be enough to get an initialized graph in g. If this were XS I'd just do that but I don't really understand how to drop down to C in Platypus.

      This allocates memory, either on the stack or in the area for global memory:

      igraph_t g;

      For Perl, you will need to allocate a buffer, more or less with:

      use vars '$g'; $g = "\x00" x IGRAPH_T_SIZE;

      ... and ideally you can call/compile sizeof(igraph_t) in C so your C compiler and Perl agree on how much memory should be allocated for it.

        Alright, I think I get it now (and I very obviously need to step back and find a C book somewhere). Platypus even exposes a sizeof() method so I can try some stuff with that.

        Thanks to all involved!