Make prio heap dynamically sized
[lttng-modules.git] / lib / prio_heap / prio_heap.h
CommitLineData
162769ef
MD
1#ifndef _LTTNG_PRIO_HEAP_H
2#define _LTTNG_PRIO_HEAP_H
f3bc08c5
MD
3
4/*
162769ef
MD
5 * prio_heap.h
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
24struct 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
MD
30
31/**
32 * 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 */
38static inline void *heap_maximum(const struct ptr_heap *heap)
39{
7369e702 40 return heap->len ? heap->ptrs[0] : NULL;
f3bc08c5
MD
41}
42
c9891db2
MD
43/**
44 * heap_init - initialize the heap
45 * @heap: the heap to initialize
7369e702 46 * @alloc_len: number of elements initially allocated
c9891db2
MD
47 * @gfp: allocation flags
48 * @gt: function to compare the elements
49 *
50 * Returns -ENOMEM if out of memory.
51 */
7369e702
MD
52extern int heap_init(struct ptr_heap *heap,
53 size_t alloc_len, gfp_t gfpmask,
54 int gt(void *a, void *b));
c9891db2
MD
55
56/**
57 * heap_free - free the heap
58 * @heap: the heap to free
59 */
60extern void heap_free(struct ptr_heap *heap);
61
62/**
63 * heap_insert - insert an element into the heap
64 * @heap: the heap to be operated on
7369e702 65 * @p: the element to add
c9891db2 66 *
7369e702
MD
67 * Insert an element into the heap.
68 *
69 * Returns -ENOMEM if out of memory.
c9891db2 70 */
7369e702 71extern int heap_insert(struct ptr_heap *heap, void *p);
f3bc08c5
MD
72
73/**
74 * heap_remove - remove the largest element from the heap
75 * @heap: the heap to be operated on
76 *
77 * Returns the largest element in the heap. It removes this element from the
78 * heap. Returns NULL if the heap is empty.
79 */
80extern void *heap_remove(struct ptr_heap *heap);
81
82/**
83 * heap_cherrypick - remove a given element from the heap
84 * @heap: the heap to be operated on
85 * @p: the element
86 *
87 * Remove the given element from the heap. Return the element if present, else
88 * return NULL. This algorithm has a complexity of O(n), which is higher than
89 * O(log(n)) provided by the rest of this API.
90 */
91extern void *heap_cherrypick(struct ptr_heap *heap, void *p);
92
93/**
94 * heap_replace_max - replace the the largest element from the heap
95 * @heap: the heap to be operated on
96 * @p: the pointer to be inserted as topmost element replacement
97 *
98 * Returns the largest element in the heap. It removes this element from the
99 * heap. The heap is rebalanced only once after the insertion. Returns NULL if
100 * the heap is empty.
101 *
102 * This is the equivalent of calling heap_remove() and then heap_insert(), but
7369e702 103 * it only rebalances the heap once. It never allocates memory.
f3bc08c5
MD
104 */
105extern void *heap_replace_max(struct ptr_heap *heap, void *p);
106
162769ef 107#endif /* _LTTNG_PRIO_HEAP_H */
This page took 0.027597 seconds and 4 git commands to generate.