diff options
Diffstat (limited to 'src/node.c')
-rw-r--r-- | src/node.c | 63 |
1 files changed, 36 insertions, 27 deletions
@@ -6,6 +6,8 @@ static void S_node_unlink(cmark_node *node); +#define NODE_MEM(node) cmark_node_mem(node) + static CMARK_INLINE bool S_is_block(cmark_node *node) { if (node == NULL) { return false; @@ -28,6 +30,9 @@ static bool S_can_contain(cmark_node *node, cmark_node *child) { if (node == NULL || child == NULL) { return false; } + if (NODE_MEM(node) != NODE_MEM(child)) { + return 0; + } // Verify that child is not an ancestor of node or equal to node. cur = node; @@ -70,8 +75,9 @@ static bool S_can_contain(cmark_node *node, cmark_node *child) { return false; } -cmark_node *cmark_node_new(cmark_node_type type) { - cmark_node *node = (cmark_node *)cmark_calloc(1, sizeof(*node)); +cmark_node *cmark_node_new2(cmark_node_type type, cmark_mem *mem) { + cmark_node *node = (cmark_node *)mem->calloc(1, sizeof(*node)); + cmark_strbuf_init(mem, &node->content, 0); node->type = type; switch (node->type) { @@ -94,33 +100,36 @@ cmark_node *cmark_node_new(cmark_node_type type) { return node; } +cmark_node *cmark_node_new(cmark_node_type type) { + extern cmark_mem DEFAULT_MEM_ALLOCATOR; + return cmark_node_new2(type, &DEFAULT_MEM_ALLOCATOR); +} + // Free a cmark_node list and any children. static void S_free_nodes(cmark_node *e) { cmark_node *next; while (e != NULL) { - if (S_is_block(e)) { - cmark_strbuf_free(&e->string_content); - } + cmark_strbuf_free(&e->content); switch (e->type) { case CMARK_NODE_CODE_BLOCK: - cmark_chunk_free(&e->as.code.info); - cmark_chunk_free(&e->as.code.literal); + cmark_chunk_free(NODE_MEM(e), &e->as.code.info); + cmark_chunk_free(NODE_MEM(e), &e->as.code.literal); break; case CMARK_NODE_TEXT: case CMARK_NODE_HTML_INLINE: case CMARK_NODE_CODE: case CMARK_NODE_HTML_BLOCK: - cmark_chunk_free(&e->as.literal); + cmark_chunk_free(NODE_MEM(e), &e->as.literal); break; case CMARK_NODE_LINK: case CMARK_NODE_IMAGE: - cmark_chunk_free(&e->as.link.url); - cmark_chunk_free(&e->as.link.title); + cmark_chunk_free(NODE_MEM(e), &e->as.link.url); + cmark_chunk_free(NODE_MEM(e), &e->as.link.title); break; case CMARK_NODE_CUSTOM_BLOCK: case CMARK_NODE_CUSTOM_INLINE: - cmark_chunk_free(&e->as.custom.on_enter); - cmark_chunk_free(&e->as.custom.on_exit); + cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_enter); + cmark_chunk_free(NODE_MEM(e), &e->as.custom.on_exit); break; default: break; @@ -131,7 +140,7 @@ static void S_free_nodes(cmark_node *e) { e->next = e->first_child; } next = e->next; - free(e); + NODE_MEM(e)->free(e); e = next; } } @@ -269,10 +278,10 @@ const char *cmark_node_get_literal(cmark_node *node) { case CMARK_NODE_TEXT: case CMARK_NODE_HTML_INLINE: case CMARK_NODE_CODE: - return cmark_chunk_to_cstr(&node->as.literal); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.literal); case CMARK_NODE_CODE_BLOCK: - return cmark_chunk_to_cstr(&node->as.code.literal); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.literal); default: break; @@ -291,11 +300,11 @@ int cmark_node_set_literal(cmark_node *node, const char *content) { case CMARK_NODE_TEXT: case CMARK_NODE_HTML_INLINE: case CMARK_NODE_CODE: - cmark_chunk_set_cstr(&node->as.literal, content); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.literal, content); return 1; case CMARK_NODE_CODE_BLOCK: - cmark_chunk_set_cstr(&node->as.code.literal, content); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.literal, content); return 1; default: @@ -452,7 +461,7 @@ const char *cmark_node_get_fence_info(cmark_node *node) { } if (node->type == CMARK_NODE_CODE_BLOCK) { - return cmark_chunk_to_cstr(&node->as.code.info); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.code.info); } else { return NULL; } @@ -464,7 +473,7 @@ int cmark_node_set_fence_info(cmark_node *node, const char *info) { } if (node->type == CMARK_NODE_CODE_BLOCK) { - cmark_chunk_set_cstr(&node->as.code.info, info); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.code.info, info); return 1; } else { return 0; @@ -479,7 +488,7 @@ const char *cmark_node_get_url(cmark_node *node) { switch (node->type) { case CMARK_NODE_LINK: case CMARK_NODE_IMAGE: - return cmark_chunk_to_cstr(&node->as.link.url); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.url); default: break; } @@ -495,7 +504,7 @@ int cmark_node_set_url(cmark_node *node, const char *url) { switch (node->type) { case CMARK_NODE_LINK: case CMARK_NODE_IMAGE: - cmark_chunk_set_cstr(&node->as.link.url, url); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.url, url); return 1; default: break; @@ -512,7 +521,7 @@ const char *cmark_node_get_title(cmark_node *node) { switch (node->type) { case CMARK_NODE_LINK: case CMARK_NODE_IMAGE: - return cmark_chunk_to_cstr(&node->as.link.title); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.link.title); default: break; } @@ -528,7 +537,7 @@ int cmark_node_set_title(cmark_node *node, const char *title) { switch (node->type) { case CMARK_NODE_LINK: case CMARK_NODE_IMAGE: - cmark_chunk_set_cstr(&node->as.link.title, title); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.link.title, title); return 1; default: break; @@ -545,7 +554,7 @@ const char *cmark_node_get_on_enter(cmark_node *node) { switch (node->type) { case CMARK_NODE_CUSTOM_INLINE: case CMARK_NODE_CUSTOM_BLOCK: - return cmark_chunk_to_cstr(&node->as.custom.on_enter); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_enter); default: break; } @@ -561,7 +570,7 @@ int cmark_node_set_on_enter(cmark_node *node, const char *on_enter) { switch (node->type) { case CMARK_NODE_CUSTOM_INLINE: case CMARK_NODE_CUSTOM_BLOCK: - cmark_chunk_set_cstr(&node->as.custom.on_enter, on_enter); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_enter, on_enter); return 1; default: break; @@ -578,7 +587,7 @@ const char *cmark_node_get_on_exit(cmark_node *node) { switch (node->type) { case CMARK_NODE_CUSTOM_INLINE: case CMARK_NODE_CUSTOM_BLOCK: - return cmark_chunk_to_cstr(&node->as.custom.on_exit); + return cmark_chunk_to_cstr(NODE_MEM(node), &node->as.custom.on_exit); default: break; } @@ -594,7 +603,7 @@ int cmark_node_set_on_exit(cmark_node *node, const char *on_exit) { switch (node->type) { case CMARK_NODE_CUSTOM_INLINE: case CMARK_NODE_CUSTOM_BLOCK: - cmark_chunk_set_cstr(&node->as.custom.on_exit, on_exit); + cmark_chunk_set_cstr(NODE_MEM(node), &node->as.custom.on_exit, on_exit); return 1; default: break; |