cmark

My personal build of CMark ✏️

Commit
f1a1393881d1e96074e448c3935f55fb9aeb7359
Parent
476f083fc0d4ac31da918f7e1110cabc10acf02f
Author
John MacFarlane <jgm@berkeley.edu>
Date

render: Simplified code, avoiding some allocations.

Diffstat

1 file changed, 16 insertions, 35 deletions

Status File Name N° Changes Insertions Deletions
Modified src/render.c 51 16 35
diff --git a/src/render.c b/src/render.c
@@ -5,36 +5,6 @@
 #include "utf8.h"
 #include "render.h"
 
-static inline
-cmark_render_state
-cmark_initialize_render_state(int options, int width,
-			      void (*outc)(cmark_render_state*,
-				           cmark_escaping,
-					   int32_t,
-					   unsigned char))
-{
-	cmark_strbuf *pref = (cmark_strbuf*)malloc(sizeof(cmark_strbuf));
-	cmark_strbuf *buf  = (cmark_strbuf*)malloc(sizeof(cmark_strbuf));
-	cmark_strbuf_init(pref, 16);
-	cmark_strbuf_init(buf, 1024);
-	cmark_render_state state = { options, buf, pref, 0, width,
-				     0, 0, 0, true, false, false, outc };
-	return state;
-}
-
-static inline
-char *
-cmark_finalize_render_state(cmark_render_state *state)
-{
-	char * result;
-	result = (char *)cmark_strbuf_detach(state->buffer);
-	cmark_strbuf_free(state->prefix);
-	cmark_strbuf_free(state->buffer);
-	free(state->prefix);
-	free(state->buffer);
-	return result;
-}
-
 void cr(cmark_render_state *state)
 {
 	if (state->need_cr < 1) {
@@ -162,14 +132,21 @@ cmark_render(cmark_node *root,
 				 cmark_event_type ev_type,
 				 cmark_render_state *state))
 {
-	if (CMARK_OPT_HARDBREAKS & options) {
-		width = 0;
-	}
-	cmark_render_state state = cmark_initialize_render_state(options, width, outc);
+	cmark_strbuf pref = GH_BUF_INIT;
+	cmark_strbuf buf = GH_BUF_INIT;
 	cmark_node *cur;
 	cmark_event_type ev_type;
+	char *result;
 	cmark_iter *iter = cmark_iter_new(root);
 
+
+	if (CMARK_OPT_HARDBREAKS & options) {
+		width = 0;
+	}
+
+	cmark_render_state state = { options, &buf, &pref, 0, width,
+				     0, 0, 0, true, false, false, outc };
+
 	while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
 		cur = cmark_iter_get_node(iter);
 		if (!render_node(cur, ev_type, &state)) {
@@ -180,7 +157,11 @@ cmark_render(cmark_node *root,
 		}
 	}
 
+	result = (char *)cmark_strbuf_detach(state.buffer);
+
 	cmark_iter_free(iter);
+	cmark_strbuf_free(state.prefix);
+	cmark_strbuf_free(state.buffer);
 
-	return cmark_finalize_render_state(&state);
+	return result;
 }