Patch: *bsd & darwin support in libiberty/physmem.c

Kaveh R. Ghazi ghazi@caip.rutgers.edu
Fri Feb 21 21:25:00 GMT 2003


Here's an untested patch I cobbled together using sysctl.  I'm hoping
this one patch works for all the BSDs and Darwin using the same code
(but I have none of those to test it on.)  Would ya'll give it a spin
and report back?  It applies cleanly against the current gcc trunk's
libiberty.  (I made an update for irix6 earlier today.)

(DJ: Ok to install if I get positive feedback?)

		Thanks,
		--Kaveh


2003-02-21  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>

	* configure.in: Check for sys/sysctl.h and sysctl.
	* physmem.c: Add support for *bsd and darwin.
	
diff -rup orig/egcc-CVS20030221/libiberty/configure.in egcc-CVS20030221/libiberty/configure.in
--- orig/egcc-CVS20030221/libiberty/configure.in	2003-02-21 15:12:38.000000000 -0500
+++ egcc-CVS20030221/libiberty/configure.in	2003-02-21 16:05:29.456443711 -0500
@@ -142,7 +142,7 @@ AC_SUBST_FILE(host_makefile_frag)
 # It's OK to check for header files.  Although the compiler may not be
 # able to link anything, it had better be able to at least compile
 # something.
-AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h)
+AC_CHECK_HEADERS(sys/file.h sys/param.h limits.h stdlib.h string.h unistd.h strings.h sys/time.h time.h sys/resource.h sys/stat.h sys/mman.h fcntl.h alloca.h sys/pstat.h sys/sysmp.h sys/sysctl.h)
 AC_HEADER_SYS_WAIT
 AC_HEADER_TIME
 
@@ -207,7 +207,7 @@ funcs="$funcs waitpid"
 vars="sys_errlist sys_nerr sys_siglist"
 
 checkfuncs="getrusage on_exit psignal strerror strsignal sysconf times sbrk gettimeofday"
-checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp"
+checkfuncs="$checkfuncs realpath canonicalize_file_name pstat_getstatic pstat_getdynamic sysmp sysctl"
 
 # These are neither executed nor required, but they help keep
 # autoheader happy without adding a bunch of text to acconfig.h.
@@ -219,7 +219,7 @@ if test "x" = "y"; then
   AC_CHECK_FUNCS(strtod strtol strtoul tmpnam vasprintf vfprintf vprintf)
   AC_CHECK_FUNCS(vsprintf waitpid getrusage on_exit psignal strerror strsignal)
   AC_CHECK_FUNCS(sysconf times sbrk gettimeofday ffs)
-  AC_CHECK_FUNCS(pstat_getstatic pstat_getdynamic sysmp)
+  AC_CHECK_FUNCS(pstat_getstatic pstat_getdynamic sysmp sysctl)
   AC_CHECK_FUNCS(realpath canonicalize_file_name)
   AC_DEFINE(HAVE_SYS_ERRLIST, 1, [Define if you have the sys_errlist variable.])
   AC_DEFINE(HAVE_SYS_NERR,    1, [Define if you have the sys_nerr variable.])
diff -rup orig/egcc-CVS20030221/libiberty/physmem.c egcc-CVS20030221/libiberty/physmem.c
--- orig/egcc-CVS20030221/libiberty/physmem.c	2003-02-21 15:12:38.000000000 -0500
+++ egcc-CVS20030221/libiberty/physmem.c	2003-02-21 16:10:03.643447382 -0500
@@ -33,6 +33,16 @@
 #include <sys/sysmp.h>
 #endif
 
+#include <sys/types.h>
+
+#if HAVE_SYS_PARAM_H
+#include <sys/param.h>
+#endif
+
+#if HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif
+
 #include "libiberty.h"
 
 /* Return the total amount of physical memory.  */
@@ -74,6 +84,21 @@ physmem_total ()
   }
 #endif
 
+#if HAVE_SYSCTL && defined HW_PHYSMEM && defined HW_PAGESIZE
+  { /* This works on *bsd and darwin.  */
+    unsigned int physmem, pagesize;
+    size_t len = sizeof(pagesize);
+    int mib[2] = {CTL_HW, HW_PAGESIZE};
+
+    if (sysctl(mib, ARRAY_SIZE(mib), &pagesize, &len, NULL, 0) == 0)
+      {
+	mib[1] = HW_PHYSMEM;
+	if (sysctl(mib, ARRAY_SIZE(mib), &physmem, &len, NULL, 0) == 0)
+	  return (double)physmem * pagesize;
+      }
+  }
+#endif
+
   /* Return 0 if we can't determine the value.  */
   return 0;
 }
@@ -119,6 +144,21 @@ physmem_available ()
   }
 #endif
 
+#if HAVE_SYSCTL && defined HW_USERMEM && defined HW_PAGESIZE
+  { /* This works on *bsd and darwin.  */
+    unsigned int usermem, pagesize;
+    size_t len = sizeof(pagesize);
+    int mib[2] = {CTL_HW, HW_PAGESIZE};
+
+    if (sysctl(mib, ARRAY_SIZE(mib), &pagesize, &len, NULL, 0) == 0)
+      {
+	mib[1] = HW_USERMEM;
+	if (sysctl(mib, ARRAY_SIZE(mib), &usermem, &len, NULL, 0) == 0)
+	  return (double)usermem * pagesize;
+      }
+  }
+#endif
+
   /* Guess 25% of physical memory.  */
   return physmem_total () / 4;
 }



More information about the Gcc-patches mailing list