Skip to content

Commit

Permalink
Amend fix for miss-operation of reference counter
Browse files Browse the repository at this point in the history
  • Loading branch information
saitoha committed Aug 2, 2015
1 parent d933718 commit d527b11
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
65 changes: 31 additions & 34 deletions src/allocator.c
Expand Up @@ -20,6 +20,7 @@
*/

#include <stdlib.h>
#include <assert.h>
#include "config.h"

#if HAVE_SYS_TYPES_H
Expand Down Expand Up @@ -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);
}


Expand All @@ -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;
}
Expand All @@ -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);
}
}
}

Expand All @@ -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);
}


Expand All @@ -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);
}


Expand All @@ -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);
}


Expand All @@ -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);
}


Expand Down
4 changes: 1 addition & 3 deletions src/dither.c
Expand Up @@ -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;
Expand All @@ -321,9 +322,6 @@ sixel_dither_new(
(*ppdither)->allocator = allocator;

end:
if (allocator) {
sixel_allocator_unref(allocator);
}
return status;
}

Expand Down

0 comments on commit d527b11

Please sign in to comment.