[m-dev.] for review: fix GC bug with asm_fast.gc.par
Fergus Henderson
fjh at cs.mu.OZ.AU
Tue Oct 3 14:15:57 AEDT 2000
For review by Tyson and/or Tom.
----------
Estimated hours taken: 20 (roughly -- including time spent by trd and conway)
Fix a GC-related bug that broke the asm_fast.gc.par grade on
some systems.
runtime/mercury_memory.h:
runtime/mercury_memory.c:
Add MR_GC_NEW_UNCOLLECTABLE() and MR_GC_malloc_uncollectable(),
which are like MR_GC_NEW() and MR_GC_malloc() except that they
call GC_MALLOC_UNCOLLECTABLE() rather than GC_MALLOC().
runtime/mercury_engine.c:
Allocate the engine with MR_GC_NEW_UNCOLLECTABLE() rather than
MR_GC_NEW(), so that it is safe to store the only pointer to it
in thread-local storage.
Workspace: /home/pgrad/fjh/ws/hg
Index: runtime/mercury_engine.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_engine.c,v
retrieving revision 1.26
diff -u -d -r1.26 mercury_engine.c
--- runtime/mercury_engine.c 2000/10/02 07:45:03 1.26
+++ runtime/mercury_engine.c 2000/10/03 02:46:15
@@ -142,7 +142,13 @@
{
MercuryEngine *eng;
- eng = MR_GC_NEW(MercuryEngine);
+ /*
+ ** We need to use MR_GC_NEW_UNCOLLECTABLE() here,
+ ** rather than MR_GC_NEW(), since the engine pointer
+ ** will normally be stored in thread-local storage, which is
+ ** not traced by the conservative garbage collector.
+ */
+ eng = MR_GC_NEW_UNCOLLECTABLE(MercuryEngine);
init_engine(eng);
return eng;
}
Index: runtime/mercury_memory.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_memory.c,v
retrieving revision 1.20
diff -u -d -r1.20 mercury_memory.c
--- runtime/mercury_memory.c 2000/09/12 19:51:06 1.20
+++ runtime/mercury_memory.c 2000/10/03 02:44:50
@@ -292,6 +292,10 @@
/*
** These routines allocate memory that will be scanned by the
** conservative garbage collector.
+**
+** XXX This is inefficient. If CONSERVATIVE_GC is enabled,
+** we should set `GC_oom_fn' (see boehm_gc/gc.h) rather than
+** testing the return value from GC_MALLOC() or GC_MALLOC_UNCOLLECTABLE().
*/
void *
@@ -301,6 +305,24 @@
#ifdef CONSERVATIVE_GC
ptr = GC_MALLOC(num_bytes);
+#else
+ ptr = malloc(num_bytes);
+#endif
+
+ if (ptr == NULL && num_bytes != 0) {
+ fatal_error("could not allocate memory");
+ }
+
+ return ptr;
+}
+
+void *
+MR_GC_malloc_uncollectable(size_t num_bytes)
+{
+ void *ptr;
+
+#ifdef CONSERVATIVE_GC
+ ptr = GC_MALLOC_UNCOLLECTABLE(num_bytes);
#else
ptr = malloc(num_bytes);
#endif
Index: runtime/mercury_memory.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_memory.h,v
retrieving revision 1.12
diff -u -d -r1.12 mercury_memory.h
--- runtime/mercury_memory.h 2000/08/03 06:18:49 1.12
+++ runtime/mercury_memory.h 2000/10/03 02:41:02
@@ -63,7 +63,7 @@
** Structures allocated with MR_malloc() and MR_realloc()
** must NOT contain pointers into GC'ed memory, because those
** pointers will never be traced by the conservative GC.
-** Use MR_GC_malloc() for that.
+** Use MR_GC_malloc() or MR_GC_malloc_uncollectable() for that.
**
** MR_NEW(type):
** allocates space for an object of the specified type.
@@ -103,7 +103,18 @@
** so the caller need not check.
**
** MR_GC_NEW(type):
+** Allocates space for an object of the specified type.
+** If conservative GC is enabled, the object will be garbage collected
+** when it is no longer referenced from GC-traced memory.
+** Memory allocated with malloc() (or MR_malloc() or MR_NEW())
+** is not GC-traced. Nor is thread-local storage.
+**
+** MR_GC_NEW_UNCOLLECTABLE(type):
** allocates space for an object of the specified type.
+** The object will not be garbage collected even if it is
+** not reference or only referenced from thread-local
+** storage or storage allocated with malloc().
+** It should be explicitly deallocated with MR_GC_free().
**
** MR_GC_NEW_ARRAY(type, num):
** allocates space for an array of objects of the specified type.
@@ -113,16 +124,27 @@
**
** MR_GC_malloc(bytes):
** allocates the given number of bytes.
+** If conservative GC is enabled, the memory will be garbage collected
+** when it is no longer referenced from GC-traced memory (see above).
+**
+** MR_GC_malloc_uncollectable(bytes):
+** allocates the given number of bytes.
+** The memory will not be garbage collected, and so
+** it should be explicitly deallocated using MR_GC_free().
**
** MR_GC_free(ptr):
** deallocates the memory.
*/
extern void *MR_GC_malloc(size_t num_bytes);
+extern void *MR_GC_malloc_uncollectable(size_t num_bytes);
extern void *MR_GC_realloc(void *ptr, size_t num_bytes);
#define MR_GC_NEW(type) \
((type *) MR_GC_malloc(sizeof(type)))
+
+#define MR_GC_NEW_UNCOLLECTABLE(type) \
+ ((type *) MR_GC_malloc_uncollectable(sizeof(type)))
#define MR_GC_NEW_ARRAY(type, num) \
((type *) MR_GC_malloc((num) * sizeof(type)))
--
Fergus Henderson <fjh at cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3 | -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list