unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Foreign wrapped C structures and guardians
@ 2012-07-23 13:59 Patrick Bernaud
  2012-07-24  3:16 ` Daniel Hartwig
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Bernaud @ 2012-07-23 13:59 UTC (permalink / raw)
  To: guile-user

[-- Attachment #1: message body and .signature --]
[-- Type: text/plain, Size: 970 bytes --]

Hello,

I am trying to create and manipulate some C structures with the
foreign interface of Guile 2.0. This is part of an extension to an
existing application.

How to make a structure created with 'make-c-struct' permanent? That
is avoiding garbage collection and then letting the application, not
Guile, ultimately taking care of its memory.

I tried with guardians but the memory still seems to be overwritten
from time to time. For example, with the script attached, here is what
I get:

  $ guile --no-auto-compile -s test.scm
  (0 1 2 3 4 5 6 7 8 9)
  (0 1 2 3 4 5 6 7 8 9)
  (80 62 102 97 107 101 60 47 80 62)
  $ guile --version
  guile (GNU Guile) 2.0.6.1-abd73-dirty
  Copyright (C) 2012 Free Software Foundation, Inc.
  
  License LGPLv3+: GNU LGPL 3 or later <http://gnu.org/licenses/lgpl.html>.
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.
  $

Regards,

-- 
Patrick Bernaud


[-- Attachment #2: test.scm --]
[-- Type: application/octet-stream, Size: 464 bytes --]

(use-modules (system foreign))
(define my-guardian (make-guardian))
(define l 10)
(define x (make-c-struct (make-list l int) (iota l)))
(define a (pointer-address x))
;(my-guardian (pointer->bytevector x (sizeof (make-list l int))))
(my-guardian x)
(set! x #f)
(define (dump-struct)
  (write (parse-c-struct (make-pointer a) (make-list l int)))(newline))
(dump-struct)
(gc)
(dump-struct)
;(use-modules (gnome-2) (gnome gtk))
(use-modules (htmlprag))
(dump-struct)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Foreign wrapped C structures and guardians
  2012-07-23 13:59 Foreign wrapped C structures and guardians Patrick Bernaud
@ 2012-07-24  3:16 ` Daniel Hartwig
  2012-07-30 16:59   ` Patrick Bernaud
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Hartwig @ 2012-07-24  3:16 UTC (permalink / raw)
  To: Patrick Bernaud; +Cc: guile-user

Hello

On 23 July 2012 21:59, Patrick Bernaud <patrickb@chez.com> wrote:
> Hello,
>
> I am trying to create and manipulate some C structures with the
> foreign interface of Guile 2.0. This is part of an extension to an
> existing application.

Your best option is to handle the low-level allocation and
manipulation of these structs from the c side, exposing the
appropriate functions to scheme.  Your scheme code then treats the
pointers as opaque objects and does not directly manipulate the
memory:

(use-modules (my-struct)) ;; make-my-struct, my-struct->list
(use-modules (system foreign))
(define l 10)
(define x (make-my-struct (iota l))
(define a (pointer-address x))
(set! x #f)
(define (dump-struct)
  (write (my-struct->list (make-pointer a))) (newline))

The procedure define-wrapped-pointer-type is useful for this.

> How to make a structure created with 'make-c-struct' permanent? That
> is avoiding garbage collection and then letting the application, not
> Guile, ultimately taking care of its memory.

Your guardian test should work but on my system it is broken also.

The libguile function scm_malloc will allocate memory that the
collector avoids.  You can then copy the data to this:

(define malloc
  (let ((this (dynamic-link)))
    (pointer->procedure '*
                        (dynamic-func "scm_malloc" this)
                        (list size_t))))
(define memcpy
  (let ((this (dynamic-link)))
    (pointer->procedure '*
                        (dynamic-func "memcpy" this)
                        (list '* '* size_t))))
(define l 10)
(define s (make-list l int))
(define x (malloc (sizeof s)))
(define a (pointer-address x))
(memcpy x (make-c-struct s (iota l)) (sizeof s))

But this is an extra malloc and memcpy which could be avoided if you
implement the allocator in c.

>
> I tried with guardians but the memory still seems to be overwritten
> from time to time. For example, with the script attached, here is what
> I get:
>
>   $ guile --no-auto-compile -s test.scm
>   (0 1 2 3 4 5 6 7 8 9)
>   (0 1 2 3 4 5 6 7 8 9)
>   (80 62 102 97 107 101 60 47 80 62)

Although the guardian should be protecting this, it does appear not to
in your test.scm.  If I use --fresh-auto-compile it works fine.

$ /usr/bin/guile --version | sed 1q
guile (GNU Guile) 2.0.6

Regards



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Foreign wrapped C structures and guardians
  2012-07-24  3:16 ` Daniel Hartwig
@ 2012-07-30 16:59   ` Patrick Bernaud
  0 siblings, 0 replies; 3+ messages in thread
From: Patrick Bernaud @ 2012-07-30 16:59 UTC (permalink / raw)
  To: Daniel Hartwig; +Cc: guile-user


Hello,

Daniel Hartwig writes:
 > [...]
 > Your guardian test should work but on my system it is broken also.

Thank you for confirming the problem. I have now stripped the test a
bit more and filed a bug report [1] with it.


 > The libguile function scm_malloc will allocate memory that the
 > collector avoids.  You can then copy the data to this:
 > 
 > (define malloc
 >   (let ((this (dynamic-link)))
 >     (pointer->procedure '*
 >                         (dynamic-func "scm_malloc" this)
 >                         (list size_t))))
 > [...]

Good tip, I may end up using something like this since, ultimately, I
would rather not have allocators in C.


 > [...]
 > Although the guardian should be protecting this, it does appear not to
 > in your test.scm.  If I use --fresh-auto-compile it works fine.

Indeed, I also can not make it to fail with auto compilation turned
on.

Regards,


[1] http://debbugs.gnu.org/cgi/bugreport.cgi?bug=12095

-- 
Patrick Bernaud



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-07-30 16:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-07-23 13:59 Foreign wrapped C structures and guardians Patrick Bernaud
2012-07-24  3:16 ` Daniel Hartwig
2012-07-30 16:59   ` Patrick Bernaud

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).