unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Dimitris Papavasiliou <dpapavas@protonmail.ch>
To: Olivier Dion <olivier.dion@polymtl.ca>
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 11:41:22 +0000	[thread overview]
Message-ID: <WWY6y0l_3CLQBQ3lY8USgsTlJdsW4emFCsD2Unm9m3GpAT7owx7oX4od7R3ldvSKsmjHmDaQieM5HFIONMaKheC0Ev6uPp0tKHgkHG_9Q4A=@protonmail.ch> (raw)
In-Reply-To: <87ee65shtv.fsf@laura>

Hi Olivier,

Thanks for chiming in!

On Wednesday, December 22nd, 2021 at 12:39 AM, Olivier Dion wrote:

> From this description, what I understand is that you want to use Scheme
> as a configuration file for batching the operations to be done in a
> second phase in C++.

From an architectural point of view, the situation is probably similar
to simple configuration parsing, as you say.  In practice though, the
scope of Guile's role won't be nearly as limited as what this might
imply.

In a way, what I'm working on, can be described as a "compiler" for
geometry.  That is to say, it reads in the description of geometry in
some language and then evaluates it and dumps it in a format that can
be used for e.g. 3D printing.  Guile (amongst other options) supplies
the language frontend.  For example, one might invoke it on the shell
like this:

$ compile hollow_cube.scm

Here, `hollow_cube.scm' would contain, say:

(write-stl
  "hollow_cube.stl"
  (difference
    (cube 2 2 2)
    (cube 1 1 1)))

When the `cube' procedure is evaluated, it creates a node in the C++
backend that evaluates to the geometry of a cube.  Similarly
`difference' creates a node, with edges to the two cubes, which when
evaluated computes their boolean difference and so on.  Of course, in
this instance, the Scheme code amounts to little more than what might
be termed a configuration file, but imagine the code to create a
complex mechanical part and the situation is (conceptually) quite
different.

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.

> However, I failed to see why you need to finalize these objects
> since you're going to use them in your second phase?

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.

2. "Complex" arguments to the above operations such as
   transforamtions, planes, points, etc. which are C++ classes, for
   which the destructor needs to be called in one way or the other.
   These are generally passed by value to the operations and can
   therefore be freed once the first phase is complete.

> One way I think of would be to fork the process and create your C++
> objects in a shared memory area between the parent and the child. Once
> Guile is done reading your inputs, the child process dies and all its
> memory is reclaimed by the OS.

I have considered that but, besides the fact that it seems a bit
heavy-handed, it still won't allow me to make sure the C++ objects are
properly finalized.  This creates two problems: for one, some object
might need finalization that's more than just freeing memory and I'd
have to make sure that this isn't the case for each specific type
(which typically come from external libraries).  And for another,
these would show up as leaks in tools such as Valgrind, which would
make checking for memory bugs difficult.

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...

Dimitris




  reply	other threads:[~2021-12-22 11:41 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 [this message]
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
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='WWY6y0l_3CLQBQ3lY8USgsTlJdsW4emFCsD2Unm9m3GpAT7owx7oX4od7R3ldvSKsmjHmDaQieM5HFIONMaKheC0Ev6uPp0tKHgkHG_9Q4A=@protonmail.ch' \
    --to=dpapavas@protonmail.ch \
    --cc=guile-user@gnu.org \
    --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).