public
Description: An implementation of markdown in C, using a PEG grammar
Clone URL: git://github.com/jgm/peg-markdown.git
Search Repo:
Improved freeing of markdown elements:
- Create SPACE and LINEBREAK elements using mk_element (so
  we don't need to make an exception for them in markdown_free).
- Refactored markdown_free and free_element_tree into markdown_free,
  free_element_contents, and free_element_list.  Handle some cases
  not handled by the original functions (links, string contents
  of SPACE, HTML, CODE, and VERBATIM elements, etc.).
- Result is a very slight reduction in memory leak.
jgm (author)
Sun Jun 08 18:47:24 -0700 2008
commit  e830badec6c39938944f87d8c1548d8d2b48d328
tree    5ea9192fdf90243fbda4d497859ea01e7384a918
parent  58fcb5982cb62085d2076a36ed7e20635111d8fb
...
572
573
574
575
 
576
577
578
...
587
588
589
590
 
591
592
593
 
594
595
596
 
597
598
599
...
869
870
871
872
873
 
 
 
 
874
875
876
877
878
 
 
 
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
899
900
901
 
 
 
 
 
 
902
903
904
...
572
573
574
 
575
576
577
578
...
587
588
589
 
590
591
592
 
593
594
595
 
596
597
598
599
...
869
870
871
 
 
872
873
874
875
876
877
878
 
 
879
880
881
882
883
 
 
 
 
884
885
886
887
888
 
 
 
 
 
 
 
 
 
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
 
 
906
907
908
909
910
911
912
913
914
0
@@ -572,7 +572,7 @@ Inline = Str
0
         | Symbol
0
 
0
 Space = Spacechar+
0
- { $$.key = SPACE; $$.contents.str = strdup(" "); }
0
+ { $$ = mk_element(SPACE); $$.contents.str = strdup(" "); }
0
 
0
 Str = < NormalChar+ >
0
         { $$ = mk_str(yytext); }
0
@@ -587,13 +587,13 @@ Endline = TerminalEndline | NormalEndline
0
 
0
 NormalEndline = Sp Newline !BlankLine !BlockQuote !AtxStart
0
                   !(Line ("===" '='* | "---" '-'*) Newline)
0
- { $$.key = SPACE; $$.contents.str = strdup("\n"); }
0
+ { $$ = mk_element(SPACE); $$.contents.str = strdup("\n"); }
0
 
0
 TerminalEndline = Sp Newline Eof
0
- { $$.key = SPACE; $$.contents.str = strdup(""); }
0
+ { $$ = mk_element(SPACE); $$.contents.str = strdup(""); }
0
 
0
 LineBreak = " " Endline
0
- { $$.key = LINEBREAK; }
0
+ { $$ = mk_element(LINEBREAK); }
0
 
0
 Symbol = < SpecialChar >
0
             { $$ = mk_str(yytext); }
0
@@ -869,36 +869,46 @@ RawNoteBlock = a:StartList
0
 
0
 %%
0
 
0
-/* free_element - free next and child elements recursively */
0
-static void free_element_tree(element * elt) {
0
+static void free_element_contents(element elt);
0
+
0
+/* free_element_list - free list of elements recursively */
0
+static void free_element_list(element * elt) {
0
     element * next = NULL;
0
     while (elt != NULL) {
0
         next = elt->next;
0
- if (elt->children != NULL && elt->key != SPACE) {
0
- free_element_tree(elt->children);
0
+ free_element_contents(*elt);
0
+ if (elt->children != NULL) {
0
+ free_element_list(elt->children);
0
             elt->children = NULL;
0
         }
0
- if (elt->key == STR || elt->key == RAW) {
0
- free(elt->contents.str);
0
- elt->contents.str = NULL;
0
- }
0
         free(elt);
0
         elt = next;
0
     }
0
 }
0
 
0
-/* free_element - free element recursively */
0
-void markdown_free(element result) {
0
- if (result.children) {
0
- free_element_tree(result.children);
0
- result.children = NULL;
0
- }
0
- if (result.next) {
0
- free_element_tree(result.next);
0
- result.next = NULL;
0
+/* free_element_contents - free element contents depending on type */
0
+static void free_element_contents(element elt) {
0
+ switch (elt.key) {
0
+ case STR: case SPACE: case RAW: case HTMLBLOCK: case HTML: case VERBATIM:
0
+ free(elt.contents.str);
0
+ elt.contents.str = NULL;
0
+ break;
0
+ case LINK: case IMAGE:
0
+ free(elt.contents.link.url);
0
+ elt.contents.link.url = NULL;
0
+ free(elt.contents.link.title);
0
+ elt.contents.link.title = NULL;
0
+ free_element_list(elt.contents.link.label);
0
+ break;
0
+ default:
0
+ ;
0
     }
0
- if (result.key == STR || result.key == RAW)
0
- free(result.contents.str);
0
+}
0
+
0
+/* markdown_free - free element contents and children recursively */
0
+void markdown_free(element elt) {
0
+ free_element_list(elt.children);
0
+ free_element_contents(elt);
0
 }
0
 
0
 element markdown(char *string, int extensions) {

Comments

    No one has commented yet.