Skip to content

Commit

Permalink
libc/minimal: assorted fixes to malloc() and friends
Browse files Browse the repository at this point in the history
- When malloc() is called with a size of 0 we should not set errno
  to ENOMEM as there is no actual allocation failure in that case.
  This duplicates the realloc() behavior.

- Put unlock_ret assignments on separate lines, otherwise gcc complains
  about unused variables when the tests on it are disabled.

- There NULL return added in 952970d are completely pointless.
  First, there is no reason for sys_mutex_unlock() to fail, and even
  if it did, those returns would be blatent memory leaks. Remove them.
  No one should blindly modify code just to make static code
  analysers happy.

- Replace all CHECKIF() by explicit assertion statements to uniformize
  those checks and drop the NULL returns entirely. We can't return
  anything in the free() case, and there are no runtime conditions
  for sys_mutex_lock() to sometimes succeed and sometimes fail anyway.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
  • Loading branch information
Nicolas Pitre committed Mar 30, 2021
1 parent 58a0ba6 commit 816c6c4
Showing 1 changed file with 14 additions and 26 deletions.
40 changes: 14 additions & 26 deletions lib/libc/minimal/source/stdlib/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <sys/math_extras.h>
#include <string.h>
#include <app_memory/app_memdomain.h>
#include <sys/check.h>
#include <sys/mutex.h>
#include <sys/sys_heap.h>
#include <zephyr/types.h>
Expand All @@ -38,24 +37,19 @@ Z_GENERIC_SECTION(POOL_SECTION) static char z_malloc_heap_mem[HEAP_BYTES];

void *malloc(size_t size)
{
int lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);
int lock_ret;

CHECKIF(lock_ret != 0) {
return NULL;
}
lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);
__ASSERT_NO_MSG(lock_ret == 0);

void *ret = sys_heap_aligned_alloc(&z_malloc_heap,
__alignof__(z_max_align_t),
size);
if (ret == NULL) {
if (ret == NULL && size != 0) {
errno = ENOMEM;
}

int unlock_ret = sys_mutex_unlock(&z_malloc_heap_mutex);

CHECKIF(unlock_ret != 0) {
return NULL;
}
(void) sys_mutex_unlock(&z_malloc_heap_mutex);

return ret;
}
Expand All @@ -72,36 +66,30 @@ static int malloc_prepare(const struct device *unused)

void *realloc(void *ptr, size_t requested_size)
{
int lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);
int lock_ret;

CHECKIF(lock_ret != 0) {
return NULL;
}
lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);
__ASSERT_NO_MSG(lock_ret == 0);

void *ret = sys_heap_aligned_realloc(&z_malloc_heap, ptr,
__alignof__(z_max_align_t),
requested_size);
__alignof__(z_max_align_t),
requested_size);

if (ret == NULL && requested_size != 0) {
errno = ENOMEM;
}

int unlock_ret = sys_mutex_unlock(&z_malloc_heap_mutex);
(void) sys_mutex_unlock(&z_malloc_heap_mutex);

CHECKIF(unlock_ret != 0) {
return NULL;
}
return ret;
}

void free(void *ptr)
{
int lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);

CHECKIF(lock_ret != 0) {
return;
}
int lock_ret;

lock_ret = sys_mutex_lock(&z_malloc_heap_mutex, K_FOREVER);
__ASSERT_NO_MSG(lock_ret == 0);
sys_heap_free(&z_malloc_heap, ptr);
(void) sys_mutex_unlock(&z_malloc_heap_mutex);
}
Expand Down

0 comments on commit 816c6c4

Please sign in to comment.