[m-rev.] diff: avoid problems with windows.h and winsock.h

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Oct 27 01:06:32 AEDT 2011


Branches: main, 11.07

Avoid problems with the inclusion of windows.h in the runtime and a few
standard library modules.  By default, windows.h includes a lot of other
header files, some of which conflict with other parts of the Windows API.

Such a conflict occurs with G12 tcp module where the inclusion of winsock2.h
conflicts with the inclusion of winsock.h by windows.h.  We can avoid this by
defining the macro WIN32_LEAN_AND_MEAN.  It causes winsock.h and some other
headers not to be included by windows.h.  (Should the other headers actually be
required -- the Mercury runtime and standard library do not require them --
then they will need to be included separately.)

To make this (and other workarounds for silly things in windows.h) possible
this change adds wrapper header for windows.h to the runtime.  We now use
this wrapper header throughout the rest of the system rather than including
windows.h directly.

runtime/mercury_windows.h:
  	Add a wrapper around windows.h that allows us to control
  	how windows.h is included in a consistent manner.

  	Define WIN32_LEAN_AND_MEAN in order to avoid problems
  	with code that uses winsock2.h.

runtime/Mmakefile:
  	Include the new header.

runtime/mercury_memory.c:
runtime/mercury_memory_handlers.h
runtime/mercury_memory_zones.c:
runtime/mercury_timing.c:
library/dir.m:
library/io.m:
library/time.m:
  	Include mercury_windows.h rather than directly including
  	windows.h.

Julien.

Index: library/dir.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/dir.m,v
retrieving revision 1.62
diff -u -r1.62 dir.m
--- library/dir.m	11 Oct 2011 04:31:04 -0000	1.62
+++ library/dir.m	26 Oct 2011 12:42:37 -0000
@@ -1533,7 +1533,7 @@
   #include ""mercury_types.h""

   #if defined(MR_WIN32) && defined(MR_HAVE_WINDOWS_H)
-  #include <windows.h>
+  #include ""mercury_windows.h""
   #endif

   #ifdef HAVE_UNISTD_H
Index: library/io.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/io.m,v
retrieving revision 1.469
diff -u -r1.469 io.m
--- library/io.m	11 Oct 2011 04:31:04 -0000	1.469
+++ library/io.m	26 Oct 2011 12:42:37 -0000
@@ -5648,7 +5648,7 @@
   #endif

   #ifdef MR_WIN32
-  #include <windows.h>
+  #include ""mercury_windows.h""
   #endif

   #if defined(MR_MSVC)
Index: library/time.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/time.m,v
retrieving revision 1.71
diff -u -r1.71 time.m
--- library/time.m	11 Oct 2011 04:31:04 -0000	1.71
+++ library/time.m	26 Oct 2011 12:42:37 -0000
@@ -329,9 +329,9 @@
           Result = Ret
       ).

