xref: /linux/Documentation/core-api/min_heap.rst (revision 4f9786035f9e519db41375818e1d0b5f20da2f10)
1ec7c2bdaSKuan-Wei Chiu.. SPDX-License-Identifier: GPL-2.0
2ec7c2bdaSKuan-Wei Chiu
3ec7c2bdaSKuan-Wei Chiu============
4ec7c2bdaSKuan-Wei ChiuMin Heap API
5ec7c2bdaSKuan-Wei Chiu============
6ec7c2bdaSKuan-Wei Chiu
7b9da6f70SKuan-Wei Chiu:Author: Kuan-Wei Chiu <visitorckw@gmail.com>
8b9da6f70SKuan-Wei Chiu
9ec7c2bdaSKuan-Wei ChiuIntroduction
10ec7c2bdaSKuan-Wei Chiu============
11ec7c2bdaSKuan-Wei Chiu
12ec7c2bdaSKuan-Wei ChiuThe Min Heap API provides a set of functions and macros for managing min-heaps
13ec7c2bdaSKuan-Wei Chiuin the Linux kernel. A min-heap is a binary tree structure where the value of
14ec7c2bdaSKuan-Wei Chiueach node is less than or equal to the values of its children, ensuring that
15ec7c2bdaSKuan-Wei Chiuthe smallest element is always at the root.
16ec7c2bdaSKuan-Wei Chiu
17ec7c2bdaSKuan-Wei ChiuThis document provides a guide to the Min Heap API, detailing how to define and
18ec7c2bdaSKuan-Wei Chiuuse min-heaps. Users should not directly call functions with **__min_heap_*()**
19ec7c2bdaSKuan-Wei Chiuprefixes, but should instead use the provided macro wrappers.
20ec7c2bdaSKuan-Wei Chiu
21ec7c2bdaSKuan-Wei ChiuIn addition to the standard version of the functions, the API also includes a
22ec7c2bdaSKuan-Wei Chiuset of inline versions for performance-critical scenarios. These inline
23ec7c2bdaSKuan-Wei Chiufunctions have the same names as their non-inline counterparts but include an
24ec7c2bdaSKuan-Wei Chiu**_inline** suffix. For example, **__min_heap_init_inline** and its
25ec7c2bdaSKuan-Wei Chiucorresponding macro wrapper **min_heap_init_inline**. The inline versions allow
26ec7c2bdaSKuan-Wei Chiucustom comparison and swap functions to be called directly, rather than through
27ec7c2bdaSKuan-Wei Chiuindirect function calls. This can significantly reduce overhead, especially
28ec7c2bdaSKuan-Wei Chiuwhen CONFIG_MITIGATION_RETPOLINE is enabled, as indirect function calls become
29ec7c2bdaSKuan-Wei Chiumore expensive. As with the non-inline versions, it is important to use the
30ec7c2bdaSKuan-Wei Chiumacro wrappers for inline functions instead of directly calling the functions
31ec7c2bdaSKuan-Wei Chiuthemselves.
32ec7c2bdaSKuan-Wei Chiu
33ec7c2bdaSKuan-Wei ChiuData Structures
34ec7c2bdaSKuan-Wei Chiu===============
35ec7c2bdaSKuan-Wei Chiu
36ec7c2bdaSKuan-Wei ChiuMin-Heap Definition
37ec7c2bdaSKuan-Wei Chiu-------------------
38ec7c2bdaSKuan-Wei Chiu
39ec7c2bdaSKuan-Wei ChiuThe core data structure for representing a min-heap is defined using the
40ec7c2bdaSKuan-Wei Chiu**MIN_HEAP_PREALLOCATED** and **DEFINE_MIN_HEAP** macros. These macros allow
41ec7c2bdaSKuan-Wei Chiuyou to define a min-heap with a preallocated buffer or dynamically allocated
42ec7c2bdaSKuan-Wei Chiumemory.
43ec7c2bdaSKuan-Wei Chiu
44ec7c2bdaSKuan-Wei ChiuExample:
45ec7c2bdaSKuan-Wei Chiu
46ec7c2bdaSKuan-Wei Chiu.. code-block:: c
47ec7c2bdaSKuan-Wei Chiu
48ec7c2bdaSKuan-Wei Chiu    #define MIN_HEAP_PREALLOCATED(_type, _name, _nr)
49ec7c2bdaSKuan-Wei Chiu    struct _name {
50*364469e5SYu-Chun Lin        size_t nr;         /* Number of elements in the heap */
51*364469e5SYu-Chun Lin        size_t size;       /* Maximum number of elements that can be held */
52ec7c2bdaSKuan-Wei Chiu        _type *data;    /* Pointer to the heap data */
53ec7c2bdaSKuan-Wei Chiu        _type preallocated[_nr];  /* Static preallocated array */
54ec7c2bdaSKuan-Wei Chiu    }
55ec7c2bdaSKuan-Wei Chiu
56ec7c2bdaSKuan-Wei Chiu    #define DEFINE_MIN_HEAP(_type, _name) MIN_HEAP_PREALLOCATED(_type, _name, 0)
57ec7c2bdaSKuan-Wei Chiu
58ec7c2bdaSKuan-Wei ChiuA typical heap structure will include a counter for the number of elements
59ec7c2bdaSKuan-Wei Chiu(`nr`), the maximum capacity of the heap (`size`), and a pointer to an array of
60ec7c2bdaSKuan-Wei Chiuelements (`data`). Optionally, you can specify a static array for preallocated
61ec7c2bdaSKuan-Wei Chiuheap storage using **MIN_HEAP_PREALLOCATED**.
62ec7c2bdaSKuan-Wei Chiu
63ec7c2bdaSKuan-Wei ChiuMin Heap Callbacks
64ec7c2bdaSKuan-Wei Chiu------------------
65ec7c2bdaSKuan-Wei Chiu
66ec7c2bdaSKuan-Wei ChiuThe **struct min_heap_callbacks** provides customization options for ordering
67ec7c2bdaSKuan-Wei Chiuelements in the heap and swapping them. It contains two function pointers:
68ec7c2bdaSKuan-Wei Chiu
69ec7c2bdaSKuan-Wei Chiu.. code-block:: c
70ec7c2bdaSKuan-Wei Chiu
71ec7c2bdaSKuan-Wei Chiu    struct min_heap_callbacks {
72ec7c2bdaSKuan-Wei Chiu        bool (*less)(const void *lhs, const void *rhs, void *args);
73ec7c2bdaSKuan-Wei Chiu        void (*swp)(void *lhs, void *rhs, void *args);
74ec7c2bdaSKuan-Wei Chiu    };
75ec7c2bdaSKuan-Wei Chiu
76ec7c2bdaSKuan-Wei Chiu- **less** is the comparison function used to establish the order of elements.
77ec7c2bdaSKuan-Wei Chiu- **swp** is a function for swapping elements in the heap. If swp is set to
78ec7c2bdaSKuan-Wei Chiu  NULL, the default swap function will be used, which swaps the elements based on their size
79ec7c2bdaSKuan-Wei Chiu
80ec7c2bdaSKuan-Wei ChiuMacro Wrappers
81ec7c2bdaSKuan-Wei Chiu==============
82ec7c2bdaSKuan-Wei Chiu
83ec7c2bdaSKuan-Wei ChiuThe following macro wrappers are provided for interacting with the heap in a
84ec7c2bdaSKuan-Wei Chiuuser-friendly manner. Each macro corresponds to a function that operates on the
85ec7c2bdaSKuan-Wei Chiuheap, and they abstract away direct calls to internal functions.
86ec7c2bdaSKuan-Wei Chiu
87ec7c2bdaSKuan-Wei ChiuEach macro accepts various parameters that are detailed below.
88ec7c2bdaSKuan-Wei Chiu
89ec7c2bdaSKuan-Wei ChiuHeap Initialization
90ec7c2bdaSKuan-Wei Chiu--------------------
91ec7c2bdaSKuan-Wei Chiu
92ec7c2bdaSKuan-Wei Chiu.. code-block:: c
93ec7c2bdaSKuan-Wei Chiu
94ec7c2bdaSKuan-Wei Chiu    min_heap_init(heap, data, size);
95ec7c2bdaSKuan-Wei Chiu
96ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap structure to be initialized.
97ec7c2bdaSKuan-Wei Chiu- **data**: A pointer to the buffer where the heap elements will be stored. If
98ec7c2bdaSKuan-Wei Chiu  `NULL`, the preallocated buffer within the heap structure will be used.
99ec7c2bdaSKuan-Wei Chiu- **size**: The maximum number of elements the heap can hold.
100ec7c2bdaSKuan-Wei Chiu
101ec7c2bdaSKuan-Wei ChiuThis macro initializes the heap, setting its initial state. If `data` is
102ec7c2bdaSKuan-Wei Chiu`NULL`, the preallocated memory inside the heap structure will be used for
103ec7c2bdaSKuan-Wei Chiustorage. Otherwise, the user-provided buffer is used. The operation is **O(1)**.
104ec7c2bdaSKuan-Wei Chiu
105ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_init_inline(heap, data, size)
106ec7c2bdaSKuan-Wei Chiu
107ec7c2bdaSKuan-Wei ChiuAccessing the Top Element
108ec7c2bdaSKuan-Wei Chiu-------------------------
109ec7c2bdaSKuan-Wei Chiu
110ec7c2bdaSKuan-Wei Chiu.. code-block:: c
111ec7c2bdaSKuan-Wei Chiu
112ec7c2bdaSKuan-Wei Chiu    element = min_heap_peek(heap);
113ec7c2bdaSKuan-Wei Chiu
114ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap from which to retrieve the smallest
115ec7c2bdaSKuan-Wei Chiu  element.
116ec7c2bdaSKuan-Wei Chiu
117ec7c2bdaSKuan-Wei ChiuThis macro returns a pointer to the smallest element (the root) of the heap, or
118ec7c2bdaSKuan-Wei Chiu`NULL` if the heap is empty. The operation is **O(1)**.
119ec7c2bdaSKuan-Wei Chiu
120ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_peek_inline(heap)
121ec7c2bdaSKuan-Wei Chiu
122ec7c2bdaSKuan-Wei ChiuHeap Insertion
123ec7c2bdaSKuan-Wei Chiu--------------
124ec7c2bdaSKuan-Wei Chiu
125ec7c2bdaSKuan-Wei Chiu.. code-block:: c
126ec7c2bdaSKuan-Wei Chiu
127ec7c2bdaSKuan-Wei Chiu    success = min_heap_push(heap, element, callbacks, args);
128ec7c2bdaSKuan-Wei Chiu
129ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap into which the element should be inserted.
130ec7c2bdaSKuan-Wei Chiu- **element**: A pointer to the element to be inserted into the heap.
131ec7c2bdaSKuan-Wei Chiu- **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
132ec7c2bdaSKuan-Wei Chiu  `less` and `swp` functions.
133ec7c2bdaSKuan-Wei Chiu- **args**: Optional arguments passed to the `less` and `swp` functions.
134ec7c2bdaSKuan-Wei Chiu
135ec7c2bdaSKuan-Wei ChiuThis macro inserts an element into the heap. It returns `true` if the insertion
136ec7c2bdaSKuan-Wei Chiuwas successful and `false` if the heap is full. The operation is **O(log n)**.
137ec7c2bdaSKuan-Wei Chiu
138ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_push_inline(heap, element, callbacks, args)
139ec7c2bdaSKuan-Wei Chiu
140ec7c2bdaSKuan-Wei ChiuHeap Removal
141ec7c2bdaSKuan-Wei Chiu------------
142ec7c2bdaSKuan-Wei Chiu
143ec7c2bdaSKuan-Wei Chiu.. code-block:: c
144ec7c2bdaSKuan-Wei Chiu
145ec7c2bdaSKuan-Wei Chiu    success = min_heap_pop(heap, callbacks, args);
146ec7c2bdaSKuan-Wei Chiu
147ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap from which to remove the smallest element.
148ec7c2bdaSKuan-Wei Chiu- **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
149ec7c2bdaSKuan-Wei Chiu  `less` and `swp` functions.
150ec7c2bdaSKuan-Wei Chiu- **args**: Optional arguments passed to the `less` and `swp` functions.
151ec7c2bdaSKuan-Wei Chiu
152ec7c2bdaSKuan-Wei ChiuThis macro removes the smallest element (the root) from the heap. It returns
153ec7c2bdaSKuan-Wei Chiu`true` if the element was successfully removed, or `false` if the heap is
154ec7c2bdaSKuan-Wei Chiuempty. The operation is **O(log n)**.
155ec7c2bdaSKuan-Wei Chiu
156ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_pop_inline(heap, callbacks, args)
157ec7c2bdaSKuan-Wei Chiu
158ec7c2bdaSKuan-Wei ChiuHeap Maintenance
159ec7c2bdaSKuan-Wei Chiu----------------
160ec7c2bdaSKuan-Wei Chiu
161ec7c2bdaSKuan-Wei ChiuYou can use the following macros to maintain the heap's structure:
162ec7c2bdaSKuan-Wei Chiu
163ec7c2bdaSKuan-Wei Chiu.. code-block:: c
164ec7c2bdaSKuan-Wei Chiu
165ec7c2bdaSKuan-Wei Chiu    min_heap_sift_down(heap, pos, callbacks, args);
166ec7c2bdaSKuan-Wei Chiu
167ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap.
168ec7c2bdaSKuan-Wei Chiu- **pos**: The index from which to start sifting down.
169ec7c2bdaSKuan-Wei Chiu- **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
170ec7c2bdaSKuan-Wei Chiu  `less` and `swp` functions.
171ec7c2bdaSKuan-Wei Chiu- **args**: Optional arguments passed to the `less` and `swp` functions.
172ec7c2bdaSKuan-Wei Chiu
173ec7c2bdaSKuan-Wei ChiuThis macro restores the heap property by moving the element at the specified
174ec7c2bdaSKuan-Wei Chiuindex (`pos`) down the heap until it is in the correct position. The operation
175ec7c2bdaSKuan-Wei Chiuis **O(log n)**.
176ec7c2bdaSKuan-Wei Chiu
177ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_sift_down_inline(heap, pos, callbacks, args)
178ec7c2bdaSKuan-Wei Chiu
179ec7c2bdaSKuan-Wei Chiu.. code-block:: c
180ec7c2bdaSKuan-Wei Chiu
181ec7c2bdaSKuan-Wei Chiu    min_heap_sift_up(heap, idx, callbacks, args);
182ec7c2bdaSKuan-Wei Chiu
183ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap.
184ec7c2bdaSKuan-Wei Chiu- **idx**: The index of the element to sift up.
185ec7c2bdaSKuan-Wei Chiu- **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
186ec7c2bdaSKuan-Wei Chiu  `less` and `swp` functions.
187ec7c2bdaSKuan-Wei Chiu- **args**: Optional arguments passed to the `less` and `swp` functions.
188ec7c2bdaSKuan-Wei Chiu
189ec7c2bdaSKuan-Wei ChiuThis macro restores the heap property by moving the element at the specified
190ec7c2bdaSKuan-Wei Chiuindex (`idx`) up the heap. The operation is **O(log n)**.
191ec7c2bdaSKuan-Wei Chiu
192ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_sift_up_inline(heap, idx, callbacks, args)
193ec7c2bdaSKuan-Wei Chiu
194ec7c2bdaSKuan-Wei Chiu.. code-block:: c
195ec7c2bdaSKuan-Wei Chiu
196ec7c2bdaSKuan-Wei Chiu    min_heapify_all(heap, callbacks, args);
197ec7c2bdaSKuan-Wei Chiu
198ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap.
199ec7c2bdaSKuan-Wei Chiu- **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
200ec7c2bdaSKuan-Wei Chiu  `less` and `swp` functions.
201ec7c2bdaSKuan-Wei Chiu- **args**: Optional arguments passed to the `less` and `swp` functions.
202ec7c2bdaSKuan-Wei Chiu
203ec7c2bdaSKuan-Wei ChiuThis macro ensures that the entire heap satisfies the heap property. It is
204ec7c2bdaSKuan-Wei Chiucalled when the heap is built from scratch or after many modifications. The
205ec7c2bdaSKuan-Wei Chiuoperation is **O(n)**.
206ec7c2bdaSKuan-Wei Chiu
207ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heapify_all_inline(heap, callbacks, args)
208ec7c2bdaSKuan-Wei Chiu
209ec7c2bdaSKuan-Wei ChiuRemoving Specific Elements
210ec7c2bdaSKuan-Wei Chiu--------------------------
211ec7c2bdaSKuan-Wei Chiu
212ec7c2bdaSKuan-Wei Chiu.. code-block:: c
213ec7c2bdaSKuan-Wei Chiu
214ec7c2bdaSKuan-Wei Chiu    success = min_heap_del(heap, idx, callbacks, args);
215ec7c2bdaSKuan-Wei Chiu
216ec7c2bdaSKuan-Wei Chiu- **heap**: A pointer to the min-heap.
217ec7c2bdaSKuan-Wei Chiu- **idx**: The index of the element to delete.
218ec7c2bdaSKuan-Wei Chiu- **callbacks**: A pointer to a `struct min_heap_callbacks` providing the
219ec7c2bdaSKuan-Wei Chiu  `less` and `swp` functions.
220ec7c2bdaSKuan-Wei Chiu- **args**: Optional arguments passed to the `less` and `swp` functions.
221ec7c2bdaSKuan-Wei Chiu
222ec7c2bdaSKuan-Wei ChiuThis macro removes an element at the specified index (`idx`) from the heap and
223ec7c2bdaSKuan-Wei Chiurestores the heap property. The operation is **O(log n)**.
224ec7c2bdaSKuan-Wei Chiu
225ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_del_inline(heap, idx, callbacks, args)
226ec7c2bdaSKuan-Wei Chiu
227ec7c2bdaSKuan-Wei ChiuOther Utilities
228ec7c2bdaSKuan-Wei Chiu===============
229ec7c2bdaSKuan-Wei Chiu
230ec7c2bdaSKuan-Wei Chiu- **min_heap_full(heap)**: Checks whether the heap is full.
231ec7c2bdaSKuan-Wei Chiu  Complexity: **O(1)**.
232ec7c2bdaSKuan-Wei Chiu
233ec7c2bdaSKuan-Wei Chiu.. code-block:: c
234ec7c2bdaSKuan-Wei Chiu
235ec7c2bdaSKuan-Wei Chiu    bool full = min_heap_full(heap);
236ec7c2bdaSKuan-Wei Chiu
237ec7c2bdaSKuan-Wei Chiu- `heap`: A pointer to the min-heap to check.
238ec7c2bdaSKuan-Wei Chiu
239ec7c2bdaSKuan-Wei ChiuThis macro returns `true` if the heap is full, otherwise `false`.
240ec7c2bdaSKuan-Wei Chiu
241ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_full_inline(heap)
242ec7c2bdaSKuan-Wei Chiu
243ec7c2bdaSKuan-Wei Chiu- **min_heap_empty(heap)**: Checks whether the heap is empty.
244ec7c2bdaSKuan-Wei Chiu  Complexity: **O(1)**.
245ec7c2bdaSKuan-Wei Chiu
246ec7c2bdaSKuan-Wei Chiu.. code-block:: c
247ec7c2bdaSKuan-Wei Chiu
248ec7c2bdaSKuan-Wei Chiu    bool empty = min_heap_empty(heap);
249ec7c2bdaSKuan-Wei Chiu
250ec7c2bdaSKuan-Wei Chiu- `heap`: A pointer to the min-heap to check.
251ec7c2bdaSKuan-Wei Chiu
252ec7c2bdaSKuan-Wei ChiuThis macro returns `true` if the heap is empty, otherwise `false`.
253ec7c2bdaSKuan-Wei Chiu
254ec7c2bdaSKuan-Wei Chiu**Inline Version:** min_heap_empty_inline(heap)
255ec7c2bdaSKuan-Wei Chiu
256ec7c2bdaSKuan-Wei ChiuExample Usage
257ec7c2bdaSKuan-Wei Chiu=============
258ec7c2bdaSKuan-Wei Chiu
259ec7c2bdaSKuan-Wei ChiuAn example usage of the min-heap API would involve defining a heap structure,
260ec7c2bdaSKuan-Wei Chiuinitializing it, and inserting and removing elements as needed.
261ec7c2bdaSKuan-Wei Chiu
262ec7c2bdaSKuan-Wei Chiu.. code-block:: c
263ec7c2bdaSKuan-Wei Chiu
264ec7c2bdaSKuan-Wei Chiu    #include <linux/min_heap.h>
265ec7c2bdaSKuan-Wei Chiu
266ec7c2bdaSKuan-Wei Chiu    int my_less_function(const void *lhs, const void *rhs, void *args) {
267ec7c2bdaSKuan-Wei Chiu        return (*(int *)lhs < *(int *)rhs);
268ec7c2bdaSKuan-Wei Chiu    }
269ec7c2bdaSKuan-Wei Chiu
270ec7c2bdaSKuan-Wei Chiu    struct min_heap_callbacks heap_cb = {
271ec7c2bdaSKuan-Wei Chiu        .less = my_less_function,    /* Comparison function for heap order */
272ec7c2bdaSKuan-Wei Chiu        .swp  = NULL,                /* Use default swap function */
273ec7c2bdaSKuan-Wei Chiu    };
274ec7c2bdaSKuan-Wei Chiu
275ec7c2bdaSKuan-Wei Chiu    void example_usage(void) {
276ec7c2bdaSKuan-Wei Chiu        /* Pre-populate the buffer with elements */
277ec7c2bdaSKuan-Wei Chiu        int buffer[5] = {5, 2, 8, 1, 3};
278ec7c2bdaSKuan-Wei Chiu        /* Declare a min-heap */
279ec7c2bdaSKuan-Wei Chiu        DEFINE_MIN_HEAP(int, my_heap);
280ec7c2bdaSKuan-Wei Chiu
281ec7c2bdaSKuan-Wei Chiu        /* Initialize the heap with preallocated buffer and size */
282ec7c2bdaSKuan-Wei Chiu        min_heap_init(&my_heap, buffer, 5);
283ec7c2bdaSKuan-Wei Chiu
284ec7c2bdaSKuan-Wei Chiu        /* Build the heap using min_heapify_all */
285ec7c2bdaSKuan-Wei Chiu        my_heap.nr = 5;  /* Set the number of elements in the heap */
286ec7c2bdaSKuan-Wei Chiu        min_heapify_all(&my_heap, &heap_cb, NULL);
287ec7c2bdaSKuan-Wei Chiu
288ec7c2bdaSKuan-Wei Chiu        /* Peek at the top element (should be 1 in this case) */
289ec7c2bdaSKuan-Wei Chiu        int *top = min_heap_peek(&my_heap);
290ec7c2bdaSKuan-Wei Chiu        pr_info("Top element: %d\n", *top);
291ec7c2bdaSKuan-Wei Chiu
292ec7c2bdaSKuan-Wei Chiu        /* Pop the top element (1) and get the new top (2) */
293ec7c2bdaSKuan-Wei Chiu        min_heap_pop(&my_heap, &heap_cb, NULL);
294ec7c2bdaSKuan-Wei Chiu        top = min_heap_peek(&my_heap);
295ec7c2bdaSKuan-Wei Chiu        pr_info("New top element: %d\n", *top);
296ec7c2bdaSKuan-Wei Chiu
297ec7c2bdaSKuan-Wei Chiu        /* Insert a new element (0) and recheck the top */
298ec7c2bdaSKuan-Wei Chiu        int new_element = 0;
299ec7c2bdaSKuan-Wei Chiu        min_heap_push(&my_heap, &new_element, &heap_cb, NULL);
300ec7c2bdaSKuan-Wei Chiu        top = min_heap_peek(&my_heap);
301ec7c2bdaSKuan-Wei Chiu        pr_info("Top element after insertion: %d\n", *top);
302ec7c2bdaSKuan-Wei Chiu    }
303