diff options
Diffstat (limited to 'src/libical/icalmemory.c')
-rw-r--r-- | src/libical/icalmemory.c | 63 |
1 files changed, 60 insertions, 3 deletions
diff --git a/src/libical/icalmemory.c b/src/libical/icalmemory.c index 3650cfe3..de52c94a 100644 --- a/src/libical/icalmemory.c +++ b/src/libical/icalmemory.c @@ -281,6 +281,44 @@ char *icalmemory_strdup(const char *s) return res; } +#if defined(ICALMEMORY_DEFAULT_MALLOC) +static icalmemory_malloc_f global_icalmem_malloc = &ICALMEMORY_DEFAULT_MALLOC; +#else +static icalmemory_malloc_f global_icalmem_malloc = NULL; +#endif + +#if defined(ICALMEMORY_DEFAULT_REALLOC) +static icalmemory_realloc_f global_icalmem_realloc = &ICALMEMORY_DEFAULT_REALLOC; +#else +static icalmemory_realloc_f global_icalmem_realloc = NULL; +#endif + +#if defined(ICALMEMORY_DEFAULT_FREE) +static icalmemory_free_f global_icalmem_free = &ICALMEMORY_DEFAULT_FREE; +#else +static icalmemory_free_f global_icalmem_free = NULL; +#endif + + +void icalmemory_set_mem_alloc_funcs(icalmemory_malloc_f f_malloc, icalmemory_realloc_f f_realloc, icalmemory_free_f f_free) +{ + global_icalmem_malloc = f_malloc; + global_icalmem_realloc = f_realloc; + global_icalmem_free = f_free; +} + +void icalmemory_get_mem_alloc_funcs(icalmemory_malloc_f* f_malloc, icalmemory_realloc_f* f_realloc, icalmemory_free_f* f_free) { + if (f_malloc) { + *f_malloc = global_icalmem_malloc; + } + if (f_realloc) { + *f_realloc = global_icalmem_realloc; + } + if (f_free) { + *f_free = global_icalmem_free; + } +} + /* * These buffer routines create memory the old fashioned way -- so the * caller will have to deallocate the new memory @@ -288,7 +326,14 @@ char *icalmemory_strdup(const char *s) void *icalmemory_new_buffer(size_t size) { - void *b = malloc(size); + void *b; + + if (global_icalmem_malloc == NULL) { + icalerror_set_errno(ICAL_NEWFAILED_ERROR); + return 0; + } + + b = global_icalmem_malloc(size); if (b == 0) { icalerror_set_errno(ICAL_NEWFAILED_ERROR); @@ -302,7 +347,14 @@ void *icalmemory_new_buffer(size_t size) void *icalmemory_resize_buffer(void *buf, size_t size) { - void *b = realloc(buf, size); + void *b; + + if (global_icalmem_realloc == NULL) { + icalerror_set_errno(ICAL_NEWFAILED_ERROR); + return 0; + } + + b = global_icalmem_realloc(buf, size); if (b == 0) { icalerror_set_errno(ICAL_NEWFAILED_ERROR); @@ -314,7 +366,12 @@ void *icalmemory_resize_buffer(void *buf, size_t size) void icalmemory_free_buffer(void *buf) { - free(buf); + if (global_icalmem_free == NULL) { + icalerror_set_errno(ICAL_NEWFAILED_ERROR); + return; + } + + global_icalmem_free(buf); } void icalmemory_append_string(char **buf, char **pos, size_t *buf_size, const char *string) |