[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v3 03/12] util/range: move range_list_from_string() to range.c
From: |
Sven Schnelle |
Subject: |
[PATCH v3 03/12] util/range: move range_list_from_string() to range.c |
Date: |
Fri, 1 Mar 2024 18:46:00 +0100 |
Signed-off-by: Sven Schnelle <svens@stackframe.org>
---
include/qemu/range.h | 7 ++++
util/log.c | 74 ------------------------------------------
util/range.c | 76 ++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 83 insertions(+), 74 deletions(-)
diff --git a/include/qemu/range.h b/include/qemu/range.h
index 205e1da76d..530b0c7db1 100644
--- a/include/qemu/range.h
+++ b/include/qemu/range.h
@@ -233,4 +233,11 @@ void range_inverse_array(GList *in_ranges,
GList **out_ranges,
uint64_t low, uint64_t high);
+/*
+ * Parse a comma separated list of address ranges into a @GArray
+ * of @Range entries. On error @errp is set.
+ */
+void range_list_from_string(GList **out_ranges, const char *filter_spec,
+ Error **errp);
+
#endif
diff --git a/util/log.c b/util/log.c
index f183ea4e4d..90897ef0f9 100644
--- a/util/log.c
+++ b/util/log.c
@@ -382,80 +382,6 @@ bool qemu_log_in_addr_range(uint64_t addr)
return false;
}
-static void range_list_from_string(GList **out_ranges, const char *filter_spec,
- Error **errp)
-{
- gchar **ranges = g_strsplit(filter_spec, ",", 0);
- struct Range *range = NULL;
- int i;
-
- if (*out_ranges) {
- g_list_free_full(*out_ranges, g_free);
- *out_ranges = NULL;
- }
-
- for (i = 0; ranges[i]; i++) {
- const char *r = ranges[i];
- const char *range_op, *r2, *e;
- uint64_t r1val, r2val, lob, upb;
- range = g_new0(struct Range, 1);
-
- range_op = strstr(r, "-");
- r2 = range_op ? range_op + 1 : NULL;
- if (!range_op) {
- range_op = strstr(r, "+");
- r2 = range_op ? range_op + 1 : NULL;
- }
- if (!range_op) {
- range_op = strstr(r, "..");
- r2 = range_op ? range_op + 2 : NULL;
- }
- if (!range_op) {
- error_setg(errp, "Bad range specifier");
- goto out;
- }
-
- if (qemu_strtou64(r, &e, 0, &r1val)
- || e != range_op) {
- error_setg(errp, "Invalid number to the left of %.*s",
- (int)(r2 - range_op), range_op);
- goto out;
- }
- if (qemu_strtou64(r2, NULL, 0, &r2val)) {
- error_setg(errp, "Invalid number to the right of %.*s",
- (int)(r2 - range_op), range_op);
- goto out;
- }
-
- switch (*range_op) {
- case '+':
- lob = r1val;
- upb = r1val + r2val - 1;
- break;
- case '-':
- upb = r1val;
- lob = r1val - (r2val - 1);
- break;
- case '.':
- lob = r1val;
- upb = r2val;
- break;
- default:
- g_assert_not_reached();
- }
- if (lob > upb) {
- error_setg(errp, "Invalid range");
- goto out;
- }
- range_set_bounds(range, lob, upb);
- *out_ranges = g_list_append(*out_ranges, range);
- range = NULL;
- }
-out:
- g_free(range);
- g_strfreev(ranges);
-}
-
void qemu_set_dfilter_ranges(const char *filter_spec, Error **errp)
{
range_list_from_string(&debug_regions, filter_spec, errp);
diff --git a/util/range.c b/util/range.c
index f3f40098d5..bd2d0961bd 100644
--- a/util/range.c
+++ b/util/range.c
@@ -19,6 +19,8 @@
#include "qemu/osdep.h"
#include "qemu/range.h"
+#include "qemu/cutils.h"
+#include "qapi/error.h"
int range_compare(Range *a, Range *b)
{
@@ -121,3 +123,77 @@ void range_inverse_array(GList *in, GList **rev,
exit:
*rev = out;
}
+
+void range_list_from_string(GList **out_ranges, const char *filter_spec,
+ Error **errp)
+{
+ gchar **ranges = g_strsplit(filter_spec, ",", 0);
+ struct Range *range = NULL;
+ int i;
+
+ if (*out_ranges) {
+ g_list_free_full(*out_ranges, g_free);
+ *out_ranges = NULL;
+ }
+
+ for (i = 0; ranges[i]; i++) {
+ const char *r = ranges[i];
+ const char *range_op, *r2, *e;
+ uint64_t r1val, r2val, lob, upb;
+ range = g_new0(struct Range, 1);
+
+ range_op = strstr(r, "-");
+ r2 = range_op ? range_op + 1 : NULL;
+ if (!range_op) {
+ range_op = strstr(r, "+");
+ r2 = range_op ? range_op + 1 : NULL;
+ }
+ if (!range_op) {
+ range_op = strstr(r, "..");
+ r2 = range_op ? range_op + 2 : NULL;
+ }
+ if (!range_op) {
+ error_setg(errp, "Bad range specifier");
+ goto out;
+ }
+
+ if (qemu_strtou64(r, &e, 0, &r1val)
+ || e != range_op) {
+ error_setg(errp, "Invalid number to the left of %.*s",
+ (int)(r2 - range_op), range_op);
+ goto out;
+ }
+ if (qemu_strtou64(r2, NULL, 0, &r2val)) {
+ error_setg(errp, "Invalid number to the right of %.*s",
+ (int)(r2 - range_op), range_op);
+ goto out;
+ }
+
+ switch (*range_op) {
+ case '+':
+ lob = r1val;
+ upb = r1val + r2val - 1;
+ break;
+ case '-':
+ upb = r1val;
+ lob = r1val - (r2val - 1);
+ break;
+ case '.':
+ lob = r1val;
+ upb = r2val;
+ break;
+ default:
+ g_assert_not_reached();
+ }
+ if (lob > upb) {
+ error_setg(errp, "Invalid range");
+ goto out;
+ }
+ range_set_bounds(range, lob, upb);
+ *out_ranges = g_list_append(*out_ranges, range);
+ range = NULL;
+ }
+out:
+ g_free(range);
+ g_strfreev(ranges);
+}
--
2.43.2
- [PATCH v3 01/12] util/log: convert debug_regions to GList, (continued)
[PATCH v3 06/12] util/range: split up range_list_from_string(), Sven Schnelle, 2024/03/01
[PATCH v3 05/12] util/range: use append_new_range() in range_list_from_string(), Sven Schnelle, 2024/03/01
[PATCH v3 02/12] util/log: make qemu_set_dfilter_ranges() take a GList, Sven Schnelle, 2024/03/01
[PATCH v3 03/12] util/range: move range_list_from_string() to range.c,
Sven Schnelle <=
[PATCH v3 07/12] util/range: make range_list_from_string() accept a single number, Sven Schnelle, 2024/03/01
[PATCH v3 08/12] qemu/range: add range_list_contains() function, Sven Schnelle, 2024/03/01
[PATCH v3 04/12] util/range: add range_list_free(), Sven Schnelle, 2024/03/01
[PATCH v3 10/12] plugins: add range list API, Sven Schnelle, 2024/03/01
[PATCH v3 12/12] plugins/execlog: add data address match, Sven Schnelle, 2024/03/01
[PATCH v3 09/12] plugins: add API to print errors, Sven Schnelle, 2024/03/01
[PATCH v3 11/12] plugins/execlog: use range list api, Sven Schnelle, 2024/03/01