cleanup of runtime/*

Fergus Henderson fjh at cs.mu.oz.au
Sat Feb 8 05:21:17 AEDT 1997


Hi Tom,

The following diff fixes a few bugs in your recent changes,
some of which meant that it didn't compile on Solaris.
It also fixes a few things that I mentioned during the review
process but which didn't get fixed earlier, e.g. using a
higher level of abstraction and avoiding the use of `Word'
where it is not needed.

I also changed several of the types in some of the existing
code in the runtime: the stuff for computing memory sizes,
offsets, etc., should use `size_t', not `unsigned',
and the `num_uses' array can be `unsigned long', not `Word'.

Please have a careful look at this diff, Tom.

Cheers,
	Fergus.

cvs diff: Diffing .
Index: Mmake
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/Mmake,v
retrieving revision 1.48
diff -u -r1.48 Mmake
--- 1.48	1997/02/06 23:10:52
+++ Mmake	1997/02/07 18:05:16
@@ -21,10 +21,11 @@
 
 #-----------------------------------------------------------------------------#
 
-HDRS		= access.h misc.h conf.h deep_copy.h dlist.h debug.h dummy.h \
+HDRS		= access.h misc.h conf.h context.h \
+		  deep_copy.h dlist.h debug.h dummy.h \
 		  engine.h getopt.h goto.h heap.h imp.h init.h label.h \
 		  overflow.h prof.h memory.h mercury_solver_backtrack.h \
-		  regorder.h regs.h std.h stacks.h \
+		  regorder.h regs.h spinlock.h std.h stacks.h \
 		  table.h tags.h timing.h wrapper.h \
 		  prof.h prof_mem.h type_info.h context.h
 
@@ -36,7 +37,8 @@
 MOD_CS		= engine.c wrapper.c call.c context.c
 MOD_OS		= $(MOD_CS:.c=.o)
 ORIG_CS		= access.c deep_copy.c dlist.c dummy.c label.c \
-		  memory.c misc.c table.c timing.c prof.c prof_mem.c
+		  memory.c misc.c table.c timing.c prof.c prof_mem.c \
+		  spinlock.c
 ORIG_OS		= $(ORIG_CS:.c=.o)
 OBJS		= $(MOD_OS) $(ORIG_OS)
 # OBJS		= engine.o wrapper.o call.o \
Index: context.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/context.h,v
retrieving revision 1.1
diff -u -r1.1 context.h
--- 1.1	1997/02/06 23:10:56
+++ context.h	1997/02/07 17:50:22
@@ -1,7 +1,6 @@
 #ifndef CONTEXT_H
 #define CONTEXT_H
 
-#include <signal.h>
 #include "spinlock.h"
 
 /*
@@ -57,14 +56,13 @@
 */
 #define INITIAL_NUM_CONTEXTS	20
 
-typedef struct CONTEXT Context;
-
 /*
 ** The field names that correspond to virtual machine registers:
 ** 	sp, maxfr & curfr
 ** are prefixed with `context_' so that they don't get replaced
 ** during macro expansion.
 */
