]> granicus.if.org Git - multimarkdown/commitdiff
CHANGED: Refactor mmd_engine cleanup
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Sat, 15 Apr 2017 01:29:18 +0000 (21:29 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Sat, 15 Apr 2017 01:29:18 +0000 (21:29 -0400)
Sources/libMultiMarkdown/mmd.c

index 9eb16537354ba4fdb8f0d294f3f8db5245e6cf92..4e4af0fe0d80025e54a5700d5074ee3301aade07 100644 (file)
@@ -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);