aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYordan Karadzhov (VMware) <y.karadz@gmail.com>2021-01-08 16:31:35 +0200
committerSteven Rostedt (VMware) <rostedt@goodmis.org>2021-01-12 12:04:38 -0500
commitc7aae986b6944004caea4a5217a634ff052f40f5 (patch)
tree429a35fe318362b4ec71699178f59c2c0a706220
parentb63328e19b7546e113ff7429630498ff4347ba32 (diff)
downloadkernel-shark-c7aae986b6944004caea4a5217a634ff052f40f5.tar.gz
kernel-shark: Add KS_DOUBLE_SIZE macro
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. Link: https://lore.kernel.org/linux-trace-devel/20210108143140.285037-2-y.karadz@gmail.com Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
-rw-r--r--src/libkshark-plugin.h17
-rw-r--r--src/libkshark.c11
-rw-r--r--tests/libkshark-tests.cpp20
3 files changed, 39 insertions, 9 deletions
diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
index 1a642ad9..41043577 100644
--- a/src/libkshark-plugin.h
+++ b/src/libkshark-plugin.h
@@ -346,6 +346,23 @@ 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(array, size) \
+({ \
+ ssize_t __n = size; \
+ bool __ok = false; \
+ __typeof__(array) __tmp = \
+ (__typeof__(array)) realloc(array, \
+ 2 * __n * sizeof(*__tmp)); \
+ if (__tmp) { \
+ memset(__tmp + __n, 0, __n * sizeof(*__tmp)); \
+ size = 2 * __n; \
+ array = __tmp; \
+ __ok = true; \
+ } \
+ __ok; \
+}) \
+
#ifdef __cplusplus
}
#endif // __cplusplus
diff --git a/src/libkshark.c b/src/libkshark.c
index 315c24f4..0e2ce7a5 100644
--- a/src/libkshark.c
+++ b/src/libkshark.c
@@ -234,16 +234,9 @@ 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;
-
- streams_tmp = realloc(kshark_ctx->stream,
- new_size * sizeof(*kshark_ctx->stream));
- if (!streams_tmp)
+ if (!KS_DOUBLE_SIZE(kshark_ctx->stream,
+ kshark_ctx->stream_info.array_size))
return -ENOMEM;
-
- kshark_ctx->stream = streams_tmp;
- kshark_ctx->stream_info.array_size = new_size;
}
stream = kshark_stream_alloc();
diff --git a/tests/libkshark-tests.cpp b/tests/libkshark-tests.cpp
index 27c11717..d0a35944 100644
--- a/tests/libkshark-tests.cpp
+++ b/tests/libkshark-tests.cpp
@@ -46,3 +46,23 @@ BOOST_AUTO_TEST_CASE(add_remove_streams)
kshark_close_all(kshark_ctx);
}
+
+#define ARRAY_DEFAULT_SIZE 1000
+BOOST_AUTO_TEST_CASE(doule_size_macro)
+{
+ int i, n = ARRAY_DEFAULT_SIZE;
+ int *arr = (int *) malloc(n * sizeof(*arr));
+ bool ok;
+
+ for (i = 0; i < n; ++i)
+ arr[i] = i;
+
+ ok = KS_DOUBLE_SIZE(arr, n);
+ BOOST_CHECK_EQUAL(ok, true);
+ BOOST_CHECK_EQUAL(n, ARRAY_DEFAULT_SIZE * 2);
+
+ for (i = 0; i < ARRAY_DEFAULT_SIZE; ++i)
+ BOOST_CHECK_EQUAL(arr[i], i);
+ for (; i < n; ++i)
+ BOOST_CHECK_EQUAL(arr[i], 0);
+}