d01cbe61f6faf3e7acb0f487a827b165418d060a
1 /* SPDX-License-Identifier: MIT
5 * Priority heap containing pointers. Based on CLRS, chapter 6.
7 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 #ifndef _LTTNG_PRIO_HEAP_H
11 #define _LTTNG_PRIO_HEAP_H
13 #include <linux/gfp.h>
15 struct lttng_ptr_heap
{
16 size_t len
, alloc_len
;
18 int (*gt
)(void *a
, void *b
);
23 void lttng_check_heap(const struct lttng_ptr_heap
*heap
);
26 void lttng_check_heap(const struct lttng_ptr_heap
*heap
)
32 * lttng_heap_maximum - return the largest element in the heap
33 * @heap: the heap to be operated on
35 * Returns the largest element in the heap, without performing any modification
36 * to the heap structure. Returns NULL if the heap is empty.
38 static inline void *lttng_heap_maximum(const struct lttng_ptr_heap
*heap
)
40 lttng_check_heap(heap
);
41 return heap
->len
? heap
->ptrs
[0] : NULL
;
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
51 * Returns -ENOMEM if out of memory.
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
));
58 * lttng_heap_free - free the heap
59 * @heap: the heap to free
61 extern void lttng_heap_free(struct lttng_ptr_heap
*heap
);
64 * lttng_heap_insert - insert an element into the heap
65 * @heap: the heap to be operated on
66 * @p: the element to add
68 * Insert an element into the heap.
70 * Returns -ENOMEM if out of memory.
72 extern int lttng_heap_insert(struct lttng_ptr_heap
*heap
, void *p
);
75 * lttng_heap_remove - remove the largest element from the heap
76 * @heap: the heap to be operated on
78 * Returns the largest element in the heap. It removes this element from the
79 * heap. Returns NULL if the heap is empty.
81 extern void *lttng_heap_remove(struct lttng_ptr_heap
*heap
);
84 * lttng_heap_cherrypick - remove a given element from the heap
85 * @heap: the heap to be operated on
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.
92 extern void *lttng_heap_cherrypick(struct lttng_ptr_heap
*heap
, void *p
);
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
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
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.
106 extern void *lttng_heap_replace_max(struct lttng_ptr_heap
*heap
, void *p
);
108 #endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.037212 seconds and 5 git commands to generate.