unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Daniel Hartwig <mandyke@gmail.com>
To: Patrick Bernaud <patrickb@chez.com>
Cc: guile-user@gnu.org
Subject: Re: Foreign wrapped C structures and guardians
Date: Tue, 24 Jul 2012 11:16:28 +0800	[thread overview]
Message-ID: <CAN3veRdrDFvrXaHPG8Ku-zriX+tXztCPp9Xmm54exO+uja6J1w@mail.gmail.com> (raw)
In-Reply-To: <20493.22739.690939.105403@vagabond.local>

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



  reply	other threads:[~2012-07-24  3:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-23 13:59 Foreign wrapped C structures and guardians Patrick Bernaud
2012-07-24  3:16 ` Daniel Hartwig [this message]
2012-07-30 16:59   ` Patrick Bernaud

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAN3veRdrDFvrXaHPG8Ku-zriX+tXztCPp9Xmm54exO+uja6J1w@mail.gmail.com \
    --to=mandyke@gmail.com \
    --cc=guile-user@gnu.org \
    --cc=patrickb@chez.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).