2 * collectd - src/utils_heap.c
3 * Copyright (C) 2009 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
32 #include "utils_heap.h"
36 int (*compare)(const void *, const void *);
39 size_t list_len; /* # entries used */
40 size_t list_size; /* # entries allocated */
43 enum reheap_direction { DIR_UP, DIR_DOWN };
45 static void reheap(c_heap_t *h, size_t root, enum reheap_direction dir) {
51 /* Calculate the positions of the children */
52 left = (2 * root) + 1;
53 if (left >= h->list_len)
56 right = (2 * root) + 2;
57 if (right >= h->list_len)
60 /* Check which one of the children is smaller. */
61 if ((left == 0) && (right == 0))
68 status = h->compare(h->list[left], h->list[right]);
75 status = h->compare(h->list[root], h->list[min]);
77 /* We didn't need to change anything, so the rest of the tree should be
80 } else /* if (status > 0) */
85 h->list[root] = h->list[min];
89 if ((dir == DIR_UP) && (root == 0))
93 reheap(h, (root - 1) / 2, dir);
94 else if (dir == DIR_DOWN)
98 c_heap_t *c_heap_create(int (*compare)(const void *, const void *)) {
104 h = calloc(1, sizeof(*h));
108 pthread_mutex_init(&h->lock, /* attr = */ NULL);
109 h->compare = compare;
116 } /* c_heap_t *c_heap_create */
118 void c_heap_destroy(c_heap_t *h) {
127 pthread_mutex_destroy(&h->lock);
130 } /* void c_heap_destroy */
132 int c_heap_insert(c_heap_t *h, void *ptr) {
135 if ((h == NULL) || (ptr == NULL))
138 pthread_mutex_lock(&h->lock);
140 assert(h->list_len <= h->list_size);
141 if (h->list_len == h->list_size) {
144 tmp = realloc(h->list, (h->list_size + 16) * sizeof(*h->list));
146 pthread_mutex_unlock(&h->lock);
154 /* Insert the new node as a leaf. */
156 h->list[index] = ptr;
159 /* Reorganize the heap from bottom up. */
160 reheap(h, /* parent of this node = */ (index - 1) / 2, DIR_UP);
162 pthread_mutex_unlock(&h->lock);
164 } /* int c_heap_insert */
166 void *c_heap_get_root(c_heap_t *h) {
172 pthread_mutex_lock(&h->lock);
174 if (h->list_len == 0) {
175 pthread_mutex_unlock(&h->lock);
177 } else if (h->list_len == 1) {
181 } else /* if (h->list_len > 1) */
184 h->list[0] = h->list[h->list_len - 1];
185 h->list[h->list_len - 1] = NULL;
188 reheap(h, /* root = */ 0, DIR_DOWN);
191 /* free some memory */
192 if ((h->list_len + 32) < h->list_size) {
195 tmp = realloc(h->list, (h->list_len + 16) * sizeof(*h->list));
198 h->list_size = h->list_len + 16;
202 pthread_mutex_unlock(&h->lock);
205 } /* void *c_heap_get_root */