summaryrefslogtreecommitdiff
path: root/src/chunk.h
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2016-05-27 16:55:16 +0200
committerJohn MacFarlane <jgm@berkeley.edu>2016-06-06 15:39:05 -0700
commit25429c96f6554ffac415f9d865934b1183f3398e (patch)
tree84d4eef404e99ff9e88d96a86d348a863d7c466f /src/chunk.h
parentab6c81b960e86b26c7fda366f51ff29d1683a555 (diff)
cmark: Implement support for custom allocators
Diffstat (limited to 'src/chunk.h')
-rw-r--r--src/chunk.h17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/chunk.h b/src/chunk.h
index acceaa1..4dfc698 100644
--- a/src/chunk.h
+++ b/src/chunk.h
@@ -4,9 +4,10 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
+#include "cmark.h"
+#include "buffer.h"
#include "memory.h"
#include "cmark_ctype.h"
-#include "buffer.h"
#define CMARK_CHUNK_EMPTY \
{ NULL, 0, 0 }
@@ -17,9 +18,9 @@ typedef struct {
bufsize_t alloc; // also implies a NULL-terminated string
} cmark_chunk;
-static CMARK_INLINE void cmark_chunk_free(cmark_chunk *c) {
+static CMARK_INLINE void cmark_chunk_free(cmark_mem *mem, cmark_chunk *c) {
if (c->alloc)
- free(c->data);
+ mem->free(c->data);
c->data = NULL;
c->alloc = 0;
@@ -56,13 +57,13 @@ static CMARK_INLINE bufsize_t
return p ? (bufsize_t)(p - ch->data) : ch->len;
}
-static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_chunk *c) {
+static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_mem *mem, cmark_chunk *c) {
unsigned char *str;
if (c->alloc) {
return (char *)c->data;
}
- str = (unsigned char *)cmark_calloc(c->len + 1, 1);
+ str = (unsigned char *)mem->calloc(c->len + 1, 1);
if (c->len > 0) {
memcpy(str, c->data, c->len);
}
@@ -73,9 +74,9 @@ static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_chunk *c) {
return (char *)str;
}
-static CMARK_INLINE void cmark_chunk_set_cstr(cmark_chunk *c, const char *str) {
+static CMARK_INLINE void cmark_chunk_set_cstr(cmark_mem *mem, cmark_chunk *c, const char *str) {
if (c->alloc) {
- free(c->data);
+ mem->free(c->data);
}
if (str == NULL) {
c->len = 0;
@@ -83,7 +84,7 @@ static CMARK_INLINE void cmark_chunk_set_cstr(cmark_chunk *c, const char *str) {
c->alloc = 0;
} else {
c->len = strlen(str);
- c->data = (unsigned char *)cmark_calloc(c->len + 1, 1);
+ c->data = (unsigned char *)mem->calloc(c->len + 1, 1);
c->alloc = 1;
memcpy(c->data, str, c->len + 1);
}