API fix: Move callbacks to CTF plugin
[babeltrace.git] / lib / prio_heap / prio_heap.c
CommitLineData
1eb0c69c
MD
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
20#include <babeltrace/prio_heap.h>
96e1cf26 21#include <babeltrace/babeltrace-internal.h>
1eb0c69c
MD
22#include <errno.h>
23#include <stdlib.h>
24#include <string.h>
eacd552c 25#include <assert.h>
1eb0c69c
MD
26
27#ifndef max_t
28#define max_t(type, a, b) \
29 ((type) (a) > (type) (b) ? (type) (a) : (type) (b))
30#endif
31
eacd552c
MD
32#ifdef DEBUG_HEAP
33void check_heap(const struct ptr_heap *heap)
34{
35 size_t i;
36
37 if (!heap->len)
38 return;
39
40 for (i = 1; i < heap->len; i++)
41 assert(!heap->gt(heap->ptrs[i], heap->ptrs[0]));
42}
43#endif
44
557255e5 45static
1eb0c69c
MD
46size_t parent(size_t i)
47{
eacd552c 48 return (i - 1) >> 1;
1eb0c69c
MD
49}
50
51static
52size_t left(size_t i)
53{
eacd552c 54 return (i << 1) + 1;
1eb0c69c
MD
55}
56
57static
58size_t right(size_t i)
59{
eacd552c 60 return (i << 1) + 2;
1eb0c69c
MD
61}
62
557255e5
MD
63/*
64 * Copy of heap->ptrs pointer is invalid after heap_grow.
65 */
1eb0c69c
MD
66static
67int heap_grow(struct ptr_heap *heap, size_t new_len)
68{
69 void **new_ptrs;
70
96e1cf26 71 if (likely(heap->alloc_len >= new_len))
1eb0c69c
MD
72 return 0;
73
74 heap->alloc_len = max_t(size_t, new_len, heap->alloc_len << 1);
75 new_ptrs = calloc(heap->alloc_len, sizeof(void *));
96e1cf26 76 if (unlikely(!new_ptrs))
1eb0c69c 77 return -ENOMEM;
96e1cf26 78 if (likely(heap->ptrs))
1eb0c69c
MD
79 memcpy(new_ptrs, heap->ptrs, heap->len * sizeof(void *));
80 free(heap->ptrs);
81 heap->ptrs = new_ptrs;
82 return 0;
83}
84
85static
86int heap_set_len(struct ptr_heap *heap, size_t new_len)
87{
88 int ret;
89
90 ret = heap_grow(heap, new_len);
96e1cf26 91 if (unlikely(ret))
1eb0c69c
MD
92 return ret;
93 heap->len = new_len;
94 return 0;
95}
96
97int heap_init(struct ptr_heap *heap, size_t alloc_len,
98 int gt(void *a, void *b))
99{
100 heap->ptrs = NULL;
101 heap->len = 0;
102 heap->alloc_len = 0;
103 heap->gt = gt;
104 /*
105 * Minimum size allocated is 1 entry to ensure memory allocation
106 * never fails within heap_replace_max.
107 */
108 return heap_grow(heap, max_t(size_t, 1, alloc_len));
109}
110
111void heap_free(struct ptr_heap *heap)
112{
113 free(heap->ptrs);
114}
115
116static void heapify(struct ptr_heap *heap, size_t i)
117{
118 void **ptrs = heap->ptrs;
119 size_t l, r, largest;
120
121 for (;;) {
eacd552c
MD
122 void *tmp;
123
1eb0c69c
MD
124 l = left(i);
125 r = right(i);
eacd552c 126 if (l < heap->len && heap->gt(ptrs[l], ptrs[i]))
1eb0c69c
MD
127 largest = l;
128 else
129 largest = i;
eacd552c 130 if (r < heap->len && heap->gt(ptrs[r], ptrs[largest]))
1eb0c69c 131 largest = r;
96e1cf26 132 if (unlikely(largest == i))
1eb0c69c 133 break;
eacd552c
MD
134 tmp = ptrs[i];
135 ptrs[i] = ptrs[largest];
136 ptrs[largest] = tmp;
137 i = largest;
1eb0c69c 138 }
eacd552c 139 check_heap(heap);
1eb0c69c
MD
140}
141
142void *heap_replace_max(struct ptr_heap *heap, void *p)
143{
144 void *res;
1eb0c69c 145
96e1cf26 146 if (unlikely(!heap->len)) {
1eb0c69c 147 (void) heap_set_len(heap, 1);
557255e5 148 heap->ptrs[0] = p;
eacd552c 149 check_heap(heap);
1eb0c69c
MD
150 return NULL;
151 }
152
153 /* Replace the current max and heapify */
557255e5
MD
154 res = heap->ptrs[0];
155 heap->ptrs[0] = p;
1eb0c69c
MD
156 heapify(heap, 0);
157 return res;
158}
159
160int heap_insert(struct ptr_heap *heap, void *p)
161{
557255e5
MD
162 void **ptrs;
163 size_t pos;
1eb0c69c
MD
164 int ret;
165
166 ret = heap_set_len(heap, heap->len + 1);
96e1cf26 167 if (unlikely(ret))
1eb0c69c 168 return ret;
557255e5 169 ptrs = heap->ptrs;
557255e5 170 pos = heap->len - 1;
eacd552c
MD
171 while (pos > 0 && heap->gt(p, ptrs[parent(pos)])) {
172 /* Move parent down until we find the right spot */
173 ptrs[pos] = ptrs[parent(pos)];
174 pos = parent(pos);
557255e5 175 }
eacd552c
MD
176 ptrs[pos] = p;
177 check_heap(heap);
1eb0c69c
MD
178 return 0;
179}
180
181void *heap_remove(struct ptr_heap *heap)
182{
1eb0c69c
MD
183 switch (heap->len) {
184 case 0:
185 return NULL;
186 case 1:
187 (void) heap_set_len(heap, 0);
557255e5 188 return heap->ptrs[0];
1eb0c69c
MD
189 }
190 /* Shrink, replace the current max by previous last entry and heapify */
191 heap_set_len(heap, heap->len - 1);
eacd552c
MD
192 /* len changed. previous last entry is at heap->len */
193 return heap_replace_max(heap, heap->ptrs[heap->len]);
1eb0c69c
MD
194}
195
196void *heap_cherrypick(struct ptr_heap *heap, void *p)
197{
1eb0c69c
MD
198 size_t pos, len = heap->len;
199
200 for (pos = 0; pos < len; pos++)
96e1cf26 201 if (unlikely(heap->ptrs[pos] == p))
1eb0c69c
MD
202 goto found;
203 return NULL;
204found:
96e1cf26 205 if (unlikely(heap->len == 1)) {
1eb0c69c 206 (void) heap_set_len(heap, 0);
eacd552c 207 check_heap(heap);
557255e5 208 return heap->ptrs[0];
1eb0c69c
MD
209 }
210 /* Replace p with previous last entry and heapify. */
211 heap_set_len(heap, heap->len - 1);
eacd552c
MD
212 /* len changed. previous last entry is at heap->len */
213 heap->ptrs[pos] = heap->ptrs[heap->len];
1eb0c69c
MD
214 heapify(heap, pos);
215 return p;
216}
This page took 0.031915 seconds and 4 git commands to generate.