chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Chicken C interface


From: Ivan Raikov
Subject: Re: [Chicken-users] Chicken C interface
Date: Thu, 6 Jun 2013 10:54:38 +0900

The correct way is to not let C manage memory at all ;-P
In the mpi egg, I used foreign-primitive and C_alloc as follows:

;; Returns the current MPI time as a floating-point number
(define MPI:wtime
  (foreign-primitive scheme-object ()
#<<EOF
  C_word result;
  C_word *ptr;

  ptr = C_alloc (C_SIZEOF_FLONUM);

  result = C_number(&ptr, MPI_Wtime());

  C_return (result);
EOF
))

In various other C interface libraries, I use the following idiom:

(define (func arg)

  ;; determine the size of the result vector
  (let* ((n (c_func_result_length arg))

    ;; allocate memory for the result vector
     (v (make-f64vector n 0.0)))

    ;; obtain result
    (c_func arg v)

    v))

This of course assumes that the C library you are targeting is structured so as to allow you
to determine the result size. This is often the case with various numerical libraries I have had to
interface to, but that's a fairly specific use case.


   -Ivan



On Thu, Jun 6, 2013 at 6:45 AM, Dan Leslie <address@hidden> wrote:
Oh dear!

Well, it works and I haven't had problems. What's the correct way to go about this?

-Dan


On 6/5/2013 2:36 PM, Felix wrote:
From: Dan Leslie <address@hidden>
Subject: Re: [Chicken-users] Chicken C interface
Date: Wed, 05 Jun 2013 08:47:45 -0700

I do this a fair bit in the Allegro egg.

Here's an example:
https://github.com/dleslie/allegro-egg/blob/985ca2ceef0f5b4028af3f97729f13cba2976fe5/color.scm

Basically, use C_alloc to allocate the memory required to host both
the List structure and the data it is to contain, then use the C_list
macro to patch it all together.
Note that this code is not correct: C_alloc allocates on the C stack and the
data will be invalid once the function returns (Sorry). If this works, then
it is just coincidental!


cheers,
felix


_______________________________________________
Chicken-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users


reply via email to

[Prev in Thread] Current Thread [Next in Thread]