libc/minimal: assorted fixes to malloc() and friends
- 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 952970d6cb
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>
This commit is contained in:
parent
d11163741f
commit
7b6a4da206
1 changed files with 14 additions and 26 deletions
|
@ -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>
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue