[GRASS-dev] r.terraflow Windows patch

Glynn Clements glynn at gclements.plus.com
Sun Feb 24 04:35:56 EST 2008


Can someone test the attached patch on Windows (MSys)?

It eliminates various Unix-isms, specfically, it provides workarounds
for the following functions::

getpagesize
getrlimit
getrusage
gettimeofday
mkstemp
random
sbrk
unlink

It also fixes a call to putenv(), but that needs to be fixed on Unix
as well. putenv() isn't guaranteed to copy the string; in fact, POSIX
says that it shouldn't be copied. This means that passing an automatic
variable is incorrect.

According to the Linux manpage, glibc 2.1.2 and later conforms to
POSIX, while earlier versions copy the string (as does the BSD
version).

-- 
Glynn Clements <glynn at gclements.plus.com>

-------------- next part --------------
Index: raster/r.terraflow/main.cc
===================================================================
--- raster/r.terraflow/main.cc	(revision 30316)
+++ raster/r.terraflow/main.cc	(working copy)
@@ -519,7 +519,8 @@
  
   /* check STREAM path (the place where intermediate STREAMs are placed) */
   sprintf(buf, "%s=%s",STREAM_TMPDIR, opt->streamdir);
-  putenv(buf);
+  /* don't pass an automatic variable; putenv() isn't guaranteed to make a copy */
+  putenv(G_store(buf));
   if (getenv(STREAM_TMPDIR) == NULL) {
     fprintf(stderr, "%s:", STREAM_TMPDIR);
     G_fatal_error("not set");
Index: raster/r.terraflow/stats.cc
===================================================================
--- raster/r.terraflow/stats.cc	(revision 30316)
+++ raster/r.terraflow/stats.cc	(working copy)
@@ -126,7 +126,9 @@
   //ofstream that takes an fd; wrote another noclobber() function that
   //closes fd and returns the name;
   rt_start(tm);
+#ifndef __MINGW32__
   bss = sbrk(0);
+#endif
   char buf[BUFSIZ];
   *this << freeMem(buf) << endl;
 }
@@ -135,6 +137,9 @@
 
 long 
 statsRecorder::freeMem() {
+#ifdef __MINGW32__
+  return -1;
+#else
   struct rlimit rlim;
   if (getrlimit(RLIMIT_DATA, &rlim) == -1) {
 	perror("getrlimit: ");
@@ -148,6 +153,7 @@
   } 
   long freeMem = rlim.rlim_cur - ((char*)sbrk(0)-(char*)bss);
   return freeMem;
+#endif /* __MINGW32__ */
 }
 
 char *
Index: raster/r.terraflow/IOStream/include/rtimer.h
===================================================================
--- raster/r.terraflow/IOStream/include/rtimer.h	(revision 30316)
+++ raster/r.terraflow/IOStream/include/rtimer.h	(working copy)
@@ -22,6 +22,38 @@
 
 /* $Id$ */
 
+#ifdef __MINGW32__
+
+#include <time.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+
+typedef struct {
+  time_t tv1, tv2;
+} Rtimer;
+
+#define rt_start(rt)				\
+  if((time(&(rt.tv1)) == ((time_t) -1))) {	\
+	perror("time");				\
+	exit(1);				\
+  }
+
+/* doesn't really stop, just updates endtimes */
+#define rt_stop(rt)								\
+  if((time(&(rt.tv2)) == ((time_t) -1))) {	\
+	perror("time");				\
+	exit(1);				\
+  }
+
+#define rt_u_useconds(rt)	rt_w_useconds(rt)
+
+#define rt_s_useconds(rt)	rt_w_useconds(rt)
+
+#define rt_w_useconds(rt)	(1.0e6 * (rt.tv2 - rt.tv1))
+
+#else /* __MINGW32__ */
+
 #include <sys/time.h>
 #include <sys/resource.h>
 #include <stdio.h>
@@ -48,10 +80,6 @@
         perror("rusage/gettimeofday");			\
         exit(1);								\
   }
-
-/* not required to be called, but makes values print as 0. 
-   obviously a hack */
-#define rt_zero(rt) bzero(&(rt),sizeof(Rtimer));
 	
 
 #define rt_u_useconds(rt)							\
@@ -72,12 +100,16 @@
 	  - ((double)rt.tv1.tv_usec +			\
 		 (double)rt.tv1.tv_sec*1000000))
 
+#endif /* __MINGW32__ */
+
+/* not required to be called, but makes values print as 0. 
+   obviously a hack */
+#define rt_zero(rt) bzero(&(rt),sizeof(Rtimer));
+
 #define rt_seconds(rt) (rt_w_useconds(rt)/1000000)
 
 #define rt_sprint(buf, rt) rt_sprint_safe(buf,rt)
 
 char * rt_sprint_safe(char *buf, Rtimer rt);
 
-
-
 #endif /* RTIMER_H */
Index: raster/r.terraflow/IOStream/include/ami_stream.h
===================================================================
--- raster/r.terraflow/IOStream/include/ami_stream.h	(revision 30316)
+++ raster/r.terraflow/IOStream/include/ami_stream.h	(working copy)
@@ -36,6 +36,10 @@
 
 #include "mm.h" // Get the memory manager.
 
+#ifdef __MINGW32__
+#define getpagesize() (4096)
+#endif
+
 #define DEBUG_DELETE if(0)
 
 // The name of the environment variable which keeps the name of the
@@ -421,7 +425,7 @@
   
   // Get rid of the file if not persistent and if not substream.
   if ((per != PERSIST_PERSISTENT) && (substream_level == 0)) {
-    if (unlink(path) == -1) {
+    if (remove(path) == -1) {
       cerr << "AMI_STREAM: failed to unlink " << path << endl;
       perror("cannot unlink ");
       assert(0);
Index: raster/r.terraflow/IOStream/include/quicksort.h
===================================================================
--- raster/r.terraflow/IOStream/include/quicksort.h	(revision 30316)
+++ raster/r.terraflow/IOStream/include/quicksort.h	(working copy)
@@ -41,7 +41,11 @@
     
     // Try to get a good partition value and avoid being bitten by already
     // sorted input.
+#ifdef __MINGW32__
+    ptpart = data + (rand() % n);
+#else
     ptpart = data + (random() % n);
+#endif
     tpart = *ptpart;
     *ptpart = data[0];
     data[0] = tpart;
Index: raster/r.terraflow/IOStream/lib/src/ami_stream.cc
===================================================================
--- raster/r.terraflow/IOStream/lib/src/ami_stream.cc	(revision 30316)
+++ raster/r.terraflow/IOStream/lib/src/ami_stream.cc	(working copy)
@@ -42,7 +42,11 @@
   assert(base_dir);
 
   sprintf(tmp_path, "%s/%s_XXXXXX", base_dir, base);
+#ifdef __MINGW32__
+  fd = mktemp(tmp_path) ? open(tmp_path, O_CREAT|O_EXCL|O_RDWR, 0600) : -1;
+#else
   fd  = mkstemp(tmp_path);
+#endif
 
   if (fd == -1) {
     cerr <<  "ami_single_temp_name: ";


More information about the grass-dev mailing list