From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Daniel Hartwig Newsgroups: gmane.lisp.guile.user Subject: Re: Foreign wrapped C structures and guardians Date: Tue, 24 Jul 2012 11:16:28 +0800 Message-ID: References: <20493.22739.690939.105403@vagabond.local> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: dough.gmane.org 1343099798 18887 80.91.229.3 (24 Jul 2012 03:16:38 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 24 Jul 2012 03:16:38 +0000 (UTC) Cc: guile-user@gnu.org To: Patrick Bernaud Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Jul 24 05:16:38 2012 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1StVbx-0003Fr-Tu for guile-user@m.gmane.org; Tue, 24 Jul 2012 05:16:38 +0200 Original-Received: from localhost ([::1]:48946 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StVbw-0000cx-V5 for guile-user@m.gmane.org; Mon, 23 Jul 2012 23:16:36 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:60934) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StVbs-0000cg-0Y for guile-user@gnu.org; Mon, 23 Jul 2012 23:16:32 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1StVbq-0000Jv-SA for guile-user@gnu.org; Mon, 23 Jul 2012 23:16:31 -0400 Original-Received: from mail-wi0-f171.google.com ([209.85.212.171]:42589) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1StVbq-0000Jn-Kr for guile-user@gnu.org; Mon, 23 Jul 2012 23:16:30 -0400 Original-Received: by wibhq4 with SMTP id hq4so2920490wib.12 for ; Mon, 23 Jul 2012 20:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=aSY7fGVsaxqFae7W7SjLVNHhoqWQWQdu/YZVA2HLxq4=; b=XCeMg5RP93BUVAadqG+IZ5v2RuFDcWYKkrtUKqtxNEWraDzU52gUprQ/EbPA2zL0Cq t3yULxdEUo2KNzc+MBr2syyqf1JF2iTWENRU/Co8pHEMYkjTwDL8lkcbX1Hn9E+EtkCu SgHbKU4kHFHqftGOJzawcx2cs+3RoPZV7GJCD72bp0knXxx14zrF+ti2Em37W4FZAHN2 v3O1GQ/6SCvqIc4duHs5BX7xw+rLTyeQaEQToBAnlrQe7c1sNpkvXZTGR13OVBM19dW1 egRV0GsfzMOBhhprkj5jKhHZa+FPtPzRGeZJ6Mb8pRT/hPIx7LZuRwC0UmFvbupovYuk FUNw== Original-Received: by 10.217.5.72 with SMTP id v50mr9918209wes.101.1343099789009; Mon, 23 Jul 2012 20:16:29 -0700 (PDT) Original-Received: by 10.216.23.16 with HTTP; Mon, 23 Jul 2012 20:16:28 -0700 (PDT) In-Reply-To: <20493.22739.690939.105403@vagabond.local> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.212.171 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:9535 Archived-At: Hello On 23 July 2012 21:59, Patrick Bernaud 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