1 .. SPDX-License-Identifier: GPL-2.0
10 The Min Heap API provides a set of functions and macros for managing min-heaps
11 in the Linux kernel. A min-heap is a binary tree structure where the value of
12 each node is less than or equal to the values of its children, ensuring that
13 the smallest element is always at the root.
15 This document provides a guide to the Min Heap API, detailing how to define and
16 use min-heaps. Users should not directly call functions with **__min_heap_*()**
17 prefixes, but should instead use the provided macro wrappers.
19 In addition to the standard version of the functions, the API also includes a
20 set of inline versions for performance-critical scenarios. These inline
21 functions have the same names as their non-inline counterparts but include an
22 **_inline** suffix. For example, **__min_heap_init_inline** and its
23 corresponding macro wrapper **min_heap_init_inline**. The inline versions allow
24 custom comparison and swap functions to be called directly, rather than through
25 indirect function calls. This can significantly reduce overhead, especially
26 when CONFIG_MITIGATION_RETPOLINE is enabled, as indirect function calls become
27 more expensive. As with the non-inline versions, it is important to use the
28 macro wrappers for inline functions instead of directly calling the functions
37 The core data structure for representing a min-heap is defined using the
38 **MIN_HEAP_PREALLOCATED** and **DEFINE_MIN_HEAP** macros. These macros allow
39 you to define a min-heap with a preallocated buffer or dynamically allocated
46 #define MIN_HEAP_PREALLOCATED(_type, _name, _nr)
48 int nr; /* Number of elements in the heap */
49 int size; /* Maximum number of elements that can be held */
50 _type *data; /* Pointer to the heap data */
51 _type preallocated[_nr]; /* Static preallocated array */
54 #define DEFINE_MIN_HEAP(_type, _name) MIN_HEAP_PREALLOCATED(_type, _name, 0)
56 A typical heap structure will include a counter for the number of elements
57 (`nr`), the maximum capacity of the heap (`size`), and a pointer to an array of
58 elements (`data`). Optionally, you can specify a static array for preallocated
59 heap storage using **MIN_HEAP_PREALLOCATED**.
64 The **struct min_heap_callbacks** provides customization options for ordering
65 elements in the heap and swapping them. It contains two function pointers:
69 struct min_heap_callbacks {
70 bool (*less)(const void *lhs, const void *rhs, void *args);
71 void (*swp)(void *lhs, void *rhs, void *args);
74 - **less** is the comparison function used to establish the order of elements.
75 - **swp** is a function for swapping elements in the heap. If swp is set to
76 NULL, the default swap function will be used, which swaps the elements based on their size
81 The following macro wrappers are provided for interacting with the heap in a
82 user-friendly manner. Each macro corresponds to a function that operates on the
83 heap, and they abstract away direct calls to internal functions.
85 Each macro accepts various parameters that are detailed below.
92 min_heap_init(heap, data, size);
94 - **heap**: A pointer to the min-heap structure to be initialized.
95 - **data**: A pointer to the buffer where the heap elements will be stored. If
96 `NULL`, the preallocated buffer within the heap structure will be used.
97 - **size**: The maximum number of elements the heap can hold.
99 This macro initializes the heap, setting its initial state. If `data` is
100 `NULL`, the preallocated memory inside the heap structure will be used for
101 storage. Otherwise, the user-provided buffer is used. The operation is **O(1)**.
103 **Inline Version:** min_heap_init_inline(heap, data, size)
105 Accessing the Top Element
106 -------------------------
110 element = min_heap_peek(heap);
112 - **heap**: A pointer to the min-heap from which to retrieve the smallest
115 This macro returns a pointer to the smallest element (the root) of the heap, or
116 `NULL` if the heap is empty. The operation is **O(1)**.
118 **Inline Version:** min_heap_peek_inline(heap)
125 success = min_heap_push(heap, element, callbacks, args);
127 - **heap**: A pointer to the min-heap into which the element should be inserted.
128 - **element**: A pointer to the element to be inserted into the heap.
129 - **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
130 `less` and `swp` functions.
131 - **args**: Optional arguments passed to the `less` and `swp` functions.
133 This macro inserts an element into the heap. It returns `true` if the insertion
134 was successful and `false` if the heap is full. The operation is **O(log n)**.
136 **Inline Version:** min_heap_push_inline(heap, element, callbacks, args)
143 success = min_heap_pop(heap, callbacks, args);
145 - **heap**: A pointer to the min-heap from which to remove the smallest element.
146 - **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
147 `less` and `swp` functions.
148 - **args**: Optional arguments passed to the `less` and `swp` functions.
150 This macro removes the smallest element (the root) from the heap. It returns
151 `true` if the element was successfully removed, or `false` if the heap is
152 empty. The operation is **O(log n)**.
154 **Inline Version:** min_heap_pop_inline(heap, callbacks, args)
159 You can use the following macros to maintain the heap's structure:
163 min_heap_sift_down(heap, pos, callbacks, args);
165 - **heap**: A pointer to the min-heap.
166 - **pos**: The index from which to start sifting down.
167 - **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
168 `less` and `swp` functions.
169 - **args**: Optional arguments passed to the `less` and `swp` functions.
171 This macro restores the heap property by moving the element at the specified
172 index (`pos`) down the heap until it is in the correct position. The operation
175 **Inline Version:** min_heap_sift_down_inline(heap, pos, callbacks, args)
179 min_heap_sift_up(heap, idx, callbacks, args);
181 - **heap**: A pointer to the min-heap.
182 - **idx**: The index of the element to sift up.
183 - **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
184 `less` and `swp` functions.
185 - **args**: Optional arguments passed to the `less` and `swp` functions.
187 This macro restores the heap property by moving the element at the specified
188 index (`idx`) up the heap. The operation is **O(log n)**.
190 **Inline Version:** min_heap_sift_up_inline(heap, idx, callbacks, args)
194 min_heapify_all(heap, callbacks, args);
196 - **heap**: A pointer to the min-heap.
197 - **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
198 `less` and `swp` functions.
199 - **args**: Optional arguments passed to the `less` and `swp` functions.
201 This macro ensures that the entire heap satisfies the heap property. It is
202 called when the heap is built from scratch or after many modifications. The
203 operation is **O(n)**.
205 **Inline Version:** min_heapify_all_inline(heap, callbacks, args)
207 Removing Specific Elements
208 --------------------------
212 success = min_heap_del(heap, idx, callbacks, args);
214 - **heap**: A pointer to the min-heap.
215 - **idx**: The index of the element to delete.
216 - **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
217 `less` and `swp` functions.
218 - **args**: Optional arguments passed to the `less` and `swp` functions.
220 This macro removes an element at the specified index (`idx`) from the heap and
221 restores the heap property. The operation is **O(log n)**.
223 **Inline Version:** min_heap_del_inline(heap, idx, callbacks, args)
228 - **min_heap_full(heap)**: Checks whether the heap is full.
229 Complexity: **O(1)**.
233 bool full = min_heap_full(heap);
235 - `heap`: A pointer to the min-heap to check.
237 This macro returns `true` if the heap is full, otherwise `false`.
239 **Inline Version:** min_heap_full_inline(heap)
241 - **min_heap_empty(heap)**: Checks whether the heap is empty.
242 Complexity: **O(1)**.
246 bool empty = min_heap_empty(heap);
248 - `heap`: A pointer to the min-heap to check.
250 This macro returns `true` if the heap is empty, otherwise `false`.
252 **Inline Version:** min_heap_empty_inline(heap)
257 An example usage of the min-heap API would involve defining a heap structure,
258 initializing it, and inserting and removing elements as needed.
262 #include <linux/min_heap.h>
264 int my_less_function(const void *lhs, const void *rhs, void *args) {
265 return (*(int *)lhs < *(int *)rhs);
268 struct min_heap_callbacks heap_cb = {
269 .less = my_less_function, /* Comparison function for heap order */
270 .swp = NULL, /* Use default swap function */
273 void example_usage(void) {
274 /* Pre-populate the buffer with elements */
275 int buffer[5] = {5, 2, 8, 1, 3};
276 /* Declare a min-heap */
277 DEFINE_MIN_HEAP(int, my_heap);
279 /* Initialize the heap with preallocated buffer and size */
280 min_heap_init(&my_heap, buffer, 5);
282 /* Build the heap using min_heapify_all */
283 my_heap.nr = 5; /* Set the number of elements in the heap */
284 min_heapify_all(&my_heap, &heap_cb, NULL);
286 /* Peek at the top element (should be 1 in this case) */
287 int *top = min_heap_peek(&my_heap);
288 pr_info("Top element: %d\n", *top);
290 /* Pop the top element (1) and get the new top (2) */
291 min_heap_pop(&my_heap, &heap_cb, NULL);
292 top = min_heap_peek(&my_heap);
293 pr_info("New top element: %d\n", *top);
295 /* Insert a new element (0) and recheck the top */
297 min_heap_push(&my_heap, &new_element, &heap_cb, NULL);
298 top = min_heap_peek(&my_heap);
299 pr_info("Top element after insertion: %d\n", *top);