libc: newlib: make sbrk() thread-safe
Concurrent use of this function could lead to corruption. Use a sys_sem to synchronize access. Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
This commit is contained in:
parent
db48d3e22a
commit
aed767a98c
1 changed files with 15 additions and 4 deletions
|
@ -16,6 +16,7 @@
|
||||||
#include <syscall_handler.h>
|
#include <syscall_handler.h>
|
||||||
#include <app_memory/app_memdomain.h>
|
#include <app_memory/app_memdomain.h>
|
||||||
#include <init.h>
|
#include <init.h>
|
||||||
|
#include <sys/sem.h>
|
||||||
|
|
||||||
#define LIBC_BSS K_APP_BMEM(z_libc_partition)
|
#define LIBC_BSS K_APP_BMEM(z_libc_partition)
|
||||||
#define LIBC_DATA K_APP_DMEM(z_libc_partition)
|
#define LIBC_DATA K_APP_DMEM(z_libc_partition)
|
||||||
|
@ -237,20 +238,30 @@ void _exit(int status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static LIBC_DATA SYS_SEM_DEFINE(heap_sem, 1, 1);
|
||||||
|
|
||||||
void *_sbrk(int count)
|
void *_sbrk(int count)
|
||||||
{
|
{
|
||||||
|
void *ret, *ptr;
|
||||||
|
|
||||||
|
sys_sem_take(&heap_sem, K_FOREVER);
|
||||||
|
|
||||||
#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
|
#if CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE
|
||||||
void *ptr = heap_base + heap_sz;
|
ptr = heap_base + heap_sz;
|
||||||
#else
|
#else
|
||||||
void *ptr = ((char *)HEAP_BASE) + heap_sz;
|
ptr = ((char *)HEAP_BASE) + heap_sz;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if ((heap_sz + count) < MAX_HEAP_SIZE) {
|
if ((heap_sz + count) < MAX_HEAP_SIZE) {
|
||||||
heap_sz += count;
|
heap_sz += count;
|
||||||
return ptr;
|
ret = ptr;
|
||||||
} else {
|
} else {
|
||||||
return (void *)-1;
|
ret = (void *)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sys_sem_give(&heap_sem);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
FUNC_ALIAS(_sbrk, sbrk, void *);
|
FUNC_ALIAS(_sbrk, sbrk, void *);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue