| 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 <stdint.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <errno.h> |
| 30 | |
| 31 | #include "rbt_common.h" |
| 32 | |
| 33 | rbt_t *rbt_new(rbt_type_t type) |
| 34 | { |
| 35 | rbt_t *rbt; |
| 36 | |
| 37 | switch (type) { |
| 38 | case RBT_GENKEY: |
| 39 | case RBT_STRKEY: |
| 40 | case RBT_I32KEY: |
| 41 | case RBT_I64KEY: |
| 42 | break; |
| 43 | default: |
| 44 | errno = EINVAL; |
| 45 | return (NULL); |
| 46 | } |
| 47 | |
| 48 | rbt = malloc (sizeof (rbt_t)); |
| 49 | if (rbt == NULL) |
| 50 | return NULL; |
| 51 | |
| 52 | rbt->type = type; |
| 53 | rbt->root = NULL; |
| 54 | rbt->size = 0; |
| 55 | |
| 56 | #if defined(RBT_IMPLICIT_LOCKING) |
| 57 | pthread_rwlock_init (&rbt->lock, NULL); |
| 58 | #endif |
| 59 | return (rbt); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Free resources used to hold information about the tree. This |
| 64 | * function does not free memory pointed to by the data pointer |
| 65 | * in a node - you have to free this memory by walking thru the |
| 66 | * tree or use the callback version of rbt_free for a specific |
| 67 | * tree type. |
| 68 | * @param rbt tree pointer |
| 69 | */ |
| 70 | void rbt_free(rbt_t *rbt, void (*callback)(void *)) |
| 71 | { |
| 72 | struct rbt_node *stack[48], *n; |
| 73 | register uint8_t depth; |
| 74 | |
| 75 | rbt_wlock(rbt); |
| 76 | |
| 77 | depth = 0; |
| 78 | n = rbt_node_ptr(rbt->root); |
| 79 | |
| 80 | if (n != NULL) { |
| 81 | rbt_walk_push(n); |
| 82 | |
| 83 | while(depth > 0) { |
| 84 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SL]); |
| 85 | |
| 86 | if (n != NULL) |
| 87 | rbt_walk_push(n); |
| 88 | else { |
| 89 | found_in: |
| 90 | if (callback != NULL) |
| 91 | callback((void *)&(rbt_walk_top()->_node)); |
| 92 | |
| 93 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SR]); |
| 94 | #ifndef OS_WINDOWS |
| 95 | free(rbt_walk_top()); |
| 96 | #else |
| 97 | // using free for memory allocated through _aligned_malloc is illegal |
| 98 | // rbt_str.c -> rbt_str_node_alloc |
| 99 | // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx |
| 100 | _aligned_free(rbt_walk_top()); |
| 101 | #endif |
| 102 | |
| 103 | if (n != NULL) |
| 104 | rbt_walk_top() = n; |
| 105 | else { |
| 106 | if (--depth > 0) |
| 107 | goto found_in; |
| 108 | else |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | rbt_wunlock(rbt); |
| 115 | |
| 116 | rbt->root = NULL; |
| 117 | rbt->size = 0; |
| 118 | rbt->type = -1; |
| 119 | |
| 120 | #if defined(RBT_IMPLICIT_LOCKING) |
| 121 | pthread_rwlock_destroy (&rbt->lock); |
| 122 | #endif |
| 123 | free (rbt); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | void rbt_free2(rbt_t *rbt, void (*callback)(void *, void *), void *user) |
| 128 | { |
| 129 | struct rbt_node *stack[48], *n; |
| 130 | register uint8_t depth; |
| 131 | |
| 132 | rbt_wlock(rbt); |
| 133 | |
| 134 | depth = 0; |
| 135 | n = rbt_node_ptr(rbt->root); |
| 136 | |
| 137 | if (n != NULL) { |
| 138 | rbt_walk_push(n); |
| 139 | |
| 140 | while(depth > 0) { |
| 141 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SL]); |
| 142 | |
| 143 | if (n != NULL) |
| 144 | rbt_walk_push(n); |
| 145 | else { |
| 146 | found_in: |
| 147 | if (callback != NULL) |
| 148 | callback((void *)&(rbt_walk_top()->_node), user); |
| 149 | |
| 150 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SR]); |
| 151 | #ifndef OS_WINDOWS |
| 152 | free(rbt_walk_top()); |
| 153 | #else |
| 154 | // using free for memory allocated through _aligned_malloc is illegal |
| 155 | // rbt_str.c -> rbt_str_node_alloc |
| 156 | // https://msdn.microsoft.com/en-us/library/8z34s9c6.aspx |
| 157 | _aligned_free(rbt_walk_top()); |
| 158 | #endif |
| 159 | |
| 160 | if (n != NULL) |
| 161 | rbt_walk_top() = n; |
| 162 | else { |
| 163 | if (--depth > 0) |
| 164 | goto found_in; |
| 165 | else |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | rbt_wunlock(rbt); |
| 172 | |
| 173 | rbt->root = NULL; |
| 174 | rbt->size = 0; |
| 175 | rbt->type = -1; |
| 176 | |
| 177 | #if defined(RBT_IMPLICIT_LOCKING) |
| 178 | pthread_rwlock_destroy (&rbt->lock); |
| 179 | #endif |
| 180 | free (rbt); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | #if defined(RBT_IMPLICIT_LOCKING) |
| 185 | int rbt_rlock(rbt_t *rbt) |
| 186 | { |
| 187 | if (rbt == NULL) { |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | if (pthread_rwlock_rdlock(&rbt->lock) != 0) |
| 192 | return (-1); |
| 193 | else |
| 194 | return (0); |
| 195 | } |
| 196 | |
| 197 | void rbt_runlock(rbt_t *rbt) |
| 198 | { |
| 199 | if (rbt == NULL) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | if (pthread_rwlock_unlock(&rbt->lock) != 0) { |
| 204 | /* |
| 205 | * Unlock failed - we abort() here so as |
| 206 | * not to cause a deadlock |
| 207 | */ |
| 208 | abort (); |
| 209 | } |
| 210 | |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | int rbt_wlock(rbt_t *rbt) |
| 215 | { |
| 216 | if (rbt == NULL) { |
| 217 | return -1; |
| 218 | } |
| 219 | if (pthread_rwlock_wrlock(&rbt->lock) != 0) |
| 220 | return (-1); |
| 221 | else |
| 222 | return (0); |
| 223 | } |
| 224 | |
| 225 | void rbt_wunlock(rbt_t *rbt) |
| 226 | { |
| 227 | if (rbt == NULL) { |
| 228 | return; |
| 229 | } |
| 230 | rbt_runlock(rbt); |
| 231 | return; |
| 232 | } |
| 233 | #else |
| 234 | int rbt_rlock(rbt_t *rbt) { return(0); } |
| 235 | void rbt_runlock(rbt_t *rbt) { return; } |
| 236 | int rbt_wlock(rbt_t *rbt) { return(0); } |
| 237 | void rbt_wunlock(rbt_t *rbt) { return; } |
| 238 | #endif /* RBT_IMPLICIT_LOCKING */ |
| 239 | |
| 240 | /* |
| 241 | * Single rotation to the left. Used for fixing a black violation. |
| 242 | * |
| 243 | * == Assumptions == |
| 244 | * - input node is black |
| 245 | * - output node is black |
| 246 | * - child nodes of the new subtree node (the output node) are red |
| 247 | */ |
| 248 | struct rbt_node *rbt_node_rotate_L(struct rbt_node *r) |
| 249 | { |
| 250 | register struct rbt_node *n, *o; |
| 251 | |
| 252 | o = rbt_node_ptr(r); |
| 253 | |
| 254 | /* |
| 255 | * New subtree root node `n' is the left child node of |
| 256 | * the old subtree root node `r'. This node has the |
| 257 | * color black. |
| 258 | */ |
| 259 | n = rbt_node_ptr(o->_chld[RBT_NODE_SR]); |
| 260 | |
| 261 | /* |
| 262 | * Now the left child has to be updated so that it |
| 263 | * points to the right child of the old left child |
| 264 | * node. The old left child node pointer is stored |
| 265 | * in `n'. |
| 266 | */ |
| 267 | o->_chld[RBT_NODE_SR] = n->_chld[RBT_NODE_SL]; |
| 268 | n->_chld[RBT_NODE_SL] = o; |
| 269 | |
| 270 | /* |
| 271 | * Make sure that nodes have proper colors set. |
| 272 | */ |
| 273 | rbt_node_setcolor(n, RBT_NODE_CB); |
| 274 | rbt_node_setcolor(o, RBT_NODE_CR); |
| 275 | |
| 276 | return (struct rbt_node *)((uintptr_t)(n)|((uintptr_t)(r)&1)); |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Single rotation to the right. Used for fixing a black violation. |
| 281 | * No-recoloring needed here. |
| 282 | */ |
| 283 | struct rbt_node *rbt_node_rotate_R(struct rbt_node *r) |
| 284 | { |
| 285 | register struct rbt_node *n, *o; |
| 286 | |
| 287 | o = rbt_node_ptr(r); |
| 288 | |
| 289 | /* |
| 290 | * New subtree root node `n' is the left child node of |
| 291 | * the old subtree root node `r'. This node has the |
| 292 | * color black. |
| 293 | */ |
| 294 | n = rbt_node_ptr(o->_chld[RBT_NODE_SL]); |
| 295 | |
| 296 | /* |
| 297 | * Now the right child has to be updated so that it |
| 298 | * points to the left child of the old right child |
| 299 | * node. The old right child node pointer is stored |
| 300 | * in `n'. |
| 301 | */ |
| 302 | o->_chld[RBT_NODE_SL] = n->_chld[RBT_NODE_SR]; |
| 303 | n->_chld[RBT_NODE_SR] = o; |
| 304 | |
| 305 | /* |
| 306 | * Make sure that nodes have proper colors set. |
| 307 | */ |
| 308 | rbt_node_setcolor(n, RBT_NODE_CB); |
| 309 | rbt_node_setcolor(o, RBT_NODE_CR); |
| 310 | |
| 311 | return (struct rbt_node *)((uintptr_t)(n)|((uintptr_t)(r)&1)); |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Double rotation to the left and then to the right. Used for fixing a red violation. |
| 316 | */ |
| 317 | struct rbt_node *rbt_node_rotate_LR(struct rbt_node *r) |
| 318 | { |
| 319 | register struct rbt_node *n, *o; |
| 320 | |
| 321 | o = rbt_node_ptr(r); |
| 322 | n = rbt_node_ptr(rbt_node_ptr(o->_chld[RBT_NODE_SR])->_chld[RBT_NODE_SL]); |
| 323 | |
| 324 | rbt_node_setptr(rbt_node_ptr(o->_chld[RBT_NODE_SR])->_chld[RBT_NODE_SL], n->_chld[RBT_NODE_SR]); |
| 325 | |
| 326 | n->_chld[RBT_NODE_SR] = o->_chld[RBT_NODE_SR]; |
| 327 | o->_chld[RBT_NODE_SR] = n->_chld[RBT_NODE_SL]; |
| 328 | n->_chld[RBT_NODE_SL] = o; |
| 329 | |
| 330 | rbt_node_setcolor(n->_chld[RBT_NODE_SR], RBT_NODE_CR); |
| 331 | rbt_node_setcolor(n->_chld[RBT_NODE_SL], RBT_NODE_CR); |
| 332 | rbt_node_setcolor(n, RBT_NODE_CB); |
| 333 | |
| 334 | return (struct rbt_node *)((uintptr_t)(n)|((uintptr_t)(r)&1)); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Double rotation to the right and then to the left. Used for fixing a red violation. |
| 339 | */ |
| 340 | struct rbt_node *rbt_node_rotate_RL(struct rbt_node *r) |
| 341 | { |
| 342 | register struct rbt_node *n, *o; |
| 343 | |
| 344 | o = rbt_node_ptr(r); |
| 345 | n = rbt_node_ptr(rbt_node_ptr(o->_chld[RBT_NODE_SL])->_chld[RBT_NODE_SR]); |
| 346 | |
| 347 | rbt_node_ptr(o->_chld[RBT_NODE_SL])->_chld[RBT_NODE_SR] = n->_chld[RBT_NODE_SL]; |
| 348 | |
| 349 | n->_chld[RBT_NODE_SL] = o->_chld[RBT_NODE_SL]; |
| 350 | rbt_node_setptr(o->_chld[RBT_NODE_SL], n->_chld[RBT_NODE_SR]); |
| 351 | n->_chld[RBT_NODE_SR] = o; |
| 352 | |
| 353 | rbt_node_setcolor(n->_chld[RBT_NODE_SR], RBT_NODE_CR); |
| 354 | rbt_node_setcolor(n->_chld[RBT_NODE_SL], RBT_NODE_CR); |
| 355 | rbt_node_setcolor(n, RBT_NODE_CB); |
| 356 | |
| 357 | return (struct rbt_node *)((uintptr_t)(n)|((uintptr_t)(r)&1)); |
| 358 | } |
| 359 | |
| 360 | size_t rbt_size(rbt_t *rbt) |
| 361 | { |
| 362 | size_t size; |
| 363 | |
| 364 | rbt_rlock(rbt); |
| 365 | size = rbt->size; |
| 366 | rbt_runlock(rbt); |
| 367 | |
| 368 | return (size); |
| 369 | } |
| 370 | |
| 371 | int rbt_walk_preorder(rbt_t *rbt, int (*callback)(void *), rbt_walk_t flags) |
| 372 | { |
| 373 | return(-1); |
| 374 | } |
| 375 | |
| 376 | int rbt_walk_inorder(rbt_t *rbt, int (*callback)(void *), rbt_walk_t flags) |
| 377 | { |
| 378 | struct rbt_node *stack[48], *n; |
| 379 | register uint8_t depth, delta; |
| 380 | register int r; |
| 381 | |
| 382 | if (rbt == NULL) { |
| 383 | return -1; |
| 384 | } |
| 385 | |
| 386 | if (flags & RBT_WALK_RAWNODE) |
| 387 | delta = 0; |
| 388 | else { |
| 389 | delta = sizeof(void *) * 2; |
| 390 | } |
| 391 | |
| 392 | depth = 0; |
| 393 | n = rbt_node_ptr(rbt->root); |
| 394 | |
| 395 | if (n != NULL) |
| 396 | rbt_walk_push(n); |
| 397 | else |
| 398 | return (0); |
| 399 | |
| 400 | while(depth > 0) { |
| 401 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SL]); |
| 402 | |
| 403 | if (n != NULL) |
| 404 | rbt_walk_push(n); |
| 405 | else { |
| 406 | found_in: |
| 407 | r = callback((void *)((uintptr_t)(rbt_walk_top()) + delta)); |
| 408 | |
| 409 | if (r != 0) |
| 410 | return (r); |
| 411 | |
| 412 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SR]); |
| 413 | |
| 414 | if (n != NULL) |
| 415 | rbt_walk_top() = n; |
| 416 | else { |
| 417 | if (--depth > 0) |
| 418 | goto found_in; |
| 419 | else |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return(0); |
| 426 | } |
| 427 | |
| 428 | int rbt_walk_inorder2(rbt_t *rbt, int (*callback)(void *, void *), void *user, rbt_walk_t flags) |
| 429 | { |
| 430 | struct rbt_node *stack[48], *n; |
| 431 | register uint8_t depth, delta; |
| 432 | register int r; |
| 433 | |
| 434 | if (rbt == NULL) { |
| 435 | return -1; |
| 436 | } |
| 437 | |
| 438 | if (flags & RBT_WALK_RAWNODE) |
| 439 | delta = 0; |
| 440 | else { |
| 441 | delta = sizeof(void *) * 2; |
| 442 | } |
| 443 | |
| 444 | depth = 0; |
| 445 | n = rbt_node_ptr(rbt->root); |
| 446 | |
| 447 | if (n != NULL) |
| 448 | rbt_walk_push(n); |
| 449 | else |
| 450 | return (0); |
| 451 | |
| 452 | while(depth > 0) { |
| 453 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SL]); |
| 454 | |
| 455 | if (n != NULL) |
| 456 | rbt_walk_push(n); |
| 457 | else { |
| 458 | found_in: |
| 459 | r = callback((void *)((uintptr_t)(rbt_walk_top()) + delta), user); |
| 460 | |
| 461 | if (r != 0) |
| 462 | return (r); |
| 463 | |
| 464 | n = rbt_node_ptr(rbt_walk_top()->_chld[RBT_NODE_SR]); |
| 465 | |
| 466 | if (n != NULL) |
| 467 | rbt_walk_top() = n; |
| 468 | else { |
| 469 | if (--depth > 0) |
| 470 | goto found_in; |
| 471 | else |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | return(0); |
| 478 | } |
| 479 | |
| 480 | int rbt_walk_postorder(rbt_t *rbt, int (*callback)(void *), rbt_walk_t flags) |
| 481 | { |
| 482 | return(-1); |
| 483 | } |
| 484 | |
| 485 | #ifndef HAVE_POSIX_MEMALIGN |
| 486 | # ifdef HAVE_MEMALIGN |
| 487 | |
| 488 | /* Implementing posix_memalign using memalign */ |
| 489 | int posix_memalign(void **memptr, size_t alignment, size_t size) |
| 490 | { |
| 491 | if ((alignment % sizeof(void *)) != 0) |
| 492 | { |
| 493 | return EINVAL; |
| 494 | } |
| 495 | |
| 496 | *memptr = memalign(alignment, size); |
| 497 | |
| 498 | /* The spec for posix_memalign requires that alignment be a power |
| 499 | of 2. Memalign checks for that case, and returns NULL on failure. */ |
| 500 | if (*memptr == NULL) |
| 501 | { |
| 502 | /* posix_memalign must return an appropriate error code */ |
| 503 | return EINVAL; |
| 504 | } |
| 505 | return 0; |
| 506 | } |
| 507 | # endif /* HAVE_MEMALIGN */ |
| 508 | #endif /* HAVE_POSIX_MEMALIGN */ |