[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230110222003.1591436-6-irogers@google.com>
Date: Tue, 10 Jan 2023 14:20:01 -0800
From: Ian Rogers <irogers@...gle.com>
To: Peter Zijlstra <peterz@...radead.org>,
Ingo Molnar <mingo@...hat.com>,
Arnaldo Carvalho de Melo <acme@...nel.org>,
Mark Rutland <mark.rutland@....com>,
Alexander Shishkin <alexander.shishkin@...ux.intel.com>,
Jiri Olsa <jolsa@...nel.org>,
Namhyung Kim <namhyung@...nel.org>,
Nathan Chancellor <nathan@...nel.org>,
Nick Desaulniers <ndesaulniers@...gle.com>,
Tom Rix <trix@...hat.com>, Nicolas Schier <nicolas@...sle.eu>,
Masahiro Yamada <masahiroy@...nel.org>,
Athira Rajeev <atrajeev@...ux.vnet.ibm.com>,
Christy Lee <christylee@...com>,
Andrii Nakryiko <andrii@...nel.org>,
Ravi Bangoria <ravi.bangoria@....com>,
Leo Yan <leo.yan@...aro.org>,
Yang Jihong <yangjihong1@...wei.com>,
Qi Liu <liuqi115@...wei.com>,
James Clark <james.clark@....com>,
Adrian Hunter <adrian.hunter@...el.com>,
"Masami Hiramatsu (Google)" <mhiramat@...nel.org>,
Kan Liang <kan.liang@...ux.intel.com>,
Sean Christopherson <seanjc@...gle.com>,
Zhengjun Xing <zhengjun.xing@...ux.intel.com>,
Rob Herring <robh@...nel.org>, Xin Gao <gaoxin@...rlc.com>,
Zechuan Chen <chenzechuan1@...wei.com>,
Jason Wang <wangborong@...rlc.com>,
Christophe JAILLET <christophe.jaillet@...adoo.fr>,
Stephane Eranian <eranian@...gle.com>,
German Gomez <german.gomez@....com>,
linux-kernel@...r.kernel.org, linux-perf-users@...r.kernel.org,
bpf@...r.kernel.org, llvm@...ts.linux.dev
Cc: Ian Rogers <irogers@...gle.com>
Subject: [PATCH v1 5/7] tools lib api: Tweak strbuf allocation size computation
alloc_nr gives an estimate of the actual memory behind an allocation
but isn't accurate. Use malloc_usable_size to accurately set the
strbuf alloc, which potentially avoids realloc calls.
Signed-off-by: Ian Rogers <irogers@...gle.com>
---
tools/lib/api/strbuf.c | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/tools/lib/api/strbuf.c b/tools/lib/api/strbuf.c
index eafa2c01f46a..a3d7f96d8b9f 100644
--- a/tools/lib/api/strbuf.c
+++ b/tools/lib/api/strbuf.c
@@ -4,6 +4,7 @@
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/zalloc.h>
+#include <malloc.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
@@ -42,7 +43,6 @@ char *strbuf_detach(struct strbuf *sb, size_t *sz)
return res;
}
-#define alloc_nr(x) (((x)+16)*3/2)
int strbuf_grow(struct strbuf *sb, size_t extra)
{
char *buf;
@@ -54,9 +54,6 @@ int strbuf_grow(struct strbuf *sb, size_t extra)
if (nr <= sb->len)
return -E2BIG;
- if (alloc_nr(sb->alloc) > nr)
- nr = alloc_nr(sb->alloc);
-
/*
* Note that sb->buf == strbuf_slopbuf if sb->alloc == 0, and it is
* a static variable. Thus we have to avoid passing it to realloc.
@@ -66,10 +63,9 @@ int strbuf_grow(struct strbuf *sb, size_t extra)
return -ENOMEM;
sb->buf = buf;
- sb->alloc = nr;
+ sb->alloc = malloc_usable_size(buf);
return 0;
}
-#undef alloc_nr
int strbuf_addch(struct strbuf *sb, int c)
{
--
2.39.0.314.g84b9a713c41-goog
Powered by blists - more mailing lists