[m-dev.] for review: don't use c_pointers as map keys in io.m

Tyson Dowd trd at cs.mu.OZ.AU
Wed Nov 1 00:38:54 AEDT 2000


Hi,

This change is one I've had to make in the IL library, and I thought it
would be a good idea anyway as I don't like the idea of comparing
c_pointers (at least, not directly).

===================================================================


Estimated hours taken: 1.5

The io__stream is used as a key in two maps in io.m.
This is a bad idea, io__stream is defined as a c_pointer, which should
not necessarily be comparable (there is no guarantee that it is even
constant -- a garbage collector might move it).

In the IL backend, this causes more trouble, as the .NET GC is a
compacting generational garbage collector, so in practice the
io__stream gets moved pretty soon after being allocated.

library/io.m:
	Add a new type -- the io__stream_id, which is a unique
	integer identifier for an IO stream, which can be extracted from
	the stream itself.
	Use io__stream_id as a key instead of io__stream.
	Implement the stream_id as simply the pointer itself in the
	boehm_gc C backend.
	(For the IL backend and the accurate GC backend the stream_id
	will be a unique integer stored in each stream).


Index: library/io.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.210
diff -u -r1.210 io.m
--- library/io.m	2000/10/22 19:23:22	1.210
+++ library/io.m	2000/10/31 13:35:56
@@ -1129,8 +1129,8 @@
 	#endif
 ").
 
-:- type io__stream_names ==	map(io__stream, string).
-:- type io__stream_putback ==	map(io__stream, list(char)).
+:- type io__stream_names ==	map(io__stream_id, string).
+:- type io__stream_putback ==	map(io__stream_id, list(char)).
 
 :- type io__input_stream ==	io__stream.
 :- type io__output_stream ==	io__stream.
@@ -1139,6 +1139,11 @@
 
 :- type io__stream == c_pointer.
 
+	% a unique identifier for an IO stream
+:- type io__stream_id == int.
+
+:- func io__get_stream_id(io__stream) = io__stream_id.
+
 /*
  * In NU-Prolog: 
  *	io__stream	--->	stream(int, int)
@@ -2470,7 +2475,7 @@
 
 io__stream_name(Stream, Name) -->
 	io__get_stream_names(StreamNames),
-	{ map__search(StreamNames, Stream, Name1) ->
+	{ map__search(StreamNames, get_stream_id(Stream), Name1) ->
 		Name = Name1
 	;
 		Name = "<stream name unavailable>"
@@ -2500,7 +2505,7 @@
 
 io__delete_stream_name(Stream) -->
 	io__get_stream_names(StreamNames0),
-	{ map__delete(StreamNames0, Stream, StreamNames) },
+	{ map__delete(StreamNames0, get_stream_id(Stream), StreamNames) },
 	io__set_stream_names(StreamNames).
 
 :- pred io__insert_stream_name(io__stream, string, io__state, io__state).
@@ -2508,7 +2513,7 @@
 
 io__insert_stream_name(Stream, Name) -->
 	io__get_stream_names(StreamNames0),
-	{ map__set(StreamNames0, Stream, Name, StreamNames) },
+	{ map__set(StreamNames0, get_stream_id(Stream), Name, StreamNames) },
 	io__set_stream_names(StreamNames).
 
 %-----------------------------------------------------------------------------%
@@ -2535,6 +2540,26 @@
 io__progname_base(DefaultName, PrognameBase) -->
 	io__progname(DefaultName, Progname),
 	{ dir__basename(Progname, PrognameBase) }.
+
+:- pragma c_code(io__get_stream_id(Stream::in) = (Id::out), 
+		will_not_call_mercury, "
+	/* 
+	** Most of the time, we can just use the pointer to the stream
+	** as a unique identifier.
+	*/
+	
+	Id = (MR_Word) Stream;
+
+#ifdef NATIVE_GC
+	/* 
+	** XXX for accurate GC we should embed an ID in the MercuryFile
+	** and retrieve it here.
+	*/
+	MR_fatal_error(""not implemented -- stream ids in native GC grades"");
+#endif
+").
+
+
 
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%


-- 
       Tyson Dowd           # 
                            #  Surreal humour isn't everyone's cup of fur.
     trd at cs.mu.oz.au        # 
http://www.cs.mu.oz.au/~trd #
--------------------------------------------------------------------------
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