Skip to content

Commit 41adc8a

Browse files
sayoojkkarunnashif
authored andcommitted
include: zephyr: sys: Refactor MIN_HEAP_DEFINE macro
Remove calling of `min_heap_init()` from MIN_HEAP_DEFINE Signed-off-by: Sayooj K Karun <[email protected]>
1 parent ff65707 commit 41adc8a

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

include/zephyr/sys/min_heap.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,21 @@ struct min_heap {
6666
};
6767

6868
/**
69-
* @brief Define and initialize a heap instance at runtime.
69+
* @brief Define a min-heap instance.
7070
*
71-
* @param name Name of the heap variable.
72-
* @param storage Pointer to the preallocated storage.
71+
* @param name Base name for the heap instance.
7372
* @param cap Capacity (number of elements).
74-
* @param size Size of each element.
75-
* @param cmp_func Comparator function for the heap.
73+
* @param elem_sz Size in bytes of each element.
74+
* @param align Required alignment of each element.
75+
* @param cmp_func Comparator function used by the heap
7676
*/
77-
#define MIN_HEAP_DEFINE(name, storage, cap, size, cmp_func) \
78-
struct min_heap name; \
79-
min_heap_init(&name, storage, cap, size, cmp_func)
77+
#define MIN_HEAP_DEFINE(name, cap, elem_sz, align, cmp_func) \
78+
static uint8_t name##_storage[(cap) * (elem_sz)] __aligned(align); \
79+
struct min_heap name = {.storage = name##_storage, \
80+
.capacity = (cap), \
81+
.elem_size = (elem_sz), \
82+
.size = 0, \
83+
.cmp = (cmp_func)}
8084

8185
/**
8286
* @brief Define a statically allocated and aligned min-heap instance.

tests/lib/min_heap/src/test_min_heap.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,9 @@ ZTEST(min_heap_api, test_insert)
136136
ZTEST(min_heap_api, test_peek_and_pop)
137137
{
138138
int ret;
139-
uint8_t storage[HEAP_CAPACITY * sizeof(struct data)];
140139

141-
MIN_HEAP_DEFINE(runtime_heap, storage, HEAP_CAPACITY,
142-
sizeof(struct data), compare_ls);
140+
MIN_HEAP_DEFINE_STATIC(runtime_heap, HEAP_CAPACITY, sizeof(struct data),
141+
__alignof__(struct data), compare_ls);
143142

144143
for (int i = 0; i < ARRAY_SIZE(elements); i++) {
145144

0 commit comments

Comments
 (0)