[m-dev.] for review: clean up memory allocation routines

Fergus Henderson fjh at cs.mu.OZ.AU
Sat Oct 16 06:27:57 AEST 1999


Estimated hours taken: 6

Reorganize the routines for allocating and deallocating memory.

runtime/mercury_std.h:
runtime/mercury_misc.c:
runtime/mercury_memory.h:
runtime/mercury_memory.c:
	- Put the routines in the proper place.
		Previously the declarations and definitions of the memory
		allocation/deallocation routines were spread amoungst
		all four of these files; I moved the ones in mercury_std.h
		and mercury_misc.c so that they are now all defined
		in mercury_memory.{h,c}
	- Avoid unnecessary duplication
		The following routines did exactly the same thing,
		modulo bugs(!):
			allocate_bytes()	and newmem()
			deallocate_bytes()	and oldmem()
			make()			and allocate_object()
			make_many()		and allocate_array()
	- Use appropriate names.
		I added `MR_' prefixes, and ensured that macros that are not
		function-like macros use all uppercase.  I also used a more
		consistent naming scheme.
		Previously the names used were
			(1) checked_malloc, checked_realloc
			(2a) allocate_bytes, deallocate_bytes, reallocate_bytes,
				allocate_object, allocate_array, resize_array
			(2b) newmem, oldmem, resizemem,
				make, make_many, resize_many
		The new names are
			(1) MR_malloc, MR_free, MR_realloc,
				MR_NEW, MR_NEW_ARRAY, MR_RESIZE_ARRAY
			(2) MR_GC_malloc, MR_GC_free, MR_GC_realloc,
				MR_GC_NEW, MR_GC_NEW_ARRAY, MR_GC_RESIZE_ARRAY

runtime/*.[ch]:
trace/*.[ch]:
library/array.m:
library/benchmarking.m:
library/io.m:
library/std_util.m:
extras/odbc/odbc.m:
extras/aditi/aditi.m:
	Use the new names.

Workspace: /mount/autofs/home_mercury1/fjh/mercury-sparc
Index: extras/aditi/aditi.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/aditi/aditi.m,v
retrieving revision 1.3
diff -u -r1.3 aditi.m
--- aditi.m	1999/08/02 05:55:47	1.3
+++ aditi.m	1999/10/15 19:15:58
@@ -570,8 +570,8 @@
 
 	/* create temporary relation to hold the input tuple */
 	DEBUG(printf(""creating input temporary...""));
-	input_ticket = (ticket *) checked_malloc(sizeof(ticket));
-	output_ticket = (ticket *) checked_malloc(sizeof(ticket));
+	input_ticket = MR_NEW(ticket);
+	output_ticket = MR_NEW(ticket);
 	MADITI_check(ADITI_NAME(tmp_create)(&MADITI_ticket,
 		input_schema, input_ticket));
 	DEBUG(printf(""done\\n""));
@@ -582,7 +582,7 @@
 	** registers around each call to MADITI__attr_to_string,
 	** which calls back into Mercury. 
 	*/
-	input_save_area = make_many(Word, num_input_args * 2);
+	input_save_area = MR_GC_NEW_ARRAY(Word, num_input_args * 2);
 	save_registers();	
 	for (i = 0; i < num_input_args * 2; i++) {
 		input_save_area[i] = virtual_reg(MADITI_FIRST_INPUT + i);
@@ -616,7 +616,7 @@
 	/*
 	** We're done with the saved input arguments.
 	*/
-	oldmem(input_save_area);
+	MR_GC_free(input_save_area);
 
 	/* insert the input tuple into the relation */
 	DEBUG(printf(""adding input tuple...%s"", tuple));
@@ -632,12 +632,12 @@
 	/* drop the input relation */
 	DEBUG(printf(""dropping input temporary...""));
 	MADITI_check(ADITI_NAME(tmp_destroy)(input_ticket));
-	free(input_ticket);
+	MR_free(input_ticket);
 	DEBUG(printf(""done\\n""));
 
 	/* create cursor on the output relation */
 	DEBUG(printf(""opening output cursor...""));
-	cursor = (ticket *) checked_malloc(sizeof(ticket));
+	cursor = MR_NEW(ticket);
 	MADITI_check(ADITI_NAME(tmp_cursor_create)(output_ticket, cursor));
 	MADITI_check(ADITI_NAME(cursor_open)(cursor, CUR_FORWARD));
 	DEBUG(printf(""done\\n""));
@@ -698,7 +698,7 @@
 		** Allocate some space to hold the output arguments
 		** before we put them in registers. 
 		*/
-		output_save_area = make_many(Word, num_output_args);
+		output_save_area = MR_GC_NEW_ARRAY(Word, num_output_args);
 	
 		/* convert tuple, put output args in stack slots */
 		MADITI__init_posn(&pos);
@@ -721,7 +721,7 @@
 			virtual_reg(first_reg + i) = output_save_area[i];
 		}
 		restore_registers();
-		oldmem(output_save_area);
+		MR_GC_free(output_save_area);
 
 		found_result = TRUE;
 	}
@@ -744,12 +744,12 @@
 	/* destroy cursor */
 	MADITI_check(ADITI_NAME(cursor_destroy)(
 		(ticket *)MADITI_saved_cursor));
-	free((ticket *)MADITI_saved_cursor);
+	MR_free((ticket *)MADITI_saved_cursor);
 
 	/* destroy output temporary */
 	MADITI_check(ADITI_NAME(tmp_destroy)(
 		(ticket *)MADITI_saved_output_rel));
-	free((ticket *)MADITI_saved_output_rel);
+	MR_free((ticket *)MADITI_saved_output_rel);
 
 	save_transient_registers();
 }
Index: extras/odbc/odbc.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/extras/odbc/odbc.m,v
retrieving revision 1.7
diff -u -r1.7 odbc.m
--- odbc.m	1999/04/30 04:25:17	1.7
+++ odbc.m	1999/10/15 19:06:42
@@ -892,7 +892,7 @@
 	** Notes on memory allocation:
 	**
 	** C data structures (MODBC_Statement and MODBC_Column) are allocated 
-	** using newmem/oldmem. 
+	** using MR_GC_malloc/MR_GC_free.
 	**
 	** MODBC_Statement contains a statement handle which must be freed 
 	** using SQLFreeStmt.
@@ -905,7 +905,7 @@
 	** it is stored within a MODBC_Column.
 	**
 	** Other data types have a buffer which is allocated once using
-	** newmem.
+	** MR_GC_malloc.
 	*/
 
 	/*
@@ -914,7 +914,7 @@
 	** MODBC_CHUNK_SIZE must be a multiple of sizeof(Word).
 	*/
 #define MODBC_CHUNK_WORDS	1024
-#define MODBC_CHUNK_SIZE	MODBC_CHUNK_WORDS * sizeof(Word)
+#define MODBC_CHUNK_SIZE	(MODBC_CHUNK_WORDS * sizeof(Word))
 
 typedef enum {
 	MODBC_INT	= 0,	/* Word-sized Integer */
@@ -987,7 +987,7 @@
 
 
 		/* Doing manual deallocation of the statement object. */
-	statement = make(MODBC_Statement);
+	statement = MR_NEW(MODBC_Statement);
 		
 	statement->num_columns = 0;
 	statement->row = NULL;
@@ -1117,7 +1117,7 @@
 	** Allocate an array containing the info for each column.
 	** The extra column is because ODBC counts columns starting from 1.
 	*/
-	statement->row = make_many(MODBC_Column, num_columns + 1);
+	statement->row = MR_NEW_ARRAY(MODBC_Column, num_columns + 1);
 
 	/*
 	** Use SQLBindCol unless there are columns with no set maximum length.
@@ -1605,13 +1605,13 @@
 			if (! is_variable_length_sql_type(
 				    	stat->row[i].sql_type)) 
 			{
-				oldmem(stat->row[i].data);
+				MR_GC_free(stat->row[i].data);
 			}
 		    }
-		    oldmem(stat->row);
+		    MR_GC_free(stat->row);
 		}
 		rc = SQLFreeStmt(stat->stat_handle, SQL_DROP);
-		oldmem(stat);
+		MR_GC_free(stat);
 		return rc;
 	} else {
 		return SQL_SUCCESS;
Index: library/array.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/array.m,v
retrieving revision 1.60
diff -u -r1.60 array.m
--- array.m	1999/10/13 07:01:27	1.60
+++ array.m	1999/10/15 18:48:18
@@ -557,7 +557,7 @@
 		elements_to_copy = array_size;
 	}
 
-	array = (MR_ArrayType *) make_many(Word, array_size + 1);
+	array = (MR_ArrayType *) MR_GC_NEW_ARRAY(Word, array_size + 1);
 	array->size = array_size;
 	for (i = 0; i < elements_to_copy; i++) {
 		array->elements[i] = old_array->elements[i];
@@ -570,7 +570,7 @@
 	** since the mode on the old array is `array_di', it is safe to
 	** deallocate the storage for it
 	*/
