Debian Codegraph @

src/OVAL/probes/SEAP/generic/rbt/rbt_i32.c

raw symbols SCIP
1 /*
2 * Copyright 2010 Red Hat Inc., Durham, North Carolina.
3 * All Rights Reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * "Daniel Kopecek" <[email protected]>
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <errno.h>
28 #include <stdlib.h>
29 #include "rbt_common.h"
30 #include "rbt_i32.h"
31
32 static struct rbt_node *rbt_i32_node_alloc(void)
33 {
34 struct rbt_node *n = NULL;
35 #ifndef OS_WINDOWS
36 if (posix_memalign((void **)(void *)(&n), sizeof(void *),
37 sizeof (struct rbt_node) + sizeof (struct rbt_i32_node)) != 0)
38 {
39 abort ();
40 }
41 #else
42 // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx
43 n = _aligned_malloc(sizeof (struct rbt_node) + sizeof (struct rbt_i32_node), sizeof(void *));
44 #endif
45 n->_chld[0] = NULL;
46 n->_chld[1] = NULL;
47
48 return(n);
49 }
50
51 static void rbt_i32_node_free(struct rbt_node *n)
52 {
53 if (n != NULL)
54 {
55 #ifndef OS_WINDOWS
56 free(rbt_node_ptr(n));
57 #else
58 // using free for memory allocated through _aligned_malloc is illegal
59 // rbt_i32.c -> rbt_i32_node_alloc
60 // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx
61 _aligned_free(rbt_node_ptr(n));
62 #endif
63 }
64 }
65
66 rbt_t *rbt_i32_new (void)
67 {
68 return rbt_new (RBT_I32KEY);
69 }
70
71 void rbt_i32_free (rbt_t *rbt)
72 {
73 rbt_free(rbt, NULL);
74 }
75
76 void rbt_i32_free_cb (rbt_t *rbt, void (*callback)(rbt_i32_node_t *))
77 {
78 rbt_free(rbt, (void(*)(void *))callback);
79 }
80
81 /*
82 * Add new entry to the tree. If `coll' is not NULL and there is
83 * already an entry with the same key (i.e. a collision), then
84 * place the old data pointer at `*coll' and update it to the
85 * new data pointer. Otherwise, if `coll' is NULL and there is a
86 * collision, then an error is returned and no modification of
87 * the tree is made.
88 *
89 * @param rbt pointer to the tree
90 * @param key key
91 * @param data data pointer
92 * @param coll where to store the data pointer from an colliding entry
93 */
94 int rbt_i32_add(rbt_t *rbt, int32_t key, void *data, void **coll)
95 {
96 struct rbt_node fake;
97 struct rbt_node *h[4];
98 register uint8_t dvec;
99 register int32_t n_key, u_key;
100
101 u_key = key;
102 rbt_wlock(rbt);
103
104 /*
105 * Fake node
106 */
107 fake._chld[RBT_NODE_SL] = NULL;
108 fake._chld[RBT_NODE_SR] = rbt->root;
109 rbt_node_setcolor(&fake, RBT_NODE_CB);
110
111 /*
112 * Prepare node history stack & direction history vector
113 */
114 h[3] = NULL;
115 h[2] = NULL;
116 h[1] = &fake;
117 h[0] = rbt->root;
118 dvec = RBT_NODE_SR;
119
120 for (;;) {
121 if (rbt_node_ptr(h[0]) == NULL) {
122 /*
123 * Allocate new node aligned to sizeof(void *) bytes.
124 * On most systems, malloc already returns aligned
125 * memory but we want to ensure that its aligned using
126 * posix_memalign(3).
127 */
128 h[0] = rbt_i32_node_alloc ();
129
130 rbt_i32_node(h[0])->key = key;
131 rbt_i32_node(h[0])->data = data;
132
133 /*
134 * Set the new node as the child of the last node we've
135 * visited. The last direction is the lowest bit of the
136 * direction history vector.
137 */
138 rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec & 1], h[0]);
139 rbt_node_setcolor(h[0], RBT_NODE_CR);
140
141 /*
142 * Since we are inserting a new red node, we need to fix
143 * a red violation if the parent of the new node is red.
144 */
145 if (rbt_node_getcolor(h[1]) == RBT_NODE_CR) {
146 rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2) & 1]);
147 }
148
149 /*
150 * Update root node and color it black
151 */
152 rbt->root = fake._chld[RBT_NODE_SR];
153 rbt_node_setcolor(rbt->root, RBT_NODE_CB);
154
155 /*
156 * Update node counter
157 */
158 ++(rbt->size);
159
160 break;
161 } else if (rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[0]) == RBT_NODE_CR &&
162 rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[1]) == RBT_NODE_CR)
163 {
164 /*
165 * Color switch
166 */
167 rbt_node_setcolor(h[0], RBT_NODE_CR);
168 rbt_node_setcolor(rbt_node_ptr(h[0])->_chld[0], RBT_NODE_CB);
169 rbt_node_setcolor(rbt_node_ptr(h[0])->_chld[1], RBT_NODE_CB);
170
171 /*
172 * Fix red violation
173 */
174 if (rbt_node_getcolor(h[1]) == RBT_NODE_CR) {
175 rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2) & 1]);
176 }
177 } else if (rbt_node_getcolor(h[0]) == RBT_NODE_CR &&
178 rbt_node_getcolor(h[1]) == RBT_NODE_CR)
179 {
180 /*
181 * Fix red violation
182 */
183 rbt_redfix(h, dvec, rbt_node_ptr(h[3])->_chld[(dvec >> 2)&1]);
184 }
185
186 n_key = rbt_i32_node(h[0])->key;
187
188 if (u_key < n_key) {
189 rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[RBT_NODE_SL]);
190 dvec = (dvec << 1) | RBT_NODE_SL;
191 } else if (u_key > n_key) {
192 rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[RBT_NODE_SR]);
193 dvec = (dvec << 1) | RBT_NODE_SR;
194 } else {
195 int r;
196
197 if (coll != NULL) {
198 *coll = rbt_i32_node(h[0])->data;
199 rbt_i32_node(h[0])->data = data;
200 r = 0;
201 } else
202 r = 1;
203
204 /*
205 * Update root node and color it black
206 */
207 rbt->root = fake._chld[RBT_NODE_SR];
208 rbt_node_setcolor(rbt->root, RBT_NODE_CB);
209
210 rbt_wunlock(rbt);
211 return (r);
212 }
213 }
214
215 rbt_wunlock(rbt);
216 return (0);
217 }
218
219 void *rbt_i32_rep(rbt_t *rbt, int32_t key, void *data)
220 {
221 return (NULL);
222 }
223
224 int rbt_i32_del(rbt_t *rbt, int32_t key, void **n)
225 {
226 struct rbt_node fake, *save;
227 struct rbt_node *h[4];
228 register uint8_t dvec;
229 register int32_t n_key, u_key;
230
231 save = NULL;
232 u_key = key;
233
234 rbt_wlock(rbt);
235
236 if (rbt->size == 0) {
237 rbt_wunlock(rbt);
238 return (1);
239 }
240
241 if (rbt_node_ptr(rbt->root) == NULL) {
242 return -1;
243 }
244
245 /*
246 * Fake node
247 */
248 fake._chld[RBT_NODE_SL] = NULL;
249 fake._chld[RBT_NODE_SR] = rbt->root;
250 rbt_node_setcolor(&fake, RBT_NODE_CB);
251
252 /*
253 * Prepare node history stack & direction history vector
254 */
255 h[2] = NULL;
256 h[1] = NULL;
257 h[0] = &fake;
258 dvec = RBT_NODE_SR;
259
260 for (;;) {
261 if (rbt_node_ptr(rbt_node_ptr(h[0])->_chld[dvec & 1]) == NULL)
262 break;
263
264 rbt_hpush4(h, rbt_node_ptr(h[0])->_chld[dvec & 1]);
265 n_key = rbt_i32_node(h[0])->key;
266
267 if (u_key < n_key) {
268 dvec = (dvec << 1) | RBT_NODE_SL;
269 } else if (u_key > n_key) {
270 dvec = (dvec << 1) | RBT_NODE_SR;
271 } else {
272 save = rbt_node_ptr(h[0]);
273 dvec = (dvec << 1) | RBT_NODE_SL;
274 }
275
276 if (rbt_node_getcolor(h[0]) == RBT_NODE_CB &&
277 rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[dvec & 1]) == RBT_NODE_CB)
278 {
279 if (rbt_node_getcolor(rbt_node_ptr(h[0])->_chld[!(dvec & 1)]) == RBT_NODE_CR) {
280 switch(dvec & 1) {
281 case RBT_NODE_SR:
282 rbt_node_setptr(rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1],
283 rbt_node_rotate_R(h[0]));
284 break;
285 case RBT_NODE_SL:
286 rbt_node_setptr(rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1],
287 rbt_node_rotate_L(h[0]));
288 break;
289 }
290
291 h[1] = rbt_node_ptr(h[1])->_chld[(dvec >> 1) & 1];
292 } else {
293 register struct rbt_node *s;
294
295 s = rbt_node_ptr(rbt_node_ptr(h[1])->_chld[!((dvec >> 1) & 1)]);
296
297 if (s != NULL) {
298 if (rbt_node_getcolor(s->_chld[0]) == RBT_NODE_CB &&
299 rbt_node_getcolor(s->_chld[1]) == RBT_NODE_CB)
300 {
301 rbt_node_setcolor(h[0], RBT_NODE_CR);
302 rbt_node_setcolor(h[1], RBT_NODE_CB);
303 rbt_node_setcolor(s, RBT_NODE_CR);
304 } else {
305 register uint8_t d = rbt_node_ptr(rbt_node_ptr(h[2])->_chld[RBT_NODE_SR]) == rbt_node_ptr(h[1]);
306
307 if (rbt_node_getcolor(s->_chld[(dvec >> 1) & 1]) == RBT_NODE_CR) {
308 switch((dvec >> 1) & 1) {
309 case RBT_NODE_SR:
310 rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d],
311 rbt_node_rotate_RL(h[1]));
312 break;
313 case RBT_NODE_SL:
314 rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d],
315 rbt_node_rotate_LR(h[1]));
316 break;
317 }
318 } else if (rbt_node_getcolor(s->_chld[!((dvec >> 1) & 1)]) == RBT_NODE_CR) {
319 switch((dvec >> 1) & 1) {
320 case RBT_NODE_SR:
321 rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d],
322 rbt_node_rotate_R(h[1]));
323 break;
324 case RBT_NODE_SL:
325 rbt_node_setptr(rbt_node_ptr(h[2])->_chld[d],
326 rbt_node_rotate_L(h[1]));
327 break;
328 }
329 }
330
331 s = rbt_node_ptr(rbt_node_ptr(h[2])->_chld[d]);
332
333 rbt_node_setcolor(h[0], RBT_NODE_CR);
334 rbt_node_setcolor(s, RBT_NODE_CR);
335 rbt_node_setcolor(s->_chld[0], RBT_NODE_CB);
336 rbt_node_setcolor(s->_chld[1], RBT_NODE_CB);
337 }
338 }
339 }
340 }
341 }
342
343 if (save != NULL) {
344 h[0] = rbt_node_ptr(h[0]);
345 /*
346 * The node color of the node that will be delete is always
347 * red in case the node is not the root node.
348 */
349 if (rbt_node_ptr(fake._chld[RBT_NODE_SR]) != h[0]
350 && rbt_node_getcolor(h[0]) != RBT_NODE_CR) {
351 return -1;
352 }
353 if (n != NULL)
354 *n = rbt_i32_node(save)->data;
355
356 rbt_i32_node(save)->data = rbt_i32_node(h[0])->data;
357 rbt_i32_node(save)->key = rbt_i32_node(h[0])->key;
358
359 dvec = rbt_node_ptr(rbt_node_ptr(h[1])->_chld[1]) == h[0];
360
361 if (rbt_node_ptr(h[0]->_chld[RBT_NODE_SR]) == NULL) {
362 rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec], h[0]->_chld[RBT_NODE_SL]);
363 } else {
364 rbt_node_setptr(rbt_node_ptr(h[1])->_chld[dvec], h[0]->_chld[RBT_NODE_SR]);
365 }
366
367 /*
368 * Free memory used by the deleted node and decrement
369 * the node counter.
370 */
371 rbt_i32_node_free(h[0]);
372 --(rbt->size);
373 }
374
375 /*
376 * Update root node and color it black and unlock the tree.
377 */
378 rbt->root = fake._chld[RBT_NODE_SR];
379 rbt_node_setcolor(rbt->root, RBT_NODE_CB);
380 rbt_wunlock(rbt);
381
382 return (save == NULL ? 1 : 0);
383 }
384
385 int rbt_i32_get(rbt_t *rbt, int32_t key, void **data)
386 {
387 int r;
388 register struct rbt_node *n;
389 register int32_t u_key, n_key;
390
391 u_key = key;
392 r = -1;
393 rbt_rlock(rbt);
394 n = rbt_node_ptr(rbt->root);
395
396 while (n != NULL) {
397 n_key = rbt_i32_node(n)->key;
398
399 if (u_key < n_key)
400 n = rbt_node_ptr(n->_chld[RBT_NODE_SL]);
401 else if (u_key > n_key)
402 n = rbt_node_ptr(n->_chld[RBT_NODE_SR]);
403 else {
404 r = 0;
405 *data = rbt_i32_node(n)->data;
406 break;
407 }
408 }
409
410 rbt_runlock(rbt);
411 return (r);
412 }
413
414 int rbt_i32_walk_preorder(rbt_t *rbt, int (*callback)(rbt_i32_node_t *), rbt_walk_t flags)
415 {
416 errno = ENOSYS;
417 return (-1);
418 }
419
420 int rbt_i32_walk_inorder(rbt_t *rbt, int (*callback)(rbt_i32_node_t *), rbt_walk_t flags)
421 {
422 return rbt_walk_inorder(rbt, (int(*)(void *))callback, flags);
423 }
424
425 int rbt_i32_walk_postorder(rbt_t *rbt, int (*callback)(rbt_i32_node_t *), rbt_walk_t flags)
426 {
427 errno = ENOSYS;
428 return (-1);
429 }
430
431 int rbt_i32_walk_levelorder(rbt_t *rbt, int (*callback)(rbt_i32_node_t *), rbt_walk_t flags)
432 {
433 errno = ENOSYS;
434 return (-1);
435 }
436
437 int rbt_i32_walk(rbt_t *rbt, rbt_walk_t type, int (*callback)(rbt_i32_node_t *))
438 {
439 if (rbt == NULL || callback == NULL) {
440 errno = EINVAL;
441 return -1;
442 }
443
444 switch (type & RBT_WALK_TYPEMASK) {
445 case RBT_WALK_PREORDER: return rbt_i32_walk_preorder(rbt, callback, type);
446 case RBT_WALK_INORDER: return rbt_i32_walk_inorder(rbt, callback, type);
447 case RBT_WALK_POSTORDER: return rbt_i32_walk_postorder(rbt, callback, type);
448 case RBT_WALK_LEVELORDER: return rbt_i32_walk_levelorder(rbt, callback, type);
449 }
450
451 errno = EINVAL;
452 return (-1);
453 }
454
455 size_t rbt_i32_size(rbt_t *rbt)
456 {
457 return rbt_size(rbt);
458 }