unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Olivier Dion via General Guile related discussions <guile-user@gnu.org>
To: Dimitris Papavasiliou <dpapavas@protonmail.ch>
Cc: Maxime Devos <maximedevos@telenet.be>,
	"guile-user@gnu.org" <guile-user@gnu.org>
Subject: Re: Need help embedding Guile
Date: Wed, 22 Dec 2021 09:46:28 -0500	[thread overview]
Message-ID: <878rwcsnnf.fsf@laura> (raw)
In-Reply-To: <WWY6y0l_3CLQBQ3lY8USgsTlJdsW4emFCsD2Unm9m3GpAT7owx7oX4od7R3ldvSKsmjHmDaQieM5HFIONMaKheC0Ev6uPp0tKHgkHG_9Q4A=@protonmail.ch>

On Wed, 22 Dec 2021, Dimitris Papavasiliou <dpapavas@protonmail.ch> wrote:

> Foreign objects currently come in two categories:
>
> 1. Complete geometric operations such as `cube' and `difference'
>    above.  These are allocated on the C++ side and a so-called "smart
>    pointer" (shared_ptr) is exported to Scheme.  Failure to finalize
>    this retains a reference on the C++ side, which would prevent
>    destroying the operation.  Since these can get quite large in terms
>    of memory, destroying them after they're no longer needed can be
>    essential.

Since you have a graph of all the primitives in the second phase, you're
basicaly doing garbage collection there.  But from:

> Creating the complete graph before evaluation begins in the second
> phase is probably not necessary (nodes could be evaluated as they're
> created), but it creates the opportunity for certain optimizations
> (like dead code elimination for instance).  This makes some sort of
> forcing/ensuring that Guile has terminated desirable.

If I understood, objects can be garbage before phase 2, thus not
appearing in the final graph of operations.

> One idea would be to simply call `scm_gc()' and `scm_run_finalizers()'
> until the latter returns 0.  As far as I can see, this should ensure
> all finalizers are called, assumming no references to any foreign
> objects remain, but I see no way of ensuring the latter short of
> process termination...

One way I think you could do this is to evaluate all the user operations
in a sandbox environment.

Example:
--------------------------------------------------------------------------------
(use-modules (ice-9 sandbox))
;; ...
(let ([mod (make-sandbox-module (cons '((my-app primitives))
                                       all-pure-bindings))])
   (eval-in-sandbox '(eval-user-file "...") #:module mod))
--------------------------------------------------------------------------------

From `eval-in-sandbox` documentation:

 If SEVER-MODULE? is true (the default), the module will be unlinked
 from the global module tree after the evaluation returns, to allow MOD
 to be garbage-collected.

So I _think_ you're guarantee here that all references in your module
will be garbage collected.  You can then do a single `gc/finalizer`.

-- 
Olivier Dion
Polymtl



  parent reply	other threads:[~2021-12-22 14:46 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-21 11:12 Need help embedding Guile Dimitris Papavasiliou
2021-12-21 11:37 ` Maxime Devos
2021-12-21 13:45   ` Maxime Devos
2021-12-21 13:48   ` Maxime Devos
2021-12-21 21:50   ` Dimitris Papavasiliou
2021-12-21 22:39     ` Olivier Dion via General Guile related discussions
2021-12-22 11:41       ` Dimitris Papavasiliou
2021-12-22 13:52         ` Thien-Thi Nguyen
2021-12-22 22:05           ` Dimitris Papavasiliou
2021-12-22 14:46         ` Olivier Dion via General Guile related discussions [this message]
2021-12-22 15:23           ` Maxime Devos
2021-12-22 15:29         ` Maxime Devos
2021-12-22 22:14           ` Dimitris Papavasiliou
2021-12-22 17:37         ` Mikael Djurfeldt
2021-12-23  9:40           ` Mikael Djurfeldt
2021-12-29 16:26           ` Olivier Dion via General Guile related discussions

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=878rwcsnnf.fsf@laura \
    --to=guile-user@gnu.org \
    --cc=dpapavas@protonmail.ch \
    --cc=maximedevos@telenet.be \
    --cc=olivier.dion@polymtl.ca \
    /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).