[plt-scheme] Memory management and MzScheme

From: Anton van Straaten (anton at appsolutions.com)
Date: Sat Nov 23 20:13:13 EST 2002

A reference for this is Inside MzScheme:
http://download.plt-scheme.org/doc/insidemz/insidemz.html

In particular, Ch. 3, Memory Allocation:
http://download.plt-scheme.org/doc/insidemz/insidemz-Z-H-3.html :

"MzScheme uses both malloc and allocation functions provided the
conservative garbage collector. Embedding/extension C/C++ code may use
either allocation method, keeping in mind that pointers to
garbage-collectable blocks in malloced memory are invisible (i.e., such
pointers will not prevent the block from being garbage-collected)."

Another important page is:
http://download.plt-scheme.org/doc/insidemz/insidemz-Z-H-1.html,
specifically:

"IMPORTANT: Scheme values are garbage collected using a conservative garbage
collector, so pointers to MzScheme objects can be kept in registers, stack
variables, or structures allocated with scheme_malloc. However, static
variables that contain pointers to collectable memory must be registered
using scheme_register_extension_global (see section 3)."

There's more useful info on both those pages, but I think the above two
quotes answer your questions.  The two examples you gave:

> Scheme_Object * s1 = scheme_make_string();
> Scheme_Object * s2 = scheme_make_string();
> ...
> return s1;
...
> Scheme_Object **array = (Scheme_Object**) scheme_malloc (123);
> Scheme_Object * s2 = scheme_make_string();
> ...
> return array;

should both be safe, because the pointers are stored in stack variables
which are accessible to the collector.  A problem would only arise when
storing Scheme_Object pointers in memory returned by the regular malloc
function.

Anton



Posted on the users mailing list.