Cleanup: Move lib/ringbuffer/ headers to include/ringbuffer/
[lttng-modules.git] / lib / prio_heap / lttng_prio_heap.h
CommitLineData
9f36eaed
MJ
1/* SPDX-License-Identifier: MIT
2 *
a88db018 3 * lttng_prio_heap.h
162769ef 4 *
7369e702 5 * Priority heap containing pointers. Based on CLRS, chapter 6.
162769ef
MD
6 *
7 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
f3bc08c5
MD
8 */
9
9f36eaed
MJ
10#ifndef _LTTNG_PRIO_HEAP_H
11#define _LTTNG_PRIO_HEAP_H
12
c9891db2
MD
13#include <linux/gfp.h>
14
a88db018 15struct lttng_ptr_heap {
7369e702 16 size_t len, alloc_len;
c9891db2
MD
17 void **ptrs;
18 int (*gt)(void *a, void *b);
7369e702 19 gfp_t gfpmask;
c9891db2 20};
f3bc08c5 21
0b6d2ab3 22#ifdef DEBUG_HEAP
a88db018 23void lttng_check_heap(const struct lttng_ptr_heap *heap);
0b6d2ab3
MD
24#else
25static inline
a88db018 26void lttng_check_heap(const struct lttng_ptr_heap *heap)
0b6d2ab3
MD
27{
28}
29#endif
30
f3bc08c5 31/**
a88db018 32 * lttng_heap_maximum - return the largest element in the heap
f3bc08c5
MD
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 */
a88db018 38static inline void *lttng_heap_maximum(const struct lttng_ptr_heap *heap)
f3bc08c5 39{
a88db018 40 lttng_check_heap(heap);
7369e702 41 return heap->len ? heap->ptrs[0] : NULL;
f3bc08c5
MD
42}
43
c9891db2 44/**
a88db018 45 * lttng_heap_init - initialize the heap
c9891db2 46 * @heap: the heap to initialize
7369e702 47 * @alloc_len: number of elements initially allocated
c9891db2
MD
48 * @gfp: allocation flags
49 * @gt: function to compare the elements
50 *
51 * Returns -ENOMEM if out of memory.
52 */
a88db018 53extern int lttng_heap_init(struct lttng_ptr_heap *heap,
7369e702
MD
54 size_t alloc_len, gfp_t gfpmask,
55 int gt(void *a, void *b));
c9891db2
MD
56
57/**
a88db018 58 * lttng_heap_free - free the heap
c9891db2
MD
59 * @heap: the heap to free
60 */
a88db018 61extern void lttng_heap_free(struct lttng_ptr_heap *heap);
c9891db2
MD
62
63/**
a88db018 64 * lttng_heap_insert - insert an element into the heap
c9891db2 65 * @heap: the heap to be operated on
7369e702 66 * @p: the element to add
c9891db2 67 *
7369e702
MD
68 * Insert an element into the heap.
69 *
70 * Returns -ENOMEM if out of memory.
c9891db2 71 */
a88db018 72extern int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p);
f3bc08c5
MD
73
74/**
a88db018 75 * lttng_heap_remove - remove the largest element from the heap
f3bc08c5
MD
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 */
a88db018 81extern void *lttng_heap_remove(struct lttng_ptr_heap *heap);
f3bc08c5
MD
82
83/**
a88db018 84 * lttng_heap_cherrypick - remove a given element from the heap
f3bc08c5
MD
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 */
a88db018 92extern void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p);
f3bc08c5
MD
93
94/**
a88db018 95 * lttng_heap_replace_max - replace the the largest element from the heap
f3bc08c5
MD
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
7369e702 104 * it only rebalances the heap once. It never allocates memory.
f3bc08c5 105 */
a88db018 106extern void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p);
f3bc08c5 107
162769ef 108#endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.04132 seconds and 4 git commands to generate.