/*
* Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
* Copyright (c) 1991 by Xerox Corporation. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to copy this garbage collector for any purpose,
* provided the above notices are retained on all copies.
*/
#ifndef GC←H
# define GC←H
# include <stddef.h>
/* Define word and signed←word to be unsigned and signed types of the */
/* size as char * or void *. There seems to be no way to do this */
/* even semi-portably. The following is probably no better/worse */
/* than almost anything else. */
/* The ANSI standard suggests that size←t and ptr←diff←t might be */
/* better choices. But those appear to have incorrect definitions */
/* on may systems. Notably "typedef int size←t" seems to be both */
/* frequent and WRONG. */
typedef unsigned long GC←word;
typedef long GC←signed←word;
/* Public read-only variables */
extern GC←word GC←heapsize; /* Heap size in bytes */
extern GC←word GC←gc←no;/* Counter incremented per collection. */
/* Includes empty GCs at startup. */
extern int GC←incremental; /* Using incremental/generational collection. */
/* Public R/W variables */
extern int GC←quiet; /* Disable statistics output. Only matters if */
/* collector has been compiled with statistics */
/* enabled. This involves a performance cost, */
/* and is thus not the default. */
extern int GC←dont←gc; /* Dont collect unless explicitly requested, e.g. */
/* beacuse it's not safe. */
extern int GC←dont←expand;
/* Dont expand heap unless explicitly requested */
/* or forced to. */
extern int GC←full←freq; /* Number of partial collections between */
/* full collections. Matters only if */
/* GC←incremental is set. */
extern GC←word GC←non←gc←bytes;
/* Bytes not considered candidates for collection. */
/* Used only to control scheduling of collections. */
extern GC←word GC←free←space←divisor;
/* We try to make sure that we allocate at */
/* least N/GC←free←space←divisor bytes between */
/* collections, where N is the heap size plus */
/* a rough estimate of the root set size. */
/* Initially, GC←free←space←divisor = 4. */
/* Increasing its value will use less space */
/* but more collection time. Decreasing it */
/* will appreciably decrease collection time */
/* at the expens of space. */
/* GC←free←space←divisor = 1 will effectively */
/* disable collections. */
/* Public procedures */
/*
* general purpose allocation routines, with roughly malloc calling conv.
* The atomic versions promise that no relevant pointers are contained
* in the object. The nonatomic versions guarantee that the new object
* is cleared. GC←malloc←stubborn promises that no changes to the object
* will occur after GC←end←stubborn←change has been called on the
* result of GC←malloc←stubborn. GC←malloc←uncollectable allocates an object
* that is scanned for pointers to collectable objects, but is not itself
* collectable. GC←malloc←uncollectable and GC←free called on the resulting
* object implicitly update GC←non←gc←bytes appropriately.
*/
# ifdef ←←STDC←←
extern void * GC←malloc(size←t size←in←bytes);
extern void * GC←malloc←atomic(size←t size←in←bytes);
extern void * GC←malloc←uncollectable(size←t size←in←bytes);
extern void * GC←malloc←stubborn(size←t size←in←bytes);
# else
extern char * GC←malloc(/* size←in←bytes */);
extern char * GC←malloc←atomic(/* size←in←bytes */);
extern char * GC←malloc←uncollectable(/* size←in←bytes */);
extern char * GC←malloc←stubborn(/* size←in←bytes */);
# endif
/* Explicitly deallocate an object. Dangerous if used incorrectly. */
/* Requires a pointer to the base of an object. */
/* If the argument is stubborn, it should not be changeable when freed. */
/* An object should not be enable for finalization when it is */
/* explicitly deallocated. */
# ifdef ←←STDC←←
extern void GC←free(void * object←addr);
# else
extern void GC←free(/* object←addr */);
# endif
/*
* Stubborn objects may be changed only if the collector is explicitly informed.
* The collector is implicitly informed of coming change when such
* an object is first allocated. The following routines inform the
* collector that an object will no longer be changed, or that it will
* once again be changed. Only nonNIL pointer stores into the object
* are considered to be changes. The argument to GC←end←stubborn←change
* must be exacly the value returned by GC←malloc←stubborn or passed to
* GC←change←stubborn. (In the second case it may be an interior pointer
* within 512 bytes of the beginning of the objects.)
* There is a performance penalty for allowing more than
* one stubborn object to be changed at once, but it is acceptable to
* do so. The same applies to dropping stubborn objects that are still
* changeable.
*/
void GC←change←stubborn(/* p */);
void GC←end←stubborn←change(/* p */);
/* Return a pointer to the base (lowest address) of an object given */
/* a pointer to a location within the object. */
/* Return 0 if displaced←pointer doesn't point to within a valid */
/* object. */
# ifdef ←←STDC←←
void * GC←base(void * displaced←pointer);
# else
char * GC←base(/* char * displaced←pointer */);
# endif
/* Given a pointer to the base of an object, return its size in bytes. */
/* The returned size may be slightly larger than what was originally */
/* requested. */
# ifdef ←←STDC←←
size←t GC←size(void * object←addr);
# else
size←t GC←size(/* char * object←addr */);
# endif
/* For compatibility with C library. This is occasionally faster than */
/* a malloc followed by a bcopy. But if you rely on that, either here */
/* or with the standard C library, your code is broken. In my */
/* opinion, it shouldn't have been invented, but now we're stuck. -HB */
/* The resulting object has the same kind as the original. */
/* If the argument is stubborn, the result will have changes enabled. */
/* It is an error to have changes enabled for the original object. */
# ifdef ←←STDC←←
extern void * GC←realloc(void * old←object, size←t new←size←in←bytes);
# else
extern char * GC←realloc(/* old←object, new←size←in←bytes */);
# endif
/* Explicitly increase the heap size. */
/* Returns 0 on failure, 1 on success. */
extern int GC←expand←hp(/* number←of←4K←blocks */);
/* Clear the set of root segments */
extern void GC←clear←roots();
/* Add a root segment */
extern void GC←add←roots(/* low←address, high←address←plus←1 */);
/* Add a displacement to the set of those considered valid by the */
/* collector. GC←register←displacement(n) means that if p was returned */
/* by GC←malloc, then (char *)p + n will be considered to be a valid */
/* pointer to n. N must be small and less than the size of p. */
/* (All pointers to the interior of objects from the stack are */
/* considered valid in any case. This applies to heap objects and */
/* static data.) */
/* Preferably, this should be called before any other GC procedures. */
/* Calling it later adds to the probability of excess memory */
/* retention. */
void GC←register←displacement(/* n */);
/* Explicitly trigger a collection. */
void GC←gcollect();
/* Enable incremental/generational collection. */
/* Not advisable unless dirty bits are */
/* available or most heap objects are */
/* pointerfree(atomic) or immutable. */
/* Don't use in leak finding mode. */
void GC←enable←incremental();
/* Debugging (annotated) allocation. GC←gcollect will check */
/* objects allocated in this way for overwrites, etc. */
# ifdef ←←STDC←←
extern void * GC←debug←malloc(size←t size←in←bytes,
char * descr←string, int descr←int);
extern void * GC←debug←malloc←atomic(size←t size←in←bytes,
char * descr←string, int descr←int);
extern void * GC←debug←malloc←uncollectable(size←t size←in←bytes,
char * descr←string, int descr←int);
extern void * GC←debug←malloc←stubborn(size←t size←in←bytes,
char * descr←string, int descr←int);
extern void GC←debug←free(void * object←addr);
extern void * GC←debug←realloc(void * old←object,
size←t new←size←in←bytes,
char * descr←string, int descr←int);
# else
extern char * GC←debug←malloc(/* size←in←bytes, descr←string, descr←int */);
extern char * GC←debug←malloc←atomic(/* size←in←bytes, descr←string,
descr←int */);
extern char * GC←debug←malloc←uncollectable(/* size←in←bytes, descr←string,
descr←int */);
extern char * GC←debug←malloc←stubborn(/* size←in←bytes, descr←string,
descr←int */);
extern void GC←debug←free(/* object←addr */);
extern char * GC←debug←realloc(/* old←object, new←size←in←bytes,
descr←string, descr←int */);
# endif
void GC←debug←change←stubborn(/* p */);
void GC←debug←end←stubborn←change(/* p */);
# ifdef GC←DEBUG
# define GC←MALLOC(sz) GC←debug←malloc(sz, ←←FILE←←, ←←LINE←←)
# define GC←MALLOC←ATOMIC(sz) GC←debug←malloc←atomic(sz, ←←FILE←←, ←←LINE←←)
# define GC←MALLOC←UNCOLLECTABLE(sz) GC←debug←malloc←uncollectable(sz, \
←←FILE←←, ←←LINE←←)
# define GC←REALLOC(old, sz) GC←debug←realloc(old, sz, ←←FILE←←, \
←←LINE←←)
# define GC←FREE(p) GC←debug←free(p)
# define GC←REGISTER←FINALIZER(p, f, d, of, od) \
GC←register←finalizer(GC←base(p), GC←debug←invoke←finalizer, \
GC←make←closure(f,d), of, od)
# define GC←MALLOC←STUBBORN(sz) GC←debug←malloc←stubborn(sz, ←←FILE←←, \
←←LINE←←)
# define GC←CHANGE←STUBBORN(p) GC←debug←change←stubborn(p)
# define GC←END←STUBBORN←CHANGE(p) GC←debug←end←stubborn←change(p)
# else
# define GC←MALLOC(sz) GC←malloc(sz)
# define GC←MALLOC←ATOMIC(sz) GC←malloc←atomic(sz)
# define GC←MALLOC←UNCOLLECTABLE(sz) GC←malloc←uncollectable(sz)
# define GC←REALLOC(old, sz) GC←realloc(old, sz)
# define GC←FREE(p) GC←free(p)
# define GC←REGISTER←FINALIZER(p, f, d, of, od) \
GC←register←finalizer(p, f, d, of, od)
# define GC←MALLOC←STUBBORN(sz) GC←malloc←stubborn(sz)
# define GC←CHANGE←STUBBORN(p) GC←change←stubborn(p)
# define GC←END←STUBBORN←CHANGE(p) GC←end←stubborn←change(p)
# endif
/* Finalization. Some of these primitives are grossly unsafe. */
/* The idea is to make them both cheap, and sufficient to build */
/* a safer layer, closer to PCedar finalization. */
/* The interface represents my conclusions from a long discussion */
/* with Alan Demers, Dan Greene, Carl Hauser, Barry Hayes, */
/* Christian Jacobi, and Russ Atkinson. It's not perfect, and */
/* probably nobody else agrees with it. Hans-J. Boehm 3/13/92 */
# ifdef ←←STDC←←
typedef void (*GC←finalization←proc)(void * obj, void * client←data);
# else
typedef void (*GC←finalization←proc)(/* void * obj, void * client←data */);
# endif
void GC←register←finalizer(/* void * obj,
GC←finalization←proc fn, void * cd,
GC←finalization←proc *ofn, void ** ocd */);
/* When obj is no longer accessible, invoke */
/* (*fn)(obj, cd). If a and b are inaccessible, and */
/* a points to b (after disappearing links have been */
/* made to disappear), then only a will be */
/* finalized. (If this does not create any new */
/* pointers to b, then b will be finalized after the */
/* next collection.) Any finalizable object that */
/* is reachable from itself by following one or more */
/* pointers will not be finalized (or collected). */
/* Thus cycles involving finalizable objects should */
/* be avoided, or broken by disappearing links. */
/* fn is invoked with the allocation lock held. It may */
/* not allocate. (Any storage it might need */
/* should be preallocated and passed as part of cd.) */
/* fn should terminate as quickly as possible, and */
/* defer extended computation. */
/* All but the last finalizer registered for an object */
/* is ignored. */
/* Finalization may be removed by passing 0 as fn. */
/* The old finalizer and client data are stored in */
/* *ofn and *ocd. */
/* Fn is never invoked on an accessible object, */
/* provided hidden pointers are converted to real */
/* pointers only if the allocation lock is held, and */
/* such conversions are not performed by finalization */
/* routines. */
/* The following routine may be used to break cycles between */
/* finalizable objects, thus causing cyclic finalizable */
/* objects to be finalized in the correct order. Standard */
/* use involves calling GC←register←disappearing←link(&p), */
/* where p is a pointer that is not followed by finalization */
/* code, and should not be considered in determining */
/* finalization order. */
int GC←register←disappearing←link(/* void ** link */);
/* Link should point to a field of a heap allocated */
/* object obj. *link will be cleared when obj is */
/* found to be inaccessible. This happens BEFORE any */
/* finalization code is invoked, and BEFORE any */
/* decisions about finalization order are made. */
/* This is useful in telling the finalizer that */
/* some pointers are not essential for proper */
/* finalization. This may avoid finalization cycles. */
/* Note that obj may be resurrected by another */
/* finalizer, and thus the clearing of *link may */
/* be visible to non-finalization code. */
/* There's an argument that an arbitrary action should */
/* be allowed here, instead of just clearing a pointer. */
/* But this causes problems if that action alters, or */
/* examines connectivity. */
/* Returns 1 if link was already registered, 0 */
/* otherwise. */
/* Only exists for backward compatibility. See below: */
int GC←general←register←disappearing←link(/* void ** link, void * obj */);
/* A slight generalization of the above. *link is */
/* cleared when obj first becomes inaccessible. This */
/* can be used to implement weak pointers easily and */
/* safely. Typically link will point to a location */
/* holding a disguised pointer to obj. In this way */
/* soft pointers are broken before any object */
/* reachable from them are finalized. Each link */
/* May be registered only once, i.e. with one obj */
/* value. This was added after a long email discussion */
/* with John Ellis. */
int GC←unregister←disappearing←link(/* void ** link */);
/* Returns 0 if link was not actually registered. */
/* Undoes a registration by either of the above two */
/* routines. */
/* Auxiliary fns to make finalization work correctly with displaced */
/* pointers introduced by the debugging allocators. */
# ifdef ←←STDC←←
void * GC←make←closure(GC←finalization←proc fn, void * data);
void GC←debug←invoke←finalizer(void * obj, void * data);
# else
char * GC←make←closure(/* GC←finalization←proc fn, char * data */);
void GC←debug←invoke←finalizer(/* void * obj, void * data */);
# endif
/* The following is intended to be used by a higher level */
/* (e.g. cedar-like) finalization facility. It is expected */
/* that finalization code will arrange for hidden pointers to */
/* disappear. Otherwise objects can be accessed after they */
/* have been collected. */
# ifdef I←HIDE←POINTERS
# ifdef ←←STDC←←
# define HIDE←POINTER(p) (~(size←t)(p))
# define REVEAL←POINTER(p) ((void *)(HIDE←POINTER(p)))
# else
# define HIDE←POINTER(p) (~(unsigned long)(p))
# define REVEAL←POINTER(p) ((char *)(HIDE←POINTER(p)))
# endif
/* Converting a hidden pointer to a real pointer requires verifying */
/* that the object still exists. This involves acquiring the */
/* allocator lock to avoid a race with the collector. */
typedef char * (*GC←fn←type)();
# ifdef ←←STDC←←
void * GC←call←with←alloc←lock(GC←fn←type fn, void * client←data);
# else
char * GC←call←with←alloc←lock(/* GC←fn←type fn, char * client←data */);
# endif
# endif
#endif