|
4 | 4 | #include "expr.h" |
5 | 5 | #include "expr-bison.h" |
6 | 6 | #include "expr-flex.h" |
| 7 | +#include <linux/kernel.h> |
7 | 8 |
|
8 | 9 | #ifdef PARSER_DEBUG |
9 | 10 | extern int expr_debug; |
10 | 11 | #endif |
11 | 12 |
|
| 13 | +static size_t key_hash(const void *key, void *ctx __maybe_unused) |
| 14 | +{ |
| 15 | + const char *str = (const char *)key; |
| 16 | + size_t hash = 0; |
| 17 | + |
| 18 | + while (*str != '\0') { |
| 19 | + hash *= 31; |
| 20 | + hash += *str; |
| 21 | + str++; |
| 22 | + } |
| 23 | + return hash; |
| 24 | +} |
| 25 | + |
| 26 | +static bool key_equal(const void *key1, const void *key2, |
| 27 | + void *ctx __maybe_unused) |
| 28 | +{ |
| 29 | + return !strcmp((const char *)key1, (const char *)key2); |
| 30 | +} |
| 31 | + |
12 | 32 | /* Caller must make sure id is allocated */ |
13 | | -void expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val) |
| 33 | +int expr__add_id(struct expr_parse_ctx *ctx, const char *name, double val) |
14 | 34 | { |
15 | | - int idx; |
| 35 | + double *val_ptr = NULL, *old_val = NULL; |
| 36 | + char *old_key = NULL; |
| 37 | + int ret; |
| 38 | + |
| 39 | + if (val != 0.0) { |
| 40 | + val_ptr = malloc(sizeof(double)); |
| 41 | + if (!val_ptr) |
| 42 | + return -ENOMEM; |
| 43 | + *val_ptr = val; |
| 44 | + } |
| 45 | + ret = hashmap__set(&ctx->ids, name, val_ptr, |
| 46 | + (const void **)&old_key, (void **)&old_val); |
| 47 | + free(old_key); |
| 48 | + free(old_val); |
| 49 | + return ret; |
| 50 | +} |
| 51 | + |
| 52 | +int expr__get_id(struct expr_parse_ctx *ctx, const char *id, double *val_ptr) |
| 53 | +{ |
| 54 | + double *data; |
16 | 55 |
|
17 | | - assert(ctx->num_ids < MAX_PARSE_ID); |
18 | | - idx = ctx->num_ids++; |
19 | | - ctx->ids[idx].name = name; |
20 | | - ctx->ids[idx].val = val; |
| 56 | + if (!hashmap__find(&ctx->ids, id, (void **)&data)) |
| 57 | + return -1; |
| 58 | + *val_ptr = (data == NULL) ? 0.0 : *data; |
| 59 | + return 0; |
21 | 60 | } |
22 | 61 |
|
23 | 62 | void expr__ctx_init(struct expr_parse_ctx *ctx) |
24 | 63 | { |
25 | | - ctx->num_ids = 0; |
| 64 | + hashmap__init(&ctx->ids, key_hash, key_equal, NULL); |
| 65 | +} |
| 66 | + |
| 67 | +void expr__ctx_clear(struct expr_parse_ctx *ctx) |
| 68 | +{ |
| 69 | + struct hashmap_entry *cur; |
| 70 | + size_t bkt; |
| 71 | + |
| 72 | + hashmap__for_each_entry((&ctx->ids), cur, bkt) { |
| 73 | + free((char *)cur->key); |
| 74 | + free(cur->value); |
| 75 | + } |
| 76 | + hashmap__clear(&ctx->ids); |
26 | 77 | } |
27 | 78 |
|
28 | 79 | static int |
@@ -56,61 +107,25 @@ __expr__parse(double *val, struct expr_parse_ctx *ctx, const char *expr, |
56 | 107 | return ret; |
57 | 108 | } |
58 | 109 |
|
59 | | -int expr__parse(double *final_val, struct expr_parse_ctx *ctx, const char *expr, int runtime) |
| 110 | +int expr__parse(double *final_val, struct expr_parse_ctx *ctx, |
| 111 | + const char *expr, int runtime) |
60 | 112 | { |
61 | 113 | return __expr__parse(final_val, ctx, expr, EXPR_PARSE, runtime) ? -1 : 0; |
62 | 114 | } |
63 | 115 |
|
64 | | -static bool |
65 | | -already_seen(const char *val, const char *one, const char **other, |
66 | | - int num_other) |
67 | | -{ |
68 | | - int i; |
69 | | - |
70 | | - if (one && !strcasecmp(one, val)) |
71 | | - return true; |
72 | | - for (i = 0; i < num_other; i++) |
73 | | - if (!strcasecmp(other[i], val)) |
74 | | - return true; |
75 | | - return false; |
76 | | -} |
77 | | - |
78 | | -int expr__find_other(const char *expr, const char *one, const char ***other, |
79 | | - int *num_other, int runtime) |
| 116 | +int expr__find_other(const char *expr, const char *one, |
| 117 | + struct expr_parse_ctx *ctx, int runtime) |
80 | 118 | { |
81 | | - int err, i = 0, j = 0; |
82 | | - struct expr_parse_ctx ctx; |
83 | | - |
84 | | - expr__ctx_init(&ctx); |
85 | | - err = __expr__parse(NULL, &ctx, expr, EXPR_OTHER, runtime); |
86 | | - if (err) |
87 | | - return -1; |
88 | | - |
89 | | - *other = malloc((ctx.num_ids + 1) * sizeof(char *)); |
90 | | - if (!*other) |
91 | | - return -ENOMEM; |
92 | | - |
93 | | - for (i = 0, j = 0; i < ctx.num_ids; i++) { |
94 | | - const char *str = ctx.ids[i].name; |
95 | | - |
96 | | - if (already_seen(str, one, *other, j)) |
97 | | - continue; |
98 | | - |
99 | | - str = strdup(str); |
100 | | - if (!str) |
101 | | - goto out; |
102 | | - (*other)[j++] = str; |
103 | | - } |
104 | | - (*other)[j] = NULL; |
105 | | - |
106 | | -out: |
107 | | - if (i != ctx.num_ids) { |
108 | | - while (--j) |
109 | | - free((char *) (*other)[i]); |
110 | | - free(*other); |
111 | | - err = -1; |
| 119 | + double *old_val = NULL; |
| 120 | + char *old_key = NULL; |
| 121 | + int ret = __expr__parse(NULL, ctx, expr, EXPR_OTHER, runtime); |
| 122 | + |
| 123 | + if (one) { |
| 124 | + hashmap__delete(&ctx->ids, one, |
| 125 | + (const void **)&old_key, (void **)&old_val); |
| 126 | + free(old_key); |
| 127 | + free(old_val); |
112 | 128 | } |
113 | 129 |
|
114 | | - *num_other = j; |
115 | | - return err; |
| 130 | + return ret; |
116 | 131 | } |
0 commit comments