-	oldmem(old_array);
+	MR_GC_free(old_array);
 
 	return array;
 }
@@ -603,7 +603,7 @@
 		fatal_error(""array__shrink: can't shrink to a larger size"");
 	}
 
-	array = (MR_ArrayType *) make_many(Word, array_size + 1);
+	array = (MR_ArrayType *) MR_GC_NEW_ARRAY(Word, array_size + 1);
 	array->size = array_size;
 	for (i = 0; i < array_size; i++) {
 		array->elements[i] = old_array->elements[i];
@@ -613,7 +613,7 @@
 	** since the mode on the old array is `array_di', it is safe to
 	** deallocate the storage for it
 	*/
-	oldmem(old_array);
+	MR_GC_free(old_array);
 
 	return array;
 }
Index: library/benchmarking.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/benchmarking.m,v
retrieving revision 1.25
diff -u -r1.25 benchmarking.m
--- benchmarking.m	1999/09/14 23:15:47	1.25
+++ benchmarking.m	1999/10/15 18:51:59
@@ -255,7 +255,7 @@
 	} else {
 		table_size = MR_memprof_types.num_entries;
 	}
-	table = make_many(ML_memprof_report_entry, table_size);
+	table = MR_GC_NEW_ARRAY(ML_memprof_report_entry, table_size);
 
 	/*
 	** Print the by-procedure memory profile
@@ -286,7 +286,7 @@
 	/*
 	** Deallocate space for the table
 	*/
-	oldmem(table);
+	MR_GC_free(table);
 
 	/*
 	** Print the overall memory usage
Index: library/io.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/io.m,v
retrieving revision 1.185
diff -u -r1.185 io.m
--- io.m	1999/10/03 10:40:50	1.185
+++ io.m	1999/10/15 18:51:10
@@ -1340,13 +1340,13 @@
 			/* Grow the read buffer */
 			read_buf_size = ML_IO_READ_LINE_GROW(read_buf_size);
 			if (read_buffer == initial_read_buffer) {
-				read_buffer = checked_malloc(read_buf_size
-						* sizeof(Char));
+				read_buffer = MR_NEW_ARRAY(Char,
+						read_buf_size);
 				memcpy(read_buffer, initial_read_buffer,
 					ML_IO_READ_LINE_START);
 			} else {
-				read_buffer = checked_realloc(read_buffer,
-						read_buf_size * sizeof(Char));
+				read_buffer = MR_RESIZE_ARRAY(read_buffer,
+						Char, read_buf_size);
 			}
 		}
 	}
@@ -1360,7 +1360,7 @@
 		RetString = NULL;
 	}
 	if (read_buffer != initial_read_buffer) {
-		free(read_buffer);
+		MR_free(read_buffer);
 	}
 	update_io(IO0, IO);
 ").
@@ -2687,7 +2687,7 @@
 	
 	f = fopen(filename, type);
 	if (!f) return NULL;
-	mf = make(MercuryFile);
+	mf = MR_NEW(MercuryFile);
 	mf->file = f;
 	mf->line_number = 1;
 	return mf;
@@ -2793,7 +2793,7 @@
 			mercury_io_error(mf, ""error closing file: %s"",
 				strerror(errno));
 		}
-		oldmem(mf);
+		MR_GC_free(mf);
 	}
 }
 
Index: library/std_util.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/std_util.m,v
retrieving revision 1.162
diff -u -r1.162 std_util.m
--- std_util.m	1999/10/13 07:01:29	1.162
+++ std_util.m	1999/10/15 18:23:01
@@ -2204,13 +2204,13 @@
 ** Expand the given data using its type_info, find its
 ** functor, arity, argument vector and type_info vector.
 ** 
-** The info.type_info_vector is allocated using newmem().
-** (We need to use newmem() rather than malloc(), since this
-** vector may contain pointers into the Mercury heap, and
-** memory allocated with malloc will not be traced by the
+** The info.type_info_vector is allocated using MR_GC_malloc().
+** (We need to use MR_GC_malloc() rather than MR_malloc() or malloc(),
+** since this vector may contain pointers into the Mercury heap, and
+** memory allocated with MR_malloc() or malloc() will not be traced by the
 ** Boehm collector.)
 ** It is the responsibility of the caller to deallocate this
-** memory (using oldmem()), and to copy any fields of this vector to
+** memory (using MR_GC_free()), and to copy any fields of this vector to
 ** the Mercury heap. The type_infos that the elements of
 ** this vector point to are either
 ** 	- already allocated on the heap.
@@ -2320,7 +2320,8 @@
                 if (info->need_args) {
                     info->argument_vector = (Word *) data_value;
     
-                    info->type_info_vector = newmem(info->arity * sizeof(Word));
+                    info->type_info_vector = MR_GC_NEW_ARRAY(Word,
+		    				info->arity);
 
                     for (i = 0; i < info->arity ; i++) {
                         Word *arg_pseudo_type_info;
@@ -2361,7 +2362,8 @@
                      */
                 info->argument_vector = (Word *) data_word_ptr;
 
-                info->type_info_vector = newmem(info->arity * sizeof(Word));
+                info->type_info_vector = MR_GC_NEW_ARRAY(Word,
+						info->arity);
 
                 for (i = 0; i < info->arity ; i++) {
                     Word *arg_pseudo_type_info;
@@ -2653,7 +2655,7 @@
 	** Free the allocated type_info_vector, since we just copied
 	** the stuff we want out of it.
 	*/
-	oldmem(info.type_info_vector);
+	MR_GC_free(info.type_info_vector);
 
 	return success;
 }
@@ -2855,7 +2857,7 @@
 	 * all its arguments onto the heap. 
 	 */
 
-	oldmem(info.type_info_vector);
+	MR_GC_free(info.type_info_vector);
 
 }").
 
Index: runtime/mercury_accurate_gc.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_accurate_gc.c,v
retrieving revision 1.6
diff -u -r1.6 mercury_accurate_gc.c
--- mercury_accurate_gc.c	1999/04/30 06:21:13	1.6
+++ mercury_accurate_gc.c	1999/10/15 16:58:59
@@ -331,7 +331,7 @@
             }
         }
 
-        free(type_params);
+        MR_free(type_params);
 
         result = MR_stack_walk_step(entry_layout, &return_label_layout,
             (Word **) &stack_pointer, &current_frame, &problem);
@@ -481,7 +481,7 @@
 {
 	MR_RootList node;
 
-	node = checked_malloc(sizeof(*node));
+	node = MR_malloc(sizeof(*node));
 	node->root = root_addr;
 	node->type_info = type_info;
 
Index: runtime/mercury_agc_debug.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_agc_debug.c,v
retrieving revision 1.9
diff -u -r1.9 mercury_agc_debug.c
--- mercury_agc_debug.c	1999/04/26 03:34:28	1.9
+++ mercury_agc_debug.c	1999/10/15 17:00:12
@@ -162,7 +162,7 @@
 			fflush(NULL);
 
 		}
-		free(type_params);
+		MR_free(type_params);
 
 		/* 
 		** Move to the next stack frame.
Index: runtime/mercury_array_macros.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_array_macros.h,v
retrieving revision 1.4
diff -u -r1.4 mercury_array_macros.h
--- mercury_array_macros.h	1999/03/26 04:43:58	1.4
+++ mercury_array_macros.h	1999/10/15 18:12:40
@@ -47,7 +47,7 @@
 **	int	widget_max = 0;
 **	int	widget_next = 0;
 **
-** where widgets is a pointer to a malloc'd array of items, widget_max
+** where widgets is a pointer to a MR_malloc'd array of items, widget_max
 ** gives the number of elements in the array, and widget_next is the
 ** index of the first free slot in the widgets array. Widget_max is
 ** zero if and only if widgets is NULL.
@@ -63,13 +63,12 @@
 	do {								    \
 		if (base##_next >= base##_max) {			    \
 			if (base##_max == 0) {				    \
-				base##_max = init;			    \
-				base##s = checked_malloc(		    \
-						base##_max * sizeof(type)); \
+				base##_max = (init);			    \
+				base##s = MR_NEW_ARRAY(type, base##_max);   \
 			} else {					    \
 				base##_max *= 2;			    \
-				base##s = checked_realloc(base##s, 	    \
-						base##_max * sizeof(type)); \
+				base##s = MR_RESIZE_ARRAY(base##s, type,    \
+						base##_max);		    \
 			}						    \
 		}							    \
 	} while(0)
@@ -85,15 +84,14 @@
 
 #define	MR_ensure_big_enough(slot, base, type, init)	  		    \
 	do {								    \
-		if (slot >= base##_max) {				    \
+		if ((slot) >= base##_max) {				    \
 			if (base##_max == 0) {				    \
-				base##_max = max(init, slot + 1);	    \
-				base##s = checked_malloc(		    \
-						base##_max * sizeof(type)); \
+				base##_max = max((init), (slot) + 1);	    \
+				base##s = MR_NEW_ARRAY(type, base##_max); \
 			} else {					    \
-				base##_max = max(base##_max * 2, slot + 1); \
-				base##s = checked_realloc(base##s, 	    \
-						base##_max * sizeof(type)); \
+				base##_max = max(base##_max * 2, (slot) + 1); \
+				base##s = MR_RESIZE_ARRAY(base##s, type,    \
+						base##_max);		    \
 			}						    \
 		}							    \
 	} while(0)
@@ -105,23 +103,21 @@
 ** base##_max variable.
 */
 
-#define	MR_ensure_big_enough2(slot, base, s1, s2, type, init)  		    \
-	do {								    \
-		if (slot >= base##_max) {				    \
-			if (base##_max == 0) {				    \
-				base##_max = max(init, slot + 1);	    \
-				base##s1 = checked_malloc(		    \
-						base##_max * sizeof(type)); \
-				base##s2 = checked_malloc(		    \
-						base##_max * sizeof(type)); \
-			} else {					    \
-				base##_max = max(base##_max * 2, slot + 1); \
-				base##s1 = checked_realloc(base##s1, 	    \
-						base##_max * sizeof(type)); \
-				base##s2 = checked_realloc(base##s2, 	    \
-						base##_max * sizeof(type)); \
-			}						    \
-		}							    \
+#define	MR_ensure_big_enough2(slot, base, s1, s2, type, init)  		      \
+	do {								      \
+		if ((slot) >= base##_max) {				      \
+			if (base##_max == 0) {				      \
+				base##_max = max((init), (slot) + 1);	      \
+				base##s1 = MR_NEW_ARRAY(type,	base##_max);  \
+				base##s2 = MR_NEW_ARRAY(type,	base##_max);  \
+			} else {					      \
+				base##_max = max(base##_max * 2, (slot) + 1); \
+				base##s1 = MR_RESIZE_ARRAY(base##s1, type,    \
+						base##_max);		      \
+				base##s2 = MR_RESIZE_ARRAY(base##s2, type,    \
+						base##_max);		      \
+			}						      \
+		}							      \
 	} while(0)
 
 /*
cvs diff: I know nothing about runtime/mercury_conf.h
Index: runtime/mercury_context.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_context.c,v
retrieving revision 1.19
diff -u -r1.19 mercury_context.c
--- mercury_context.c	1999/04/20 11:48:12	1.19
+++ mercury_context.c	1999/10/15 15:53:08
@@ -55,18 +55,18 @@
 {
 #ifdef	MR_THREAD_SAFE
 
-	MR_runqueue_lock = make(MercuryLock);
+	MR_runqueue_lock = MR_GC_NEW(MercuryLock);
 	pthread_mutex_init(MR_runqueue_lock, MR_MUTEX_ATTR);
 
-	MR_runqueue_cond = make(MercuryCond);
+	MR_runqueue_cond = MR_GC_NEW(MercuryCond);
 	pthread_cond_init(MR_runqueue_cond, MR_COND_ATTR);
 
-	free_context_list_lock = make(MercuryLock);
+	free_context_list_lock = MR_GC_NEW(MercuryLock);
 	pthread_mutex_init(free_context_list_lock, MR_MUTEX_ATTR);
 
 	pthread_mutex_init(&MR_global_lock, MR_MUTEX_ATTR);
 
-	MR_pending_contexts_lock = make(MercuryLock);
+	MR_pending_contexts_lock = MR_GC_NEW(MercuryLock);
 	pthread_mutex_init(MR_pending_contexts_lock, MR_MUTEX_ATTR);
 
 	MR_KEY_CREATE(&MR_engine_base_key, NULL);
@@ -160,7 +160,7 @@
 	MR_LOCK(free_context_list_lock, "create_context");
 	if (free_context_list == NULL) {
 		MR_UNLOCK(free_context_list_lock, "create_context i");
-		c = (MR_Context *) make(MR_Context);
+		c = MR_GC_NEW(MR_Context);
 		c->detstack_zone = NULL;
 		c->nondetstack_zone = NULL;
 #ifdef MR_USE_TRAIL
Index: runtime/mercury_dlist.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_dlist.c,v
retrieving revision 1.2
diff -u -r1.2 mercury_dlist.c
--- mercury_dlist.c	1997/11/23 07:21:18	1.2
+++ mercury_dlist.c	1999/10/15 16:05:37
@@ -9,7 +9,7 @@
 */
 
 #include	<stdio.h>
-#include	"mercury_std.h"
+#include	"mercury_memory.h"
 #include	"mercury_dlist.h"
 
 /*
@@ -21,7 +21,7 @@
 {
 	reg	List	*list;
 
-	list = make(List);
+	list = MR_GC_NEW(List);
 	ldata(list) = NULL;
 	next(list) = list;
 	prev(list) = list;
@@ -57,7 +57,7 @@
 		list = makelist0();
 	}
 
-	item = make(List);
+	item = MR_GC_NEW(List);
 	ldata(item) = data;
 	llength(list)++;
 
@@ -84,7 +84,7 @@
 		list = makelist0();
 	}
 
-	item = make(List);
+	item = MR_GC_NEW(List);
 	ldata(item) = data;
 	llength(list)++;
 
@@ -134,7 +134,7 @@
 		}
 	}
 
-	oldmem(list2);
+	MR_GC_free(list2);
 	return list1;
 }
 
@@ -173,7 +173,7 @@
 {
 	reg	List	*item;
 
-	item = make(List);
+	item = MR_GC_NEW(List);
 	ldata(item) = data;
 	llength(list)++;
 
@@ -194,7 +194,7 @@
 {
 	reg	List	*item;
 
-	item = make(List);
+	item = MR_GC_NEW(List);
 	ldata(item) = data;
 	llength(list)++;
 
@@ -246,7 +246,7 @@
 	next(prev(item)) = next(item);
 	prev(next(item)) = prev(item);
 
-	oldmem(item);
+	MR_GC_free(item);
 
 	return;
 }
@@ -254,7 +254,7 @@
 /*
 **	Free a whole list, including the header and maybe the data
 **	pointed to by the list. Of course, if they were not allocated
-**	by newmem, then all Hell will break loose.
+**	by MR_GC_malloc, then all Hell will break loose.
 */
 
 void 
@@ -276,10 +276,10 @@
 			func(ldata(item));
 		}
  
-		oldmem(item);
+		MR_GC_free(item);
 	}
 	 
-	oldmem(list);
+	MR_GC_free(list);
 
 	return;
 }
Index: runtime/mercury_engine.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_engine.c,v
retrieving revision 1.18
diff -u -r1.18 mercury_engine.c
--- mercury_engine.c	1999/09/27 05:20:44	1.18
+++ mercury_engine.c	1999/10/15 17:00:39
@@ -137,7 +137,7 @@
 {
 	MercuryEngine *eng;
 
-	eng = make(MercuryEngine);
+	eng = MR_GC_NEW(MercuryEngine);
 	init_engine(eng);
 	return eng;
 }
@@ -146,7 +146,7 @@
 destroy_engine(MercuryEngine *eng)
 {
 	finalize_engine(eng);
-	free(eng);
+	MR_GC_free(eng);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -352,7 +352,7 @@
 {
 	MercuryThreadList *new_element;
 
-	new_element = make(MercuryThreadList);
+	new_element = MR_GC_NEW(MercuryThreadList);
 	new_element->thread = MR_ENGINE(this_context)->owner_thread;
 	new_element->next = MR_ENGINE(saved_owners);
 	MR_ENGINE(saved_owners) = new_element;
@@ -389,7 +389,7 @@
 	{
 		val = tmp->thread;
 		MR_ENGINE(saved_owners) = tmp->next;
-		oldmem(tmp);
+		MR_GC_free(tmp);
 	} else {
 		val = 0;
 	}
Index: runtime/mercury_hash_table.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_hash_table.c,v
retrieving revision 1.1
diff -u -r1.1 mercury_hash_table.c
--- mercury_hash_table.c	1998/09/15 07:19:17	1.1
+++ mercury_hash_table.c	1999/10/15 15:35:26
@@ -29,7 +29,7 @@
 {
 	reg	int	i;
 
-	table->MR_ht_store = make_many(List *, table->MR_ht_size);
+	table->MR_ht_store = MR_GC_NEW_ARRAY(List *, table->MR_ht_size);
 
 	for (i = 0; i < table->MR_ht_size; i++) {
 		table->MR_ht_store[i] = NULL;
Index: runtime/mercury_label.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_label.c,v
retrieving revision 1.14
diff -u -r1.14 mercury_label.c
--- mercury_label.c	1999/08/09 08:48:29	1.14
+++ mercury_label.c	1999/10/15 16:17:15
@@ -75,8 +75,7 @@
 		entry_array_next = 0;
 		entry_array_size = INIT_ENTRY_SIZE;
 		entry_array_sorted = TRUE;
-		entry_array = checked_malloc(entry_array_size
-					* sizeof(MR_Entry));
+		entry_array = MR_NEW_ARRAY(MR_Entry, entry_array_size);
 #endif
 
 		MR_init_hash_table(internal_addr_table);
@@ -209,7 +208,7 @@
 
 	MR_do_init_label_tables();
 
-	internal = make(MR_Internal);
+	internal = MR_GC_NEW(MR_Internal);
 	internal->i_addr = addr;
 	internal->i_layout = label_layout;
 	internal->i_name = name;
Index: runtime/mercury_layout_util.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_layout_util.c,v
retrieving revision 1.10
diff -u -r1.10 mercury_layout_util.c
--- mercury_layout_util.c	1999/05/27 01:01:19	1.10
+++ mercury_layout_util.c	1999/10/15 16:17:39
@@ -82,7 +82,7 @@
 
 	if (vars->MR_slvs_tvars != NULL) {
 		count = vars->MR_slvs_tvars->MR_tp_param_count;
-		type_params = checked_malloc((count + 1) * sizeof(Word));
+		type_params = MR_NEW_ARRAY(Word, count + 1);
 
 		/*
 		** type_params should look like a typeinfo;
Index: runtime/mercury_library_types.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_library_types.h,v
retrieving revision 1.2
diff -u -r1.2 mercury_library_types.h
--- mercury_library_types.h	1998/11/09 10:24:37	1.2
+++ mercury_library_types.h	1999/10/15 15:14:27
@@ -14,6 +14,7 @@
 
 #include <stdio.h>		/* for `FILE' */
 #include "mercury_types.h"	/* for `Word' and `Integer' */
+#include "mercury_std.h"	/* for MR_VARIABLE_SIZED */
 
 /*
 ** The C `MercuryFile' type is used for the Mercury `io__stream' type
@@ -34,9 +35,9 @@
 
 typedef struct {
 	Integer size;
-	Word elements[1]; /* really this is variable-length */
+	Word elements[MR_VARIABLE_SIZED];
 } MR_ArrayType;
 
