Merge pull request #76 from jmendeth/memory-allocation

Add custom memory functions
This commit is contained in:
Devin Torres 2014-05-13 09:33:44 -05:00
commit cfea09a7f0
2 changed files with 44 additions and 12 deletions

View file

@ -5,18 +5,32 @@
#include <string.h>
#include <assert.h>
/* hoedown_buffer_init: initialize a buffer with custom allocators */
void
hoedown_buffer_init(
hoedown_buffer *buf,
size_t unit,
hoedown_realloc_callback data_realloc,
hoedown_free_callback data_free,
hoedown_free_callback buffer_free)
{
if (!buf)
return;
buf->data = NULL;
buf->size = buf->asize = 0;
buf->unit = unit;
buf->data_realloc = data_realloc;
buf->data_free = data_free;
buf->buffer_free = buffer_free;
}
/* hoedown_buffer_new: allocation of a new buffer */
hoedown_buffer *
hoedown_buffer_new(size_t unit)
{
hoedown_buffer *ret;
ret = malloc(sizeof (hoedown_buffer));
if (ret) {
ret->data = 0;
ret->size = ret->asize = 0;
ret->unit = unit;
}
hoedown_buffer *ret = malloc(sizeof (hoedown_buffer));
hoedown_buffer_init(ret, unit, realloc, free, free);
return ret;
}
@ -27,8 +41,10 @@ hoedown_buffer_free(hoedown_buffer *buf)
if (!buf)
return;
free(buf->data);
free(buf);
buf->data_free(buf->data);
if (buf->buffer_free)
buf->buffer_free(buf);
}
/* hoedown_buffer_reset: frees internal data of the buffer */
@ -38,7 +54,7 @@ hoedown_buffer_reset(hoedown_buffer *buf)
if (!buf)
return;
free(buf->data);
buf->data_free(buf->data);
buf->data = NULL;
buf->size = buf->asize = 0;
}
@ -59,7 +75,7 @@ hoedown_buffer_grow(hoedown_buffer *buf, size_t neosz)
while (neoasz < neosz)
neoasz += buf->unit;
neodata = realloc(buf->data, neoasz);
neodata = buf->data_realloc(buf->data, neoasz);
if (!neodata)
return HOEDOWN_BUF_ENOMEM;

View file

@ -21,12 +21,19 @@ typedef enum {
HOEDOWN_BUF_ENOMEM = -1
} hoedown_buferror_t;
typedef void *(*hoedown_realloc_callback)(void *, size_t);
typedef void (*hoedown_free_callback)(void *);
/* hoedown_buffer: character array buffer */
struct hoedown_buffer {
uint8_t *data; /* actual character data */
size_t size; /* size of the string */
size_t asize; /* allocated size (0 = volatile buffer) */
size_t unit; /* reallocation unit size (0 = read-only buffer) */
hoedown_realloc_callback data_realloc;
hoedown_free_callback data_free;
hoedown_free_callback buffer_free;
};
typedef struct hoedown_buffer hoedown_buffer;
@ -35,6 +42,15 @@ typedef struct hoedown_buffer hoedown_buffer;
#define HOEDOWN_BUFPUTSL(output, literal) \
hoedown_buffer_put(output, literal, sizeof(literal) - 1)
/* hoedown_buffer_init: initialize a buffer with custom allocators */
void hoedown_buffer_init(
hoedown_buffer *buffer,
size_t unit,
hoedown_realloc_callback data_realloc,
hoedown_free_callback data_free,
hoedown_free_callback buffer_free
);
/* hoedown_buffer_new: allocation of a new buffer */
hoedown_buffer *hoedown_buffer_new(size_t unit) __attribute__ ((malloc));