linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org,
	"Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 1/6] kernel-shark: Add KS_DOUBLE_SIZE macro
Date: Wed,  6 Jan 2021 18:11:15 +0200	[thread overview]
Message-ID: <20210106161120.119085-2-y.karadz@gmail.com> (raw)
In-Reply-To: <20210106161120.119085-1-y.karadz@gmail.com>

The macro is useful for resizing of dynamic arrays. It is currently
used to resize the Data stream descriptor array, owned by the session
context. We will later use the macro with the arrays of data fields and
plugin contexts.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/libkshark-plugin.h | 14 ++++++++++++++
 src/libkshark.c        | 15 +++++++--------
 2 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
index 1a642ad..f3c724f 100644
--- a/src/libkshark-plugin.h
+++ b/src/libkshark-plugin.h
@@ -346,6 +346,20 @@ int kshark_handle_dpi(struct kshark_data_stream *stream,
 int kshark_handle_all_dpis(struct kshark_data_stream *stream,
 			   enum kshark_plugin_actions  task_id);
 
+/** General purpose macro for resizing dynamic arrays. */
+#define KS_DOUBLE_SIZE(type, array, size, ok)				\
+{									\
+	ssize_t n = *size;						\
+	*ok = false;							\
+	type *tmp = (type *) realloc(array, 2 * n * sizeof(*tmp));	\
+	if (tmp) {							\
+		memset(tmp + n, 0, n * sizeof(*tmp));			\
+		*size = 2 * n;						\
+		array = tmp;						\
+		*ok = true;						\
+	}								\
+}									\
+
 #ifdef __cplusplus
 }
 #endif // __cplusplus
diff --git a/src/libkshark.c b/src/libkshark.c
index 315c24f..3aa3fa2 100644
--- a/src/libkshark.c
+++ b/src/libkshark.c
@@ -234,16 +234,15 @@ int kshark_add_stream(struct kshark_context *kshark_ctx)
 
 	if (kshark_ctx->stream_info.next_free_stream_id ==
 	    kshark_ctx->stream_info.array_size) {
-		size_t new_size = 2 * kshark_ctx->stream_info.array_size;
-		struct kshark_data_stream **streams_tmp;
+		bool ok;
 
-		streams_tmp = realloc(kshark_ctx->stream,
-				      new_size * sizeof(*kshark_ctx->stream));
-		if (!streams_tmp)
-			return -ENOMEM;
+		KS_DOUBLE_SIZE(struct kshark_data_stream *,
+			       kshark_ctx->stream,
+			       &kshark_ctx->stream_info.array_size,
+			       &ok);
 
-		kshark_ctx->stream = streams_tmp;
-		kshark_ctx->stream_info.array_size = new_size;
+		if (!ok)
+			return -ENOMEM;
 	}
 
 	stream = kshark_stream_alloc();
-- 
2.25.1


  reply	other threads:[~2021-01-06 16:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-06 16:11 [PATCH 0/6] kernel-shark: Visualization plugin tools Yordan Karadzhov (VMware)
2021-01-06 16:11 ` Yordan Karadzhov (VMware) [this message]
2021-01-06 17:02   ` [PATCH 1/6] kernel-shark: Add KS_DOUBLE_SIZE macro Steven Rostedt
2021-01-06 17:28     ` Steven Rostedt
2021-01-06 16:11 ` [PATCH 2/6] kernel-shark: Add kshark_data_container to libkshark Yordan Karadzhov (VMware)
2021-01-06 17:38   ` Steven Rostedt
2021-01-06 16:11 ` [PATCH 3/6] kernel-shark: Add KS_DEFINE_PLUGIN_CONTEXT macro Yordan Karadzhov (VMware)
2021-01-06 17:45   ` Steven Rostedt
2021-01-06 16:11 ` [PATCH 4/6] kernel-shark: Start using C++17 Yordan Karadzhov (VMware)
2021-01-06 16:11 ` [PATCH 5/6] kernel-shark: Add plotting methods to KsPlugins Yordan Karadzhov (VMware)
2021-01-06 16:11 ` [PATCH 6/6] kernel-shark: Speed-up the sched_events plugin Yordan Karadzhov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210106161120.119085-2-y.karadz@gmail.com \
    --to=y.karadz@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).