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; | |
ff77b142 | 94 | heap->gfpmask = gfpmask; |
7369e702 MD |
95 | /* |
96 | * Minimum size allocated is 1 entry to ensure memory allocation | |
97 | * never fails within heap_replace_max. | |
98 | */ | |
c8d547f0 | 99 | return heap_grow(heap, max_t(size_t, 1, alloc_len)); |
7369e702 MD |
100 | } |
101 | ||
a88db018 | 102 | void lttng_heap_free(struct lttng_ptr_heap *heap) |
c9891db2 MD |
103 | { |
104 | kfree(heap->ptrs); | |
105 | } | |
f3bc08c5 | 106 | |
a88db018 | 107 | static void heapify(struct lttng_ptr_heap *heap, size_t i) |
f3bc08c5 | 108 | { |
7369e702 MD |
109 | void **ptrs = heap->ptrs; |
110 | size_t l, r, largest; | |
111 | ||
112 | for (;;) { | |
0b6d2ab3 MD |
113 | void *tmp; |
114 | ||
7369e702 MD |
115 | l = left(i); |
116 | r = right(i); | |
0b6d2ab3 | 117 | if (l < heap->len && heap->gt(ptrs[l], ptrs[i])) |
7369e702 MD |
118 | largest = l; |
119 | else | |
120 | largest = i; | |
0b6d2ab3 | 121 | if (r < heap->len && heap->gt(ptrs[r], ptrs[largest])) |
7369e702 | 122 | largest = r; |
0b6d2ab3 | 123 | if (largest == i) |
7369e702 | 124 | break; |
0b6d2ab3 MD |
125 | tmp = ptrs[i]; |
126 | ptrs[i] = ptrs[largest]; | |
127 | ptrs[largest] = tmp; | |
128 | i = largest; | |
7369e702 | 129 | } |
a88db018 | 130 | lttng_check_heap(heap); |
f3bc08c5 MD |
131 | } |
132 | ||
a88db018 | 133 | void *lttng_heap_replace_max(struct lttng_ptr_heap *heap, void *p) |
f3bc08c5 MD |
134 | { |
135 | void *res; | |
f3bc08c5 | 136 | |
7369e702 MD |
137 | if (!heap->len) { |
138 | (void) heap_set_len(heap, 1); | |
a29e892a | 139 | heap->ptrs[0] = p; |
a88db018 | 140 | lttng_check_heap(heap); |
f3bc08c5 MD |
141 | return NULL; |
142 | } | |
143 | ||
144 | /* Replace the current max and heapify */ | |
a29e892a MD |
145 | res = heap->ptrs[0]; |
146 | heap->ptrs[0] = p; | |
f3bc08c5 MD |
147 | heapify(heap, 0); |
148 | return res; | |
149 | } | |
150 | ||
a88db018 | 151 | int lttng_heap_insert(struct lttng_ptr_heap *heap, void *p) |
c9891db2 | 152 | { |
a29e892a MD |
153 | void **ptrs; |
154 | size_t pos; | |
7369e702 | 155 | int ret; |
c9891db2 | 156 | |
7369e702 MD |
157 | ret = heap_set_len(heap, heap->len + 1); |
158 | if (ret) | |
159 | return ret; | |
a29e892a | 160 | ptrs = heap->ptrs; |
a29e892a | 161 | pos = heap->len - 1; |
0b6d2ab3 MD |
162 | while (pos > 0 && heap->gt(p, ptrs[parent(pos)])) { |
163 | /* Move parent down until we find the right spot */ | |
164 | ptrs[pos] = ptrs[parent(pos)]; | |
165 | pos = parent(pos); | |
a29e892a | 166 | } |
0b6d2ab3 | 167 | ptrs[pos] = p; |
a88db018 | 168 | lttng_check_heap(heap); |
7369e702 | 169 | return 0; |
c9891db2 MD |
170 | } |
171 | ||
a88db018 | 172 | void *lttng_heap_remove(struct lttng_ptr_heap *heap) |
f3bc08c5 | 173 | { |
7369e702 | 174 | switch (heap->len) { |
f3bc08c5 MD |
175 | case 0: |
176 | return NULL; | |
177 | case 1: | |
7369e702 | 178 | (void) heap_set_len(heap, 0); |
a29e892a | 179 | return heap->ptrs[0]; |
f3bc08c5 | 180 | } |
f3bc08c5 | 181 | /* Shrink, replace the current max by previous last entry and heapify */ |
7369e702 | 182 | heap_set_len(heap, heap->len - 1); |
0b6d2ab3 | 183 | /* len changed. previous last entry is at heap->len */ |
a88db018 | 184 | return lttng_heap_replace_max(heap, heap->ptrs[heap->len]); |
f3bc08c5 MD |
185 | } |
186 | ||
a88db018 | 187 | void *lttng_heap_cherrypick(struct lttng_ptr_heap *heap, void *p) |
f3bc08c5 | 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; | |
195 | found: | |
7369e702 MD |
196 | if (heap->len == 1) { |
197 | (void) heap_set_len(heap, 0); | |
a88db018 | 198 | lttng_check_heap(heap); |
a29e892a | 199 | return heap->ptrs[0]; |
f3bc08c5 | 200 | } |
7369e702 MD |
201 | /* Replace p with previous last entry and heapify. */ |
202 | heap_set_len(heap, heap->len - 1); | |
0b6d2ab3 MD |
203 | /* len changed. previous last entry is at heap->len */ |
204 | heap->ptrs[pos] = heap->ptrs[heap->len]; | |
f3bc08c5 MD |
205 | heapify(heap, pos); |
206 | return p; | |
207 | } |