check_free_memory() on Linux 2.4.0

Larry Doolittle ldoolitt at recycle.lbl.gov
Fri Oct 6 18:46:37 UTC 2000


Package: busybox
Version: 0.47
Severity: normal

The check_free_memory routine in init.c gets the wrong answer on
Linux kernel 2.4.0test4.  The code looks bogus for other versions,
too.  The attached patch is definitely clean for 2.4.0, and I
believe 2.0.x as well.  I don't know about 2.2.x.

--- busybox-0.47/init.c	Mon Sep 25 14:42:27 2000
+++ busybox-0.47-arm/init.c	Fri Oct  6 11:29:35 2000
@@ -288,30 +288,28 @@
 	tcsetattr(fd, TCSANOW, &tty);
 }
 
-/* How much memory does this machine have? */
+/* How much memory does this machine have?
+   Units are kBytes to avoid overflow on 4GB machines */
 static int check_free_memory()
 {
 	struct sysinfo info;
+	int result;
 
-	/* Pre initialize mem_unit in case this kernel is something prior to
-	 * the linux 2.4 kernel (which will actually fill in mem_unit... */
-	sysinfo(&info);
 	if (sysinfo(&info) != 0) {
 		printf("Error checking free memory: %s\n", strerror(errno));
 		return -1;
 	}
-	/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
+
+	/* Kernels 2.0.x return info.mem_unit==0, and values in bytes.
+	 * Kernels 2.4.0 return info.mem_unit in bytes.
+     * Someone else will have to test this on 2.2.x */
 	if (info.mem_unit==0) {
-		info.mem_unit=1;
+		result = info.totalram/1024 + info.totalswap/1024;
+	} else {
+		result = (info.totalram+info.totalswap)*(info.mem_unit/1024);
 	}
-	info.mem_unit*=1024;
 	
-	/* Note:  These values can in theory overflow a 32 bit unsigned long (i.e.
-	 * mem >=  Gib), but who puts more then 4GiB ram+swap on an embedded
-	 * system? */
-	info.totalram/=info.mem_unit; 
-	info.totalswap/=info.mem_unit;
-	return(info.totalram+info.totalswap);
+	return result;
 }
 
 static void console_init()

---------------------------------------
Received: (at 1046-done) by bugs.lineo.com; 9 Oct 2000 18:59:22 +0000


More information about the busybox mailing list