[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20180223175213.25561-1-edgard.arvelaez-diaz@senecacollege.ca>
Date: Fri, 23 Feb 2018 12:52:13 -0500
From: Alex Diaz <edgard.arvelaez-diaz@...ecacollege.ca>
To: acme@...nel.org
Cc: linux-kernel@...r.kernel.org,
Alex Diaz <edgard.arvelaez-diaz@...ecacollege.ca>
Subject: [PATCH] perf: Reduce memory usage of asnprinf_expr_inout_ints
Originally a formula was hardcoded to allocate 28 bytes
per integer passed to asprintf_expr_inout_ints. This commit
replaces said formula and only allocates as many bytes as
needed.
This commit saves memory in cases where numbers passed
are small in magnitude.
Signed-off-by: Alex Diaz <edgard.arvelaez-diaz@...ecacollege.ca>
---
tools/perf/util/string.c | 20 ++++++++++++--------
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/tools/perf/util/string.c b/tools/perf/util/string.c
index d8bfd0c4d2cb..b04146099205 100644
--- a/tools/perf/util/string.c
+++ b/tools/perf/util/string.c
@@ -3,6 +3,7 @@
#include <linux/kernel.h>
#include <linux/string.h>
#include <stdlib.h>
+#include <math.h>
#include "sane_ctype.h"
@@ -360,15 +361,18 @@ char *rtrim(char *s)
char *asprintf_expr_inout_ints(const char *var, bool in, size_t nints, int *ints)
{
- /*
- * FIXME: replace this with an expression using log10() when we
- * find a suitable implementation, maybe the one in the dvb drivers...
- *
- * "%s == %d || " = log10(MAXINT) * 2 + 8 chars for the operators
- */
- size_t size = nints * 28 + 1; /* \0 */
+ int max_int = 1;
+ int no_digits;
+ size_t size;
size_t i, printed = 0;
- char *expr = malloc(size);
+ char *expr = NULL;
+
+ for (i = 0; i < nints; ++i)
+ max_int = max(ints[i], max_int);
+
+ no_digits = log10(max_int) + 1;
+ size = (no_digits + strlen(var) + 8) * nints;
+ expr = malloc(size);
if (expr) {
const char *or_and = "||", *eq_neq = "==";
--
2.16.1
Powered by blists - more mailing lists