[git commit] free: code shrink

Denys Vlasenko vda.linux at googlemail.com
Fri Nov 27 11:40:31 UTC 2020


commit: https://git.busybox.net/busybox/commit/?id=fc63549352c371617e68fcf7ecbe4978fa207c6a
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

function                                             old     new   delta
free_main                                            664     657      -7

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 procps/free.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/procps/free.c b/procps/free.c
index b33506b9e..0adef501f 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -97,10 +97,10 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
 		case 'k': /* 2^10 */
 			/* G.unit_steps = 10; - already is */
 			break;
-		case 'm': /* 2^(2*10) */
+		case 'm': /* 2^20 */
 			G.unit_steps = 20;
 			break;
-		case 'g': /* 2^(3*10) */
+		case 'g': /* 2^30 */
 			G.unit_steps = 30;
 			break;
 		default:
@@ -117,16 +117,24 @@ int free_main(int argc UNUSED_PARAM, char **argv IF_NOT_DESKTOP(UNUSED_PARAM))
 	);
 
 	sysinfo(&info);
-	/* Kernels prior to 2.4.x will return info.mem_unit==0, so cope... */
-	G.mem_unit = (info.mem_unit ? info.mem_unit : 1);
 	/* Extract cached and memavailable from /proc/meminfo and convert to mem_units */
 	seen_available = parse_meminfo(&G);
+	G.mem_unit = (info.mem_unit ? info.mem_unit : 1); /* kernels < 2.4.x return mem_unit==0, so cope */
 	available = ((unsigned long long) G.available_kb * 1024) / G.mem_unit;
 	cached = ((unsigned long long) G.cached_kb * 1024) / G.mem_unit;
 	cached += info.bufferram;
 	cached += ((unsigned long long) G.reclaimable_kb * 1024) / G.mem_unit;
 	cached_plus_free = cached + info.freeram;
 
+/* In case (long long * G.mem_unit) can overflow, this can be used to reduce the chances */
+#if 0 //ENABLE_DESKTOP
+	while (!(G.mem_unit & 1) && G.unit_steps != 0) {
+		G.mem_unit >>= 1;
+		G.unit_steps--;
+		//bb_error_msg("mem_unit:%d unit_steps:%d", G.mem_unit, G.unit_steps);
+	}
+#endif
+
 #define FIELDS_6 "%12llu %11llu %11llu %11llu %11llu %11llu\n"
 #define FIELDS_3 (FIELDS_6 + 6 + 7 + 7)
 #define FIELDS_2 (FIELDS_6 + 6 + 7 + 7 + 7)


More information about the busybox-cvs mailing list