|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// Copyright(c) 2025 Intel Corporation. |
| 3 | + |
| 4 | +/* Pre Allocated Contiguous Virtual Region */ |
| 5 | +#ifndef __SOF_LIB_VREGION_H__ |
| 6 | +#define __SOF_LIB_VREGION_H__ |
| 7 | + |
| 8 | +#include <zephyr/kernel.h> |
| 9 | +#include <stddef.h> |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | +extern "C" { |
| 13 | +#endif |
| 14 | + |
| 15 | +struct vregion; |
| 16 | + |
| 17 | +/** |
| 18 | + * @brief Create a new virtual region instance. |
| 19 | + * |
| 20 | + * Create a new virtual region instance with specified static, dynamic, and shared static sizes |
| 21 | + * plus an optional read-only text partition and optional shared static partition. |
| 22 | + * Total size is the sum of static, dynamic, shared static, and text sizes. |
| 23 | + * |
| 24 | + * @param[in] lifetime_size Size of the virtual region lifetime partition. |
| 25 | + * @param[in] interim_size Size of the virtual region interim partition. |
| 26 | + * @param[in] lifetime_shared_size Size of the virtual region shared lifetime partition. |
| 27 | + * @param[in] interim_shared_size Size of the virtual region shared interim partition. |
| 28 | + * @param[in] text_size Size of the optional read-only text partition. |
| 29 | + * @return struct vregion* Pointer to the new virtual region instance, or NULL on failure. |
| 30 | + */ |
| 31 | +struct vregion *vregion_create(size_t lifetime_size, size_t interim_size, |
| 32 | + size_t lifetime_shared_size, size_t interim_shared_size, |
| 33 | + size_t text_size); |
| 34 | + |
| 35 | +/** |
| 36 | + * @brief Destroy a virtual region instance. |
| 37 | + * |
| 38 | + * Free all associated resources and deallocate the virtual region instance. |
| 39 | + * |
| 40 | + * @param[in] vr Pointer to the virtual region instance to destroy. |
| 41 | + */ |
| 42 | +void vregion_destroy(struct vregion *vr); |
| 43 | + |
| 44 | +/** |
| 45 | + * @brief Memory types for virtual region allocations. |
| 46 | + * Used to specify the type of memory allocation within a virtual region. |
| 47 | + * |
| 48 | + * @note |
| 49 | + * - interim: allocation that can be freed i.e. get/set large config, kcontrols. |
| 50 | + * - lifetime: allocation that cannot be freed i.e. init data, pipeline data. |
| 51 | + * - shared: allocation that can be shared between multiple virtual regions and |
| 52 | + * - with different cores. |
| 53 | + */ |
| 54 | +enum vregion_mem_type { |
| 55 | + VREGION_MEM_TYPE_INTERIM, /* interim allocation that can be freed */ |
| 56 | + VREGION_MEM_TYPE_LIFETIME, /* lifetime allocation */ |
| 57 | + VREGION_MEM_TYPE_INTERIM_SHARED, /* shared interim allocation */ |
| 58 | + VREGION_MEM_TYPE_LIFETIME_SHARED /* shared lifetime allocation */ |
| 59 | +}; |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Allocate memory from the specified virtual region. |
| 63 | + * |
| 64 | + * @param[in] vr Pointer to the virtual region instance. |
| 65 | + * @param[in] type Type of memory to allocate (static, dynamic, or shared static). |
| 66 | + * @param[in] size Size of memory to allocate in bytes. |
| 67 | + * @return void* Pointer to the allocated memory, or NULL on failure. |
| 68 | + */ |
| 69 | +void *vregion_alloc(struct vregion *vr, enum vregion_mem_type type, size_t size); |
| 70 | + |
| 71 | +/** |
| 72 | + * @brief Allocate aligned memory from the specified virtual region. |
| 73 | + * |
| 74 | + * Allocate aligned memory from the specified virtual region based on the memory type. |
| 75 | + * |
| 76 | + * @param[in] vr Pointer to the virtual region instance. |
| 77 | + * @param[in] type Type of memory to allocate (static, dynamic, or shared static). |
| 78 | + * @param[in] size Size of memory to allocate in bytes. |
| 79 | + * @param[in] alignment Alignment of memory to allocate in bytes. |
| 80 | + * @return void* Pointer to the allocated memory, or NULL on failure. |
| 81 | + */ |
| 82 | +void *vregion_alloc_align(struct vregion *vr, enum vregion_mem_type type, |
| 83 | + size_t size, size_t alignment); |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief Free memory allocated from the specified virtual region. |
| 87 | + * |
| 88 | + * Free memory previously allocated from the specified virtual region. |
| 89 | + * |
| 90 | + * @param[in] vr Pointer to the virtual region instance. |
| 91 | + * @param[in] ptr Pointer to the memory to free. |
| 92 | + */ |
| 93 | +void vregion_free(struct vregion *vr, void *ptr); |
| 94 | + |
| 95 | +/** |
| 96 | + * @brief Log virtual region memory usage. |
| 97 | + * |
| 98 | + * @param[in] vr Pointer to the virtual region instance. |
| 99 | + */ |
| 100 | +void vregion_info(struct vregion *vr); |
| 101 | + |
| 102 | +#ifdef __cplusplus |
| 103 | +} |
| 104 | +#endif |
| 105 | + |
| 106 | +#endif /* __SOF_LIB_VREGION_H__ */ |
0 commit comments