soc: nordic: dmm: Add lock around sys_heap operations
sys_heap alloc and free are not thread safe so lock is needed to prevent data corruption. Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
parent
37b4407a69
commit
80b9040146
1 changed files with 15 additions and 1 deletions
|
@ -44,6 +44,7 @@ struct dmm_region {
|
||||||
struct dmm_heap {
|
struct dmm_heap {
|
||||||
struct sys_heap heap;
|
struct sys_heap heap;
|
||||||
const struct dmm_region *region;
|
const struct dmm_region *region;
|
||||||
|
struct k_spinlock lock;
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct dmm_region dmm_regions[] = {
|
static const struct dmm_region dmm_regions[] = {
|
||||||
|
@ -54,6 +55,7 @@ struct {
|
||||||
struct dmm_heap dmm_heaps[ARRAY_SIZE(dmm_regions)];
|
struct dmm_heap dmm_heaps[ARRAY_SIZE(dmm_regions)];
|
||||||
} dmm_heaps_data;
|
} dmm_heaps_data;
|
||||||
|
|
||||||
|
|
||||||
static struct dmm_heap *dmm_heap_find(void *region)
|
static struct dmm_heap *dmm_heap_find(void *region)
|
||||||
{
|
{
|
||||||
struct dmm_heap *dh;
|
struct dmm_heap *dh;
|
||||||
|
@ -113,13 +115,25 @@ static size_t dmm_heap_size_get(struct dmm_heap *dh)
|
||||||
|
|
||||||
static void *dmm_buffer_alloc(struct dmm_heap *dh, size_t length)
|
static void *dmm_buffer_alloc(struct dmm_heap *dh, size_t length)
|
||||||
{
|
{
|
||||||
|
void *ret;
|
||||||
|
k_spinlock_key_t key;
|
||||||
|
|
||||||
length = ROUND_UP(length, dh->region->dt_align);
|
length = ROUND_UP(length, dh->region->dt_align);
|
||||||
return sys_heap_aligned_alloc(&dh->heap, dh->region->dt_align, length);
|
|
||||||
|
key = k_spin_lock(&dh->lock);
|
||||||
|
ret = sys_heap_aligned_alloc(&dh->heap, dh->region->dt_align, length);
|
||||||
|
k_spin_unlock(&dh->lock, key);
|
||||||
|
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dmm_buffer_free(struct dmm_heap *dh, void *buffer)
|
static void dmm_buffer_free(struct dmm_heap *dh, void *buffer)
|
||||||
{
|
{
|
||||||
|
k_spinlock_key_t key;
|
||||||
|
|
||||||
|
key = k_spin_lock(&dh->lock);
|
||||||
sys_heap_free(&dh->heap, buffer);
|
sys_heap_free(&dh->heap, buffer);
|
||||||
|
k_spin_unlock(&dh->lock, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
int dmm_buffer_out_prepare(void *region, void const *user_buffer, size_t user_length,
|
int dmm_buffer_out_prepare(void *region, void const *user_buffer, size_t user_length,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue