diff mbox series

[23/35] perf tools: Add reallocarray_as_needed()

Message ID 20220711093218.10967-24-adrian.hunter@intel.com (mailing list archive)
State New, archived
Headers show
Series perf intel-pt: Add support for tracing virtual machine user space on the host | expand

Commit Message

Adrian Hunter July 11, 2022, 9:32 a.m. UTC
Add helper reallocarray_as_needed() to reallocate an array to a larger
size and initialize the extra entries to an arbitrary value.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++
 tools/perf/util/util.h | 15 +++++++++++++++
 2 files changed, 48 insertions(+)

Comments

Ian Rogers July 20, 2022, 12:55 a.m. UTC | #1
On Mon, Jul 11, 2022 at 2:33 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> Add helper reallocarray_as_needed() to reallocate an array to a larger
> size and initialize the extra entries to an arbitrary value.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
> ---
>  tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++
>  tools/perf/util/util.h | 15 +++++++++++++++
>  2 files changed, 48 insertions(+)
>
> diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
> index 9b02edf9311d..391c1e928bd7 100644
> --- a/tools/perf/util/util.c
> +++ b/tools/perf/util/util.c
> @@ -18,6 +18,7 @@
>  #include <linux/kernel.h>
>  #include <linux/log2.h>
>  #include <linux/time64.h>
> +#include <linux/overflow.h>
>  #include <unistd.h>
>  #include "cap.h"
>  #include "strlist.h"
> @@ -500,3 +501,35 @@ char *filename_with_chroot(int pid, const char *filename)
>
>         return new_name;
>  }
> +
> +/*
> + * Reallocate an array *arr of size *arr_sz so that it is big enough to contain
> + * x elements of size msz, initializing new entries to *init_val or zero if
> + * init_val is NULL
> + */
> +int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val)

This feels a little like a 1-dimensional xyarray, could we make a
similar abstraction to avoid passing all these values around?

Thanks,
Ian

> +{
> +       size_t new_sz = *arr_sz;
> +       void *new_arr;
> +       size_t i;
> +
> +       if (!new_sz)
> +               new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */
> +       while (x >= new_sz) {
> +               if (check_mul_overflow(new_sz, (size_t)2, &new_sz))
> +                       return -ENOMEM;
> +       }
> +       if (new_sz == *arr_sz)
> +               return 0;
> +       new_arr = calloc(new_sz, msz);
> +       if (!new_arr)
> +               return -ENOMEM;
> +       memcpy(new_arr, *arr, *arr_sz * msz);
> +       if (init_val) {
> +               for (i = *arr_sz; i < new_sz; i++)
> +                       memcpy(new_arr + (i * msz), init_val, msz);
> +       }
> +       *arr = new_arr;
> +       *arr_sz = new_sz;
> +       return 0;
> +}
> diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
> index 0f78f1e7782d..c1f2d423a9ec 100644
> --- a/tools/perf/util/util.h
> +++ b/tools/perf/util/util.h
> @@ -79,4 +79,19 @@ struct perf_debuginfod {
>  void perf_debuginfod_setup(struct perf_debuginfod *di);
>
>  char *filename_with_chroot(int pid, const char *filename);
> +
> +int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
> +                              size_t msz, const void *init_val);
> +
> +#define realloc_array_as_needed(a, n, x, v) ({                 \
> +       typeof(x) __x = (x);                                    \
> +       __x >= (n) ?                                            \
> +               do_realloc_array_as_needed((void **)&(a),       \
> +                                          &(n),                \
> +                                          __x,                 \
> +                                          sizeof(*(a)),        \
> +                                          (const void *)(v)) : \
> +               0;                                              \
> +       })
> +
>  #endif /* GIT_COMPAT_UTIL_H */
> --
> 2.25.1
>
Adrian Hunter Aug. 9, 2022, 4:48 p.m. UTC | #2
On 20/07/22 03:55, Ian Rogers wrote:
> On Mon, Jul 11, 2022 at 2:33 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>>
>> Add helper reallocarray_as_needed() to reallocate an array to a larger
>> size and initialize the extra entries to an arbitrary value.
>>
>> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
>> ---
>>  tools/perf/util/util.c | 33 +++++++++++++++++++++++++++++++++
>>  tools/perf/util/util.h | 15 +++++++++++++++
>>  2 files changed, 48 insertions(+)
>>
>> diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
>> index 9b02edf9311d..391c1e928bd7 100644
>> --- a/tools/perf/util/util.c
>> +++ b/tools/perf/util/util.c
>> @@ -18,6 +18,7 @@
>>  #include <linux/kernel.h>
>>  #include <linux/log2.h>
>>  #include <linux/time64.h>
>> +#include <linux/overflow.h>
>>  #include <unistd.h>
>>  #include "cap.h"
>>  #include "strlist.h"
>> @@ -500,3 +501,35 @@ char *filename_with_chroot(int pid, const char *filename)
>>
>>         return new_name;
>>  }
>> +
>> +/*
>> + * Reallocate an array *arr of size *arr_sz so that it is big enough to contain
>> + * x elements of size msz, initializing new entries to *init_val or zero if
>> + * init_val is NULL
>> + */
>> +int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val)
> 
> This feels a little like a 1-dimensional xyarray, could we make a
> similar abstraction to avoid passing all these values around?

xyarray does not realloc which is the only thing that is needed in
this case. C isn't C++, so adding an abstraction would be clunky.

> 
> Thanks,
> Ian
> 
>> +{
>> +       size_t new_sz = *arr_sz;
>> +       void *new_arr;
>> +       size_t i;
>> +
>> +       if (!new_sz)
>> +               new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */
>> +       while (x >= new_sz) {
>> +               if (check_mul_overflow(new_sz, (size_t)2, &new_sz))
>> +                       return -ENOMEM;
>> +       }
>> +       if (new_sz == *arr_sz)
>> +               return 0;
>> +       new_arr = calloc(new_sz, msz);
>> +       if (!new_arr)
>> +               return -ENOMEM;
>> +       memcpy(new_arr, *arr, *arr_sz * msz);
>> +       if (init_val) {
>> +               for (i = *arr_sz; i < new_sz; i++)
>> +                       memcpy(new_arr + (i * msz), init_val, msz);
>> +       }
>> +       *arr = new_arr;
>> +       *arr_sz = new_sz;
>> +       return 0;
>> +}
>> diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
>> index 0f78f1e7782d..c1f2d423a9ec 100644
>> --- a/tools/perf/util/util.h
>> +++ b/tools/perf/util/util.h
>> @@ -79,4 +79,19 @@ struct perf_debuginfod {
>>  void perf_debuginfod_setup(struct perf_debuginfod *di);
>>
>>  char *filename_with_chroot(int pid, const char *filename);
>> +
>> +int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
>> +                              size_t msz, const void *init_val);
>> +
>> +#define realloc_array_as_needed(a, n, x, v) ({                 \
>> +       typeof(x) __x = (x);                                    \
>> +       __x >= (n) ?                                            \
>> +               do_realloc_array_as_needed((void **)&(a),       \
>> +                                          &(n),                \
>> +                                          __x,                 \
>> +                                          sizeof(*(a)),        \
>> +                                          (const void *)(v)) : \
>> +               0;                                              \
>> +       })
>> +
>>  #endif /* GIT_COMPAT_UTIL_H */
>> --
>> 2.25.1
>>
diff mbox series

Patch

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 9b02edf9311d..391c1e928bd7 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -18,6 +18,7 @@ 
 #include <linux/kernel.h>
 #include <linux/log2.h>
 #include <linux/time64.h>
+#include <linux/overflow.h>
 #include <unistd.h>
 #include "cap.h"
 #include "strlist.h"
@@ -500,3 +501,35 @@  char *filename_with_chroot(int pid, const char *filename)
 
 	return new_name;
 }
+
+/*
+ * Reallocate an array *arr of size *arr_sz so that it is big enough to contain
+ * x elements of size msz, initializing new entries to *init_val or zero if
+ * init_val is NULL
+ */
+int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x, size_t msz, const void *init_val)
+{
+	size_t new_sz = *arr_sz;
+	void *new_arr;
+	size_t i;
+
+	if (!new_sz)
+		new_sz = msz >= 64 ? 1 : roundup(64, msz); /* Start with at least 64 bytes */
+	while (x >= new_sz) {
+		if (check_mul_overflow(new_sz, (size_t)2, &new_sz))
+			return -ENOMEM;
+	}
+	if (new_sz == *arr_sz)
+		return 0;
+	new_arr = calloc(new_sz, msz);
+	if (!new_arr)
+		return -ENOMEM;
+	memcpy(new_arr, *arr, *arr_sz * msz);
+	if (init_val) {
+		for (i = *arr_sz; i < new_sz; i++)
+			memcpy(new_arr + (i * msz), init_val, msz);
+	}
+	*arr = new_arr;
+	*arr_sz = new_sz;
+	return 0;
+}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index 0f78f1e7782d..c1f2d423a9ec 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -79,4 +79,19 @@  struct perf_debuginfod {
 void perf_debuginfod_setup(struct perf_debuginfod *di);
 
 char *filename_with_chroot(int pid, const char *filename);
+
+int do_realloc_array_as_needed(void **arr, size_t *arr_sz, size_t x,
+			       size_t msz, const void *init_val);
+
+#define realloc_array_as_needed(a, n, x, v) ({			\
+	typeof(x) __x = (x);					\
+	__x >= (n) ?						\
+		do_realloc_array_as_needed((void **)&(a),	\
+					   &(n),		\
+					   __x,			\
+					   sizeof(*(a)),	\
+					   (const void *)(v)) :	\
+		0;						\
+	})
+
 #endif /* GIT_COMPAT_UTIL_H */