Skip to content

Commit

Permalink
core/alloc.c, core/allocator.c: fix calloc(3) and free(3) usage
Browse files Browse the repository at this point in the history
calloc(3)'s 2 arguments are, in order: number of members, size of member.
-> Swap the arguments to the correct order.

free(3) is well-documented to do nothing when passed NULL.
-> Remove conditional check for non-NULL.

Also add malloc and alloc_size function attributes where necessary.
  • Loading branch information
aaronmdjones committed Jan 18, 2019
1 parent 8c26f03 commit f7ab897
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/libmowgli/core/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@ typedef struct
static void * MOWGLI_FATTR_MALLOC MOWGLI_FATTR_ALLOC_SIZE(1)
_mowgli_bootstrap_alloc(size_t size)
{
return calloc(size, 1);
return calloc(1, size);
}

static void
_mowgli_bootstrap_free(void *ptr)
{
if (ptr)
free(ptr);
(void) free(ptr);
}

static mowgli_allocation_policy_t _mowgli_allocator_bootstrap =
Expand Down
11 changes: 5 additions & 6 deletions src/libmowgli/core/allocator.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,21 @@

mowgli_allocation_policy_t *mowgli_allocator_malloc = NULL;

static void *
static void * MOWGLI_FATTR_MALLOC MOWGLI_FATTR_ALLOC_SIZE(1)
mowgli_allocator_func_malloc(size_t size)
{
return calloc(size, 1);
return calloc(1, size);
}

static void
mowgli_allocator_func_free(void *ptr)
{
if (ptr)
free(ptr);
(void) free(ptr);
}

void
mowgli_allocator_bootstrap(void)
{
mowgli_allocator_malloc = mowgli_allocation_policy_create("malloc", mowgli_allocator_func_malloc,
mowgli_allocator_func_free);
mowgli_allocator_malloc = \
mowgli_allocation_policy_create("malloc", &mowgli_allocator_func_malloc, &mowgli_allocator_func_free);
}

0 comments on commit f7ab897

Please sign in to comment.