From 4b55f8e1ccafa45c19960e414c3c4c4890836a2f Mon Sep 17 00:00:00 2001 From: William Woodall Date: Tue, 24 Nov 2015 15:35:21 -0800 Subject: [PATCH] update allocator to properly support realloc --- rcl/include/rcl/allocator.h | 6 +++++- rcl/src/rcl/allocator.c | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/rcl/include/rcl/allocator.h b/rcl/include/rcl/allocator.h index 44b509d..1ae76b3 100644 --- a/rcl/include/rcl/allocator.h +++ b/rcl/include/rcl/allocator.h @@ -31,7 +31,11 @@ typedef struct rcl_allocator_t { /// Deallocate previously allocated memory, mimicking free(). void (*deallocate)(void * pointer, void * state); /// Reallocates if possible, otherwise it deallocates and allocates. - /* If unsupported then do deallocate and then allocate. */ + /* If unsupported then do deallocate and then allocate. + * This should behave as realloc is described, as opposed to reallocf, i.e. + * the memory given by pointer will not be free'd automatically if realloc + * fails. + */ void * (*reallocate)(void * pointer, size_t size, void * state); /// Implementation defined state storage. /* This is passed as the second parameter to the (de)allocate functions. */ diff --git a/rcl/src/rcl/allocator.c b/rcl/src/rcl/allocator.c index b4caf3e..93eee17 100644 --- a/rcl/src/rcl/allocator.c +++ b/rcl/src/rcl/allocator.c @@ -30,8 +30,15 @@ __default_deallocate(void * pointer, void * state) return free(pointer); } +static void * +__default_reallocate(void * pointer, size_t size, void * state) +{ + (void)state; // unused + return realloc(pointer, size); +} + rcl_allocator_t rcl_get_default_allocator() { - return {__default_allocate, __default_deallocate, NULL}; + return {__default_allocate, __default_deallocate, __default_reallocate, NULL}; }