aboutsummaryrefslogtreecommitdiff
path: root/init
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2000-12-06 23:17:37 +0000
committerEric Andersen <andersen@codepoet.org>2000-12-06 23:17:37 +0000
commitbc5941a5408c9b55628b1757d0ea89df1218ec5c (patch)
tree6e0ea07a1546037736c35fed783d67279ed5b56f /init
parentbfa54143f62a0de98d2b0c47b64f73c6fa25cdae (diff)
downloadbusybox-bc5941a5408c9b55628b1757d0ea89df1218ec5c.tar.gz
Close but #1071...
Diffstat (limited to 'init')
-rw-r--r--init/init.c31
1 files changed, 14 insertions, 17 deletions
diff --git a/init/init.c b/init/init.c
index 17e605ba0..c4440894c 100644
--- a/init/init.c
+++ b/init/init.c
@@ -288,30 +288,27 @@ void set_term(int fd)
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;
+ unsigned int result, u, s=10;
- /* 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));
+ perrorMsg("Error checking free memory: ");
return -1;
}
- /* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
- if (info.mem_unit==0) {
- info.mem_unit=1;
- }
- 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);
+
+ /* Kernels 2.0.x and 2.2.x return info.mem_unit==0 with values in bytes.
+ * Kernels 2.4.0 return info.mem_unit in bytes. */
+ u = info.mem_unit;
+ if (u==0) u=1;
+ while ( (u&1) == 0 && s > 0 ) { u>>=1; s--; }
+ result = (info.totalram>>s) + (info.totalswap>>s);
+ result = result*u;
+ if (result < 0) result = INT_MAX;
+ return result;
}
static void console_init()