diff options
author | Bart Polot <bart@net.in.tum.de> | 2012-12-17 19:10:43 +0000 |
---|---|---|
committer | Bart Polot <bart@net.in.tum.de> | 2012-12-17 19:10:43 +0000 |
commit | 39a13b80f71488e0c91f7fb6a78834017f589cbb (patch) | |
tree | 33db348a47d5a517d4147173f6a2130dc09483c3 /src/regex/regex_test_lib.c | |
parent | 822fccfb35ac30d61f1122e9349132ade6c7fba5 (diff) | |
download | gnunet-39a13b80f71488e0c91f7fb6a78834017f589cbb.tar.gz gnunet-39a13b80f71488e0c91f7fb6a78834017f589cbb.zip |
- free memory
Diffstat (limited to 'src/regex/regex_test_lib.c')
-rw-r--r-- | src/regex/regex_test_lib.c | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/src/regex/regex_test_lib.c b/src/regex/regex_test_lib.c index d02afa942..9863d6f4f 100644 --- a/src/regex/regex_test_lib.c +++ b/src/regex/regex_test_lib.c | |||
@@ -39,6 +39,13 @@ struct RegexCombineCtx { | |||
39 | }; | 39 | }; |
40 | 40 | ||
41 | 41 | ||
42 | /** | ||
43 | * Extract a string from all prefix-combined regexes. | ||
44 | * | ||
45 | * @param ctx Context with 0 or more regexes. | ||
46 | * | ||
47 | * @return Regex that matches any of the added regexes. | ||
48 | */ | ||
42 | static char * | 49 | static char * |
43 | regex_combine (struct RegexCombineCtx *ctx) | 50 | regex_combine (struct RegexCombineCtx *ctx) |
44 | { | 51 | { |
@@ -76,6 +83,13 @@ regex_combine (struct RegexCombineCtx *ctx) | |||
76 | return regex; | 83 | return regex; |
77 | } | 84 | } |
78 | 85 | ||
86 | |||
87 | /** | ||
88 | * Add a single regex to a context, combining with exisiting regex by-prefix. | ||
89 | * | ||
90 | * @param ctx Context with 0 or more regexes. | ||
91 | * @param regex Regex to add. | ||
92 | */ | ||
79 | static void | 93 | static void |
80 | regex_add (struct RegexCombineCtx *ctx, const char *regex) | 94 | regex_add (struct RegexCombineCtx *ctx, const char *regex) |
81 | { | 95 | { |
@@ -120,6 +134,27 @@ regex_add (struct RegexCombineCtx *ctx, const char *regex) | |||
120 | 134 | ||
121 | 135 | ||
122 | /** | 136 | /** |
137 | * Free all resources used by the context node and all its children. | ||
138 | * | ||
139 | * @param ctx Context to free. | ||
140 | */ | ||
141 | static void | ||
142 | regex_ctx_destroy (struct RegexCombineCtx *ctx) | ||
143 | { | ||
144 | struct RegexCombineCtx *p; | ||
145 | struct RegexCombineCtx *next; | ||
146 | |||
147 | for (p = ctx->head; NULL != p; p = next) | ||
148 | { | ||
149 | next = p->next; | ||
150 | regex_ctx_destroy (p); | ||
151 | } | ||
152 | GNUNET_free (ctx->s); | ||
153 | GNUNET_free (ctx); | ||
154 | } | ||
155 | |||
156 | |||
157 | /** | ||
123 | * Return a prefix-combine regex that matches the same strings as | 158 | * Return a prefix-combine regex that matches the same strings as |
124 | * any of the original regexes. | 159 | * any of the original regexes. |
125 | * | 160 | * |
@@ -133,18 +168,20 @@ GNUNET_REGEX_combine (char * const regexes[]) | |||
133 | unsigned int i; | 168 | unsigned int i; |
134 | char *combined; | 169 | char *combined; |
135 | const char *current; | 170 | const char *current; |
136 | struct RegexCombineCtx ctx; | 171 | struct RegexCombineCtx *ctx; |
137 | 172 | ||
138 | memset (&ctx, 0, sizeof (struct RegexCombineCtx)); | 173 | ctx = GNUNET_malloc (sizeof (struct RegexCombineCtx)); |
139 | for (i = 0; regexes[i]; i++) | 174 | for (i = 0; regexes[i]; i++) |
140 | { | 175 | { |
141 | current = regexes[i]; | 176 | current = regexes[i]; |
142 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); | 177 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex %u: %s\n", i, current); |
143 | regex_add (&ctx, current); | 178 | regex_add (ctx, current); |
144 | } | 179 | } |
145 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); | 180 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nCombining...\n"); |
146 | 181 | ||
147 | combined = regex_combine (&ctx); | 182 | combined = regex_combine (ctx); |
183 | |||
184 | regex_ctx_destroy (ctx); | ||
148 | 185 | ||
149 | return combined; | 186 | return combined; |
150 | } | 187 | } |