lkml.org 
[lkml]   [2018]   [Feb]   [23]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
Subject[PATCH] perf: Reduce memory usage of asnprinf_expr_inout_ints
Date
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@senecacollege.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
\
 
 \ /
  Last update: 2018-02-23 18:53    [W:0.025 / U:2.808 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site