+typedef struct CONTEXT Context;
 struct CONTEXT {
 	struct CONTEXT	*next;	
 		/*
@@ -185,9 +183,11 @@
 		** (Assuming that Words can be read and written in a
 		** coherent manner is sufficiently important in terms of
 		** simplifying the synchronization mechanisms, that
-		** we really need to do so.)
+		** we really need to do so -- or so says Tom, at least.
+		** I remain unconvinced. -Fergus.)
 		*/
-extern	Word	*procwaiting;
+typedef Word AtomicBool;
+extern	AtomicBool	*procwaiting;
 
 		/*
 		** my_procnum is the number of the current process.
Index: context.mod
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/context.mod,v
retrieving revision 1.1
diff -u -r1.1 context.mod
--- 1.1	1997/02/06 23:11:00
+++ context.mod	1997/02/07 18:02:11
@@ -3,6 +3,8 @@
 #include <unistd.h>
 #include <signal.h>
 
+#include "context.h"
+
 #ifdef	PARALLEL
 int numprocs	=	1;
 #endif
@@ -12,7 +14,7 @@
 
 #ifdef	PARALLEL
 pid_t	*procid;
-Word	*procwaiting;
+AtomicBool *procwaiting;
 #endif
 int	my_procnum;
 pid_t	my_procid;
@@ -21,8 +23,8 @@
 Context	*this_context;
 Context	**runqueue_ptr;
 Context	**free_context_list_ptr;
-Word	*runqueue_lock;
-Word	*free_context_list_lock;
+SpinLock *runqueue_lock;
+SpinLock *free_context_list_lock;
 
 Context	*do_schedule_cptr;
 Code	*do_schedule_resume;
@@ -41,12 +43,12 @@
 	my_procid = getpid();
 
 	runqueue_lock = allocate_lock();
-	runqueue_ptr = (Context **) allocate_words(sizeof(Context *));
+	runqueue_ptr = allocate_object(Context *);
 	*runqueue_ptr = NULL;
 
 #ifdef	PARALLEL
-	procid = (pid_t *) allocate_bytes(sizeof(pid_t) * numprocs);
-	procwaiting = allocate_words(numprocs);
+	procid = allocate_array(pid_t, numprocs);
+	procwaiting = allocate_array(AtomicBool, numprocs);
 	procid[0] = my_procid;
 	procwaiting[0] = FALSE;
 
@@ -85,10 +87,8 @@
 	Context *tmp;
 
 	free_context_list_lock = allocate_lock();
-	free_context_list_ptr = (Context **) allocate_bytes(sizeof(Context *));
-	*free_context_list_ptr =
-		(Context *) allocate_bytes(INITIAL_NUM_CONTEXTS *
-				sizeof(Context));
+	free_context_list_ptr = allocate_object(Context *);
+	*free_context_list_ptr = allocate_array(Context, INITIAL_NUM_CONTEXTS);
 	tmp = *free_context_list_ptr;
 	for (i = 0; i < INITIAL_NUM_CONTEXTS; i++) {
 		if (i != INITIAL_NUM_CONTEXTS - 1)
@@ -276,7 +276,7 @@
 	/* Check to see if we need to signal a sleeping process */
 	if (old == NULL) {
 		int i;
-		for(i=0; i < numprocs; i++) {
+		for(i = 0; i < numprocs; i++) {
 			if (procwaiting[i] == TRUE) {
 				kill(procid[i], SIGUSR1);
 				break;
@@ -302,17 +302,17 @@
 
 	syncterm = do_join_and_terminate_syncterm;
 
-	get_lock(syncterm[SYNC_TERM_LOCK]);
+	get_lock((SpinLock *)&syncterm[SYNC_TERM_LOCK]);
 	if (--(syncterm[SYNC_TERM_COUNTER]) == 0) {
 		assert(syncterm[SYNC_TERM_PARENT] != NULL);
-		release_lock(syncterm[SYNC_TERM_LOCK]);
+		release_lock((SpinLock *)&syncterm[SYNC_TERM_LOCK]);
 		ctxt = (Context *) syncterm[SYNC_TERM_PARENT];
 		delete_context(this_context);
 		this_context = ctxt;
 		load_context(this_context);
 		GOTO(this_context->resume);
 	} else {
-		release_lock(syncterm[SYNC_TERM_LOCK]);
+		release_lock((SpinLock *)&syncterm[SYNC_TERM_LOCK]);
 		delete_context(this_context);
 		runnext();
 	}
@@ -331,16 +331,16 @@
 	register Word *syncterm;
 
 	syncterm = do_join_and_continue_syncterm;
-	get_lock(syncterm[SYNC_TERM_LOCK]);
+	get_lock((SpinLock *)&syncterm[SYNC_TERM_LOCK]);
 	if (--(syncterm[SYNC_TERM_COUNTER]) == 0) {
 		assert(syncterm[SYNC_TERM_PARENT] == NULL);
-		release_lock(syncterm[SYNC_TERM_LOCK]);
+		release_lock((SpinLock *)&syncterm[SYNC_TERM_LOCK]);
 		GOTO(do_join_and_continue_whereto);
 	} else {
 		save_context(this_context);
 		this_context->resume = do_join_and_continue_whereto;
 		syncterm[SYNC_TERM_PARENT] = (Word) this_context;
-		release_lock(syncterm[SYNC_TERM_LOCK]);
+		release_lock((SpinLock *)&syncterm[SYNC_TERM_LOCK]);
 		runnext();
 	}
 }
Index: imp.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/imp.h,v
retrieving revision 1.104
diff -u -r1.104 imp.h
--- 1.104	1997/02/06 23:11:25
+++ imp.h	1997/02/07 17:07:06
@@ -33,7 +33,6 @@
 typedef WORD_TYPE		Integer;
 typedef unsigned WORD_TYPE	Unsigned;
 typedef void			Code;	/* code addresses are `void *' */
-typedef	Word			SpinLock;
 
 #include	"regs.h"	/* must come before system headers */
 
Index: memory.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/memory.c,v
retrieving revision 1.58
diff -u -r1.58 memory.c
--- 1.58	1997/02/07 17:44:52
+++ memory.c	1997/02/07 17:41:58
@@ -135,7 +135,7 @@
 
 Word	virtual_reg_map[MAX_REAL_REG] = VIRTUAL_REG_MAP_BODY;
 
-Word	num_uses[MAX_RN];
+unsigned long	num_uses[MAX_RN];
 
 MemoryZone *zone_table;
 
@@ -154,8 +154,8 @@
 Word       *solutions_heap_pointer;
 #endif
 
-static	unsigned	unit;
-static	unsigned	page_size;
+static	size_t		unit;
+static	size_t		page_size;
 
 static MemoryZone	*get_zone(void);
 static void		unget_zone(MemoryZone *zone);
@@ -170,10 +170,10 @@
 
 #define	CACHE_SLICES	8
 
-static	Word	*offset_vector;
-static	Word	*offset_counter;
-static	Word	*offset_lock;
-Word	next_offset(void);
+static	size_t		*offset_vector;
+static	int		*offset_counter;
+static	SpinLock	*offset_lock;
+size_t	next_offset(void);
 
 static void init_memory_arena(void);
 static void init_zones(void);
@@ -255,7 +255,7 @@
 static void init_zones()
 {
 	int i;
-	Word fake_reg_offset;
+	size_t fake_reg_offset;
 
 	/*
 	** Allocate the MemoryZone table.
@@ -289,10 +289,10 @@
 			zone_table[i].next = NULL;
 	}
 
-	offset_counter = allocate_words(1);
+	offset_counter = allocate_object(int);
 	*offset_counter = 0;
 
-	offset_vector = allocate_words(CACHE_SLICES - 1);
+	offset_vector = allocate_array(size_t, CACHE_SLICES - 1);
 
 	fake_reg_offset = (Unsigned) fake_reg % pcache_size;
 
@@ -376,9 +376,9 @@
 ** a fixed amount (eg 2Kb) so that as primary caches get bigger, we
 ** allocate more offsets across them.
 */
-Word	next_offset(void)
+size_t	next_offset(void)
 {
-	Word offset;
+	size_t offset;
 
 	get_lock(offset_lock);
 
@@ -391,12 +391,12 @@
 	return offset;
 }
 
-MemoryZone *create_zone(const char *name, int id, unsigned size,
-		unsigned offset, unsigned redsize,
+MemoryZone *create_zone(const char *name, int id, size_t size,
+		size_t offset, size_t redsize,
 		bool ((*handler)(Word *addr, MemoryZone *zone, void *context)))
 {
 	Word		*base;
-	unsigned	total_size;
+	size_t		total_size;
 
 		/*
 		** total allocation is:
@@ -426,12 +426,12 @@
 	return construct_zone(name, id, base, size, offset, redsize, handler);
 }
 
-MemoryZone *construct_zone(const char *name, int id, Word *base, unsigned size,
-		unsigned offset, unsigned redsize,
+MemoryZone *construct_zone(const char *name, int id, Word *base,
+		size_t size, size_t offset, size_t redsize,
 		bool ((*handler)(Word *addr, MemoryZone *zone, void *context)))
 {
 	MemoryZone	*zone;
-	unsigned	total_size;
+	size_t		total_size;
 
 	if (base == NULL)
 		fatal_error("construct_zone called with NULL pointer");
@@ -465,8 +465,8 @@
 #ifdef	HAVE_MPROTECT
 #ifdef	HAVE_SIGINFO
 	zone->redzone_base = zone->redzone = (Word *)
-			round_up((Unsigned)base+size-redsize, unit);
-	if (mprotect((char *)zone->redzone, redsize+unit, MY_PROT) < 0)
+			round_up((Unsigned)base + size - redsize, unit);
+	if (mprotect((char *)zone->redzone, redsize + unit, MY_PROT) < 0)
 	{
 		char buf[2560];
 		sprintf(buf, "unable to set %s#%d redzone\n"
@@ -589,7 +589,6 @@
 	Word *    fault_addr;
 	Word *    new_zone;
 	MemoryZone *zone;
-	unsigned zone_size;
 
 	fault_addr = (Word *) addr;
 
@@ -627,11 +626,10 @@
 
 bool default_handler(Word *fault_addr, MemoryZone *zone, void *context)
 {
-	Word *new_zone;
-	Unsigned zone_size;
+    Word *new_zone;
+    size_t zone_size;
 
-    new_zone = (Word *) round_up((Unsigned) fault_addr +
-		    sizeof(Word), unit);
+    new_zone = (Word *) round_up((Unsigned) fault_addr + sizeof(Word), unit);
 
     if (new_zone <= zone->hardmax)
     {
@@ -673,11 +671,11 @@
 	}
 
 	{
-		char buf[2560];
-		sprintf(buf,
+	    char buf[2560];
+	    sprintf(buf,
 		    "\nMercury runtime: memory zone %s#%d overflowed\n",
 		    zone->name, zone->id);
-		fatal_abort(context, buf, FALSE);
+	    fatal_abort(context, buf, FALSE);
 	}
     }
 
@@ -924,7 +922,7 @@
 
 #ifdef	CONSERVATIVE_GC
 
-void *allocate_bytes(int numbytes)
+void *allocate_bytes(size_t numbytes)
 {
 	void	*tmp;
 
@@ -942,7 +940,7 @@
 #error "shared memory not implemented"
 #endif
 
-void *allocate_bytes(int numbytes)
+void *allocate_bytes(size_t numbytes)
 {
 	void	*tmp;
 
@@ -956,22 +954,12 @@
 
 #endif
 
-Word *allocate_words(int numwords)
-{
-	return (Word *)allocate_bytes(numwords*sizeof(Word));
-}
-
-SpinLock *allocate_lock(void)
-{
-	return (SpinLock *)allocate_bytes(sizeof(SpinLock));
-}
-
 void deallocate_memory(void *ptr)
 {
 #ifdef CONSERVATIVE_GC
 	GC_FREE(ptr);
 #elif	PARALLEL
-#error	Shared memory not implemented
+#error	"shared memory not implemented"
 #else
 	free(ptr);
 #endif
Index: memory.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/memory.h,v
retrieving revision 1.22
diff -u -r1.22 memory.h
--- 1.22	1997/02/07 14:26:34
+++ memory.h	1997/02/07 17:00:53
@@ -8,6 +8,7 @@
 #define	MEMORY_H
 
 #include "regs.h"
+#include <stdlib.h>
 #include "std.h"
 
 /* these cannot be changed without lots of modifications elsewhere */
@@ -31,7 +32,7 @@
 extern	Word	virtual_reg_map[MAX_REAL_REG];
 
 /* used for counting register usage */
-extern	Word 	num_uses[MAX_RN];
+extern	unsigned long 	num_uses[MAX_RN];
 
 /*
 ** The Mercury runtime uses a number of memory areas or *zones*. These
@@ -137,8 +138,8 @@
 ** are ignored.
 */
 
-MemoryZone	*create_zone(const char *name, int id, unsigned size,
-			unsigned offset, unsigned redsize,
+MemoryZone	*create_zone(const char *name, int id,
+			size_t size, size_t offset, size_t redsize,
 			ZoneHandler *handler);
 
 /*
@@ -155,7 +156,7 @@
 */
 
 MemoryZone	*construct_zone(const char *name, int Id, Word *base,
-			unsigned size, unsigned offset, unsigned redsize,
+			size_t size, size_t offset, size_t redsize,
 			ZoneHandler *handler);
 
 /*
@@ -187,24 +188,27 @@
 ** next_offset() returns sucessive offsets across the primary cache. Useful
 ** when calling {create,construct}_zone().
 */
-extern	Word	next_offset(void);
+extern	size_t	next_offset(void);
 
 /*
-** allocate_bytes() allocates the given number of bytes. If shared memory
-** is being used, this allocation routine will allocate in shared memory.
+** allocate_bytes() allocates the given number of bytes.
+**
+** allocate_object(type) allocates space for an object of the specified type.
+**
+** allocate_array(type, num) allocates space for an array of objects of the
+** specified type.
+**
+** If shared memory is being used, these allocation routines will allocate
+** in shared memory.
 */
-extern	void	*allocate_bytes(int numbytes);
 
-/*
-** allocate_words() works the same as allocate_bytes() except that it allocates
-** the given number of words instead of bytes.
-*/
-extern	Word	*allocate_words(int numwords);
+extern	void	*allocate_bytes(size_t numbytes);
 
-/*
-** allocate_lock() returns a pointer to a new lock.
-*/
-extern	SpinLock *allocate_lock(void);
+#define allocate_object(type) \
+	((type *)allocate_bytes(sizeof(type)))
+
+#define allocate_array(type, num) \
+	((type *)allocate_bytes((num) * sizeof(type)))
 
 /*
 ** deallocate_memory() deallocates the memory allocated by one of the
Index: spinlock.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/spinlock.c,v
retrieving revision 1.1
diff -u -r1.1 spinlock.c
--- 1.1	1997/02/06 23:11:41
+++ spinlock.c	1997/02/07 17:33:15
@@ -1,6 +1,7 @@
 #include "imp.h"
+#include "spinlock.h"
 
-void do_spinlock(Word *)
+void do_spinlock(Word *l)
 {
 	fatal_error("do_spinlock unimplemented");
 }
Index: spinlock.h
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/spinlock.h,v
retrieving revision 1.1
diff -u -r1.1 spinlock.h
--- 1.1	1997/02/06 23:11:46
+++ spinlock.h	1997/02/07 17:59:48
@@ -1,6 +1,51 @@
 #ifndef SPINLOCK_H
 #define SPINLOCK_H
 
+/*
+** This part of the header file documents the abstract interface for a spinlock.
+*/
+
+#if 0 /* everything here is really implemented as macros */
+
+/*
+** You can assume that a SpinLock will fit into a Word,
+** i.e. that sizeof(SpinLock) <= sizeof(Word).
+** But you should not assume anything else.
+*/
+typedef ... SpinLock;
+
+/*
+** allocate_lock() returns a pointer to a new initialized lock,
+** allocated in shared memory.
+** deallocate_lock() deinitializes and deallocates a lock.
+*/
+extern	SpinLock *allocate_lock(void);
+extern	void deallocate_lock(SpinLock *);
+
+/*
+** get_lock() and release_lock() are used to
+** acquire and relinquish a lock.
+*/
+void get_lock(SpinLock *);
+void release_lock(SpinLock *);
+
+#endif /* end of #if 0 */
+
+/*---------------------------------------------------------------------------*/
+
+/*
+** The following are implementation details.
+** They're defined here in the header file so that they can be inlined,
+** but code elsewhere should avoid depending on these details.
+*/
+
+#include "imp.h"	/* for `Word' */
+#include <stddef.h>	/* for `NULL' */
+#include "conf.h"	/* for `PARALLEL' */
+#include "context.h"	/* for `numprocs' */
+
+typedef Word SpinLock;
+
 void do_spinlock(SpinLock *s);
 
 void do_spinunlock(SpinLock *s);
@@ -9,21 +54,30 @@
 
 #define get_lock(addr)		do {			\
 		if (numprocs != 1) {			\
-			do_spinlock((SpinLock *)addr);	\
+			do_spinlock(addr);	\
 		}					\
 	} while(0)
 
 #define	release_lock(addr)	do {			\
 		if (numprocs != 1) {			\
-			do_spinunlock((SpinLock *)addr);	\
+			do_spinunlock(addr);	\
 		}					\
 	} while(0)
 
+#define allocate_lock() 	allocate_type(SpinLock)
+
+#define deallocate_lock(lock)	deallocate(lock)
+
 #else
 
 #define	get_lock(addr)		do { } while (0)
 
 #define	release_lock(addr)	do { } while (0)
+
+#define allocate_lock() 	NULL
+
+#define deallocate_lock(lock)	do { } while(0)
+
 
 #endif /* PARALLEL */
 
Index: wrapper.mod
===================================================================
RCS file: /home/staff/zs/imp/mercury/runtime/wrapper.mod,v
retrieving revision 1.69
diff -u -r1.69 wrapper.mod
--- 1.69	1997/02/06 23:11:50
+++ wrapper.mod	1997/02/07 17:00:03
@@ -736,7 +736,7 @@
 			}
 		}
 
-		printf("\t%lu\n", (unsigned long)num_uses[i]);
+		printf("\t%lu\n", num_uses[i]);
 	}
 }
 #endif
cvs diff: Diffing machdeps

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



More information about the developers mailing list