Commit | Line | Data |
---|---|---|
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. | |
f3bc08c5 MD |
17 | */ |
18 | ||
19 | #include <linux/slab.h> | |
a88db018 | 20 | #include "lttng_prio_heap.h" |
f3bc08c5 | 21 | |
0b6d2ab3 | 22 | #ifdef DEBUG_HEAP |
a88db018 | 23 | void lttng_check_heap(const struct lttng_ptr_heap *heap) |
0b6d2ab3 MD |
24 | { |
25 | size_t i; | |
26 | ||
27 | if (!heap->len) | |
28 | return; | |
29 | ||
30 | for (i = 1; i < heap->len; i++) | |
31 | WARN_ON_ONCE(!heap->gt(heap->ptrs[i], heap->ptrs[0])); | |
32 | } | |
33 | #endif | |
34 | ||
a29e892a | 35 | static |
7369e702 | 36 | size_t parent(size_t i) |
c9891db2 | 37 | { |
0b6d2ab3 | 38 | return (i -1) >> 1; |
7369e702 MD |
39 | } |
40 | ||
41 | static | |
42 | size_t left(size_t i) | |
43 | { | |
0b6d2ab3 | 44 | return (i << 1) + 1; |
7369e702 MD |
45 | } |
46 | ||
47 | static | |
48 | size_t right(size_t i) | |
49 | { | |
0b6d2ab3 | 50 | return (i << 1) + 2; |
7369e702 MD |
51 | } |
52 | ||
a29e892a MD |
53 | /* |
54 | * Copy of heap->ptrs pointer is invalid after heap_grow. | |
55 | */ | |
7369e702 | 56 | static |
a88db018 | 57 | int heap_grow(struct lttng_ptr_heap *heap, size_t new_len) |
7369e702 MD |
58 | { |
59 | void **new_ptrs; | |
60 | ||
61 | if (heap->alloc_len >= new_len) | |
62 | return 0; | |
63 | ||
64 | heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1); | |
65 | new_ptrs = kmalloc(heap->alloc_len * sizeof(void *), heap->gfpmask); | |
66 | if (!new_ptrs) | |
c9891db2 | 67 | return -ENOMEM; |
7369e702 MD |
68 | if (heap->ptrs) |
69 | memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *)); | |
70 | kfree(heap->ptrs); | |
71 | heap->ptrs = new_ptrs; | |
72 | return 0; | |
73 | } | |
74 | ||
75 | static | |
a88db018 | 76 | int heap_set_len(struct lttng_ptr_heap *heap, size_t new_len) |
7369e702 MD |
77 | { |
78 | int ret; | |
79 | ||
80 | ret = heap_grow(heap, new_len); | |
81 | if (ret) | |
82 | return ret; | |
83 | heap->len = new_len; | |
c9891db2 MD |
84 | return 0; |
85 | } | |
86 | ||
a88db018 | 87 | int lttng_heap_init(struct lttng_ptr_heap *heap, size_t alloc_len, |
7369e702 MD |
88 | gfp_t gfpmask, int gt(void *a, void *b)) |
89 | { | |
90 | heap->ptrs = NULL; | |
91 | heap->len = 0; | |
92 | heap->alloc_len = 0; | |
93 | heap->gt = gt; | |
94 | /* | |
95 | * Minimum size allocated is 1 entry to ensure memory allocation | |
96 | * never fails within heap_replace_max. | |
97 | */ | |
c8d547f0 | 98 | return heap_grow(heap, max_t(size_t, 1, alloc_len)); |
7369e702 MD |
99 | } |
100 | ||
a88db018 | 101 | void lttng_heap_free(struct lttng_ptr_heap *heap) |
c9891db2 MD |
102 | { |
103 | kfree(heap->ptrs); | |
104 | } | |
f3bc08c5 | 105 | |
a88db018 | 106 | static void heapify(struct lttng_ptr_heap *heap, size_t i) |
f3bc08c5 | 107 | { |
7369e702 MD |
108 | void **ptrs = heap->ptrs; |
109 | size_t l, r, largest; | |
110 | ||
111 | for (;;) { | |
0b6d2ab3 MD |
112 | void *tmp; |
113 | ||
7369e702 MD |
114 | l = left(i); |
115 | r = right(i); | |
0b6d2ab3 | 116 | if (l < heap->len && heap->gt(ptrs[l], ptrs[i])) |
7369e702 MD |
117 | largest = l; |
118 | else | |
119 | largest = i; | |
0b6d2ab3 | 120 | if (r < heap->len && heap->gt(ptrs[r], ptrs[largest])) |
7369e702 | 121 | largest = r; |
0b6d2ab3 | 122 | if (largest == i) |
7369e702 | 123 | break; |
0b6d2ab3 MD |
124 | tmp = ptrs[i]; |
125 | ptrs[i] = ptrs[largest]; | |
126 | ptrs[largest] = tmp; | |
127 | i = largest; | |
7369e702 | 128 | } |
a88db018 | 129 | lttng_check_heap(heap); |
f3bc08c5 MD |
130 | } |
131 | ||
a88db018 | 132 | void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p) |
f3bc08c5 MD |
133 | { |
134 | void *res; | |
f3bc08c5 | 135 | |
7369e702 MD |
136 | if (!heap->len) { |
137 | (void) heap_set_len(heap, 1); | |
a29e892a | 138 | heap->ptrs[0] = p; |
a88db018 | 139 | lttng_check_heap(heap); |
f3bc08c5 MD |
140 | return NULL; |
141 | } | |
142 | ||
143 | /* Replace the current max and heapify */ | |
a29e892a MD |
144 | res = heap->ptrs[0]; |
145 | heap->ptrs[0] = p; | |
f3bc08c5 MD |
146 | heapify(heap, 0); |
147 | return res; | |
148 | } | |
149 | ||
a88db018 | 150 | int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p) |
c9891db2 | 151 | { |
a29e892a MD |
152 | void **ptrs; |
153 | size_t pos; | |
7369e702 | 154 | int ret; |
c9891db2 | 155 | |
7369e702 MD |
156 | ret = heap_set_len(heap, heap->len + 1); |
157 | if (ret) | |
158 | return ret; | |
a29e892a | 159 | ptrs = heap->ptrs; |
a29e892a | 160 | pos = heap->len - 1; |
0b6d2ab3 MD |
161 | while (pos > 0 && heap->gt(p, ptrs[parent(pos)])) { |
162 | /* Move parent down until we find the right spot */ | |
163 | ptrs[pos] = ptrs[parent(pos)]; | |
164 | pos = parent(pos); | |
a29e892a | 165 | } |
0b6d2ab3 | 166 | ptrs[pos] = p; |
a88db018 | 167 | lttng_check_heap(heap); |
7369e702 | 168 | return 0; |
c9891db2 MD |
169 | } |
170 | ||
a88db018 | 171 | void *lttng_heap_remove(struct lttng_ptr_heap *heap) |
f3bc08c5 | 172 | { |
7369e702 | 173 | switch (heap->len) { |
f3bc08c5 MD |
174 | case 0: |
175 | return NULL; | |
176 | case 1: | |
7369e702 | 177 | (void) heap_set_len(heap, 0); |
a29e892a | 178 | return heap->ptrs[0]; |
f3bc08c5 | 179 | } |
f3bc08c5 | 180 | /* Shrink, replace the current max by previous last entry and heapify */ |
7369e702 | 181 | heap_set_len(heap, heap->len - 1); |
0b6d2ab3 | 182 | /* len changed. previous last entry is at heap->len */ |
a88db018 | 183 | return lttng_heap_replace_max(heap, heap->ptrs[heap->len]); |
f3bc08c5 MD |
184 | } |
185 | ||
a88db018 | 186 | void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p) |
f3bc08c5 | 187 | { |
7369e702 | 188 | size_t pos, len = heap->len; |
f3bc08c5 | 189 | |
7369e702 | 190 | for (pos = 0; pos < len; pos++) |
a29e892a | 191 | if (heap->ptrs[pos] == p) |
f3bc08c5 MD |
192 | goto found; |
193 | return NULL; | |
194 | found: | |
7369e702 MD |
195 | if (heap->len == 1) { |
196 | (void) heap_set_len(heap, 0); | |
a88db018 | 197 | lttng_check_heap(heap); |
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); | |
0b6d2ab3 MD |
202 | /* len changed. previous last entry is at heap->len */ |
203 | heap->ptrs[pos] = heap->ptrs[heap->len]; | |
f3bc08c5 MD |
204 | heapify(heap, pos); |
205 | return p; | |
206 | } |