[Chicken-users] Question about gc-roots

2014-03-13 Thread pluijzer .
I am assigning non-immediate objects to foreign void pointers. To
prevent them from moving during garbage collection I turn the pointer
into a gc-root.

This works like I expect it would work, with one exception:

It seems that the finalizers of the objects assigned to the objects
inside the gc-root get fired prematurely.

I think I'm a little difficult to follow, I'll give an example:


#
void* ptr;
#

(define init-root
 (foreign-lambda* void ((scheme-object obj)) 
ptr = CHICKEN_new_gc_root();
CHICKEN_gc_root_set(ptr, obj);))

(define root-ref
  (foreign-lambda* scheme-object () 
C_return(CHICKEN_gc_root_ref(ptr));))

(define (make-some-string)
  (set-finalizer! hello (lambda (s) (print s , just got freed.

(define-record metadata
  data)

(init-root (make-metadata (make-some-string)))

; prints hello
(print (metadata-data (root-ref)))

; will fire finalizer printing hello, just got freed.
; I didn't expect this because there is still a refrence to the object
inside the root.
(gc #t)

; prints hello, object seems to still be around.
(print (metadata-data (root-ref)))
---

Maybe I don't understand gc-roots and use them wrongly or for the
wrong purpose. If so, would somebody kindly explain.

Thanks in advance,
Pluijzer

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


[Chicken-users] mutate-procedure documentation

2014-03-08 Thread pluijzer .
Hello everybody,
I was reading the documentation for mutate-procedure in the lolevel unit (
http://wiki.call-cc.org/man/3/Unit%20lolevel#mutate-procedure) but got
confused by the example given.
Should 'new' not be called 'old', or do I misunderstand?
Thx,
Pluijzer
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


[Chicken-users] pass by value and callbacks

2014-03-08 Thread pluijzer .
Hello chickeners,

I am trying to wrap a function that accepts a function pointer with an
argument to a struct passed by value.
I don't know how to do this (and fear it is not possible) but maybe
somebody else has a solution.

A little example of what I mean:



#
typedef struct { int x, y; } Point;

typedef void (*SomeCallbackFuncType)(Point point);

void someFunc(SomeCallbackFuncType callback)
{
Point p = {1, 1};
callback(p);
}
#

(define-external (callbackFunction (??? point))
  void
  (print Hello from callback!))

((foreign-lambda void someFunc c-pointer) #$callbackFunction)



I don't actually need the the value of the object in my callback.
Maybe that helps.

Thank you in advance,
Pluijzer

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


Re: [Chicken-users] #invalid forwarded object using FFI callback

2013-12-21 Thread pluijzer .
Hello Alyn,

Thank you for the extra info.

For my sitituation, which is a little different to yours I think, a work
aroundt he issue at the moment by wrapping the scheme-object around a
structure that contains a root node.

I do not know how appropriate or convinient this is to your situation.

...:

typedef struct
{
void* node;
} Object;

(define make-object
  (foreign-lambda* (c-pointer Object) ((scheme-object value)) 
Object* object = malloc(sizeof(Object));
object-node = CHICKEN_new_gc_root();
CHICKEN_gc_root_set(object-node, value);
C_return( object );))

(define read-object
  (foreign-lambda* scheme-object (((c-pointer Object) object)) 
C_return( CHICKEN_gc_root_ref(object-node) );))
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


Re: [Chicken-users] #invalid forwarded object using FFI callback

2013-12-19 Thread pluijzer .
Hello Alyn,

Did you find a solution to this problem? I seem to suffer a similar problem.

thanks,
Pluijzer



2013/12/8 .alyn.post. alyn.p...@lodockikumazvati.org

 Greetings,

 I'm encountering the following error message inside a callback using
 Chicken's FFI interface:

 Error: (cdr) bad argument type: #invalid forwarded object

 Call history:

 pwent.scm:100: loop
 pwent.scm:56: ##sys#gc
 pwent.scm:56: g42
 pwent.scm:24: make-string
 pwent.scm:24: make-string
 pwent.scm:24: make-string
 pwent.scm:24: make-string
 pwent.scm:50: clist-append!
 pwent.scm:100: loop
 pwent.scm:56: ##sys#gc
 pwent.scm:56: g42
 pwent.scm:24: make-string
 pwent.scm:24: make-string
 pwent.scm:24: make-string
 pwent.scm:24: make-string
 pwent.scm:50: clist-append! --


 What seems to be happening is that I'm running out of (stack?) memory
 while inside/around a C thunk.  I cannot seem to avoid this message by
 modifying the nursery, either from csc -nursery or from -:s.

 I have only a topical grasp of Chicken's FFI, so I suspect I'm simply
 doing something ill-advised, though I would appreciate advice on how
 to approach my problem:

 I'm trying to read /etc/passwd using getpwent(3) and store each
 record returned in a scheme list.  (My particular /etc/passwd file
 contains 100 elements, the above error happens near the end of the
 call.)  I'm marshalling records using two callbacks: one to create the
 memory for string data, and one to append each record to the list.

 May I have feedback on the pattern I'm using here to marshall data?
 And a suggestion on how to avoid invalid forward objects while
 reading large but disjoint data from C?  I'm at a loss as to why
 exactly I'm getting the above message, and it well could be incorrect
 code on my part.  Do my multiple calls to _make_string from C,
 below, cause the GC to loose track of my string pointers?  Something
 else?

 ++ csc -o pwent pwent.scm  ./pwent
 (use extras)

 ; a circular list (where we track the
 ; head and tail) with a dummy head.
 ;
 (define (make-clist)
   (let ((head (list #f)))
 (cons head head)))

 ; O(1) list append.
 ;
 (define (clist-append! d v)
   (let ((l (list v)))
 (set-cdr! (cdr d) l)
 (set-cdr! d l)))

 ; return proper list
 ;
 (define (clist-list d)
   (cdr (car d)))

 ; allocate a scheme string available in C.
 ;
 (define-external
   (_make_string (size_t n)) scheme-object
 (make-string n))

 (declare (foreign-declare #EOF
 #include sys/types.h
 #include string.h
 #include pwd.h
 EOF
 ))

 (define (getpwent)
   ; append! each pwent record to our record list.
   ;
   (define-external
 (_getpwent_cb (scheme-object clist)
   (scheme-object user)
   (scheme-object passwd)
   (scheme-object uid)
   (scheme-object gid)
   (scheme-object home)
   (scheme-object shell)) void
   (let ((pwent `((user   . ,user)
  (passwd . ,passwd)
  (uid. ,uid)
  (gid. ,gid)
  (home   . ,home)
  (shell  . ,shell
 (clist-append! clist pwent)))

   ; retrieve the next pwent record and marshall it
   ; in to scheme.
   ;
   (define _getpwent
 (foreign-safe-lambda* bool ((scheme-object clist)) #EOF
 struct passwd *pw;
 C_word shell, dir, passwd, name;
 size_t n;

 pw = getpwent();

 if(!pw) {
   endpwent();
   C_return(0);
 }

 n = strlen(pw-pw_name);
 name = _make_string(n);
 C_memcpy(C_c_string(name), pw-pw_name, n);

 n = strlen(pw-pw_passwd);
 passwd = _make_string(n);
 C_memcpy(C_c_string(passwd), pw-pw_passwd, n);

 n = strlen(pw-pw_dir);
 dir = _make_string(n);
 C_memcpy(C_c_string(dir), pw-pw_dir, n);

 n = strlen(pw-pw_shell);
 shell = _make_string(n);
 C_memcpy(C_c_string(shell), pw-pw_shell, n);

 _getpwent_cb(clist,
  name,
  passwd,
  C_fix(pw-pw_uid),
  C_fix(pw-pw_gid),
  dir,
  shell);
 C_return(1);
 EOF
 ))

   ; loop ever every entry in pwent and append
   ; it to our list.
   ;
   (let loop ((clist (make-clist)))
 (if (_getpwent clist)
 (loop clist)
 (clist-list clist

 (pretty-print (getpwent))
 --

 Thank you,

 -a

 $ csc -version
 (c) 2008-2013, The Chicken Team
 (c) 2000-2007, Felix L. Winkelmann
 Version 4.8.0.5 (stability/4.8.0) (rev 5bd53ac)
 openbsd-unix-gnu-x86 [ manyargs dload ptables ]
 compiled 2013-10-03 on aeryn.xorinia.dim (Darwin)
 --
 my personal website: http://c0redump.org/

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

[Chicken-users] assigning scheme object to foreign pointer

2013-12-15 Thread pluijzer
Hello everybody.

I am using a C-library that lets you assign user data to objects via a void 
pointer.
I would like to assign a scheme object to this pointer, but as I understand I 
cannot use
'object-pointer' for this, as the garbage collector might move the object.

Is there another way to accomplish this, maybe by telling the gc not to move 
the object?

thank you very much in advance,
Pluijzer

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


Re: [Chicken-users] assigning scheme object to foreign pointer

2013-12-15 Thread pluijzer
Hello Evan and Thomas,

Thank you for your suggestions, I decided to go for `CHICKEN_new_gc_root` and 
it works like I hoped.

I was the lookup-table before but found it to be too unwieldy in my project.

Thanks again,
Pluijzer 

On Mon, 16 Dec 2013 13:28:27 +1300
Evan Hanson ev...@foldling.org wrote:

 Hi Pluijzer,
 
 On 16/12/13 12:59, pluijzer wrote:
  I am using a C-library that lets you assign user data to objects via a
  void pointer. I would like to assign a scheme object to this pointer,
  but as I understand I cannot use 'object-pointer' for this, as the
  garbage collector might move the object.
 
 There are a couple of ways to do this.
 
 One is to manually move the object into static memory, via
 `object-evict` (http://api.call-cc.org/doc/lolevel#def:object-evict).
 This is nice and easy, but might not work as expected for all data
 types.
 
 Another is to create a new GC root for the object, via
 `CHICKEN_new_gc_root` and its associated procedures
 (http://api.call-cc.org/doc/foreign/embedding#sec:CHICKEN_new_gc_root).
 
 You might also sidestep the issue entirely by keeping your objects in a
 Scheme-side lookup table and only storing immediate values in the
 pointer that you can then use to retrieve the objects as needed.
 
 Other folks may chime in with more ideas, but I hope these help some.
 
 Cheers,
 
 Evan
 
 ___
 Chicken-users mailing list
 Chicken-users@nongnu.org
 https://lists.nongnu.org/mailman/listinfo/chicken-users


-- 
pluijzer pluij...@gmail.com

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


[Chicken-users] Problem with creating extensions

2013-10-16 Thread pluijzer .
Hello everybody,

I fail to get extensions that I have created and installed via
'chicken-install' to load.

For example (using the example from the documentation):

*/tmp/hello $ cat hello.scm*
(define (hello name)
(print Hello,  name  !))

*/tmp/hello $ cat hello.setup*
(compile -s hello.scm)
(install-extension 'hello hello.so)

*/tmp/hello $ cat hello.meta*
((author Me)
 (synopsis A cool hello-world library)
 (license GPLv3)
 (files hello.scm hello.setup))

*/tmp/hello $ chicken-install *
retrieving ...
checking platform for `hello' ...
checking dependencies for `hello' ...
install order:
(hello)
installing hello: ...
changing current directory to .
  /usr/bin/csi -bnq -setup-mode -e (require-library setup-api) -e
(import setup-api) -e (setup-error-handling) -e
(extension-name-and-version '(\hello\ \\)) hello.setup
  /usr/bin/csc -feature compiling-extension -setup-mode-s hello.scm
  cp -r hello.so /var/lib/chicken/6/hello.so
  chmod a+r /var/lib/chicken/6/hello.so
  chmod a+r /var/lib/chicken/6/hello.setup-info

*/tmp/hello $ csi -p '(use hello)'*

Error: (import) during expansion of (import ...) - cannot import from
undefined module: hello

Call history:

syntax  (use hello)
syntax  (##core#require-extension (hello) #t)
syntax  (##core#begin (##core#begin (##core#begin (##sys#require
(quote hello))) (import hello)) (##core#und..
syntax  (##core#begin (##core#begin (##sys#require (quote
hello))) (import hello))
syntax  (##core#begin (##sys#require (quote hello)))
syntax  (##sys#require (quote hello))
syntax  (quote hello)
syntax  (##core#quote hello)
syntax  (import hello)--

Some extra information:

*/tmp/hello $ csi -p '(repository-path)'*
/var/lib//chicken/6

*/tmp/hello $ ls /var/lib/chicken/6/hello.**
/var/lib/chicken/6/hello.setup-info  /var/lib/chicken/6/hello.so

*/tmp/hello $ csi -version*

CHICKEN
(c)2008-2011 The Chicken Team
(c)2000-2007 Felix L. Winkelmann
Version 4.7.0
linux-unix-gnu-x86 [ manyargs dload ptables ]
compiled 2011-09-06 on murphy (Linux)

---
I am using Debain Wheezy

Am I making a silly mistake, have I forgotten something?

Thank you in advance,
Pluijzer
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


[Chicken-users] Chicken C interface

2013-06-05 Thread pluijzer .
Hello everybody,

I was planning to use Chicken Scheme in a fashion more similar to Guile and
Lua. i.e. passing Scheme Data Objects from Chicken to C and back using the
C interface.

I am a little confused though as how to have a C function return a Scheme
List that is seen by the garbage collector.

For example, is the code below correct, will the list be seen by the
garbage collector? And if not, is there correct way to do this.

#
C_word give_12()
{
  C_word *mem  = C_alloc(C_SIZEOF_LIST(2));
  C_word  list = C_list(mem, 2, C_fix(1), C_fix(2));
  return list;
}
#
(print ((foreign-lambda scheme-object give_12)))

Also there doesn't seem to be a C_listp predicate. Is this a concious
omission?

thank you in advance,
Richard
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users


[Chicken-users] Passing floats to and from foreign functions

2013-05-11 Thread pluijzer .
Hello everybody,
I'm having trouble passing floating point numbers to and from foreign
functions.

This is what I'm doing:

-- test.scm --

(require-extension easyffi)
(print ((foreign-lambda float testGet)))
((foreign-lambda void testSet float) 4.0)
(print ((foreign-lambda float testGet)))

-- wrapper.c --

float a = 5.0;
void testSet(float value) { a = value; }
float testGet(void) { return a; }

What I get is this:

 csc -X easyffi wrapper.c test.scm
 ./test
-1080529876.0
-1080533140.0

But I expected the output to be:
4.0
5.0

I'm I doing something wrong/forgetting something?
When I use integers instead of floats I do get the desired result.

Thank you in advance,
Righard
___
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users