|
| 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 and dynamic partitions. |
| 21 | + * Total size is the sum of static and dynamic sizes. |
| 22 | + * |
| 23 | + * @param[in] lifetime_size Size of the virtual region lifetime partition. |
| 24 | + * @param[in] interim_size Size of the virtual region interim partition. |
| 25 | + * @return struct vregion* Pointer to the new virtual region instance, or NULL on failure. |
| 26 | + */ |
| 27 | +struct vregion *vregion_create(size_t lifetime_size, size_t interim_size); |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief Destroy a virtual region instance. |
| 31 | + * |
| 32 | + * Free all associated resources and deallocate the virtual region instance. |
| 33 | + * |
| 34 | + * @param[in] vr Pointer to the virtual region instance to destroy. |
| 35 | + */ |
| 36 | +void vregion_destroy(struct vregion *vr); |
| 37 | + |
| 38 | +/** |
| 39 | + * @brief Memory types for virtual region allocations. |
| 40 | + * Used to specify the type of memory allocation within a virtual region. |
| 41 | + * |
| 42 | + * @note |
| 43 | + * - interim: allocation that can be freed i.e. get/set large config, kcontrols. |
| 44 | + * - lifetime: allocation that cannot be freed i.e. init data, pipeline data. |
| 45 | + */ |
| 46 | +enum vregion_mem_type { |
| 47 | + VREGION_MEM_TYPE_INTERIM, /* interim allocation that can be freed */ |
| 48 | + VREGION_MEM_TYPE_LIFETIME, /* lifetime allocation */ |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * @brief Allocate memory from the specified virtual region. |
| 53 | + * |
| 54 | + * @param[in] vr Pointer to the virtual region instance. |
| 55 | + * @param[in] type Type of memory to allocate (static, dynamic, or shared static). |
| 56 | + * @param[in] size Size of memory to allocate in bytes. |
| 57 | + * @return void* Pointer to the allocated memory, or NULL on failure. |
| 58 | + */ |
| 59 | +void *vregion_alloc(struct vregion *vr, enum vregion_mem_type type, size_t size); |
| 60 | + |
| 61 | +/** |
| 62 | + * @brief Allocate aligned memory from the specified virtual region. |
| 63 | + * |
| 64 | + * Allocate aligned memory from the specified virtual region based on the memory type. |
| 65 | + * |
| 66 | + * @param[in] vr Pointer to the virtual region instance. |
| 67 | + * @param[in] type Type of memory to allocate (static, dynamic, or shared static). |
| 68 | + * @param[in] size Size of memory to allocate in bytes. |
| 69 | + * @param[in] alignment Alignment of memory to allocate in bytes. |
| 70 | + * @return void* Pointer to the allocated memory, or NULL on failure. |
| 71 | + */ |
| 72 | +void *vregion_alloc_align(struct vregion *vr, enum vregion_mem_type type, |
| 73 | + size_t size, size_t alignment); |
| 74 | + |
| 75 | +/** |
| 76 | + * @brief Free memory allocated from the specified virtual region. |
| 77 | + * |
| 78 | + * Free memory previously allocated from the specified virtual region. |
| 79 | + * |
| 80 | + * @param[in] vr Pointer to the virtual region instance. |
| 81 | + * @param[in] ptr Pointer to the memory to free. |
| 82 | + */ |
| 83 | +void vregion_free(struct vregion *vr, void *ptr); |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief Log virtual region memory usage. |
| 87 | + * |
| 88 | + * @param[in] vr Pointer to the virtual region instance. |
| 89 | + */ |
| 90 | +void vregion_info(struct vregion *vr); |
| 91 | + |
| 92 | +#ifdef __cplusplus |
| 93 | +} |
| 94 | +#endif |
| 95 | + |
| 96 | +#endif /* __SOF_LIB_VREGION_H__ */ |
0 commit comments