Re: 'native' and pointers to pointers

2017-04-02 Thread Michel Pelletier
Thanks for the explanation Erik!  Sorry I misspelled your name. :)

On Sun, Apr 2, 2017 at 12:11 PM, Erik Gustafson 
wrote:

> Michel,
>
> Here's a link to exactly what you need:
>
> https://github.com/michelp/0pl/blob/master/zmq/zctx.l#L5
>
>
> I totally forgot about 0pl - That's it! Thanks for the quick response.
>
>
> a thread in the archives where Alexander explains it to me...
>
>
> I wasn't able to find it, but here's my attempt:
>
> Passing a pointer reference to 'native' is similar to the integer
> reference example found here: https://software-labde/
> doc/native.html#structArg
> .
>
> It becomes clearer when we think of a reference (a pointer) to a pointer
> instead as a buffer of 8 bytes - sizeof(*ptr) | sizeof(long) - that
> contains a pointer.
>
> We can pass this buffer to 'native' with:
>
>(list NIL (8) (cons Ptr 8))
>
> 'native' then receives something like:
>
>(NIL (8) (35680432 . 8))
>
> In English, I'd read this as "a buffer of 8 bytes - that we won't need to
> reference again (hence NIL) - initialized to contain our pointer, literally
> 'long 35680432' in C."
>
> That's a mouthful. Let's create a naive little helper function so we don't
> have to think about it anymore:
>
>(de  (P) (list NIL (8) (cons P 8)))
>
> A bit more intuitive now:
>
>: (setq *N (native "libzyre.so" "zyre_create" 'N "node"))
>-> 36274819
>: (setq *N (native "libzyre.so" "zyre_destroy" NIL ( *N)))
>-> NIL
>
> Look how nicely '' pairs with the global variable naming convention ;)
> not that I'd ever recommend a global variable for each node on a network...
>
> Corrections/clarifications encouraged!
>
> Cheers,
> Erik
>
>


Re: 'native' and pointers to pointers

2017-04-02 Thread Michel Pelletier
Hi Eric,

Here's a link to exactly what you need:

https://github.com/michelp/0pl/blob/master/zmq/zctx.l#L5

There's a thread in the archives where Alexander explains it to me,
unfortunately it's been a while I don't remember all the details myself...

-Michel

On Sun, Apr 2, 2017 at 10:00 AM, Erik Gustafson 
wrote:

> Hi list,
>
> I'm playing with PicoLisp's 'native' functionality and
> https://github.com/zeromq/zyre Most everything works as expected. Trivial
> example:
>
>: (setq Node (native "libzyre.so" "zyre_new" 'N "Node"))
>-> 9322320  # Node set to pointer
>: (native "libzyre.so" "zyre_uuid" 'S Node)
>-> "44536C1E..."  # uuid string
>
> Perfect. The trouble is with 'zyre_destroy'. The C function signature is,
>
>void zyre_destroy (zyre_t **self_p)
>
> In C code it would be called as,
>
>zyre_t *node = zyre_new("node");
>
>zyre_destroy();
>
> How would I do the same in PicoLisp (call 'native' with the equivalent of
> '')? The obviously incorrect,
>
>: (native "libzyre.so" "zyre_destroy" NIL Node)
>
> segfaults. As does,
>
>: (native [...] NIL (struct Node 'N))
>
> and
>
>: (native [...] NIL (car (struct Node '(N
>
> I think I need to do *something* with 'struct', but everything I've tried
> ends with a segfault.
>
> Note: In the Zyre header file the only mention of 'zyre_t' is,
>
>// Opaque class structures to allow forward references
>typedef struct _zyre_t zyre_t;
>
> so I'm not sure how to use 'struct' with it.
>
> Thanks,
> Erik
>