Add kmalloc failover to vmalloc
[lttng-modules.git] / lib / prio_heap / lttng_prio_heap.c
CommitLineData
f3bc08c5 1/*
a88db018 2 * lttng_prio_heap.c
162769ef 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.
5fd8c00a
MD
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
f3bc08c5
MD
25 */
26
27#include <linux/slab.h>
5671a661 28#include <lib/prio_heap/lttng_prio_heap.h>
fd0cebd8 29#include <wrapper/vmalloc.h>
f3bc08c5 30
0b6d2ab3 31#ifdef DEBUG_HEAP
a88db018 32void lttng_check_heap(const struct lttng_ptr_heap *heap)
0b6d2ab3
MD
33{
34 size_t i;
35
36 if (!heap->len)
37 return;
38
39 for (i = 1; i < heap->len; i++)
40 WARN_ON_ONCE(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
41}
42#endif
43
a29e892a 44static
7369e702 45size_t parent(size_t i)
c9891db2 46{
0b6d2ab3 47 return (i -1) >> 1;
7369e702
MD
48}
49
50static
51size_t left(size_t i)
52{
0b6d2ab3 53 return (i << 1) + 1;
7369e702
MD
54}
55
56static
57size_t right(size_t i)
58{
0b6d2ab3 59 return (i << 1) + 2;
7369e702
MD
60}
61
a29e892a
MD
62/*
63 * Copy of heap->ptrs pointer is invalid after heap_grow.
64 */
7369e702 65static
a88db018 66int heap_grow(struct lttng_ptr_heap *heap, size_t new_len)
7369e702
MD
67{
68 void **new_ptrs;
69
70 if (heap->alloc_len >= new_len)
71 return 0;
72
73 heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1);
fd0cebd8 74 new_ptrs = lttng_kvmalloc(heap->alloc_len * sizeof(void *), heap->gfpmask);
7369e702 75 if (!new_ptrs)
c9891db2 76 return -ENOMEM;
7369e702
MD
77 if (heap->ptrs)
78 memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *));
fd0cebd8 79 lttng_kvfree(heap->ptrs);
7369e702
MD
80 heap->ptrs = new_ptrs;
81 return 0;
82}
83
84static
a88db018 85int heap_set_len(struct lttng_ptr_heap *heap, size_t new_len)
7369e702
MD
86{
87 int ret;
88
89 ret = heap_grow(heap, new_len);
90 if (ret)
91 return ret;
92 heap->len = new_len;
c9891db2
MD
93 return 0;
94}
95
a88db018 96int lttng_heap_init(struct lttng_ptr_heap *heap, size_t alloc_len,
7369e702
MD
97 gfp_t gfpmask, int gt(void *a, void *b))
98{
99 heap->ptrs = NULL;
100 heap->len = 0;
101 heap->alloc_len = 0;
102 heap->gt = gt;
ff77b142 103 heap->gfpmask = gfpmask;
7369e702
MD
104 /*
105 * Minimum size allocated is 1 entry to ensure memory allocation
106 * never fails within heap_replace_max.
107 */
c8d547f0 108 return heap_grow(heap, max_t(size_t, 1, alloc_len));
7369e702
MD
109}
110
a88db018 111void lttng_heap_free(struct lttng_ptr_heap *heap)
c9891db2 112{
fd0cebd8 113 lttng_kvfree(heap->ptrs);
c9891db2 114}
f3bc08c5 115
a88db018 116static void heapify(struct lttng_ptr_heap *heap, size_t i)
f3bc08c5 117{
7369e702
MD
118 void **ptrs = heap->ptrs;
119 size_t l, r, largest;
120
121 for (;;) {
0b6d2ab3
MD
122 void *tmp;
123
7369e702
MD
124 l = left(i);
125 r = right(i);
0b6d2ab3 126 if (l < heap->len && heap->gt(ptrs[l], ptrs[i]))
7369e702
MD
127 largest = l;
128 else
129 largest = i;
0b6d2ab3 130 if (r < heap->len && heap->gt(ptrs[r], ptrs[largest]))
7369e702 131 largest = r;
0b6d2ab3 132 if (largest == i)
7369e702 133 break;
0b6d2ab3
MD
134 tmp = ptrs[i];
135 ptrs[i] = ptrs[largest];
136 ptrs[largest] = tmp;
137 i = largest;
7369e702 138 }
a88db018 139 lttng_check_heap(heap);
f3bc08c5
MD
140}
141
a88db018 142void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p)
f3bc08c5
MD
143{
144 void *res;
f3bc08c5 145
7369e702
MD
146 if (!heap->len) {
147 (void) heap_set_len(heap, 1);
a29e892a 148 heap->ptrs[0] = p;
a88db018 149 lttng_check_heap(heap);
f3bc08c5
MD
150 return NULL;
151 }
152
153 /* Replace the current max and heapify */
a29e892a
MD
154 res = heap->ptrs[0];
155 heap->ptrs[0] = p;
f3bc08c5
MD
156 heapify(heap, 0);
157 return res;
158}
159
a88db018 160int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p)
c9891db2 161{
a29e892a
MD
162 void **ptrs;
163 size_t pos;
7369e702 164 int ret;
c9891db2 165
7369e702
MD
166 ret = heap_set_len(heap, heap->len + 1);
167 if (ret)
168 return ret;
a29e892a 169 ptrs = heap->ptrs;
a29e892a 170 pos = heap->len - 1;
0b6d2ab3
MD
171 while (pos > 0 && heap->gt(p, ptrs[parent(pos)])) {
172 /* Move parent down until we find the right spot */
173 ptrs[pos] = ptrs[parent(pos)];
174 pos = parent(pos);
a29e892a 175 }
0b6d2ab3 176 ptrs[pos] = p;
a88db018 177 lttng_check_heap(heap);
7369e702 178 return 0;
c9891db2
MD
179}
180
a88db018 181void *lttng_heap_remove(struct lttng_ptr_heap *heap)
f3bc08c5 182{
7369e702 183 switch (heap->len) {
f3bc08c5
MD
184 case 0:
185 return NULL;
186 case 1:
7369e702 187 (void) heap_set_len(heap, 0);
a29e892a 188 return heap->ptrs[0];
f3bc08c5 189 }
f3bc08c5 190 /* Shrink, replace the current max by previous last entry and heapify */
7369e702 191 heap_set_len(heap, heap->len - 1);
0b6d2ab3 192 /* len changed. previous last entry is at heap->len */
a88db018 193 return lttng_heap_replace_max(heap, heap->ptrs[heap->len]);
f3bc08c5
MD
194}
195
a88db018 196void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p)
f3bc08c5 197{
7369e702 198 size_t pos, len = heap->len;
f3bc08c5 199
7369e702 200 for (pos = 0; pos < len; pos++)
a29e892a 201 if (heap->ptrs[pos] == p)
f3bc08c5
MD
202 goto found;
203 return NULL;
204found:
7369e702
MD
205 if (heap->len == 1) {
206 (void) heap_set_len(heap, 0);
a88db018 207 lttng_check_heap(heap);
a29e892a 208 return heap->ptrs[0];
f3bc08c5 209 }
7369e702
MD
210 /* Replace p with previous last entry and heapify. */
211 heap_set_len(heap, heap->len - 1);
0b6d2ab3
MD
212 /* len changed. previous last entry is at heap->len */
213 heap->ptrs[pos] = heap->ptrs[heap->len];
f3bc08c5
MD
214 heapify(heap, pos);
215 return p;
216}
This page took 0.05162 seconds and 4 git commands to generate.