chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] Some allocation and object-passing tests


From: Felix Winkelmann
Subject: Re: [Chicken-users] Some allocation and object-passing tests
Date: Thu, 13 Nov 2003 07:30:59 +0100
User-agent: Opera7.21/Win32 M2 build 3218

Am Wed, 12 Nov 2003 19:53:09 +0000 hat Category 5 <address@hidden> geschrieben:

Felix Winkelmann <address@hidden> writes:

With these changes, your test-code works fine on my machine.

Ah!  My problem was in my (lack of) understanding of how C_gc_protect
works.  Now it makes more sense, and with your changes the test code
(original and modified versions) also runs fine for me, both in the
gc-protect and alloc-in-heap cases.

Unfortunately, in my real code, the dispatcher() function is opaque -
it's part of foreign library code that I'm trying to create bindings
for.  So I can't add C_gc_protect calls inside it.  Are there any
options left except for static memory or malloc?


Instead of passing the object you can pass a pointer to the gc-protected
array:

------------------8<--------------------
#>
#define TRIES  5
#define SCHEME_CALLBACK  scheme_callback2

int dispatcher(void (*fn)(unsigned char*), unsigned char *udata) {
  int i, n = TRIES;
   for (i = 0; i < n; i++)  {
    (*fn)(udata);
    Sleep(1);
  }
  return(0);
}
<#

(define fetchit
  (foreign-lambda* scheme-object ([(pointer (pointer "C_word")) ptr])
    "return(**ptr);") )

(define-external (scheme_callback2 (c-pointer udata))
  void
  (gc)
  (let ([proc (fetchit udata)])
    (assert (procedure? proc) "assertion failed")
    (proc) ) )

(define dispatcher-gc-protect
  (foreign-callback-lambda*
   int
   ((scheme-object obj))
    #<<EOF
   int result;
   C_word *data[ 1 ];
   data[ 0 ] = &obj;
   C_gc_protect(data, 1);
   result = dispatcher(SCHEME_CALLBACK, (unsigned char *)data);
   C_gc_unprotect(1);
   return(result);
EOF
))

(define handler (lambda () (print "it worked")))
(print "-- dispatcher-gc-protect")
(dispatcher-gc-protect handler)
(print "-- done")
---------------------8<------------------------------


cheers,
felix




reply via email to

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