d01cbe61f6faf3e7acb0f487a827b165418d060a
[lttng-modules.git] / include / lttng / prio_heap.h
1 /* SPDX-License-Identifier: MIT
2 *
3 * lttng/prio_heap.h
4 *
5 * Priority heap containing pointers. Based on CLRS, chapter 6.
6 *
7 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 #ifndef _LTTNG_PRIO_HEAP_H
11 #define _LTTNG_PRIO_HEAP_H
12
13 #include <linux/gfp.h>
14
15 struct lttng_ptr_heap {
16 size_t len, alloc_len;
17 void **ptrs;
18 int (*gt)(void *a, void *b);
19 gfp_t gfpmask;
20 };
21
22 #ifdef DEBUG_HEAP
23 void lttng_check_heap(const struct lttng_ptr_heap *heap);
24 #else
25 static inline
26 void lttng_check_heap(const struct lttng_ptr_heap *heap)
27 {
28 }
29 #endif
30
31 /**
32 * lttng_heap_maximum - return the largest element in the heap
33 * @heap: the heap to be operated on
34 *
35 * Returns the largest element in the heap, without performing any modification
36 * to the heap structure. Returns NULL if the heap is empty.
37 */
38 static inline void *lttng_heap_maximum(const struct lttng_ptr_heap *heap)
39 {
40 lttng_check_heap(heap);
41 return heap->len ? heap->ptrs[0] : NULL;
42 }
43
44 /**
45 * lttng_heap_init - initialize the heap
46 * @heap: the heap to initialize
47 * @alloc_len: number of elements initially allocated
48 * @gfp: allocation flags
49 * @gt: function to compare the elements
50 *
51 * Returns -ENOMEM if out of memory.
52 */
53 extern int lttng_heap_init(struct lttng_ptr_heap *heap,
54 size_t alloc_len, gfp_t gfpmask,
55 int gt(void *a, void *b));
56
57 /**
58 * lttng_heap_free - free the heap
59 * @heap: the heap to free
60 */
61 extern void lttng_heap_free(struct lttng_ptr_heap *heap);
62
63 /**
64 * lttng_heap_insert - insert an element into the heap
65 * @heap: the heap to be operated on
66 * @p: the element to add
67 *
68 * Insert an element into the heap.
69 *
70 * Returns -ENOMEM if out of memory.
71 */
72 extern int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p);
73
74 /**
75 * lttng_heap_remove - remove the largest element from the heap
76 * @heap: the heap to be operated on
77 *
78 * Returns the largest element in the heap. It removes this element from the
79 * heap. Returns NULL if the heap is empty.
80 */
81 extern void *lttng_heap_remove(struct lttng_ptr_heap *heap);
82
83 /**
84 * lttng_heap_cherrypick - remove a given element from the heap
85 * @heap: the heap to be operated on
86 * @p: the element
87 *
88 * Remove the given element from the heap. Return the element if present, else
89 * return NULL. This algorithm has a complexity of O(n), which is higher than
90 * O(log(n)) provided by the rest of this API.
91 */
92 extern void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p);
93
94 /**
95 * lttng_heap_replace_max - replace the the largest element from the heap
96 * @heap: the heap to be operated on
97 * @p: the pointer to be inserted as topmost element replacement
98 *
99 * Returns the largest element in the heap. It removes this element from the
100 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
101 * the heap is empty.
102 *
103 * This is the equivalent of calling heap_remove() and then heap_insert(), but
104 * it only rebalances the heap once. It never allocates memory.
105 */
106 extern void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p);
107
108 #endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.037212 seconds and 5 git commands to generate.