|
| 1 | +/* Copyright (c) 2021 Intel Corporation |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + */ |
| 4 | + |
| 5 | +#ifndef ZEPHYR_INCLUDE_SYS_MULTI_HEAP_H_ |
| 6 | +#define ZEPHYR_INCLUDE_SYS_MULTI_HEAP_H_ |
| 7 | + |
| 8 | +#include <zephyr/types.h> |
| 9 | + |
| 10 | +#define MAX_MULTI_HEAPS 8 |
| 11 | + |
| 12 | +/** |
| 13 | + * @brief Multi-heap allocator |
| 14 | + * |
| 15 | + * A sys_multi_heap represents a single allocator made from multiple, |
| 16 | + * separately managed pools of memory that must be accessed via a |
| 17 | + * unified API. They can be discontiguous, and in many cases will be |
| 18 | + * expected to have different capabilities (for example: latency, |
| 19 | + * cacheability, cpu affinity, etc...) |
| 20 | + * |
| 21 | + * Allocation from the multiheap provides an opaque "configuration" |
| 22 | + * value to specify requirements and heuristics to assist the choice |
| 23 | + * in backend, which is then provided to a user-specified "choice" |
| 24 | + * function whose job it is to select a heap based on information in |
| 25 | + * the config specifier and runtime state (heap full state, etc...) |
| 26 | + */ |
| 27 | +struct sys_multi_heap; |
| 28 | + |
| 29 | +/** |
| 30 | + * @brief Multi-heap choice function |
| 31 | + * |
| 32 | + * This is a user-provided functions whose responsibility is selecting |
| 33 | + * a specific sys_heap backend based on the opaque cfg value, which is |
| 34 | + * specified by the user as an argument to sys_multi_heap_alloc(), and |
| 35 | + * performing the allocation on behalf of the caller. The callback is |
| 36 | + * free to choose any registered heap backend to perform the |
| 37 | + * allocation, and may choose to pad the user-provided values as |
| 38 | + * needed, and to use an aligned allocation where required by the |
| 39 | + * specified configuration. |
| 40 | + * |
| 41 | + * NULL may be returned, which will cause the |
| 42 | + * allocation to fail and a NULL reported to the calling code. |
| 43 | + * |
| 44 | + * @param cfg An opaque user-provided value. It may be interpreted in |
| 45 | + * any way by the application |
| 46 | + * @param align Alignment of requested memory (or zero for no alignment) |
| 47 | + * @param size The user-specified allocation size in bytes |
| 48 | + * @return A pointer to the allocated memory |
| 49 | + */ |
| 50 | +typedef void *(*sys_multi_heap_fn_t)(struct sys_multi_heap *mheap, void *cfg, |
| 51 | + size_t align, size_t size); |
| 52 | + |
| 53 | +struct sys_multi_heap { |
| 54 | + int nheaps; |
| 55 | + sys_multi_heap_fn_t choice; |
| 56 | + struct sys_heap *heaps[MAX_MULTI_HEAPS]; |
| 57 | +}; |
| 58 | + |
| 59 | +/** |
| 60 | + * @brief Initialize multi-heap |
| 61 | + * |
| 62 | + * Initialize a sys_multi_heap struct with the specified choice |
| 63 | + * function. Note that individual heaps must be added later with |
| 64 | + * sys_multi_heap_add_heap so that the heap bounds can be tracked by |
| 65 | + * the multi heap code. |
| 66 | + * |
| 67 | + * @note In general a multiheap is likely to be instantiated |
| 68 | + * semi-statically from system configuration (for example, via |
| 69 | + * linker-provided bounds on available memory in different regions, or |
| 70 | + * from devicetree definitions of hardware-provided addressible |
| 71 | + * memory, etc...). The general expectation is that a soc- or |
| 72 | + * board-level platform device will be initialized at system boot from |
| 73 | + * these upstream configuration sources and not that an application |
| 74 | + * will assemble a multi-heap on its own. |
| 75 | + * |
| 76 | + * @param heap A sys_multi_heap to initialize |
| 77 | + * @param choice_fn A sys_multi_heap_fn_t callback used to select |
| 78 | + * heaps at allocation time |
| 79 | + */ |
| 80 | +void sys_multi_heap_init(struct sys_multi_heap *heap, |
| 81 | + sys_multi_heap_fn_t choice_fn); |
| 82 | + |
| 83 | +/** |
| 84 | + * @brief Add sys_heap to multi heap |
| 85 | + * |
| 86 | + * This adds a known sys_heap backend to an existing multi heap, |
| 87 | + * allowing the multi heap internals to track the bounds of the heap |
| 88 | + * and determine which heap (if any) from which a freed block was |
| 89 | + * allocated. |
| 90 | + * |
| 91 | + * @param mheap A sys_multi_heap to which to add a heap |
| 92 | + * @param heap The heap to add |
| 93 | + */ |
| 94 | +void sys_multi_heap_add_heap(struct sys_multi_heap *mheap, struct sys_heap *heap); |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Allocate memory from multi heap |
| 98 | + * |
| 99 | + * Just as for sys_heap_alloc(), allocates a block of memory of the |
| 100 | + * specified size in bytes. Takes an opaque configuration pointer |
| 101 | + * passed to the multi heap choice function, which is used by |
| 102 | + * integration code to choose a heap backend. |
| 103 | + * |
| 104 | + * @param mheap Multi heap pointer |
| 105 | + * @param cfg Opaque configuration parameter, as for sys_multi_heap_fn_t |
| 106 | + * @param bytes Requested size of the allocation, in bytes |
| 107 | + * @return A valid pointer to heap memory, or NULL if no memory is available |
| 108 | + */ |
| 109 | +void *sys_multi_heap_alloc(struct sys_multi_heap *mheap, void *cfg, size_t bytes); |
| 110 | + |
| 111 | +/** |
| 112 | + * @brief Allocate aligned memory from multi heap |
| 113 | + * |
| 114 | + * Just as for sys_multi_heap_alloc(), allocates a block of memory of |
| 115 | + * the specified size in bytes. Takes an additional parameter |
| 116 | + * specifying a power of two alignment, in bytes. |
| 117 | + * |
| 118 | + * @param mheap Multi heap pointer |
| 119 | + * @param cfg Opaque configuration parameter, as for sys_multi_heap_fn_t |
| 120 | + * @param align Power of two alignment for the returned pointer, in bytes |
| 121 | + * @param bytes Requested size of the allocation, in bytes |
| 122 | + * @return A valid pointer to heap memory, or NULL if no memory is available |
| 123 | + */ |
| 124 | +void *sys_multi_heap_aligned_alloc(struct sys_multi_heap *mheap, |
| 125 | + void *cfg, size_t align, size_t bytes); |
| 126 | + |
| 127 | +/** |
| 128 | + * @brief Free memory allocated from multi heap |
| 129 | + * |
| 130 | + * Returns the specified block, which must be the return value of a |
| 131 | + * previously successful sys_multi_heap_alloc() or |
| 132 | + * sys_multi_heap_aligned_alloc() call, to the heap backend from which |
| 133 | + * it was allocated. |
| 134 | + * |
| 135 | + * Accepts NULL as a block parameter, which is specified to have no |
| 136 | + * effect. |
| 137 | + * |
| 138 | + * @param mheap Multi heap pointer |
| 139 | + * @param block Block to free |
| 140 | + */ |
| 141 | +void sys_multi_heap_free(struct sys_multi_heap *mheap, void *block); |
| 142 | + |
| 143 | +#endif /* ZEPHYR_INCLUDE_SYS_MULTI_HEAP_H_ */ |
0 commit comments