-#define MR_make_array(sz) ((MR_ArrayType *) make_many(Word, (sz) + 1))
+#define MR_make_array(sz) ((MR_ArrayType *) MR_GC_NEW_ARRAY(Word, (sz) + 1))
 
 #endif /* not MERCURY_LIBRARY_TYPES_H */
Index: runtime/mercury_memory.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_memory.c,v
retrieving revision 1.16
diff -u -r1.16 mercury_memory.c
--- mercury_memory.c	1999/04/20 11:48:14	1.16
+++ mercury_memory.c	1999/10/15 17:34:54
@@ -103,6 +103,10 @@
   static	char	*explain_context(void *context);
 #endif /* HAVE_SIGINFO */
 
+/*
+** Define the memory zones used by the Mercury runtime.
+** (The trail zone is declared in mercury_trail.c.)
+*/
 MemoryZone *detstack_zone;
 MemoryZone *nondetstack_zone;
 #ifndef CONSERVATIVE_GC
@@ -216,89 +220,93 @@
 	if (MR_memdebug) debug_memory();
 } /* end init_memory() */
 
-#ifdef	CONSERVATIVE_GC
+/*---------------------------------------------------------------------------*/
+
+/*
+** These routines allocate memory that will NOT be scanned by the
+** conservative garbage collector.  You MUST NOT uses these to
+** store pointers into GC'ed memory.
+** 
+*/
 
 void *
