diff --git a/src/allocator.c b/src/allocator.c index 626421dd..216fa341 100644 --- a/src/allocator.c +++ b/src/allocator.c @@ -20,6 +20,7 @@ */ #include +#include #include "config.h" #if HAVE_SYS_TYPES_H @@ -98,9 +99,11 @@ sixel_allocator_destroy( sixel_allocator_t /* in */ *allocator) /* allocator object to be destroyed */ { - sixel_free_t fn_free = allocator->fn_free; + /* precondition */ + assert(allocator); + assert(allocator->fn_free); - fn_free(allocator); + allocator->fn_free(allocator); } @@ -110,6 +113,9 @@ sixel_allocator_ref( sixel_allocator_t /* in */ *allocator) /* allocator object to be increment reference counter */ { + /* precondition */ + assert(allocator); + /* TODO: be thread safe */ ++allocator->ref; } @@ -121,8 +127,12 @@ sixel_allocator_unref( sixel_allocator_t /* in */ *allocator) /* allocator object to be unreference */ { /* TODO: be thread safe */ - if (allocator != NULL && --allocator->ref == 0) { - sixel_allocator_destroy(allocator); + if (allocator) { + assert(allocator->ref > 0); + --allocator->ref; + if (allocator->ref == 0) { + sixel_allocator_destroy(allocator); + } } } @@ -133,13 +143,11 @@ sixel_allocator_malloc( sixel_allocator_t /* in */ *allocator, /* allocator object */ size_t /* in */ n) /* allocation size */ { - void *p = NULL; - - if (allocator->fn_malloc) { - p = allocator->fn_malloc(n); - } + /* precondition */ + assert(allocator); + assert(allocator->fn_malloc); - return p; + return allocator->fn_malloc(n); } @@ -150,22 +158,11 @@ sixel_allocator_calloc( size_t /* in */ nelm, /* number of elements */ size_t /* in */ elsize) /* size of element */ { - void *p = NULL; - - if (allocator->fn_calloc) { - p = allocator->fn_calloc(nelm, elsize); - if (p) { - goto end; - } - } + /* precondition */ + assert(allocator); + assert(allocator->fn_calloc); - p = allocator->fn_malloc(nelm * elsize); - if (p) { - memset(p, 0x00, nelm * elsize); - } - -end: - return p; + return allocator->fn_calloc(nelm, elsize); } @@ -176,13 +173,11 @@ sixel_allocator_realloc( void /* in */ *p, /* existing buffer to be re-allocated */ size_t /* in */ n) /* re-allocation size */ { - void *result = NULL; + /* precondition */ + assert(allocator); + assert(allocator->fn_realloc); - if (allocator->fn_realloc) { - result = allocator->fn_realloc(p, n); - } - - return result; + return allocator->fn_realloc(p, n); } @@ -192,9 +187,11 @@ sixel_allocator_free( sixel_allocator_t /* in */ *allocator, /* allocator object */ void /* in */ *p) /* existing buffer to be freed */ { - if (allocator->fn_free) { - allocator->fn_free(p); - } + /* precondition */ + assert(allocator); + assert(allocator->fn_free); + + allocator->fn_free(p); } diff --git a/src/dither.c b/src/dither.c index dab9960c..20283db6 100644 --- a/src/dither.c +++ b/src/dither.c @@ -296,6 +296,7 @@ sixel_dither_new( *ppdither = (sixel_dither_t *)sixel_allocator_malloc(allocator, wholesize); if (*ppdither == NULL) { + sixel_allocator_unref(allocator); sixel_helper_set_additional_message( "sixel_dither_new: sixel_allocator_malloc() failed."); status = SIXEL_BAD_ALLOCATION; @@ -321,9 +322,6 @@ sixel_dither_new( (*ppdither)->allocator = allocator; end: - if (allocator) { - sixel_allocator_unref(allocator); - } return status; }