-:- pragma foreign_decl(c, local, "
+:- pragma foreign_decl("C", local, "
   #ifdef MR_WIN32
-    #include <windows.h>
+    #include ""mercury_windows.h""
       typedef union
       {
           FILETIME ft;
@@ -986,8 +986,6 @@

   %-----------------------------------------------------------------------------%

-%:- func time.ctime(time_t) = string.
-
   time.ctime(Time) = asctime(localtime(Time)).

   %-----------------------------------------------------------------------------%
Index: runtime/Mmakefile
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/Mmakefile,v
retrieving revision 1.153
diff -u -r1.153 Mmakefile
--- runtime/Mmakefile	13 Oct 2011 02:42:20 -0000	1.153
+++ runtime/Mmakefile	26 Oct 2011 12:42:37 -0000
@@ -48,7 +48,7 @@
   			mercury_dummy.h		\
   			mercury_dword.h		\
   			mercury_engine.h	\
-            mercury_expanding_array.h \
+            		mercury_expanding_array.h \
   			mercury_file.h		\
   			mercury_float.h		\
   			mercury_getopt.h	\
@@ -105,6 +105,7 @@
   			mercury_typeclass_info.h	\
   			mercury_types.h		\
   			mercury_univ.h		\
+			mercury_windows.h	\
   			mercury_wrapper.h	\
   			mercury_wsdeque.h

Index: runtime/mercury_memory.c
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_memory.c,v
retrieving revision 1.43
diff -u -r1.43 mercury_memory.c
--- runtime/mercury_memory.c	20 May 2011 04:16:55 -0000	1.43
+++ runtime/mercury_memory.c	26 Oct 2011 12:42:37 -0000
@@ -86,7 +86,7 @@
     #define   getpagesize()   sysconf(_SC_PAGESIZE)
   #elif !defined(MR_HAVE_GETPAGESIZE) || defined(MR_MINGW)
     #if defined(MR_WIN32_GETSYSTEMINFO)
-    #include <windows.h>
+    #include "mercury_windows.h"

       #define getpagesize() MR_win32_getpagesize()

Index: runtime/mercury_memory_handlers.h
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_memory_handlers.h,v
retrieving revision 1.5
diff -u -r1.5 mercury_memory_handlers.h
--- runtime/mercury_memory_handlers.h	24 Nov 2000 06:03:38 -0000	1.5
+++ runtime/mercury_memory_handlers.h	26 Oct 2011 12:42:37 -0000
@@ -60,7 +60,7 @@
   **  execution at the assembly instruction that was executing
   **  when the exception was raised.
   */
-#include <windows.h>
+#include "mercury_windows.h"

   int MR_filter_win32_exception(LPEXCEPTION_POINTERS exception_ptrs);
   #endif
Index: runtime/mercury_memory_zones.c
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_memory_zones.c,v
retrieving revision 1.41
diff -u -r1.41 mercury_memory_zones.c
--- runtime/mercury_memory_zones.c	16 Jul 2011 07:51:30 -0000	1.41
+++ runtime/mercury_memory_zones.c	26 Oct 2011 12:42:37 -0000
@@ -73,7 +73,7 @@
   ** includes?
   */
   #ifdef MR_WIN32_VIRTUAL_ALLOC
-  #include <windows.h>
+  #include "mercury_windows.h"
   #endif

   static  void    MR_setup_redzones(MR_MemoryZone *zone);
Index: runtime/mercury_timing.c
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/runtime/mercury_timing.c,v
retrieving revision 1.11
diff -u -r1.11 mercury_timing.c
--- runtime/mercury_timing.c	14 Nov 2006 00:15:41 -0000	1.11
+++ runtime/mercury_timing.c	26 Oct 2011 12:42:37 -0000
@@ -29,7 +29,7 @@
   #endif

   #ifdef MR_WIN32_GETPROCESSTIMES
-  #include <windows.h>
+  #include "mercury_windows.h"
   #endif

   #include "mercury_timing.h"
Index: runtime/mercury_windows.h
===================================================================
RCS file: runtime/mercury_windows.h
diff -N runtime/mercury_windows.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ runtime/mercury_windows.h	26 Oct 2011 12:42:37 -0000
@@ -0,0 +1,37 @@
+/*
+** vim:ts=4 sw=4 expandtab
+*/
+/*
+** Copyright (C) 2011 The University of Melbourne.
+** This file may only be copied under the terms of the GNU Library General
+** Public License - see the file COPYING.LIB in the Mercury distribution.
+*/
+
+/*
+** mercury_windows.h - this header provides a wrapper around windows.h
+** Mercury runtime and library modules should #include this header rather
+** than #including windows.h directly.
+**
+*/
+
+#ifndef MERCURY_WINDOWS_H
+#define MERCURY_WINDOWS_H
+
+/*
+** NOTE: the following is not protected by any guards - it is the
+** responsibility of modules that #include this one to ensure that
+** the windows.h is available before #including this file.
+*/
+
+/*
+** Defining WIN32_LEAN_AND_MEAN disables a series of #includes inside
+** windows.h -- notably it disables the #include of winsock.h, the inclusion
+** of which renders the winsock2 API unusable in Mercury foreign_procs.
+*/
+#if !defined(WIN32_LEAN_AND_MEAN)
+    #define WIN2_LEAN_AND_MEAN
+#endif
+
+#include <windows.h>
+
+#endif /* not MERCURY_WINDOWS_H */

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list