2828 * will be contiguous or single block.
2929 */
3030struct vmh_heap {
31+ struct k_mutex lock ;
3132 const struct sys_mm_drv_region * virtual_region ;
3233 struct sys_mem_blocks * physical_blocks_allocators [MAX_MEMORY_ALLOCATORS_COUNT ];
3334 struct sys_bitarray * allocation_sizes [MAX_MEMORY_ALLOCATORS_COUNT ];
@@ -58,6 +59,7 @@ struct vmh_heap *vmh_init_heap(const struct vmh_heap_config *cfg, bool allocatin
5859 if (!new_heap )
5960 return NULL ;
6061
62+ k_mutex_init (& new_heap -> lock );
6163 struct vmh_heap_config new_config = {0 };
6264
6365 /* Search for matching attribute so we place heap on shared virtual region */
@@ -378,7 +380,7 @@ static int vmh_unmap_region(struct sys_mem_blocks *region, void *ptr, size_t siz
378380}
379381
380382/**
381- * @brief Alloc function
383+ * @brief Alloc function, not reentrant
382384 *
383385 * Allocates memory on heap from provided heap pointer.
384386 * Check if we need to map physical memory for given allocation
@@ -389,7 +391,7 @@ static int vmh_unmap_region(struct sys_mem_blocks *region, void *ptr, size_t siz
389391 * @retval ptr to allocated memory if successful
390392 * @retval NULL on allocation failure
391393 */
392- void * vmh_alloc (struct vmh_heap * heap , uint32_t alloc_size )
394+ static void * _vmh_alloc (struct vmh_heap * heap , uint32_t alloc_size )
393395{
394396 if (!alloc_size )
395397 return NULL ;
@@ -507,6 +509,20 @@ void *vmh_alloc(struct vmh_heap *heap, uint32_t alloc_size)
507509 return ptr ;
508510}
509511
512+ /**
513+ * @brief Alloc function, reentrant
514+ * see _vmh_alloc comment for details
515+ */
516+ void * vmh_alloc (struct vmh_heap * heap , uint32_t alloc_size )
517+ {
518+ k_mutex_lock (& heap -> lock , K_FOREVER );
519+
520+ void * ret = _vmh_alloc (heap , alloc_size );
521+
522+ k_mutex_unlock (& heap -> lock );
523+ return ret ;
524+ }
525+
510526/**
511527 * @brief Free virtual memory heap
512528 *
@@ -542,7 +558,7 @@ int vmh_free_heap(struct vmh_heap *heap)
542558}
543559
544560/**
545- * @brief Free ptr allocated on given heap
561+ * @brief Free ptr allocated on given heap, not reentrant
546562 *
547563 * Free the ptr allocation. After free action is complete
548564 * check if any physical memory block can be freed up as
@@ -553,7 +569,7 @@ int vmh_free_heap(struct vmh_heap *heap)
553569 * @retval 0 on success;
554570 * @retval -ENOTEMPTY on heap having active allocations.
555571 */
556- int vmh_free (struct vmh_heap * heap , void * ptr )
572+ static int _vmh_free (struct vmh_heap * heap , void * ptr )
557573{
558574 int retval ;
559575
@@ -670,6 +686,20 @@ int vmh_free(struct vmh_heap *heap, void *ptr)
670686 size_to_free );
671687}
672688
689+ /**
690+ * @brief Free ptr function, reentrant
691+ * see _vmh_free comment for details
692+ */
693+ int vmh_free (struct vmh_heap * heap , void * ptr )
694+ {
695+ k_mutex_lock (& heap -> lock , K_FOREVER );
696+
697+ int ret = _vmh_free (heap , ptr );
698+
699+ k_mutex_unlock (& heap -> lock );
700+ return ret ;
701+ }
702+
673703/**
674704 * @brief Get default configuration for heap
675705 *
0 commit comments