From 54e97b75db0091bbbd2f38128de1dd6350d3f483 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Fri, 14 Apr 2017 21:29:18 -0400 Subject: [PATCH] CHANGED: Refactor mmd_engine cleanup --- Sources/libMultiMarkdown/mmd.c | 121 +++++++++++---------------------- 1 file changed, 41 insertions(+), 80 deletions(-) diff --git a/Sources/libMultiMarkdown/mmd.c b/Sources/libMultiMarkdown/mmd.c index 9eb1653..4e4af0f 100644 --- a/Sources/libMultiMarkdown/mmd.c +++ b/Sources/libMultiMarkdown/mmd.c @@ -221,68 +221,41 @@ void mmd_engine_set_language(mmd_engine * e, short language) { } -/// Free an existing MMD Engine -void mmd_engine_free(mmd_engine * e, bool freeDString) { - if (e == NULL) - return; - - if (e->root) +void mmd_engine_reset(mmd_engine * e) { + if (e->root) { token_tree_free(e->root); - - if (freeDString) - d_string_free(e->dstr, true); - - token_pair_engine_free(e->pairings1); - - token_pair_engine_free(e->pairings2); - token_pair_engine_free(e->pairings3); - - token_tree_free(e->root); - - // Pointers to blocks that are freed elsewhere - stack_free(e->definition_stack); - stack_free(e->header_stack); - stack_free(e->table_stack); - + e->root = NULL; + } // Abbreviations need to be freed while (e->abbreviation_stack->size) { footnote_free(stack_pop(e->abbreviation_stack)); } - stack_free(e->abbreviation_stack); - + // Citations need to be freed while (e->citation_stack->size) { footnote_free(stack_pop(e->citation_stack)); } - stack_free(e->citation_stack); // Footnotes need to be freed while (e->footnote_stack->size) { footnote_free(stack_pop(e->footnote_stack)); } - stack_free(e->footnote_stack); // Glossaries need to be freed while (e->glossary_stack->size) { footnote_free(stack_pop(e->glossary_stack)); } - stack_free(e->glossary_stack); - // Links need to be freed while (e->link_stack->size) { link_free(stack_pop(e->link_stack)); } - stack_free(e->link_stack); - // Metadata needs to be freed while (e->metadata_stack->size) { meta_free(stack_pop(e->metadata_stack)); } - stack_free(e->metadata_stack); - // Free asset hash asset * a, * a_tmp; @@ -291,6 +264,41 @@ void mmd_engine_free(mmd_engine * e, bool freeDString) { asset_free(a); // Free the asset } + // Reset other stacks + e->definition_stack->size = 0; + e->header_stack->size = 0; + e->table_stack->size = 0; +} + + +/// Free an existing MMD Engine +void mmd_engine_free(mmd_engine * e, bool freeDString) { + if (e == NULL) + return; + + mmd_engine_reset(e); + + if (freeDString) + d_string_free(e->dstr, true); + + token_pair_engine_free(e->pairings1); + + token_pair_engine_free(e->pairings2); + token_pair_engine_free(e->pairings3); + + // Pointers to blocks that are freed elsewhere + stack_free(e->definition_stack); + stack_free(e->header_stack); + stack_free(e->table_stack); + + // Takedown + stack_free(e->abbreviation_stack); + stack_free(e->citation_stack); + stack_free(e->footnote_stack); + stack_free(e->glossary_stack); + stack_free(e->link_stack); + stack_free(e->metadata_stack); + free(e); } @@ -1837,54 +1845,7 @@ void strip_line_tokens_from_block(mmd_engine * e, token * block) { token * mmd_engine_parse_substring(mmd_engine * e, size_t byte_start, size_t byte_len) { // First, clean up any leftovers from previous parse - // Free existing parse tree - if (e->root) - token_tree_free(e->root); - - // Free necessary stacks - - // Abbreviations need to be freed - while (e->abbreviation_stack->size) { - footnote_free(stack_pop(e->abbreviation_stack)); - } - - // Citations need to be freed - while (e->citation_stack->size) { - footnote_free(stack_pop(e->citation_stack)); - } - - // Footnotes need to be freed - while (e->footnote_stack->size) { - footnote_free(stack_pop(e->footnote_stack)); - } - - // Glossaries need to be freed - while (e->glossary_stack->size) { - footnote_free(stack_pop(e->glossary_stack)); - } - - // Links need to be freed - while (e->link_stack->size) { - link_free(stack_pop(e->link_stack)); - } - - // Metadata needs to be freed - while (e->metadata_stack->size) { - meta_free(stack_pop(e->metadata_stack)); - } - - // Free asset hash - asset * a, * a_tmp; - HASH_ITER(hh, e->asset_hash, a, a_tmp) { - HASH_DEL(e->asset_hash, a); // Remove item from hash - asset_free(a); // Free the asset - } - - - // Reset other stacks - e->definition_stack->size = 0; - e->header_stack->size = 0; - e->table_stack->size = 0; + mmd_engine_reset(e); // Tokenize the string token * doc = mmd_tokenize_string(e, byte_start, byte_len, false); -- 2.40.0