From 498e984690895987067743bb94ee83d49da7ecb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= Date: Tue, 26 Feb 2019 11:32:02 +0100 Subject: Fix memmory errors. --- linked_list.h | 4 ++++ linked_list.inc.h | 2 +- parse.c | 19 +++++++++++-------- trie.h | 1 + trie.inc.h | 24 +++++++++++++++++++++--- 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/linked_list.h b/linked_list.h index a17bd797..ec1e17e0 100644 --- a/linked_list.h +++ b/linked_list.h @@ -63,6 +63,10 @@ int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ); */ int RESET(LLIST(TYPE)) ( LLIST(TYPE)* llist ); +/* + * Takes to lists, and merges them into a single one. Destroys new_ in + * the process. + */ LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new_); FMT_F(LLIST(TYPE)); diff --git a/linked_list.inc.h b/linked_list.inc.h index 499d4509..81974a9c 100644 --- a/linked_list.inc.h +++ b/linked_list.inc.h @@ -139,7 +139,7 @@ int EMPTY(LLIST(TYPE)) ( LLIST(TYPE)* llist ) { LLIST(TYPE)* RESOLVE(LLIST(TYPE)) (LLIST(TYPE)* dest, LLIST(TYPE)* new__) { if (dest == NULL) return new__; APPEND(LLIST(TYPE))(dest, new__); - // TODO free new__? + free(new__); return dest; } diff --git a/parse.c b/parse.c index ead020f8..536c3917 100644 --- a/parse.c +++ b/parse.c @@ -32,6 +32,9 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { /* * Create a content_line which we use as storage while we are * parsing. This object is constantly broken down and rebuilt. + * + * {cline,param}_key is also temporary register used during + * parsing. */ SNEW(content_line, cline); SNEW(strbuf, cline_key); @@ -46,7 +49,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { if (fold(&ctx, c) > 0) { /* Actuall end of line, handle value */ TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - handle_kv(¶m_key, &cline, &ctx); + handle_kv(&cline_key, &cline, &ctx); p_ctx = p_key; } /* Else continue on current line */ @@ -57,11 +60,8 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { /* Border between param {key, value} */ } else if (p_ctx == p_param_name && c == '=') { - /* Create a new parameter set and push the current string - * as its key */ + /* Save the current parameter key */ TRANSFER (¶m_key, &ctx.str); - // PUSH(LLIST(param_set))(CLINE_CUR_PARAMS(&cline), ps); - p_ctx = p_param_value; /* @@ -76,7 +76,7 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { /* We got a parameter value, push the current string to * the current parameter set. */ if (p_ctx == p_param_value) { - /* push kv pair */ + /* save current parameter value. */ NEW(strbuf, s); TRANSFER(s, &ctx.str); @@ -84,7 +84,8 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { NEW(param_set, ps); PUSH(LLIST(strbuf))(ps, s); - PUSH(TRIE(param_set))(CLINE_CUR_PARAMS(&cline), s->mem, ps); + PUSH(TRIE(param_set))(CLINE_CUR_PARAMS(&cline), param_key.mem, ps); + strbuf_soft_reset (¶m_key); } /* @@ -128,11 +129,13 @@ int parse_file(char* filename, FILE* f, vcomponent* root) { */ TRANSFER(CLINE_CUR_VAL(&cline), &ctx.str); - handle_kv(¶m_key, &cline, &ctx); + handle_kv(&cline_key, &cline, &ctx); } FREE(content_line)(&cline); + FREE(strbuf)(&cline_key); + FREE(strbuf)(¶m_key); assert(POP(LLIST(vcomponent))(&ctx.comp_stack) == root); assert(EMPTY(LLIST(strbuf))(&ctx.key_stack)); diff --git a/trie.h b/trie.h index 908c4e41..9de60771 100644 --- a/trie.h +++ b/trie.h @@ -43,6 +43,7 @@ int EMPTY(TRIE(TYPE))(TRIE(TYPE)*); FMT_F(TRIE_NODE(TYPE)); FMT_F(TRIE(TYPE)); +int DEEP_COPY(TRIE_NODE(TYPE)) (TRIE_NODE(TYPE)* dest, TRIE_NODE(TYPE)* src); int DEEP_COPY(TRIE(TYPE)) (TRIE(TYPE)* dest, TRIE(TYPE)* src); #endif /* TYPE */ diff --git a/trie.inc.h b/trie.inc.h index 90d9cb06..afa6656a 100644 --- a/trie.inc.h +++ b/trie.inc.h @@ -163,10 +163,28 @@ FMT_F(TRIE(TYPE)) { return seek; } +int DEEP_COPY(TRIE_NODE(TYPE)) (TRIE_NODE(TYPE)* dest, TRIE_NODE(TYPE)* src) { + if (dest == NULL) return 1; + if (dest->value != NULL) { + RENEW(TYPE, src->value); + DEEP_COPY(TYPE)(src->value, dest->value); + } + + if (dest->next != NULL) { + RENEW(TRIE_NODE(TYPE), src->next, dest->c); + DEEP_COPY(TRIE_NODE(TYPE))(src->next, dest->next); + } + + if (dest->child != NULL) { + RENEW(TRIE_NODE(TYPE), src->child, dest->c); + DEEP_COPY(TRIE_NODE(TYPE))(src->child, dest->child); + } + + return 0; +} + int DEEP_COPY(TRIE(TYPE)) (TRIE(TYPE)* dest, TRIE(TYPE)* src) { - (void) dest; (void) src; - ERR(deep copy on tries currently not implemented); - return 1; + return DEEP_COPY(TRIE_NODE(TYPE))(dest->root, src->root); } #endif /* TYPE */ -- cgit v1.2.3