-allocate_bytes(size_t numbytes)
+MR_malloc(size_t n)
 {
-	void	*tmp;
+	void    *ptr;
 
-	tmp = GC_MALLOC(numbytes);
-	
-	if (tmp == NULL) {
-		fatal_error("could not allocate memory");
+	ptr = malloc(n);
+	if (ptr == NULL && n != 0) {
+		fatal_error("ran out of memory");
 	}
 
-	return tmp;
+	return ptr;
 }
 
-#else /* not CONSERVATIVE_GC */
-
 void *
-allocate_bytes(size_t numbytes)
+MR_realloc(void *old_ptr, size_t num_bytes)
 {
-	void	*tmp;
+	void    *ptr;
 
-	tmp = malloc(numbytes);
-	
-	if (tmp == NULL) {
-		fatal_error("could not allocate memory");
+	ptr = realloc(old_ptr, num_bytes);
+	if (ptr == NULL && num_bytes != 0) {
+		fatal_error("ran out of memory");
 	}
 
-	return tmp;
+	return ptr;
 }
 
-#endif
-
-void 
-deallocate_memory(void *ptr)
+char *
+MR_copy_string(const char *s)
 {
-#ifdef CONSERVATIVE_GC
-	GC_FREE(ptr);
-#else
-	free(ptr);
-#endif
+	int	len;
+	char	*copy;
+
+	len = strlen(s);
+	copy = MR_malloc(len + 1);
+	strcpy(copy, s);
+	return copy;
 }
 
+/*---------------------------------------------------------------------------*/
+
 /*
-** Note: checked_malloc()ed structures never contain pointers into GCed memory,
-** so we don't need to GC_malloc() them. (cf. newmem())
+** These routines allocate memory that will be scanned by the
+** conservative garbage collector.
 */
 
 void *
-checked_malloc(size_t n)
+MR_GC_malloc(size_t num_bytes)
 {
-	reg     void    *p;
+	void	*ptr;
 
-	p = malloc(n);
-	if (p == NULL && n != 0) {
-		fatal_error("ran out of memory");
+#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 p;
+	return ptr;
 }
 
 void *
-checked_realloc(void *old, size_t n)
+MR_GC_realloc(void *old_ptr, size_t num_bytes)
 {
-	reg     void    *p;
+	void    *ptr;
 
-	p = realloc(old, n);
-	if (p == NULL && n != 0) {
+#ifdef	CONSERVATIVE_GC
+	ptr = GC_REALLOC(old_ptr, num_bytes);
+#else
+	ptr = realloc(old_ptr, num_bytes);
+#endif
+	if (ptr == NULL && num_bytes != 0) {
 		fatal_error("ran out of memory");
 	}
 
-	return p;
+	return ptr;
 }
 
-char *
-MR_copy_string(const char *s)
-{
-	int	len;
-	char	*copy;
-
-	len = strlen(s);
-	copy = checked_malloc(len + 1);
-	strcpy(copy, s);
-	return copy;
-}
+/*---------------------------------------------------------------------------*/
Index: runtime/mercury_memory.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_memory.h,v
retrieving revision 1.9
diff -u -r1.9 mercury_memory.h
--- mercury_memory.h	1998/11/09 14:35:34	1.9
+++ mercury_memory.h	1999/10/15 17:44:00
@@ -20,10 +20,11 @@
 
 #include "mercury_memory_zones.h"
 
-#include <stdlib.h>		/* for size_t */
+#include <stddef.h>		/* for size_t */
 
 #include "mercury_types.h"	/* for Word */
-#include "mercury_std.h"		/* for bool */
+#include "mercury_std.h"	/* for bool */
+#include "gc.h"			/* for GC_FREE */
 
 
 #ifdef	MR_LOWLEVEL_DEBUG
@@ -45,55 +46,105 @@
 
 extern	void	init_memory(void);
 extern	void	init_heap(void);
-extern	void	debug_memory(void);
+
+#ifdef CONSERVATIVE_GC
+  extern void	MR_init_conservative_GC(void);
+#endif
+
+/*---------------------------------------------------------------------------*/
 
 /*
-** allocate_bytes() allocates the given number of bytes.
+** MR_malloc() and MR_realloc() are like the standard C
+** malloc() and realloc() functions, except that the return values
+** are checked.
+**
+** 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.
+**
+** MR_NEW(type):
+**	allocates space for an object of the specified type.
 **
-** allocate_object(type) allocates space for an object of the specified type.
+** MR_NEW_ARRAY(type, num):
+**	allocates space for an array of objects of the specified type.
 **
-** allocate_array(type, num) allocates space for an array of objects of the
-** specified type.
+** MR_RESIZE_ARRAY(ptr, type, num):
+**	resizes the array, as with realloc().
 **
-** If shared memory is being used, these allocation routines will allocate
-** in shared memory.
+** MR_malloc(bytes):
+**	allocates the given number of bytes.
+**
+** MR_free(ptr):
+**	deallocates the memory.
 */
 
-extern	void	*allocate_bytes(size_t numbytes);
+void	*MR_malloc(size_t n);
+void	*MR_realloc(void *old, size_t n);
+#define MR_free(ptr) free(ptr)
 
-#define allocate_object(type) \
-	((type *)allocate_bytes(sizeof(type)))
+#define MR_NEW(type) \
+	((type *) MR_malloc(sizeof(type)))
 
-#define allocate_array(type, num) \
-	((type *)allocate_bytes((num) * sizeof(type)))
+#define MR_NEW_ARRAY(type, num) \
+	((type *) MR_malloc((num) * sizeof(type)))
 
-/*
-** deallocate_memory() deallocates the memory allocated by one of the
-** allocate_* functions.
-*/
+#define MR_RESIZE_ARRAY(ptr, type, num) \
+	((type *) MR_realloc((ptr), (num) * sizeof(type)))
 
-void deallocate_memory(void *);
 
 /*
-** checked_malloc() and checked_realloc() are like the standard C
-** malloc() and realloc() functions, except that the return values
-** are checked.
+** These routines all allocate memory that will be traced by the 
+** conservative garbage collector, if conservative GC is enabled.
+** (For the native GC, you need to call MR_add_root() to register roots.)
+** These routines all check for a null return value themselves,
+** so the caller need not check.
+**
+** MR_GC_NEW(type):
+**	allocates space for an object of the specified type.
 **
-** NOTE: checked_malloc()ed and checked_realloc()ed structures must
-** never contain pointers into GCed memory, otherwise those pointers
-** will never be traced.
+** MR_GC_NEW_ARRAY(type, num):
+**	allocates space for an array of objects of the specified type.
+**
+** MR_GC_RESIZE_ARRAY(ptr, type, num):
+**	resizes the array, as with realloc().
+**
+** MR_GC_malloc(bytes):
+**	allocates the given number of bytes.
+**
+** MR_GC_free(ptr):
+**	deallocates the memory.
 */
 
-#include <stddef.h>	/* for size_t */
-void	*checked_malloc(size_t n);
-void	*checked_realloc(void *old, size_t n);
+extern	void	*MR_GC_malloc(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_ARRAY(type, num) \
+	((type *) MR_GC_malloc((num) * sizeof(type)))
+
+#define MR_GC_RESIZE_ARRAY(ptr, type, num) \
+	((type *) MR_GC_realloc((ptr), (num) * sizeof(type)))
+
+#ifdef CONSERVATIVE_GC
+  #define MR_GC_free(ptr) GC_FREE(ptr)
+#else
+  #define MR_GC_free(ptr) free(ptr)
+#endif
+
+/*---------------------------------------------------------------------------*/
 
 /*
-** MR_copy_string makes a copy of the given string with checked_malloc.
+** MR_copy_string makes a copy of the given string,
+** using memory allocated with MR_malloc().
 */
 
 char	*MR_copy_string(const char *s);
 
+/*---------------------------------------------------------------------------*/
+
 /*
 ** `unit' is the size of the minimum unit of memory we allocate (in bytes).
 ** `page_size' is the size of a single page of memory.
@@ -102,6 +153,8 @@
 extern	size_t          unit;
 extern	size_t          page_size;
 
+/*---------------------------------------------------------------------------*/
+
 /*
 ** Users need to call MR_add_root() for any global variable which
 ** contains pointers to the Mercury heap.  This information is only
@@ -113,5 +166,7 @@
 #else
   #define MR_add_root(root_ptr, type_info) /* nothing */
 #endif
+
+/*---------------------------------------------------------------------------*/
 
 #endif /* not MERCURY_MEMORY_H */
Index: runtime/mercury_memory_zones.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_memory_zones.c,v
retrieving revision 1.6
diff -u -r1.6 mercury_memory_zones.c
--- mercury_memory_zones.c	1998/09/29 05:11:01	1.6
+++ mercury_memory_zones.c	1999/10/15 17:28:57
@@ -132,7 +132,7 @@
 {
 
 #ifdef  MR_THREAD_SAFE
-	free_memory_zones_lock = make(MercuryLock);
+	free_memory_zones_lock = MR_GC_NEW(MercuryLock);
 	pthread_mutex_init(free_memory_zones_lock, MR_MUTEX_ATTR);
 #endif
 
@@ -147,7 +147,7 @@
 
 	offset_counter = 0;
 
-	offset_vector = allocate_array(size_t, CACHE_SLICES - 1);
+	offset_vector = MR_GC_NEW_ARRAY(size_t, CACHE_SLICES - 1);
 
 	fake_reg_offset = (Unsigned) MR_fake_reg % pcache_size;
 
@@ -170,7 +170,7 @@
 	*/
 	MR_LOCK(free_memory_zones_lock, "get_zone");
 	if (free_memory_zones == NULL) {
-		zone = (MemoryZone *) make(MemoryZone);
+		zone = MR_GC_NEW(MemoryZone);
 	} else {
 		zone = free_memory_zones;
 		free_memory_zones = free_memory_zones->next;
Index: runtime/mercury_misc.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_misc.c,v
retrieving revision 1.19
diff -u -r1.19 mercury_misc.c
--- mercury_misc.c	1999/08/09 08:48:29	1.19
+++ mercury_misc.c	1999/10/15 14:56:36
@@ -422,48 +422,6 @@
 	fprintf(stdout, "\n");
 }
 
-void *
-newmem(size_t n)
-{
-	reg	void	*p;
-
-#ifdef CONSERVATIVE_GC
-	p = GC_MALLOC(n);
-#else
-	p = malloc(n);
-#endif
-	if (p == NULL && n != 0) {
-		fatal_error("ran out of memory");
-	}
-
-	return p;
-}
-
-void 
-oldmem(void *p)
-{
-#ifdef CONSERVATIVE_GC
-	GC_FREE(p);
-#else
-	free(p);
-#endif
-}
-
-void* 
-resizemem(void *p, size_t size)
-{
-#ifdef CONSERVATIVE_GC
-	p = GC_REALLOC(p, size);
-#else
-	p = realloc(p, size);
-#endif
-	if (p == NULL) {
-		fatal_error("ran out of memory");
-	}
-
-	return p;
-}
-
 void
 MR_warning(const char *fmt, ...)
 {
Index: runtime/mercury_misc.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_misc.h,v
retrieving revision 1.17
diff -u -r1.17 mercury_misc.h
--- mercury_misc.h	1999/10/04 15:33:54	1.17
+++ mercury_misc.h	1999/10/15 16:18:04
@@ -8,7 +8,6 @@
 ** mercury_misc.h -	debugging messages,
 **			MR_warning(),
 **			fatal_error(),
-**			checked_malloc(),
 **			MR_memcpy
 **			MR_fd_zero
 */
Index: runtime/mercury_prof_mem.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_prof_mem.c,v
retrieving revision 1.5
diff -u -r1.5 mercury_prof_mem.c
--- mercury_prof_mem.c	1999/04/09 01:19:49	1.5
+++ mercury_prof_mem.c	1999/10/15 15:01:33
@@ -21,7 +21,7 @@
 
 #include "mercury_prof_mem.h"
 
-#include "mercury_std.h"	/* for newmem() */
+#include "mercury_memory.h"	/* for MR_GC_malloc() */
 #include "mercury_types.h"	/* for Word */
 
 /*----------------------------------------------------------------------------*/
@@ -69,7 +69,7 @@
 
 	/* Here we waste a bit of space but hopefully not to much */
 	if (mem_left < size) {
-		next = newmem(MEMORY_BLOCK * size);
+		next = MR_GC_malloc(MEMORY_BLOCK * size);
 		mem_left = MEMORY_BLOCK * size;
 	}
 
Index: runtime/mercury_prof_mem.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_prof_mem.h,v
retrieving revision 1.4
diff -u -r1.4 mercury_prof_mem.h
--- mercury_prof_mem.h	1999/04/06 14:53:03	1.4
+++ mercury_prof_mem.h	1999/10/15 15:03:14
@@ -17,11 +17,11 @@
 #include <stddef.h>	/* for size_t */
 
 /*
-** MR_prof_malloc() allocates memory in large chunks using newmem(),
+** MR_prof_malloc() allocates memory in large chunks using MR_GC_malloc(),
 ** and then doles out portions of these chunks one at a time.
-** We use prof_malloc() to reduce the chance of calling newmem()
-** from a profiling interrupt that interrupted another call to newmem().
-** Doing that is bad news, because newmem() is not re-entrant.
+** We use MR_prof_malloc() to reduce the chance of calling MR_GC_malloc()
+** from a profiling interrupt that interrupted another call to MR_GC_malloc().
+** Doing that is bad news, because MR_GC_malloc() is not re-entrant.
 **
 ** Note that the current implementation of MR_prof_malloc only guarantees
 ** that the memory will be Word-aligned; if you want to allocate types
Index: runtime/mercury_stacks.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_stacks.c,v
retrieving revision 1.3
diff -u -r1.3 mercury_stacks.c
--- mercury_stacks.c	1999/09/15 01:03:40	1.3
+++ mercury_stacks.c	1999/10/15 15:51:54
@@ -145,7 +145,7 @@
 {
 	struct MR_CutGeneratorListNode	*node;
 
-	node = make(struct MR_CutGeneratorListNode);
+	node = MR_GC_NEW(struct MR_CutGeneratorListNode);
 	node->generator_ptr = generator_ptr;
 	node->next_generator = MR_cut_stack[MR_cut_next - 1].generators;
 	MR_cut_stack[MR_cut_next - 1].generators = node;
Index: runtime/mercury_std.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_std.h,v
retrieving revision 1.7
diff -u -r1.7 mercury_std.h
--- mercury_std.h	1999/04/22 06:14:00	1.7
+++ mercury_std.h	1999/10/15 15:05:03
@@ -54,11 +54,6 @@
 
 #define	ungetchar(c)		ungetc(c, stdin)
 
-/* XXX these should go in mercury_memory.h or mercury_heap.h */
-#define make(t)			((t *) newmem(sizeof(t)))
-#define make_many(t, n)		((t *) newmem((n) * sizeof(t)))
-#define resize_many(t, p, n)	((t *) resizemem((p), (n) * sizeof(t)))
-
 #ifndef	TRUE
 #define	TRUE		1
 #endif
@@ -105,10 +100,5 @@
 #endif
 
 /*---------------------------------------------------------------------------*/
-
-/* XXX these should go in mercury_memory.h or mercury_heap.h */
-extern	void	*newmem(size_t);
-extern	void	*resizemem(void *, size_t);
-extern	void	oldmem(void *);
 
 #endif /* not MERCURY_STD_H */
Index: runtime/mercury_tabling.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_tabling.c,v
retrieving revision 1.12
diff -u -r1.12 mercury_tabling.c
--- mercury_tabling.c	1999/10/08 02:56:18	1.12
+++ mercury_tabling.c	1999/10/15 15:52:02
@@ -1434,7 +1434,7 @@
 		}
 #endif
 	} else {
-		MR_cur_leader->resume_info = make(MR_ResumeInfo);
+		MR_cur_leader->resume_info = MR_GC_NEW(MR_ResumeInfo);
 
 		save_transient_registers();
 		save_state(&(MR_cur_leader->resume_info->leader_state),
Index: runtime/mercury_thread.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_thread.c,v
retrieving revision 1.9
diff -u -r1.9 mercury_thread.c
--- mercury_thread.c	1999/09/16 09:24:40	1.9
+++ mercury_thread.c	1999/10/15 15:52:21
@@ -33,7 +33,7 @@
 	pthread_attr_t attrs;
 	int err;
 
-	thread = make(MercuryThread);
+	thread = MR_GC_NEW(MercuryThread);
 	pthread_attr_init(&attrs);
 	err = pthread_create(thread, &attrs, create_thread_2, (void *) goal);
 
Index: runtime/mercury_type_info.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_type_info.c,v
retrieving revision 1.22
diff -u -r1.22 mercury_type_info.c
--- mercury_type_info.c	1999/09/27 05:20:51	1.22
+++ mercury_type_info.c	1999/10/15 15:08:42
@@ -149,8 +149,8 @@
 	**
 	** NOTE: If you are changing this code, you might also need
 	** to change the code in MR_make_type_info in this module 
-	** which does much the same thing, only allocating using newmem
-	** instead of on the heap.
+	** which does much the same thing, only allocating using MR_GC_malloc()
+	** instead of on the Mercury heap.
 	*/
 
 Word * 
@@ -389,8 +389,8 @@
 {
 	while (allocated != NULL) {
 		MR_MemoryList next = allocated->next;
-		oldmem(allocated->data);
-		oldmem(allocated);
+		MR_GC_free(allocated->data);
+		MR_GC_free(allocated);
 		allocated = next;
 	}
 }
@@ -410,22 +410,22 @@
 	** arg_pseudo_type_info with all the type variables filled in.
 	** If there are no type variables to fill in, we return the
 	** arg_pseudo_type_info, unchanged. Otherwise, we allocate
-	** memory using newmem().  Any such memory allocated will be
+	** memory using MR_GC_malloc().  Any such memory allocated will be
 	** inserted into the list of allocated memory cells.
 	** It is the caller's responsibility to free these cells
 	** by calling MR_deallocate() on the list when they are no longer
 	** needed.
 	**
 	** This code could be tighter. In general, we want to
-	** handle our own allocations rather than using newmem().
-	** (Note: we need to use newmem() rather than malloc()
-	** because the Boehm collector doesn't trace memory
-	** allocated with malloc().)
+	** handle our own allocations rather than using MR_GC_malloc().
+	** (Note: we need to use MR_GC_malloc() rather than malloc()
+	** or MR_malloc() because the Boehm collector doesn't trace memory
+	** allocated with malloc() or MR_malloc().)
 	**
 	** NOTE: If you are changing this code, you might also need
 	** to change the code in MR_create_type_info (defined above),
 	** which does much the same thing, only allocating on the 
-	** heap instead of using newmem.
+	** Mercury heap instead of using MR_GC_malloc().
 	*/
 
 Word *
@@ -492,8 +492,8 @@
 				** allocate a new type_info and copy the
 				** data across from arg_pseudo_type_info
 				*/
-				type_info = newmem(
-					(arity + extra_args) * sizeof(Word));
+				type_info = MR_GC_NEW_ARRAY(Word,
+					arity + extra_args);
 				memcpy(type_info, arg_pseudo_type_info,
 					(arity + extra_args) * sizeof(Word));
 				/*
@@ -501,7 +501,7 @@
 				** list of allocated memory cells, so we can
 				** free it later on
 				*/
-				node = newmem(sizeof(*node));
+				node = MR_GC_malloc(sizeof(*node));
 				node->data = type_info;
 				node->next = *allocated;
 				*allocated = node;
Index: runtime/mercury_wrapper.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/mercury_wrapper.c,v
retrieving revision 1.46
diff -u -r1.46 mercury_wrapper.c
--- mercury_wrapper.c	1999/10/10 02:35:18	1.46
+++ mercury_wrapper.c	1999/10/15 17:32:45
@@ -365,8 +365,8 @@
 
 /*
 ** Given a string, parse it into arguments and create an argv vector for it.
-** Returns args, argv, and argc.  It is the caller's responsibility to oldmem()
-** args and argv when they are no longer needed.
+** Returns args, argv, and argc.  It is the caller's responsibility to
+** MR_GC_free() args and argv when they are no longer needed.
 */
 
 static void
@@ -428,8 +428,8 @@
 	/*
 	** Allocate the space
 	*/
-	args = make_many(char, args_len);
-	argv = make_many(char *, argc + 1);
+	args = MR_GC_NEW_ARRAY(char, args_len);
+	argv = MR_GC_NEW_ARRAY(char *, argc + 1);
 
 	/*
 	** Now do a pass over the string, copying the arguments into `args'
@@ -519,18 +519,18 @@
 		** to getopt().
 		*/
 		cmd = "mercury_runtime ";
-		dummy_command_line = make_many(char,
+		dummy_command_line = MR_GC_NEW_ARRAY(char,
 					strlen(options) + strlen(cmd) + 1);
 		strcpy(dummy_command_line, cmd);
 		strcat(dummy_command_line, options);
 		
 		make_argv(dummy_command_line, &arg_str, &argv, &argc);
-		oldmem(dummy_command_line);
+		MR_GC_free(dummy_command_line);
 
 		process_options(argc, argv);
 
-		oldmem(arg_str);
-		oldmem(argv);
+		MR_GC_free(arg_str);
+		MR_GC_free(argv);
 	}
 }
 
Index: trace/mercury_trace_alias.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_alias.c,v
retrieving revision 1.1
diff -u -r1.1 mercury_trace_alias.c
--- mercury_trace_alias.c	1998/10/16 06:20:11	1.1
+++ mercury_trace_alias.c	1999/10/15 17:47:47
@@ -53,8 +53,8 @@
 
 	MR_alias_records[slot].MR_alias_name = MR_copy_string(name);
 	MR_alias_records[slot].MR_alias_word_count = word_count;
-	MR_alias_records[slot].MR_alias_words = checked_malloc(word_count
-							* sizeof(char *));
+	MR_alias_records[slot].MR_alias_words = MR_NEW_ARRAY(char *,
+							word_count);
 	for (i = 0; i < word_count; i++) {
 		MR_alias_records[slot].MR_alias_words[i]
 			= MR_copy_string(words[i]);
Index: trace/mercury_trace_declarative.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_declarative.c,v
retrieving revision 1.10
diff -u -r1.10 mercury_trace_declarative.c
--- mercury_trace_declarative.c	1999/09/27 05:20:57	1.10
+++ mercury_trace_declarative.c	1999/10/15 18:29:59
@@ -341,7 +341,7 @@
 		MR_edt_parent = (MR_Edt_Node *) MR_based_framevar(
 				MR_saved_curfr(saved_regs), decl_slot + 1);
 	}
-	deallocate_memory(edt_node);
+	MR_free(edt_node);
 }
 
 /*
@@ -399,8 +399,8 @@
 	}
 
 	arg_count = MR_all_desc_var_count(vars);
-	arg_values = allocate_array(Word, arg_count);
-	arg_types = allocate_array(Word, arg_count);
+	arg_values = MR_NEW_ARRAY(Word, arg_count);
+	arg_types = MR_NEW_ARRAY(Word, arg_count);
 
 	base_sp = MR_saved_sp(saved_regs);
 	base_curfr = MR_saved_curfr(saved_regs);
@@ -479,7 +479,7 @@
 {
 	MR_Edt_Node 	*edt_node;
 
-	edt_node = allocate_object(MR_Edt_Node);
+	edt_node = MR_NEW(MR_Edt_Node);
 	edt_node->MR_edt_node_tag = node_tag;
 	edt_node->MR_edt_node_layout = layout;
 	edt_node->MR_edt_node_path = NULL;
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.54
diff -u -r1.54 mercury_trace_internal.c
--- mercury_trace_internal.c	1999/10/15 10:17:48	1.54
+++ mercury_trace_internal.c	1999/10/15 18:42:36
@@ -334,7 +334,7 @@
 		return;
 	}
 
-	buf = checked_malloc(strlen(env) + strlen(MDBRC_FILENAME) + 2);
+	buf = MR_NEW_ARRAY(char, strlen(env) + strlen(MDBRC_FILENAME) + 2);
 	(void) strcpy(buf, env);
 	(void) strcat(buf, "/");
 	(void) strcat(buf, MDBRC_FILENAME);
@@ -343,7 +343,7 @@
 		fclose(fp);
 	}
 
-	free(buf);
+	MR_free(buf);
 }
 
 static void
@@ -475,8 +475,8 @@
 			event_info, event_details, jumpaddr);
 	}
 
-	free(line);
-	free(orig_words);
+	MR_free(line);
+	MR_free(orig_words);
 
 	return next;
 }
@@ -1111,7 +1111,7 @@
 			len += strlen(words[i]) + 1;
 		}
 		len++;
-		MR_mmc_options = realloc(MR_mmc_options, len);
+		MR_mmc_options = MR_realloc(MR_mmc_options, len);
 
 		/* copy the arguments to MR_mmc_options */
 		MR_mmc_options[0] = '\0';
@@ -1432,7 +1432,7 @@
 						confirmed = TRUE;
 					}
 
-					free(line2);
+					MR_free(line2);
 				}
 			}
 
@@ -1774,7 +1774,7 @@
 	while ((text = MR_trace_getline("cat> ", MR_mdb_in, MR_mdb_out))
 			!= NULL) {
 		if (streq(text, "end")) {
-			free(text);
+			MR_free(text);
 			break;
 		}
 
@@ -1788,7 +1788,7 @@
 		next_char_slot += line_len;
 		doc_chars[next_char_slot] = '\n';
 		next_char_slot += 1;
-		free(text);
+		MR_free(text);
 	}
 
 	doc_chars[next_char_slot] = '\0';
@@ -1833,8 +1833,8 @@
 ** given by *word_count.
 **
 ** The lifetime of the elements of the *words array expires when
-** the line array is freed or further modified or when MR_trace_parse_line
-** is called again, whichever comes first.
+** the line array is MR_free()'d or further modified or when
+** MR_trace_parse_line is called again, whichever comes first.
 **
 ** The return value is NULL if everything went OK, and an error message
 ** otherwise.
@@ -1877,7 +1877,7 @@
 			/* Only part of the first word constitutes a number. */
 			/* Put it in an extra word at the start. */
 			MR_ensure_big_enough(raw_word_count, raw_word,
-				char **, MR_INIT_WORD_COUNT);
+				char *, MR_INIT_WORD_COUNT);
 
 			for (i = raw_word_count; i > 0; i--) {
 				raw_words[i] = raw_words[i-1];
@@ -1944,7 +1944,7 @@
 			return token_number;
 		}
 
-		MR_ensure_big_enough(token_number, word, char **,
+		MR_ensure_big_enough(token_number, word, char *,
 			MR_INIT_WORD_COUNT);
 		words[token_number] = line + char_pos;
 
@@ -1985,7 +1985,7 @@
 	if (MR_trace_lookup_alias(alias_key, &alias_words, &alias_word_count))
 	{
 		MR_ensure_big_enough(*word_count + alias_word_count,
-			*word, const char *, MR_INIT_WORD_COUNT);
+			*word, char *, MR_INIT_WORD_COUNT);
 
 		/* Move the original words (except the alias key) up. */
 		for (i = *word_count - 1; i >= alias_copy_start; i--) {
@@ -2033,7 +2033,7 @@
 /*
 ** If there any lines waiting in the queue, return the first of these.
 ** If not, print the prompt to mdb_out, read a line from mdb_in,
-** and return it in a malloc'd buffer holding the line (without the final
+** and return it in a MR_malloc'd buffer holding the line (without the final
 ** newline).
 ** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
 ** occurs on an empty line, return NULL.
@@ -2082,7 +2082,7 @@
 			MR_line_tail = NULL;
 		}
 
-		free(old);
+		MR_free(old);
 		return contents;
 	} else {
 		return NULL;
@@ -2094,7 +2094,7 @@
 {
 	MR_Line	*line;
 
-	line = checked_malloc(sizeof(MR_Line));
+	line = MR_NEW(MR_Line);
 	line->MR_line_contents = MR_copy_string(contents);
 	line->MR_line_next = MR_line_head;
 
@@ -2108,15 +2108,9 @@
 MR_insert_line_at_tail(const char *contents)
 {
 	MR_Line	*line;
-	char	*copy;
-	int	len;
 
-	len = strlen(contents);
-	copy = checked_malloc(len + 1);
-	strcpy(copy, contents);
-
-	line = checked_malloc(sizeof(MR_Line));
-	line->MR_line_contents = copy;
+	line = MR_NEW(MR_Line);
+	line->MR_line_contents = MR_copy_string(contents);
 	line->MR_line_next = NULL;
 
 	if (MR_line_tail == NULL) {
Index: trace/mercury_trace_readline.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_readline.c,v
retrieving revision 1.2
diff -u -r1.2 mercury_trace_readline.c
--- mercury_trace_readline.c	1999/10/14 05:25:23	1.2
+++ mercury_trace_readline.c	1999/10/15 18:13:40
@@ -39,8 +39,8 @@
 
 /*
 ** Print the prompt to the `out' file, read a line from the `in' file,
-** and return it in a malloc'd buffer holding the line (without the final
-** newline).
+** and return it in a MR_malloc'd buffer holding the line (without the
+** final newline).
 ** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
 ** occurs on an empty line, return NULL.
 */
@@ -64,6 +64,21 @@
 
 	line = readline((char *) prompt);
 
+	/*
+	** readline() allocates with malloc(), and we want
+	** to return something allocated with MR_malloc(),
+	** but that's OK, because MR_malloc() and malloc()
+	** are interchangable.
+	*/
+#if 0
+	{
+		char *tmp = line;
+
+		line = MR_copy_string(line);
+		free(tmp);
+	}
+#endif
+
 	if (line != NULL && line[0] != '\0') {
 		add_history(line);
 	}
@@ -74,7 +89,7 @@
 }
 
 /*
-** Read a line from a file, and return a pointer to a malloc'd buffer
+** Read a line from a file, and return a pointer to a MR_malloc'd buffer
 ** holding the line (without the final newline). If EOF occurs on a
 ** nonempty line, treat the EOF as a newline; if EOF occurs on an empty
 ** line, return NULL.
@@ -101,7 +116,7 @@
 		contents[i] = '\0';
 		return contents;
 	} else {
-		free(contents);
+		MR_free(contents);
 		return NULL;
 	}
 }
Index: trace/mercury_trace_readline.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_readline.h,v
retrieving revision 1.1
diff -u -r1.1 mercury_trace_readline.h
--- mercury_trace_readline.h	1999/03/30 05:45:53	1.1
+++ mercury_trace_readline.h	1999/10/15 18:13:11
@@ -15,14 +15,14 @@
 /*
 ** Print the prompt to the `out' file, read a line from the `in' file,
 ** possibly using GNU readline for command-line editing, and return it
-** in a malloc'd buffer holding the line (without the final newline).
+** in a MR_malloc'd buffer holding the line (without the final newline).
 ** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
 ** occurs on an empty line, return NULL.
 */
 char *	MR_trace_readline(const char *prompt, FILE *in, FILE *out);
 
 /*
-** Read a line from a file, and return a pointer to a malloc'd buffer
+** Read a line from a file, and return a pointer to a MR_malloc'd buffer
 ** holding the line (without the final newline). If EOF occurs on a
 ** nonempty line, treat the EOF as a newline; if EOF occurs on an empty
 ** line, return NULL.  Don't use GNU readline.
Index: trace/mercury_trace_spy.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_spy.c,v
retrieving revision 1.4
diff -u -r1.4 mercury_trace_spy.c
--- mercury_trace_spy.c	1999/10/07 09:31:40	1.4
+++ mercury_trace_spy.c	1999/10/15 18:15:07
@@ -197,7 +197,7 @@
 	}
 
 	/* Insert the spy point at the head of the list for the proc. */
-	point = checked_malloc(sizeof(MR_Spy_Point));
+	point = MR_NEW(MR_Spy_Point);
 	point->spy_when    = when;
 	point->spy_exists  = TRUE;
 	point->spy_enabled = TRUE;
Index: trace/mercury_trace_tables.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/trace/mercury_trace_tables.c,v
retrieving revision 1.7
diff -u -r1.7 mercury_trace_tables.c
--- mercury_trace_tables.c	1999/05/28 05:29:41	1.7
+++ mercury_trace_tables.c	1999/10/15 18:46:24
@@ -100,7 +100,7 @@
 {
 	int	slot;
 
-	MR_ensure_room_for_next(MR_module_info, MR_Module_Layout *,
+	MR_ensure_room_for_next(MR_module_info, const MR_Module_Layout *,
 		INIT_MODULE_TABLE_SIZE);
 	MR_prepare_insert_into_sorted(MR_module_infos, MR_module_info_next,
 		slot,

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