Add missing MIT license text to 3 files under this license
[lttng-modules.git] / lib / prio_heap / lttng_prio_heap.h
CommitLineData
162769ef
MD
1#ifndef _LTTNG_PRIO_HEAP_H
2#define _LTTNG_PRIO_HEAP_H
f3bc08c5
MD
3
4/*
a88db018 5 * lttng_prio_heap.h
162769ef 6 *
7369e702 7 * Priority heap containing pointers. Based on CLRS, chapter 6.
162769ef
MD
8 *
9 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
5fd8c00a
MD
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
f3bc08c5
MD
28 */
29
c9891db2
MD
30#include <linux/gfp.h>
31
a88db018 32struct lttng_ptr_heap {
7369e702 33 size_t len, alloc_len;
c9891db2
MD
34 void **ptrs;
35 int (*gt)(void *a, void *b);
7369e702 36 gfp_t gfpmask;
c9891db2 37};
f3bc08c5 38
0b6d2ab3 39#ifdef DEBUG_HEAP
a88db018 40void lttng_check_heap(const struct lttng_ptr_heap *heap);
0b6d2ab3
MD
41#else
42static inline
a88db018 43void lttng_check_heap(const struct lttng_ptr_heap *heap)
0b6d2ab3
MD
44{
45}
46#endif
47
f3bc08c5 48/**
a88db018 49 * lttng_heap_maximum - return the largest element in the heap
f3bc08c5
MD
50 * @heap: the heap to be operated on
51 *
52 * Returns the largest element in the heap, without performing any modification
53 * to the heap structure. Returns NULL if the heap is empty.
54 */
a88db018 55static inline void *lttng_heap_maximum(const struct lttng_ptr_heap *heap)
f3bc08c5 56{
a88db018 57 lttng_check_heap(heap);
7369e702 58 return heap->len ? heap->ptrs[0] : NULL;
f3bc08c5
MD
59}
60
c9891db2 61/**
a88db018 62 * lttng_heap_init - initialize the heap
c9891db2 63 * @heap: the heap to initialize
7369e702 64 * @alloc_len: number of elements initially allocated
c9891db2
MD
65 * @gfp: allocation flags
66 * @gt: function to compare the elements
67 *
68 * Returns -ENOMEM if out of memory.
69 */
a88db018 70extern int lttng_heap_init(struct lttng_ptr_heap *heap,
7369e702
MD
71 size_t alloc_len, gfp_t gfpmask,
72 int gt(void *a, void *b));
c9891db2
MD
73
74/**
a88db018 75 * lttng_heap_free - free the heap
c9891db2
MD
76 * @heap: the heap to free
77 */
a88db018 78extern void lttng_heap_free(struct lttng_ptr_heap *heap);
c9891db2
MD
79
80/**
a88db018 81 * lttng_heap_insert - insert an element into the heap
c9891db2 82 * @heap: the heap to be operated on
7369e702 83 * @p: the element to add
c9891db2 84 *
7369e702
MD
85 * Insert an element into the heap.
86 *
87 * Returns -ENOMEM if out of memory.
c9891db2 88 */
a88db018 89extern int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p);
f3bc08c5
MD
90
91/**
a88db018 92 * lttng_heap_remove - remove the largest element from the heap
f3bc08c5
MD
93 * @heap: the heap to be operated on
94 *
95 * Returns the largest element in the heap. It removes this element from the
96 * heap. Returns NULL if the heap is empty.
97 */
a88db018 98extern void *lttng_heap_remove(struct lttng_ptr_heap *heap);
f3bc08c5
MD
99
100/**
a88db018 101 * lttng_heap_cherrypick - remove a given element from the heap
f3bc08c5
MD
102 * @heap: the heap to be operated on
103 * @p: the element
104 *
105 * Remove the given element from the heap. Return the element if present, else
106 * return NULL. This algorithm has a complexity of O(n), which is higher than
107 * O(log(n)) provided by the rest of this API.
108 */
a88db018 109extern void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p);
f3bc08c5
MD
110
111/**
a88db018 112 * lttng_heap_replace_max - replace the the largest element from the heap
f3bc08c5
MD
113 * @heap: the heap to be operated on
114 * @p: the pointer to be inserted as topmost element replacement
115 *
116 * Returns the largest element in the heap. It removes this element from the
117 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
118 * the heap is empty.
119 *
120 * This is the equivalent of calling heap_remove() and then heap_insert(), but
7369e702 121 * it only rebalances the heap once. It never allocates memory.
f3bc08c5 122 */
a88db018 123extern void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p);
f3bc08c5 124
162769ef 125#endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.030685 seconds and 4 git commands to generate.