From: "Steven Rostedt (Google)" <rostedt@xxxxxxxxxxx> Currently, the size of the keys and vals are determined by the requirement that the traceeval_type array end with an empty element of type TRACEEVAL_TYPE_NONE. To make the API be able to handle changes of the size of that structure, the traceeval_init() was converted to a macro to pass in the sizeof of that structure. This also means it can calculate the size of the array with the TRACEEVAL_ARRAY_SIZE() macro. Remove the need to have the initialization of traceeval keys and vals have to add the NONE element at the end. It can still do so and this will work just the same (the size will be determined by either the size passed in or the first NONE element, which ever is smaller). Signed-off-by: Steven Rostedt (Google) <rostedt@xxxxxxxxxxx> --- include/traceeval-hist.h | 11 +++++++++-- src/histograms.c | 23 +++++++++++++---------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/include/traceeval-hist.h b/include/traceeval-hist.h index 65a905034773..cd089b28b852 100644 --- a/include/traceeval-hist.h +++ b/include/traceeval-hist.h @@ -171,12 +171,19 @@ struct traceeval; /* Histogram interfaces */ -#define traceeval_init(keys, vals) \ - traceeval_init_data_size(keys, vals, sizeof(struct traceeval_type), \ +#define traceeval_init(keys, vals) \ + traceeval_init_size(keys, vals, \ + TRACEEVAL_ARRAY_SIZE(keys), \ + (void *)vals == NULL ? 0 : TRACEEVAL_ARRAY_SIZE(vals)) + +#define traceeval_init_size(keys, vals, nr_keys, nr_vals) \ + traceeval_init_data_size(keys, vals, nr_keys, nr_vals, \ + sizeof(struct traceeval_type), \ sizeof(struct traceeval_data)) struct traceeval *traceeval_init_data_size(struct traceeval_type *keys, struct traceeval_type *vals, + size_t nr_keys, size_t nr_vals, size_t sizeof_type, size_t sizeof_data); void traceeval_release(struct traceeval *teval); diff --git a/src/histograms.c b/src/histograms.c index d8ee373be705..0f20d76b5e04 100644 --- a/src/histograms.c +++ b/src/histograms.c @@ -151,7 +151,8 @@ static void type_release(struct traceeval_type *defs, size_t len) * Returns the size of the array pointed to by @copy, or -1 on error. */ static size_t type_alloc(const struct traceeval_type *defs, - struct traceeval_type **copy) + struct traceeval_type **copy, + size_t cnt) { struct traceeval_type *new_defs = NULL; size_t size; @@ -162,7 +163,8 @@ static size_t type_alloc(const struct traceeval_type *defs, if (!defs) return 0; - for (size = 0; defs && defs[size].type != TRACEEVAL_TYPE_NONE; size++) + for (size = 0; defs && size < cnt && + defs[size].type != TRACEEVAL_TYPE_NONE; size++) ; if (!size) @@ -198,9 +200,9 @@ fail: return -1; } -static int check_keys(struct traceeval_type *keys) +static int check_keys(struct traceeval_type *keys, int cnt) { - for (int i = 0; keys[i].type != TRACEEVAL_TYPE_NONE; i++) { + for (int i = 0; i < cnt && keys[i].type != TRACEEVAL_TYPE_NONE; i++) { /* Define this as a key */ keys[i].flags |= TRACEEVAL_FL_KEY; keys[i].flags &= ~TRACEEVAL_FL_VALUE; @@ -220,9 +222,9 @@ static int check_keys(struct traceeval_type *keys) return 0; } -static int check_vals(struct traceeval_type *vals) +static int check_vals(struct traceeval_type *vals, int cnt) { - for (int i = 0; vals[i].type != TRACEEVAL_TYPE_NONE; i++) { + for (int i = 0; i < cnt && vals[i].type != TRACEEVAL_TYPE_NONE; i++) { /* Define this as a value */ vals[i].flags |= TRACEEVAL_FL_VALUE; vals[i].flags &= ~TRACEEVAL_FL_KEY; @@ -269,6 +271,7 @@ static int check_vals(struct traceeval_type *vals) */ struct traceeval *traceeval_init_data_size(struct traceeval_type *keys, struct traceeval_type *vals, + size_t nr_keys, size_t nr_vals, size_t sizeof_type, size_t sizeof_data) { struct traceeval *teval; @@ -290,25 +293,25 @@ struct traceeval *traceeval_init_data_size(struct traceeval_type *keys, goto fail; } - ret = check_keys(keys); + ret = check_keys(keys, nr_keys); if (ret < 0) goto fail_release; if (vals) { - ret = check_vals(vals); + ret = check_vals(vals, nr_vals); if (ret < 0) goto fail_release; } /* alloc key types */ - teval->nr_key_types = type_alloc(keys, &teval->key_types); + teval->nr_key_types = type_alloc(keys, &teval->key_types, nr_keys); if (teval->nr_key_types <= 0) { err_msg = "Failed to allocate user defined keys"; goto fail_release; } /* alloc val types */ - teval->nr_val_types = type_alloc(vals, &teval->val_types); + teval->nr_val_types = type_alloc(vals, &teval->val_types, nr_vals); if (teval->nr_val_types < 0) { err_msg = "Failed to allocate user defined values"; goto fail_release; -- 2.40.1