|
| 1 | +/* SPDX-License-Identifier: BSD-3-Clause |
| 2 | + * |
| 3 | + * Copyright(c) 2025 Intel Corporation. All rights reserved. |
| 4 | + * |
| 5 | + * Author: Jaroslaw Stelter <jaroslaw.stelter@intel.com> |
| 6 | + * Adrian Warecki <adrian.warecki@intel.com> |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * \brief Userspace support functions. |
| 11 | + */ |
| 12 | +#ifndef __RTOS_USERSPACE_HELPER_H__ |
| 13 | +#define __RTOS_USERSPACE_HELPER_H__ |
| 14 | + |
| 15 | +#include <stdint.h> |
| 16 | +#include <stddef.h> |
| 17 | + |
| 18 | +#include <rtos/alloc.h> |
| 19 | + |
| 20 | +struct sys_heap; |
| 21 | + |
| 22 | +#ifdef CONFIG_USERSPACE |
| 23 | +/** |
| 24 | + * Initialize private processing module heap. |
| 25 | + * @param N/A. |
| 26 | + * @return pointer to the sys_heap structure. |
| 27 | + * |
| 28 | + * @note |
| 29 | + * Function used only when CONFIG_USERSPACE is set. |
| 30 | + * The private heap is used only for non-privileged modules |
| 31 | + * for all processing module allocations that should be isolated. |
| 32 | + * The heap helps to accumulate all dynamic allocations in single |
| 33 | + * memory region which is then added to modules memory domain. |
| 34 | + */ |
| 35 | +static inline struct sys_heap *drv_heap_init(void) |
| 36 | +{ |
| 37 | + return NULL; |
| 38 | +} |
| 39 | + |
| 40 | +#endif |
| 41 | + |
| 42 | +/** |
| 43 | + * Allocates memory block from private module sys_heap if exists, otherwise call rballoc_align(). |
| 44 | + * @param sys_heap - pointer to the sys_heap structure |
| 45 | + * @param flags - Flags, see SOF_MEM_FLAG_... |
| 46 | + * @param bytes - Size in bytes. |
| 47 | + * @param alignment - Alignment in bytes. |
| 48 | + * @return Pointer to the allocated memory or NULL if failed. |
| 49 | + * |
| 50 | + * @note When CONFIG_USERSPACE not set function calls rballoc_align() |
| 51 | + */ |
| 52 | +static inline void *drv_heap_aligned_alloc(struct sys_heap *drv_heap, uint32_t flags, size_t bytes, |
| 53 | + uint32_t align) |
| 54 | +{ |
| 55 | + return rballoc_align(flags, bytes, align); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Allocates memory block from private module sys_heap if exists, otherwise call rmalloc. |
| 60 | + * @param sys_heap - pointer to the sys_heap structure |
| 61 | + * @param flags - Flags, see SOF_MEM_FLAG_... |
| 62 | + * @param bytes - Size in bytes. |
| 63 | + * @return - Pointer to the allocated memory or NULL if failed. |
| 64 | + * |
| 65 | + * * @note When CONFIG_USERSPACE not set function calls rmalloc() |
| 66 | + */ |
| 67 | +static inline void *drv_heap_rmalloc(struct sys_heap *drv_heap, uint32_t flags, size_t bytes) |
| 68 | +{ |
| 69 | + return rmalloc(flags, bytes); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Similar to user_rmalloc(), guarantees that returned block is zeroed. |
| 74 | + * |
| 75 | + * @note When CONFIG_USERSPACE not set function calls rzalloc() |
| 76 | + */ |
| 77 | +static inline void *drv_heap_rzalloc(struct sys_heap *drv_heap, uint32_t flags, size_t bytes) |
| 78 | +{ |
| 79 | + return rzalloc(flags, bytes); |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Frees the memory block from private module sys_heap if exists. |
| 84 | + * otherwise call rfree. |
| 85 | + * @param ptr Pointer to the memory block. |
| 86 | + * |
| 87 | + * @note User should take care to not free memory allocated from sys_heap |
| 88 | + * with drv_heap set to NULL. It will cause exception. |
| 89 | + * |
| 90 | + * When CONFIG_USERSPACE not set function calls rfree() |
| 91 | + */ |
| 92 | +static inline void drv_heap_free(struct sys_heap *drv_heap, void *mem) |
| 93 | +{ |
| 94 | + rfree(mem); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Free private processing module heap. |
| 99 | + * @param sys_heap pointer to the sys_heap structure. |
| 100 | + * |
| 101 | + * @note |
| 102 | + * Function used only when CONFIG_USERSPACE is set. |
| 103 | + * Frees private module heap. |
| 104 | + */ |
| 105 | +static inline void drv_heap_remove(struct sys_heap *drv_heap) |
| 106 | +{ } |
| 107 | + |
| 108 | +#endif /* __RTOS_USERSPACE_HELPER_H__ */ |
0 commit comments