unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* Doc--protecting guile entities defined in C
@ 2003-06-02 20:46 Pascal Cedraschi
  2003-06-18 22:57 ` Christopher Cramer
  0 siblings, 1 reply; 2+ messages in thread
From: Pascal Cedraschi @ 2003-06-02 20:46 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1453 bytes --]

Dear Guile Maintainer,

I am using Guile 1.6.4 on a Debian (unstable) based i686--actually I do
not think the exact architecture is especially important since what I am
reporting is rather a problem with the documentation.  I would like to
define symbols for use in Guile and C code in the C code where I also
initialise (with `scm_c_define_gsubr') the C functions that have an
interface in Guile.  I have tried defining a global (in C) SCM variable
and setting the symbol's name with `scm_str2symbol' but the symbol thus
defined disappears unless I protect it (from the garbage collector?)
with `scm_gc_protect_object'.  Is that right?  Anyway, I have not been
able to find (in the Guile Reference Manual) an explanation of how to
define Guile symbols from the C startup code, and I think that should be
possible (to do it, and to find an explanation).

I have enclosed a complete example; it can be compiled using `gcc main.c
-lguile', and on the Guile command prompt, you can see the difference
between the symbols `exists' and `doesnt-exist' by typing

(equals-exists? 'exists)

and

(equals-doesnt-exist? 'doesnt-exist)

The functions `equals-exists?' and `equals-doesnt-exist?' are mirror
images of one another, as are the symbols `exists' and `doesnt-exist';
the only difference is that `exists' is protected using
`scm_gc_protect_object' while `doesnt-exist' is not...

Please let me know if you need more information.

Thanks,
Pascal Cedraschi

[-- Attachment #2: main.c --]
[-- Type: text/x-c, Size: 1797 bytes --]

/* showcase for the use of scm_gc_protect_object */

#include <libguile.h>


/* Global variables to hold symbols; as their names indicate, the
 * first one is to survive garbage collection, the second isn't.
 */
SCM exists;
SCM doesnt_exist;


/* initialise the above topology symols */
/* return #t if sym equals exists, #f otherwise */
static SCM equals_exists_q (SCM sym)
{
	SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG1, "equals-exists?");
	return sym==exists?SCM_BOOL_T:SCM_BOOL_F;
};


/* return #t if sym equals doesnt_exist, #f otherwise */
static SCM equals_doesnt_exist_q (SCM sym)
{
	SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG1, "equals-doesnt-exist?");
	return sym==doesnt_exist?SCM_BOOL_T:SCM_BOOL_F;
};


/* Here's the beef.  The symbol exists is protected from the garbage
 * collector whereas the symbol doesnt_exist is not.  If we access the
 * global variables exist and doesnt_exist from C code (invoked in
 * turn from Scheme code), they behave differently since doesnt_exist
 * has already been garbage collected away by the time the scheme
 * interpreter is invoked.
 */
static void init (void)
{
	scm_gc_protect_object(exists=scm_str2symbol("exists"));
	doesnt_exist=scm_str2symbol("doesnt_exist");
	scm_c_define_gsubr("equals-exists?", 1, 0, 0, equals_exists_q);
	scm_c_define_gsubr("equals-doesnt-exist?", 1, 0, 0,
			   equals_doesnt_exist_q);
};


/* The actual main function.  main (below) does not much more than
 * invoke this function.
 */
static void inner_main (void *closure, int argc, char **argv)
{
	init();
	scm_shell(argc, argv);
};


/* Just the standard main for a Guile extended program.  The actual
 * work is done by inner_main above.
 */
int main (int argc, char *argv[])
{
	scm_boot_guile (argc, argv, inner_main, NULL);
	exit(0);  /* never get here */
};

[-- Attachment #3: Type: text/plain, Size: 136 bytes --]

_______________________________________________
Bug-guile mailing list
Bug-guile@gnu.org
http://mail.gnu.org/mailman/listinfo/bug-guile

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Doc--protecting guile entities defined in C
  2003-06-02 20:46 Doc--protecting guile entities defined in C Pascal Cedraschi
@ 2003-06-18 22:57 ` Christopher Cramer
  0 siblings, 0 replies; 2+ messages in thread
From: Christopher Cramer @ 2003-06-18 22:57 UTC (permalink / raw)
  Cc: bug-guile

On Mon, Jun 02, 2003 at 10:46:58PM +0200, Pascal Cedraschi wrote:
> I have tried defining a global (in C) SCM variable
> and setting the symbol's name with `scm_str2symbol' but the symbol thus
> defined disappears unless I protect it (from the garbage collector?)
> with `scm_gc_protect_object'.  Is that right?

That is correct, although I think in most cases you should instead use
scm_permanent_object. scm_gc_protect_object should be used when you might
end up using scm_gc_unprotect_object later. The usual way of creating
a static symbol in C is:

	foo = scm_permanent_object(scm_str2symbol("foo"));

Basically whenever you store a Scheme value (whether a symbol or any
other data type) that isn't on the stack, you need to protect it.
Apparently, this isn't in the manual.

-- 
Christopher Cramer <crayc@pyro.net> <http://www.pyro.net/~crayc/>
"People said it couldn't be that our soldiers would do such things. Now
you read worse things in the mainstream media and people don't care. We
used to say that if only people know about it, it would stop. Now they
know about it, and it hasn't stopped." - Adam Keller


_______________________________________________
Bug-guile mailing list
Bug-guile@gnu.org
http://mail.gnu.org/mailman/listinfo/bug-guile


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-06-18 22:57 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-06-02 20:46 Doc--protecting guile entities defined in C Pascal Cedraschi
2003-06-18 22:57 ` Christopher Cramer

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