Add `-internal` suffix to all internal header files
[babeltrace.git] / lib / prio_heap / prio_heap.c
1 /*
2 * prio_heap.c
3 *
4 * Static-sized priority heap containing pointers. Based on CLRS,
5 * chapter 6.
6 *
7 * Copyright 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include <babeltrace/prio-heap-internal.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <assert.h>
34
35 #ifdef DEBUG_HEAP
36 void check_heap(const struct ptr_heap *heap)
37 {
38 size_t i;
39
40 if (!heap->len)
41 return;
42
43 for (i = 1; i < heap->len; i++)
44 assert(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
45 }
46 #endif
47
48 static
49 size_t parent(size_t i)
50 {
51 return (i - 1) >> 1;
52 }
53
54 static
55 size_t left(size_t i)
56 {
57 return (i << 1) + 1;
58 }
59
60 static
61 size_t right(size_t i)
62 {
63 return (i << 1) + 2;
64 }
65
66 /*
67 * Copy of heap->ptrs pointer is invalid after heap_grow.
68 */
69 static
70 int heap_grow(struct ptr_heap *heap, size_t new_len)
71 {
72 void **new_ptrs;
73
74 if (likely(heap->alloc_len >= new_len))
75 return 0;
76
77 heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1);
78 new_ptrs = calloc(heap->alloc_len, sizeof(void *));
79 if (unlikely(!new_ptrs))
80 return -ENOMEM;
81 if (likely(heap->ptrs))
82 memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *));
83 free(heap->ptrs);
84 heap->ptrs = new_ptrs;
85 return 0;
86 }
87
88 static
89 int heap_set_len(struct ptr_heap *heap, size_t new_len)
90 {
91 int ret;
92
93 ret = heap_grow(heap, new_len);
94 if (unlikely(ret))
95 return ret;
96 heap->len = new_len;
97 return 0;
98 }
99
100 int bt_heap_init(struct ptr_heap *heap, size_t alloc_len,
101 int gt(void *a, void *b))
102 {
103 heap->ptrs = NULL;
104 heap->len = 0;
105 heap->alloc_len = 0;
106 heap->gt = gt;
107 /*
108 * Minimum size allocated is 1 entry to ensure memory allocation
109 * never fails within bt_heap_replace_max.
110 */
111 return heap_grow(heap, max_t(size_t, 1, alloc_len));
112 }
113
114 void bt_heap_free(struct ptr_heap *heap)
115 {
116 free(heap->ptrs);
117 }
118
119 static void heapify(struct ptr_heap *heap, size_t i)
120 {
121 void **ptrs = heap->ptrs;
122 size_t l, r, largest;
123
124 for (;;) {
125 void *tmp;
126
127 l = left(i);
128 r = right(i);
129 if (l < heap->len && heap->gt(ptrs[l], ptrs[i]))
130 largest = l;
131 else
132 largest = i;
133 if (r < heap->len && heap->gt(ptrs[r], ptrs[largest]))
134 largest = r;
135 if (unlikely(largest == i))
136 break;
137 tmp = ptrs[i];
138 ptrs[i] = ptrs[largest];
139 ptrs[largest] = tmp;
140 i = largest;
141 }
142 check_heap(heap);
143 }
144
145 void *bt_heap_replace_max(struct ptr_heap *heap, void *p)
146 {
147 void *res;
148
149 if (unlikely(!heap->len)) {
150 (void) heap_set_len(heap, 1);
151 heap->ptrs[0] = p;
152 check_heap(heap);
153 return NULL;
154 }
155
156 /* Replace the current max and heapify */
157 res = heap->ptrs[0];
158 heap->ptrs[0] = p;
159 heapify(heap, 0);
160 return res;
161 }
162
163 int bt_heap_insert(struct ptr_heap *heap, void *p)
164 {
165 void **ptrs;
166 size_t pos;
167 int ret;
168
169 ret = heap_set_len(heap, heap->len + 1);
170 if (unlikely(ret))
171 return ret;
172 ptrs = heap->ptrs;
173 pos = heap->len - 1;
174 while (pos > 0 && heap->gt(p, ptrs[parent(pos)])) {
175 /* Move parent down until we find the right spot */
176 ptrs[pos] = ptrs[parent(pos)];
177 pos = parent(pos);
178 }
179 ptrs[pos] = p;
180 check_heap(heap);
181 return 0;
182 }
183
184 void *bt_heap_remove(struct ptr_heap *heap)
185 {
186 switch (heap->len) {
187 case 0:
188 return NULL;
189 case 1:
190 (void) heap_set_len(heap, 0);
191 return heap->ptrs[0];
192 }
193 /* Shrink, replace the current max by previous last entry and heapify */
194 heap_set_len(heap, heap->len - 1);
195 /* len changed. previous last entry is at heap->len */
196 return bt_heap_replace_max(heap, heap->ptrs[heap->len]);
197 }
198
199 void *bt_heap_cherrypick(struct ptr_heap *heap, void *p)
200 {
201 size_t pos, len = heap->len;
202
203 for (pos = 0; pos < len; pos++)
204 if (unlikely(heap->ptrs[pos] == p))
205 goto found;
206 return NULL;
207 found:
208 if (unlikely(heap->len == 1)) {
209 (void) heap_set_len(heap, 0);
210 check_heap(heap);
211 return heap->ptrs[0];
212 }
213 /* Replace p with previous last entry and heapify. */
214 heap_set_len(heap, heap->len - 1);
215 /* len changed. previous last entry is at heap->len */
216 heap->ptrs[pos] = heap->ptrs[heap->len];
217 heapify(heap, pos);
218 return p;
219 }
220
221 int bt_heap_copy(struct ptr_heap *dst, struct ptr_heap *src)
222 {
223 int ret;
224
225 ret = bt_heap_init(dst, src->alloc_len, src->gt);
226 if (ret < 0)
227 goto end;
228
229 ret = heap_set_len(dst, src->len);
230 if (ret < 0)
231 goto end;
232
233 memcpy(dst->ptrs, src->ptrs, src->len * sizeof(void *));
234
235 end:
236 return ret;
237 }
This page took 0.034747 seconds and 5 git commands to generate.