Merge branch 'collectd-5.7' into collectd-5.8
[collectd.git] / src / daemon / utils_heap.c
1 /**
2  * collectd - src/utils_heap.c
3  * Copyright (C) 2009       Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include <assert.h>
28 #include <errno.h>
29 #include <pthread.h>
30 #include <stdlib.h>
31
32 #include "utils_heap.h"
33
34 struct c_heap_s {
35   pthread_mutex_t lock;
36   int (*compare)(const void *, const void *);
37
38   void **list;
39   size_t list_len;  /* # entries used */
40   size_t list_size; /* # entries allocated */
41 };
42
43 enum reheap_direction { DIR_UP, DIR_DOWN };
44
45 static void reheap(c_heap_t *h, size_t root, enum reheap_direction dir) {
46   size_t left;
47   size_t right;
48   size_t min;
49   int status;
50
51   /* Calculate the positions of the children */
52   left = (2 * root) + 1;
53   if (left >= h->list_len)
54     left = 0;
55
56   right = (2 * root) + 2;
57   if (right >= h->list_len)
58     right = 0;
59
60   /* Check which one of the children is smaller. */
61   if ((left == 0) && (right == 0))
62     return;
63   else if (left == 0)
64     min = right;
65   else if (right == 0)
66     min = left;
67   else {
68     status = h->compare(h->list[left], h->list[right]);
69     if (status > 0)
70       min = right;
71     else
72       min = left;
73   }
74
75   status = h->compare(h->list[root], h->list[min]);
76   if (status <= 0) {
77     /* We didn't need to change anything, so the rest of the tree should be
78      * okay now. */
79     return;
80   } else /* if (status > 0) */
81   {
82     void *tmp;
83
84     tmp = h->list[root];
85     h->list[root] = h->list[min];
86     h->list[min] = tmp;
87   }
88
89   if ((dir == DIR_UP) && (root == 0))
90     return;
91
92   if (dir == DIR_UP)
93     reheap(h, (root - 1) / 2, dir);
94   else if (dir == DIR_DOWN)
95     reheap(h, min, dir);
96 } /* void reheap */
97
98 c_heap_t *c_heap_create(int (*compare)(const void *, const void *)) {
99   c_heap_t *h;
100
101   if (compare == NULL)
102     return NULL;
103
104   h = calloc(1, sizeof(*h));
105   if (h == NULL)
106     return NULL;
107
108   pthread_mutex_init(&h->lock, /* attr = */ NULL);
109   h->compare = compare;
110
111   h->list = NULL;
112   h->list_len = 0;
113   h->list_size = 0;
114
115   return h;
116 } /* c_heap_t *c_heap_create */
117
118 void c_heap_destroy(c_heap_t *h) {
119   if (h == NULL)
120     return;
121
122   h->list_len = 0;
123   h->list_size = 0;
124   free(h->list);
125   h->list = NULL;
126
127   pthread_mutex_destroy(&h->lock);
128
129   free(h);
130 } /* void c_heap_destroy */
131
132 int c_heap_insert(c_heap_t *h, void *ptr) {
133   size_t index;
134
135   if ((h == NULL) || (ptr == NULL))
136     return -EINVAL;
137
138   pthread_mutex_lock(&h->lock);
139
140   assert(h->list_len <= h->list_size);
141   if (h->list_len == h->list_size) {
142     void **tmp;
143
144     tmp = realloc(h->list, (h->list_size + 16) * sizeof(*h->list));
145     if (tmp == NULL) {
146       pthread_mutex_unlock(&h->lock);
147       return -ENOMEM;
148     }
149
150     h->list = tmp;
151     h->list_size += 16;
152   }
153
154   /* Insert the new node as a leaf. */
155   index = h->list_len;
156   h->list[index] = ptr;
157   h->list_len++;
158
159   /* Reorganize the heap from bottom up. */
160   reheap(h, /* parent of this node = */ (index - 1) / 2, DIR_UP);
161
162   pthread_mutex_unlock(&h->lock);
163   return 0;
164 } /* int c_heap_insert */
165
166 void *c_heap_get_root(c_heap_t *h) {
167   void *ret = NULL;
168
169   if (h == NULL)
170     return NULL;
171
172   pthread_mutex_lock(&h->lock);
173
174   if (h->list_len == 0) {
175     pthread_mutex_unlock(&h->lock);
176     return NULL;
177   } else if (h->list_len == 1) {
178     ret = h->list[0];
179     h->list[0] = NULL;
180     h->list_len = 0;
181   } else /* if (h->list_len > 1) */
182   {
183     ret = h->list[0];
184     h->list[0] = h->list[h->list_len - 1];
185     h->list[h->list_len - 1] = NULL;
186     h->list_len--;
187
188     reheap(h, /* root = */ 0, DIR_DOWN);
189   }
190
191   /* free some memory */
192   if ((h->list_len + 32) < h->list_size) {
193     void **tmp;
194
195     tmp = realloc(h->list, (h->list_len + 16) * sizeof(*h->list));
196     if (tmp != NULL) {
197       h->list = tmp;
198       h->list_size = h->list_len + 16;
199     }
200   }
201
202   pthread_mutex_unlock(&h->lock);
203
204   return ret;
205 } /* void *c_heap_get_root */