Update sites using kernel version checking macro to new range
[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.
f3bc08c5
MD
20 */
21
c9891db2
MD
22#include <linux/gfp.h>
23
a88db018 24struct lttng_ptr_heap {
7369e702 25 size_t len, alloc_len;
c9891db2
MD
26 void **ptrs;
27 int (*gt)(void *a, void *b);
7369e702 28 gfp_t gfpmask;
c9891db2 29};
f3bc08c5 30
0b6d2ab3 31#ifdef DEBUG_HEAP
a88db018 32void lttng_check_heap(const struct lttng_ptr_heap *heap);
0b6d2ab3
MD
33#else
34static inline
a88db018 35void lttng_check_heap(const struct lttng_ptr_heap *heap)
0b6d2ab3
MD
36{
37}
38#endif
39
f3bc08c5 40/**
a88db018 41 * lttng_heap_maximum - return the largest element in the heap
f3bc08c5
MD
42 * @heap: the heap to be operated on
43 *
44 * Returns the largest element in the heap, without performing any modification
45 * to the heap structure. Returns NULL if the heap is empty.
46 */
a88db018 47static inline void *lttng_heap_maximum(const struct lttng_ptr_heap *heap)
f3bc08c5 48{
a88db018 49 lttng_check_heap(heap);
7369e702 50 return heap->len ? heap->ptrs[0] : NULL;
f3bc08c5
MD
51}
52
c9891db2 53/**
a88db018 54 * lttng_heap_init - initialize the heap
c9891db2 55 * @heap: the heap to initialize
7369e702 56 * @alloc_len: number of elements initially allocated
c9891db2
MD
57 * @gfp: allocation flags
58 * @gt: function to compare the elements
59 *
60 * Returns -ENOMEM if out of memory.
61 */
a88db018 62extern int lttng_heap_init(struct lttng_ptr_heap *heap,
7369e702
MD
63 size_t alloc_len, gfp_t gfpmask,
64 int gt(void *a, void *b));
c9891db2
MD
65
66/**
a88db018 67 * lttng_heap_free - free the heap
c9891db2
MD
68 * @heap: the heap to free
69 */
a88db018 70extern void lttng_heap_free(struct lttng_ptr_heap *heap);
c9891db2
MD
71
72/**
a88db018 73 * lttng_heap_insert - insert an element into the heap
c9891db2 74 * @heap: the heap to be operated on
7369e702 75 * @p: the element to add
c9891db2 76 *
7369e702
MD
77 * Insert an element into the heap.
78 *
79 * Returns -ENOMEM if out of memory.
c9891db2 80 */
a88db018 81extern int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p);
f3bc08c5
MD
82
83/**
a88db018 84 * lttng_heap_remove - remove the largest element from the heap
f3bc08c5
MD
85 * @heap: the heap to be operated on
86 *
87 * Returns the largest element in the heap. It removes this element from the
88 * heap. Returns NULL if the heap is empty.
89 */
a88db018 90extern void *lttng_heap_remove(struct lttng_ptr_heap *heap);
f3bc08c5
MD
91
92/**
a88db018 93 * lttng_heap_cherrypick - remove a given element from the heap
f3bc08c5
MD
94 * @heap: the heap to be operated on
95 * @p: the element
96 *
97 * Remove the given element from the heap. Return the element if present, else
98 * return NULL. This algorithm has a complexity of O(n), which is higher than
99 * O(log(n)) provided by the rest of this API.
100 */
a88db018 101extern void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p);
f3bc08c5
MD
102
103/**
a88db018 104 * lttng_heap_replace_max - replace the the largest element from the heap
f3bc08c5
MD
105 * @heap: the heap to be operated on
106 * @p: the pointer to be inserted as topmost element replacement
107 *
108 * Returns the largest element in the heap. It removes this element from the
109 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
110 * the heap is empty.
111 *
112 * This is the equivalent of calling heap_remove() and then heap_insert(), but
7369e702 113 * it only rebalances the heap once. It never allocates memory.
f3bc08c5 114 */
a88db018 115extern void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p);
f3bc08c5 116
162769ef 117#endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.030933 seconds and 4 git commands to generate.