Lisp HUG Maillist Archive

FLI: sending binary data from Lisp to C

Hi,

I’m building a dynamic library in LispWorks, to be called from C. The library produces binary data (size not known at compile time).

My problem is that I don’t understand how to define the foreign function regarding the pointer parameters.

This is what I have in C:

typedef int(*data_function)(unsigned char**);
…
unsigned char** data;
int size = get_data(data);


And in Lisp:

(fli:define-foreign-callable (preview_pdf :result-type :int)
    ((out-data (:reference-pass :pointer)))
  (let* ((size 256)
         (test-data (make-array size :element-type '(unsigned-byte 8)))
         (c-array (fli:allocate-foreign-object :type :unsigned-byte :nelems size :initial-element 0)))
    (fli:replace-foreign-array c-array test-data)
    (fli:with-coerced-pointer (ptr) c-array
      (setf out-data ptr))
    size))


But it seems I got the pointer type wrong:

An error of type FLI:FOREIGN-TYPE-ERROR occured, arguments : (:EXPECTED-TYPE :POINTER :DATUM #<unknown object, header / pointer: 202AAD11 /EE1FEE2F>)


Any suggestions?

Thanks
Erik



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

Re: FLI: sending binary data from Lisp to C

A general approach to this, and code samples which might help:

https://www.nicklevine.org/claude/

- nick

On 6 Sep 2019, at 13:30, Erik Ronström <erik.ronstrom@doremir.com> wrote:

Hi,

I’m building a dynamic library in LispWorks, to be called from C. The library produces binary data (size not known at compile time).

My problem is that I don’t understand how to define the foreign function regarding the pointer parameters.

This is what I have in C:

typedef int(*data_function)(unsigned char**);
…
unsigned char** data;
int size = get_data(data);


And in Lisp:

(fli:define-foreign-callable (preview_pdf :result-type :int)
   ((out-data (:reference-pass :pointer)))
 (let* ((size 256)
        (test-data (make-array size :element-type '(unsigned-byte 8)))
        (c-array (fli:allocate-foreign-object :type :unsigned-byte :nelems size :initial-element 0)))
   (fli:replace-foreign-array c-array test-data)
   (fli:with-coerced-pointer (ptr) c-array
     (setf out-data ptr))
   size))


But it seems I got the pointer type wrong:

An error of type FLI:FOREIGN-TYPE-ERROR occured, arguments : (:EXPECTED-TYPE :POINTER :DATUM #<unknown object, header / pointer: 202AAD11 /EE1FEE2F>)


Any suggestions?

Thanks
Erik



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

Re: FLI: sending binary data from Lisp to C

The problem is that fli:with-coerced-pointer makes a dynamic-extent Lisp
object, which is escaping from its extent when you set out-data to it.

In the example you gave, you don't need fli:with-coerced-pointer -- just use
(setf out-data c-array).

-- 
Martin Simmons
LispWorks Ltd
http://www.lispworks.com/


>>>>> On Fri, 6 Sep 2019 14:30:50 +0200, Erik Ronström said:
> 
> Hi,
> 
> I’m building a dynamic library in LispWorks, to be called from C. The library produces binary data (size not known at compile time).
> 
> My problem is that I don’t understand how to define the foreign function regarding the pointer parameters.
> 
> This is what I have in C:
> 
> typedef int(*data_function)(unsigned char**);
> …
> unsigned char** data;
> int size = get_data(data);
> 
> 
> And in Lisp:
> 
> (fli:define-foreign-callable (preview_pdf :result-type :int)
>     ((out-data (:reference-pass :pointer)))
>   (let* ((size 256)
>          (test-data (make-array size :element-type '(unsigned-byte 8)))
>          (c-array (fli:allocate-foreign-object :type :unsigned-byte :nelems size :initial-element 0)))
>     (fli:replace-foreign-array c-array test-data)
>     (fli:with-coerced-pointer (ptr) c-array
>       (setf out-data ptr))
>     size))
> 
> 
> But it seems I got the pointer type wrong:
> 
> An error of type FLI:FOREIGN-TYPE-ERROR occured, arguments : (:EXPECTED-TYPE :POINTER :DATUM #<unknown object, header / pointer: 202AAD11 /EE1FEE2F>)
> 
> 
> Any suggestions?
> 
> Thanks
> Erik
> 
> 
> 
> _______________________________________________
> Lisp Hug - the mailing list for LispWorks users
> lisp-hug@lispworks.com
> http://www.lispworks.com/support/lisp-hug.html
> 

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

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