Skip to content

Commit

Permalink
Use malloc instead of calloc to copy C strings
Browse files Browse the repository at this point in the history
Improves benchmark by about 2%.
  • Loading branch information
nwellnhof committed Jan 19, 2020
1 parent 83dd627 commit 42ff47c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/cmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ int cmark_version() { return CMARK_VERSION; }

const char *cmark_version_string() { return CMARK_VERSION_STRING; }

static void *xmalloc(size_t size) {
void *ptr = malloc(size);
if (!ptr) {
fprintf(stderr, "[cmark] malloc returned null pointer, aborting\n");
abort();
}
return ptr;
}

static void *xcalloc(size_t nmem, size_t size) {
void *ptr = calloc(nmem, size);
if (!ptr) {
Expand All @@ -28,7 +37,7 @@ static void *xrealloc(void *ptr, size_t size) {
return new_ptr;
}

cmark_mem DEFAULT_MEM_ALLOCATOR = {xcalloc, xrealloc, free};
cmark_mem DEFAULT_MEM_ALLOCATOR = {xmalloc, xcalloc, xrealloc, free};

char *cmark_markdown_to_html(const char *text, size_t len, int options) {
cmark_node *doc;
Expand Down
1 change: 1 addition & 0 deletions src/cmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef struct cmark_iter cmark_iter;
* when parsing and allocating a document tree
*/
typedef struct cmark_mem {
void *(*malloc)(size_t);
void *(*calloc)(size_t, size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
Expand Down
8 changes: 5 additions & 3 deletions src/inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ static CMARK_INLINE cmark_node *make_simple(cmark_mem *mem, cmark_node_type t) {

static cmark_node *make_str(subject *subj, int sc, int ec, cmark_chunk s) {
cmark_node *e = make_literal(subj, CMARK_NODE_TEXT, sc, ec);
e->data = (unsigned char *)subj->mem->calloc(s.len + 1, 1);
e->data = (unsigned char *)subj->mem->malloc(s.len + 1);
memcpy(e->data, s.data, s.len);
e->data[s.len] = 0;
e->len = s.len;
return e;
}
Expand Down Expand Up @@ -133,7 +134,7 @@ static unsigned char *cmark_strdup(cmark_mem *mem, unsigned char *src) {
return NULL;
}
size_t len = strlen((char *)src);
unsigned char *data = (unsigned char *)mem->calloc(len + 1, 1);
unsigned char *data = (unsigned char *)mem->malloc(len + 1);
memcpy(data, src, len + 1);
return data;
}
Expand Down Expand Up @@ -870,8 +871,9 @@ static cmark_node *handle_pointy_brace(subject *subj, int options) {
subj->pos += matchlen;
cmark_node *node = make_literal(subj, CMARK_NODE_HTML_INLINE,
subj->pos - matchlen - 1, subj->pos - 1);
node->data = (unsigned char *)subj->mem->calloc(len + 1, 1);
node->data = (unsigned char *)subj->mem->malloc(len + 1);
memcpy(node->data, src, len);
node->data[len] = 0;
node->len = len;
adjust_subj_node_newlines(subj, node, matchlen, 1, options);
return node;
Expand Down
4 changes: 2 additions & 2 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ static bufsize_t cmark_set_cstr(cmark_mem *mem, unsigned char **dst,

if (src && src[0]) {
len = (bufsize_t)strlen(src);
*dst = (unsigned char *)mem->calloc(len + 1, 1);
memcpy(*dst, src, len);
*dst = (unsigned char *)mem->malloc(len + 1);
memcpy(*dst, src, len + 1);
} else {
len = 0;
*dst = NULL;
Expand Down

0 comments on commit 42ff47c

Please sign in to comment.