This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


Andreas Tobler <toa at pop dot agri dot ch> writes:

> Kaveh R. Ghazi wrote:
> >  > It applies cleanly against the current gcc trunk's libiberty.  (I
> >  > made an update for irix6 earlier today.)
> > This one applies cleanly after Rainer's install.  Try it instead,
> > thanks.
> >
> 
> Adding this snippet made it work on darwin.

Yes, you need to run autoconf and autoheader.

> At least your demo program
> gives something. But I'm not sure whether this is what you expect?

No, this is wrong.  The patch is multiplying by `pagesize', but Darwin
gives these sizes in bytes, not pages.

Here's the patch that does work for me.

Note that sysctl will change 'len' to describe the size actually
written; I expect that in future, if Darwin starts supporting >4G of
physical memory, we'll need more complicated code here.

If this patch does not work on other BSD systems, we'll need to do
something complicated.

Index: configure.in
===================================================================
RCS file: /cvs/gcc/gcc/libiberty/configure.in,v
retrieving revision 1.61
diff -u -p -u -p -r1.61 configure.in
--- configure.in	21 Feb 2003 20:33:50 -0000	1.61
+++ configure.in	22 Feb 2003 09:29:23 -0000
@@ -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 sys/sysinfo.h machine/hal_sysinfo.h sys/table.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/sysinfo.h machine/hal_sysinfo.h sys/table.h sys/sysctl.h)
 AC_HEADER_SYS_WAIT
 AC_HEADER_TIME
 
@@ -208,7 +208,7 @@ 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 getsysinfo table"
+checkfuncs="$checkfuncs getsysinfo table sysctl"
 
 # These are neither executed nor required, but they help keep
 # autoheader happy without adding a bunch of text to acconfig.h.
@@ -220,7 +220,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 getsysinfo table)
+  AC_CHECK_FUNCS(pstat_getstatic pstat_getdynamic sysmp getsysinfo table 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.])
Index: physmem.c
===================================================================
RCS file: /cvs/gcc/gcc/libiberty/physmem.c,v
retrieving revision 1.5
diff -u -p -u -p -r1.5 physmem.c
--- physmem.c	21 Feb 2003 20:33:50 -0000	1.5
+++ physmem.c	22 Feb 2003 09:29:24 -0000
@@ -42,6 +42,16 @@
 #  include <sys/table.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.  */
@@ -98,6 +108,18 @@ physmem_total ()
   }
 #endif
 
+#if HAVE_SYSCTL && defined HW_PHYSMEM
+  { /* This works on *bsd and darwin.  */
+    size_t physmem;
+    size_t len = sizeof (physmem);
+    int mib[2] = {CTL_HW, HW_PHYSMEM};
+
+    if (sysctl(mib, ARRAY_SIZE(mib), &physmem, &len, NULL, 0) == 0
+	|| len != sizeof (physmem))
+      return (double)physmem;
+  }
+#endif
+
   /* Return 0 if we can't determine the value.  */
   return 0;
 }
@@ -155,6 +177,18 @@ physmem_available ()
 	if (0 <= pages && 0 <= pagesize)
 	  return pages * pagesize;
       }
+  }
+#endif
+
+#if HAVE_SYSCTL && defined HW_USERMEM
+  { /* This works on *bsd and darwin.  */
+    unsigned int usermem;
+    size_t len = sizeof (usermem);
+    int mib[2] = {CTL_HW, HW_USERMEM};
+
+    if (sysctl(mib, ARRAY_SIZE(mib), &usermem, &len, NULL, 0) == 0
+	|| len != sizeof (usermem))
+      return (double)usermem;
   }
 #endif
 
Thanks for all your work on this, Kaveh; it's much appreciated!

-- 
- Geoffrey Keating <geoffk at geoffk dot org>

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]