[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Need help embedding Guile
From: |
Olivier Dion |
Subject: |
Re: Need help embedding Guile |
Date: |
Wed, 22 Dec 2021 09:46:28 -0500 |
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