list: Allow custom allocation callbacks; add list_new & free

This commit is contained in:
Xavier Mendez 2015-07-12 19:11:42 +02:00
parent 36780e9471
commit 39c9ed7c5a
3 changed files with 42 additions and 14 deletions

View file

@ -6,7 +6,7 @@
#include <assert.h>
static void *new_object(void *opaque) {
hoedown_html_renderer_object *target = malloc(sizeof(hoedown_html_renderer_object));
hoedown_html_renderer_object *target = hoedown_malloc(sizeof(hoedown_html_renderer_object));
target->ob = hoedown_buffer_new(64);
return target;
}

View file

@ -10,21 +10,43 @@
void hoedown_list_init(
hoedown_list *list,
size_t unit,
size_t initial_size
size_t initial_size,
hoedown_realloc_callback data_realloc,
hoedown_free_callback data_free
) {
if (!initial_size) initial_size = 4;
list->data = hoedown_calloc(initial_size, unit);
list->data = data_realloc(NULL, initial_size * unit);
list->size = 0;
list->asize = initial_size;
list->unit = unit;
list->data_realloc = data_realloc;
list->data_free = data_free;
}
void hoedown_list_uninit(hoedown_list *list) {
assert(list && list->unit);
list->data_free(list->data);
}
hoedown_list *hoedown_list_new(size_t unit, size_t initial_size) {
hoedown_list *list = hoedown_malloc(sizeof(hoedown_list));
hoedown_list_init(list, unit, initial_size, hoedown_realloc, free);
return list;
}
void hoedown_list_free(hoedown_list *list) {
if (!list) return;
assert(list && list->unit);
hoedown_list_uninit(list);
free(list);
}
void hoedown_list_grow(hoedown_list *list, size_t asize) {
assert(list && list->unit);
while (list->asize < asize) {
list->asize *= 2;
list->data = hoedown_realloc(list->data, list->asize*list->unit);
list->data = list->data_realloc(list->data, list->asize * list->unit);
}
}
@ -47,7 +69,7 @@ void *hoedown_list_puti(hoedown_list *list, const void *data) {
assert(list && list->unit);
if (unlikely(list->size >= list->asize)) {
list->asize *= 2;
list->data = hoedown_realloc(list->data, list->asize * list->unit);
list->data = list->data_realloc(list->data, list->asize * list->unit);
}
void *ldata = list->data + list->size*list->unit;
@ -63,8 +85,3 @@ void *hoedown_list_pop(hoedown_list *list, void *data) {
if (data) memcpy(data, ldata, list->unit);
return ldata;
}
void hoedown_list_uninit(hoedown_list *list) {
assert(list && list->unit);
free(list->data);
}

View file

@ -19,6 +19,9 @@ typedef struct hoedown_list {
size_t size;
size_t asize;
size_t unit;
hoedown_realloc_callback data_realloc;
hoedown_free_callback data_free;
} hoedown_list;
@ -30,9 +33,20 @@ typedef struct hoedown_list {
void hoedown_list_init(
hoedown_list *list,
size_t unit,
size_t initial_size
size_t initial_size,
hoedown_realloc_callback data_realloc,
hoedown_free_callback data_free
);
/* hoedown_list_uninit: uninitialize a list of objects */
void hoedown_list_uninit(hoedown_list *list);
/* hoedown_list_new: allocate a new list */
hoedown_list *hoedown_list_new(size_t unit, size_t initial_size) __attribute__ ((malloc));
/* hoedown_list_free: deallocate a list */
void hoedown_list_free(hoedown_list *list);
/* hoedown_list_get: get internal (temporary) pointer to an object */
void *hoedown_list_get(hoedown_list *list, size_t idx);
@ -48,9 +62,6 @@ void *hoedown_list_puti(hoedown_list *list, const void *data);
/* hoedown_list_pop: remove the last element of the list */
void *hoedown_list_pop(hoedown_list *list, void *data);
/* hoedown_list_uninit: uninitialize a list of objects */
void hoedown_list_uninit(hoedown_list *list);
/* HOEDOWN_LIGET: Optimized hoedown_list_get of an item */
#define HOEDOWN_LIGET(list, idx, type) \