No need to rebalance heap for insertion
[lttng-modules.git] / lib / prio_heap / prio_heap.c
CommitLineData
f3bc08c5 1/*
162769ef
MD
2 * prio_heap.c
3 *
7369e702 4 * Priority heap containing pointers. Based on CLRS, chapter 6.
162769ef
MD
5 *
6 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
f3bc08c5
MD
17 */
18
19#include <linux/slab.h>
7369e702 20#include "prio_heap.h"
f3bc08c5 21
a29e892a 22static
7369e702 23size_t parent(size_t i)
c9891db2 24{
7369e702
MD
25 return i >> 1;
26}
27
28static
29size_t left(size_t i)
30{
31 return i << 1;
32}
33
34static
35size_t right(size_t i)
36{
37 return (i << 1) + 1;
38}
39
a29e892a
MD
40/*
41 * Copy of heap->ptrs pointer is invalid after heap_grow.
42 */
7369e702
MD
43static
44int heap_grow(struct ptr_heap *heap, size_t new_len)
45{
46 void **new_ptrs;
47
48 if (heap->alloc_len >= new_len)
49 return 0;
50
51 heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1);
52 new_ptrs = kmalloc(heap->alloc_len * sizeof(void *), heap->gfpmask);
53 if (!new_ptrs)
c9891db2 54 return -ENOMEM;
7369e702
MD
55 if (heap->ptrs)
56 memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *));
57 kfree(heap->ptrs);
58 heap->ptrs = new_ptrs;
59 return 0;
60}
61
62static
63int heap_set_len(struct ptr_heap *heap, size_t new_len)
64{
65 int ret;
66
67 ret = heap_grow(heap, new_len);
68 if (ret)
69 return ret;
70 heap->len = new_len;
c9891db2
MD
71 return 0;
72}
73
7369e702
MD
74int heap_init(struct ptr_heap *heap, size_t alloc_len,
75 gfp_t gfpmask, int gt(void *a, void *b))
76{
77 heap->ptrs = NULL;
78 heap->len = 0;
79 heap->alloc_len = 0;
80 heap->gt = gt;
81 /*
82 * Minimum size allocated is 1 entry to ensure memory allocation
83 * never fails within heap_replace_max.
84 */
c8d547f0 85 return heap_grow(heap, max_t(size_t, 1, alloc_len));
7369e702
MD
86}
87
c9891db2
MD
88void heap_free(struct ptr_heap *heap)
89{
90 kfree(heap->ptrs);
91}
f3bc08c5 92
7369e702 93static void heapify(struct ptr_heap *heap, size_t i)
f3bc08c5 94{
7369e702
MD
95 void **ptrs = heap->ptrs;
96 size_t l, r, largest;
97
98 for (;;) {
99 l = left(i);
100 r = right(i);
101 if (l <= heap->len && ptrs[l] > ptrs[i])
102 largest = l;
103 else
104 largest = i;
105 if (r <= heap->len && ptrs[r] > ptrs[largest])
106 largest = r;
107 if (largest != i) {
108 void *tmp;
109
110 tmp = ptrs[i];
111 ptrs[i] = ptrs[largest];
112 ptrs[largest] = tmp;
113 i = largest;
114 continue;
115 } else {
116 break;
117 }
118 }
f3bc08c5
MD
119}
120
121void *heap_replace_max(struct ptr_heap *heap, void *p)
122{
123 void *res;
f3bc08c5 124
7369e702
MD
125 if (!heap->len) {
126 (void) heap_set_len(heap, 1);
a29e892a 127 heap->ptrs[0] = p;
f3bc08c5
MD
128 return NULL;
129 }
130
131 /* Replace the current max and heapify */
a29e892a
MD
132 res = heap->ptrs[0];
133 heap->ptrs[0] = p;
f3bc08c5
MD
134 heapify(heap, 0);
135 return res;
136}
137
7369e702 138int heap_insert(struct ptr_heap *heap, void *p)
c9891db2 139{
a29e892a
MD
140 void **ptrs;
141 size_t pos;
7369e702 142 int ret;
c9891db2 143
7369e702
MD
144 ret = heap_set_len(heap, heap->len + 1);
145 if (ret)
146 return ret;
a29e892a 147 ptrs = heap->ptrs;
7369e702
MD
148 /* Add the element to the end */
149 ptrs[heap->len - 1] = p;
a29e892a
MD
150 pos = heap->len - 1;
151 /* Bubble it up to the appropriate position. */
152 for (;;) {
153 if (pos > 0 && heap->gt(ptrs[pos], ptrs[parent(pos)])) {
154 void *tmp;
155
156 /* Need to exchange */
157 tmp = ptrs[pos];
158 ptrs[pos] = ptrs[parent(pos)];
159 ptrs[parent(pos)] = tmp;
160 pos = parent(pos);
6074d543
MD
161 /*
162 * No need to rebalance: if we are larger than
163 * our parent, we are necessarily larger than
164 * its other child.
165 */
a29e892a
MD
166 } else {
167 break;
168 }
169 }
7369e702 170 return 0;
c9891db2
MD
171}
172
f3bc08c5
MD
173void *heap_remove(struct ptr_heap *heap)
174{
7369e702 175 switch (heap->len) {
f3bc08c5
MD
176 case 0:
177 return NULL;
178 case 1:
7369e702 179 (void) heap_set_len(heap, 0);
a29e892a 180 return heap->ptrs[0];
f3bc08c5 181 }
f3bc08c5 182 /* Shrink, replace the current max by previous last entry and heapify */
7369e702 183 heap_set_len(heap, heap->len - 1);
a29e892a 184 return heap_replace_max(heap, heap->ptrs[heap->len - 1]);
f3bc08c5
MD
185}
186
187void *heap_cherrypick(struct ptr_heap *heap, void *p)
188{
7369e702 189 size_t pos, len = heap->len;
f3bc08c5 190
7369e702 191 for (pos = 0; pos < len; pos++)
a29e892a 192 if (heap->ptrs[pos] == p)
f3bc08c5
MD
193 goto found;
194 return NULL;
195found:
7369e702
MD
196 if (heap->len == 1) {
197 (void) heap_set_len(heap, 0);
a29e892a 198 return heap->ptrs[0];
f3bc08c5 199 }
7369e702
MD
200 /* Replace p with previous last entry and heapify. */
201 heap_set_len(heap, heap->len - 1);
a29e892a 202 heap->ptrs[pos] = heap->ptrs[heap->len - 1];
f3bc08c5
MD
203 heapify(heap, pos);
204 return p;
205}
This page took 0.032501 seconds and 4 git commands to generate.