update allocator to properly support realloc

This commit is contained in:
William Woodall 2015-11-24 15:35:21 -08:00
parent 2f029b560e
commit 4b55f8e1cc
2 changed files with 13 additions and 2 deletions

View file

@ -31,7 +31,11 @@ typedef struct rcl_allocator_t {
/// Deallocate previously allocated memory, mimicking free(). /// Deallocate previously allocated memory, mimicking free().
void (*deallocate)(void * pointer, void * state); void (*deallocate)(void * pointer, void * state);
/// Reallocates if possible, otherwise it deallocates and allocates. /// 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); void * (*reallocate)(void * pointer, size_t size, void * state);
/// Implementation defined state storage. /// Implementation defined state storage.
/* This is passed as the second parameter to the (de)allocate functions. */ /* This is passed as the second parameter to the (de)allocate functions. */

View file

@ -30,8 +30,15 @@ __default_deallocate(void * pointer, void * state)
return free(pointer); 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_allocator_t
rcl_get_default_allocator() rcl_get_default_allocator()
{ {
return {__default_allocate, __default_deallocate, NULL}; return {__default_allocate, __default_deallocate, __default_reallocate, NULL};
} }