Commit | Line | Data |
---|---|---|
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 | 22 | static |
7369e702 | 23 | size_t parent(size_t i) |
c9891db2 | 24 | { |
7369e702 MD |
25 | return i >> 1; |
26 | } | |
27 | ||
28 | static | |
29 | size_t left(size_t i) | |
30 | { | |
31 | return i << 1; | |
32 | } | |
33 | ||
34 | static | |
35 | size_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 |
43 | static |
44 | int 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 | ||
62 | static | |
63 | int 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 |
74 | int 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 |
88 | void heap_free(struct ptr_heap *heap) |
89 | { | |
90 | kfree(heap->ptrs); | |
91 | } | |
f3bc08c5 | 92 | |
7369e702 | 93 | static 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 | ||
121 | void *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 | 138 | int 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); | |
161 | /* rebalance */ | |
162 | heapify(heap, pos); | |
163 | } else { | |
164 | break; | |
165 | } | |
166 | } | |
7369e702 | 167 | return 0; |
c9891db2 MD |
168 | } |
169 | ||
f3bc08c5 MD |
170 | void *heap_remove(struct ptr_heap *heap) |
171 | { | |
7369e702 | 172 | switch (heap->len) { |
f3bc08c5 MD |
173 | case 0: |
174 | return NULL; | |
175 | case 1: | |
7369e702 | 176 | (void) heap_set_len(heap, 0); |
a29e892a | 177 | return heap->ptrs[0]; |
f3bc08c5 | 178 | } |
f3bc08c5 | 179 | /* Shrink, replace the current max by previous last entry and heapify */ |
7369e702 | 180 | heap_set_len(heap, heap->len - 1); |
a29e892a | 181 | return heap_replace_max(heap, heap->ptrs[heap->len - 1]); |
f3bc08c5 MD |
182 | } |
183 | ||
184 | void *heap_cherrypick(struct ptr_heap *heap, void *p) | |
185 | { | |
7369e702 | 186 | size_t pos, len = heap->len; |
f3bc08c5 | 187 | |
7369e702 | 188 | for (pos = 0; pos < len; pos++) |
a29e892a | 189 | if (heap->ptrs[pos] == p) |
f3bc08c5 MD |
190 | goto found; |
191 | return NULL; | |
192 | found: | |
7369e702 MD |
193 | if (heap->len == 1) { |
194 | (void) heap_set_len(heap, 0); | |
a29e892a | 195 | return heap->ptrs[0]; |
f3bc08c5 | 196 | } |
7369e702 MD |
197 | /* Replace p with previous last entry and heapify. */ |
198 | heap_set_len(heap, heap->len - 1); | |
a29e892a | 199 | heap->ptrs[pos] = heap->ptrs[heap->len - 1]; |
f3bc08c5 MD |
200 | heapify(heap, pos); |
201 | return p; | |
202 | } |