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