From owner-freebsd-hackers@FreeBSD.ORG Sat Dec 10 03:46:31 2005 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 2DE5C16A4F2 for ; Sat, 10 Dec 2005 03:46:31 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id A184C43D62 for ; Sat, 10 Dec 2005 03:46:30 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.13.1/8.13.3) id jBA3kTcc025106; Fri, 9 Dec 2005 21:46:29 -0600 (CST) (envelope-from dan) Date: Fri, 9 Dec 2005 21:46:29 -0600 From: Dan Nelson To: Giorgos Keramidas Message-ID: <20051210034629.GI95420@dan.emsphone.com> References: <10939303.1134158545675.JavaMail.root@vms070.mailsrvcs.net> <20051210020643.GA6206@flame.pc> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20051210020643.GA6206@flame.pc> X-OS: FreeBSD 5.4-STABLE X-message-flag: Outlook Error User-Agent: Mutt/1.5.11 Cc: freebsd-hackers@freebsd.org, babkin@users.sourceforge.net, Divacky Roman Subject: Re: sysctl, HW_PHYSMEM, and crippled gcc X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 10 Dec 2005 03:46:31 -0000 In the last episode (Dec 10), Giorgos Keramidas said: > >>On Thu, Dec 08, 2005 at 05:06:16PM -0800, Steve Kargl wrote: > >>> Anyone have any insight into fixing gcc to make better > >>> use of system memory on systems with more than 4 GB. > >>> It appears that libiberty/physmem.c tries to use sysctl() > >>> to determine the amount of physical memory in a system. > >>> > >>> { /* This works on *bsd and darwin. */ > >>> unsigned int physmem; > >>> size_t len = sizeof physmem; > >>> static int mib[2] = { CTL_HW, HW_PHYSMEM }; > >>> > >>> if (sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0 > >>> && len == sizeof (physmem)) > >>> return (double) physmem; > >>> } > >>> > >>> This works if you have less than 4GB because of the unsigned > >>> int physmem. I have 12 GB, which of course, when expanded > >>> to the number of bytes doesn't fit into a unsigned int physmem. > > Can someone with access to a system with more than 4 GB verify that the > following works correctly? > > % flame:/home/keramida/tmp/physmem$ cat -n physmem.c > % 9 int > % 10 main(void) > % 11 { > % 12 uint64_t physmem; > % 13 size_t len = sizeof physmem; > % 14 static int mib[] = { CTL_HW, HW_PHYSMEM }; > % 15 static size_t miblen = sizeof(mib) / sizeof(mib[0]); > % 16 > % 17 if (sysctl(mib, miblen, &physmem, &len, NULL, 0) != 0) > % 18 err(1, "sysctl hw.physmem"); > % 19 printf("Physical memory = %ju bytes\n", (intmax_t)physmem); > % 20 return EXIT_SUCCESS; > % 21 } Won't this break on x86, where physmem is 32 bits? Just use "unsigned long", which is what the sysctl type is according to kern_mib.c . -- Dan Nelson dnelson@allantgroup.com