Lisp HUG Maillist Archive

FLI function with void **

Hello All,

Sorry for the additional noise. I've been working through the FLI stuff trying to test the various use cases. There is only one remaining trouble spot. I can't figure out how to call a function that takes a void ** arg. I have included the code below. 

struct arg_struct
{
int val;
};

void func_handle_init(void **h)
{
struct arg_struct *handle = NULL;

  handle = (struct arg_struct *)malloc(sizeof(struct arg_struct));
  memset(handle, 0, sizeof(struct arg_struct));  
  handle->val = 12;
  *h = handle;
}


(fli:define-foreign-function (func-handle-init "func_handle_init"
                                                   :source)
                             ((handle (:pointer (:pointer :void))))
                             :result-type
                             :void
                             :language
                             :ansi-c)

I can't seem to figure out how to call this guy. Your help will be much appreciated as always.

Thanks,
Gerry


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html


Re: FLI function with void **

Looking at the function you can see that it needs h to point to a pointer, so you just allocate a pointer to pass in.
Not familiar with lisp works fli, but this is how you do it in cffi:

(cffi:with-foreign-object (h :pointer)
  (func-handle-init h)
  ;;do stuff with h
)

You can of course use CFFI:FOREIGN-ALLOC if you need dynamic extent. The key is that you need only allocate the pointer, not the object itself

On Monday, November 25, 2013, Gerry Weaver wrote:

Hello All,

Sorry for the additional noise. I've been working through the FLI stuff trying to test the various use cases. There is only one remaining trouble spot. I can't figure out how to call a function that takes a void ** arg. I have included the code below.

struct arg_struct
{
int val;
};

void func_handle_init(void **h)
{
struct arg_struct *handle = NULL;

  handle = (struct arg_struct *)malloc(sizeof(struct arg_struct));
  memset(handle, 0, sizeof(struct arg_struct));
  handle->val = 12;
  *h = handle;
}


(fli:define-foreign-function (func-handle-init "func_handle_init"
                                                   :source)
                             ((handle (:pointer (:pointer :void))))
                             :result-type
                             :void
                             :language
                             :ansi-c)

I can't seem to figure out how to call this guy. Your help will be much appreciated as always.

Thanks,
Gerry


_______________________________________________
Lisp Hug - the mailing list for LispWorks users
lisp-hug@lispworks.com
http://www.lispworks.com/support/lisp-hug.html

Re: FLI function with void **

Hi Gerry,

On 25 nov. 2013, at 11:29, Gerry Weaver <gerryw@compvia.com> wrote:

> I can't seem to figure out how to call this guy. Your help will be much appreciated as always.

Basically here is what I did:

  (fli:with-dynamic-foreign-objects ((handle :pointer))
    (func-handle-init handle)
    (format t "~&Handle: ~A~%" handle)
    ;; pointer to actual allocated memory is in (fli:dereference handle)
    (free (fli:dereference handle)))

See full example in the attached file.

Best,
Cam


Updated at: 2020-12-10 08:34 UTC