summaryrefslogtreecommitdiff
path: root/src/buffer.c
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/buffer.c
parentab6c81b960e86b26c7fda366f51ff29d1683a555 (diff)
cmark: Implement support for custom allocators
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c19
1 files changed, 12 insertions, 7 deletions
diff --git a/src/buffer.c b/src/buffer.c
index 2ac3b04..b879f0c 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -21,7 +21,8 @@ unsigned char cmark_strbuf__initbuf[1];
#define MIN(x, y) ((x < y) ? x : y)
#endif
-void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
+void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf, bufsize_t initial_size) {
+ buf->mem = mem;
buf->asize = 0;
buf->size = 0;
buf->ptr = cmark_strbuf__initbuf;
@@ -41,7 +42,7 @@ void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
return;
if (target_size > (bufsize_t)(SIZE_MAX / 4))
- cmark_trigger_oom();
+ abort();
/* Oversize the buffer by 50% to guarantee amortized linear time
* complexity on append operations. */
@@ -49,7 +50,11 @@ void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
new_size += 1;
new_size = (new_size + 7) & ~7;
- unsigned char *new_ptr = cmark_realloc(buf->asize ? buf->ptr : NULL, new_size);
+ unsigned char *new_ptr = buf->mem->calloc(new_size, 1);
+ if (buf->ptr != cmark_strbuf__initbuf) {
+ memcpy(new_ptr, buf->ptr, buf->size);
+ buf->mem->free(buf->ptr);
+ }
buf->asize = new_size;
buf->ptr = new_ptr;
@@ -62,9 +67,9 @@ void cmark_strbuf_free(cmark_strbuf *buf) {
return;
if (buf->ptr != cmark_strbuf__initbuf)
- free(buf->ptr);
+ buf->mem->free(buf->ptr);
- cmark_strbuf_init(buf, 0);
+ cmark_strbuf_init(buf->mem, buf, 0);
}
void cmark_strbuf_clear(cmark_strbuf *buf) {
@@ -147,10 +152,10 @@ unsigned char *cmark_strbuf_detach(cmark_strbuf *buf) {
if (buf->asize == 0) {
/* return an empty string */
- return cmark_calloc(1, 1);
+ return buf->mem->calloc(1, 1);
}
- cmark_strbuf_init(buf, 0);
+ cmark_strbuf_init(buf->mem, buf, 0);
return data;
}