1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
|
/*
* Copyright (C) 2011 Red Hat, Inc.
*
* Author: Angus Salkeld <asalkeld@redhat.com>
*
* This file is part of libqb.
*
* libqb is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* libqb is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libqb. If not, see <http://www.gnu.org/licenses/>.
*/
#include <os_base.h>
#include <assert.h>
#include <qb/qbdefs.h>
#include <qb/qbmap.h>
#include "map_int.h"
#define SKIPLIST_LEVEL_MAX 8
#define SKIPLIST_LEVEL_MIN 0
/* The amount of possible levels */
#define SKIPLIST_LEVEL_COUNT (SKIPLIST_LEVEL_MAX - SKIPLIST_LEVEL_MIN + 1)
struct skiplist_iter {
struct qb_map_iter i;
struct skiplist_node *n;
};
struct skiplist_node {
const char *key;
void *value;
/* special meaning when lower than SKIPLIST_LEVEL_MIN:
indication that @skiplist_node_destroy() needs to skip
disposing node->forward (unless it is the termination
of the whole list) */
int8_t level;
uint32_t refcount;
struct qb_list_head notifier_head;
/* An array of @level + 1 node pointers */
struct skiplist_node **forward;
};
struct skiplist {
struct qb_map map;
size_t length;
/* special meaning when lower than SKIPLIST_LEVEL_MIN:
indication that @skiplist_node_destroy() is the terminating
one (triggered with @skiplist_destroy()), therefore node->forward
needs to be free'd unconditionally */
int8_t level;
struct skiplist_node *header;
};
/* An array of nodes that need to be updated after an insert or delete operation
*/
typedef struct skiplist_node *skiplist_update_t[SKIPLIST_LEVEL_COUNT];
static int8_t
skiplist_level_generate(void)
{
/* This constant is found by 1 / P, where P = 0.25. */
enum { P_INVERSE = 4 };
/* The original algorithm's random number is in the range [0, 1), so
* max M = 1. Its ceiling C = M * P = 1 * P = P.
*
* Our random number is in the range [0, UINT16_MAX], so M = UINT16_MAX. Therefore,
* C = UINT16_MAX * P = UINT16_MAX / P_INVERSE.
*/
enum { P_CEIL = UINT16_MAX / P_INVERSE };
int8_t level = SKIPLIST_LEVEL_MIN;
while ((uint16_t) (random()) < P_CEIL)
level++;
if (level < SKIPLIST_LEVEL_MAX)
return level;
return SKIPLIST_LEVEL_MAX;
}
static struct skiplist_node *
skiplist_node_next(const struct skiplist_node *node)
{
const struct skiplist_node *n = node;
do {
n = n->forward[SKIPLIST_LEVEL_MIN];
} while (n && n->refcount == 0);
return (struct skiplist_node *)n;
}
/*
* Create a new level @level node with value @value. The node should eventually
* be destroyed with @skiplist_node_destroy.
*
* return: a new node on success and NULL otherwise.
*/
static struct skiplist_node *
skiplist_node_new(const int8_t level, const char *key, const void *value)
{
struct skiplist_node *new_node = (struct skiplist_node *)
(malloc(sizeof(struct skiplist_node)));
if (!new_node)
return NULL;
new_node->value = (void *)value;
new_node->key = key;
new_node->level = level;
new_node->refcount = 1;
qb_list_init(&new_node->notifier_head);
/* A level 0 node still needs to hold 1 forward pointer, etc.;
instead of "level + 1", we need to capture whole possible
width because of forward-member-resilience-upon-entry-removal
arrangment based on takeover-and-repoint. */
new_node->forward = (struct skiplist_node **)
(calloc(SKIPLIST_LEVEL_MAX + 1, sizeof(struct skiplist_node *)));
if (new_node->forward == NULL) {
free(new_node);
return NULL;
}
return new_node;
}
static struct skiplist_node *
skiplist_header_node_new(void)
{
return skiplist_node_new(SKIPLIST_LEVEL_MAX, NULL, NULL);
}
/* An operation to perform after comparing a user value or search with a
* node's value
*/
typedef enum {
OP_GOTO_NEXT_LEVEL,
OP_GOTO_NEXT_NODE,
OP_FINISH,
} op_t;
static op_t
op_search(const struct skiplist *list,
const struct skiplist_node *fwd_node, const void *search)
{
int32_t cmp;
if (!fwd_node)
return OP_GOTO_NEXT_LEVEL;
cmp = strcmp(fwd_node->key, search);
if (cmp < 0) {
return OP_GOTO_NEXT_NODE;
} else if (cmp == 0) {
return OP_FINISH;
}
return OP_GOTO_NEXT_LEVEL;
}
static struct skiplist_node *
skiplist_lookup(struct skiplist *list, const char *key)
{
struct skiplist_node *cur_node = list->header;
int8_t level = list->level;
while (level >= SKIPLIST_LEVEL_MIN) {
struct skiplist_node *fwd_node = cur_node->forward[(size_t)level];
switch (op_search(list, fwd_node, key)) {
case OP_FINISH:
return fwd_node;
case OP_GOTO_NEXT_NODE:
cur_node = fwd_node;
break;
case OP_GOTO_NEXT_LEVEL:
level--;
}
}
return NULL;
}
static void
skiplist_notify(struct skiplist *l, struct skiplist_node *n,
uint32_t event, char *key, void *old_value, void *value)
{
struct qb_list_head *list;
struct qb_map_notifier *tn;
/* node callbacks
*/
qb_list_for_each(list, &n->notifier_head) {
tn = qb_list_entry(list, struct qb_map_notifier, list);
if (tn->events & event) {
tn->callback(event, key, old_value, value,
tn->user_data);
}
}
/* global callbacks
*/
qb_list_for_each(list, &l->header->notifier_head) {
tn = qb_list_entry(list, struct qb_map_notifier, list);
if (tn->events & event) {
tn->callback(event, key, old_value, value,
tn->user_data);
}
if (((event & QB_MAP_NOTIFY_DELETED) ||
(event & QB_MAP_NOTIFY_REPLACED)) &&
(tn->events & QB_MAP_NOTIFY_FREE)) {
tn->callback(QB_MAP_NOTIFY_FREE, (char *)key,
old_value, value, tn->user_data);
}
}
}
static void
skiplist_node_destroy(struct skiplist_node *node, struct skiplist *list)
{
struct qb_list_head *pos;
struct qb_list_head *next;
struct qb_map_notifier *tn;
skiplist_notify(list, node,
QB_MAP_NOTIFY_DELETED,
(char *)node->key, node->value, NULL);
qb_list_for_each_safe(pos, next, &node->notifier_head) {
tn = qb_list_entry(pos, struct qb_map_notifier, list);
qb_list_del(&tn->list);
free(tn);
}
if (node->level >= SKIPLIST_LEVEL_MIN
|| list->level < SKIPLIST_LEVEL_MIN) {
free(node->forward);
}
free(node);
}
static void
skiplist_node_deref(struct skiplist_node *node, struct skiplist *list)
{
node->refcount--;
if (node->refcount == 0) {
skiplist_node_destroy(node, list);
}
}
static int32_t
skiplist_notify_add(qb_map_t * m, const char *key,
qb_map_notify_fn fn, int32_t events, void *user_data)
{
struct skiplist *t = (struct skiplist *)m;
struct qb_map_notifier *f;
struct skiplist_node *n;
struct qb_list_head *list;
int add_to_tail = QB_FALSE;
if (key) {
n = skiplist_lookup(t, key);
} else {
n = t->header;
}
if (events & QB_MAP_NOTIFY_FREE) {
add_to_tail = QB_TRUE;
}
if (n) {
qb_list_for_each(list, &n->notifier_head) {
f = qb_list_entry(list, struct qb_map_notifier, list);
if (events & QB_MAP_NOTIFY_FREE &&
f->events == events) {
/* only one free notifier */
return -EEXIST;
}
if (f->events == events &&
f->callback == fn &&
f->user_data == user_data) {
return -EEXIST;
}
}
f = malloc(sizeof(struct qb_map_notifier));
if (f == NULL) {
return -errno;
}
f->events = events;
f->user_data = user_data;
f->callback = fn;
qb_list_init(&f->list);
if (add_to_tail) {
qb_list_add_tail(&f->list, &n->notifier_head);
} else {
qb_list_add(&f->list, &n->notifier_head);
}
return 0;
}
return -EINVAL;
}
static int32_t
skiplist_notify_del(qb_map_t * m, const char *key,
qb_map_notify_fn fn, int32_t events,
int32_t cmp_userdata, void *user_data)
{
struct skiplist *t = (struct skiplist *)m;
struct skiplist_node *n;
struct qb_map_notifier *f;
struct qb_list_head *head = NULL;
struct qb_list_head *list;
struct qb_list_head *next;
int32_t found = QB_FALSE;
if (key) {
n = skiplist_lookup(t, key);
if (n) {
head = &n->notifier_head;
}
} else {
head = &t->header->notifier_head;
}
if (head == NULL) {
return -ENOENT;
}
qb_list_for_each_safe(list, next, head) {
f = qb_list_entry(list, struct qb_map_notifier, list);
if (f->events == events && f->callback == fn) {
if (cmp_userdata && (f->user_data == user_data)) {
found = QB_TRUE;
qb_list_del(&f->list);
free(f);
} else if (!cmp_userdata) {
found = QB_TRUE;
qb_list_del(&f->list);
free(f);
}
}
}
if (found) {
return 0;
} else {
return -ENOENT;
}
}
static void
skiplist_destroy(struct qb_map *map)
{
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *cur_node;
struct skiplist_node *fwd_node;
list->level = SKIPLIST_LEVEL_MIN - 1; /* indicate teardown */
for (cur_node = skiplist_node_next(list->header);
cur_node; cur_node = fwd_node) {
fwd_node = skiplist_node_next(cur_node);
skiplist_node_destroy(cur_node, list);
}
skiplist_node_destroy(list->header, list);
free(list);
}
static void
skiplist_put(struct qb_map *map, const char *key, const void *value)
{
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *new_node;
int8_t level = list->level;
skiplist_update_t update;
int8_t update_level;
int8_t new_node_level;
struct skiplist_node *cur_node = list->header;
char *old_k;
char *old_v;
while ((update_level = level) >= SKIPLIST_LEVEL_MIN) {
struct skiplist_node *fwd_node = cur_node->forward[(size_t)level];
switch (op_search(list, fwd_node, key)) {
case OP_FINISH:
old_k = (char *)fwd_node->key;
old_v = (char *)fwd_node->value;
fwd_node->value = (void *)value;
fwd_node->key = (void *)key;
skiplist_notify(list, fwd_node,
QB_MAP_NOTIFY_REPLACED,
old_k, old_v, fwd_node->value);
return;
case OP_GOTO_NEXT_NODE:
cur_node = fwd_node;
break;
case OP_GOTO_NEXT_LEVEL:
level--;
}
update[(size_t)update_level] = cur_node;
}
new_node_level = skiplist_level_generate();
if (new_node_level > list->level) {
for (level = list->level + 1; level <= new_node_level; level++)
update[(size_t)level] = list->header;
list->level = new_node_level;
}
new_node = skiplist_node_new(new_node_level, key, value);
assert(new_node != NULL);
skiplist_notify(list, new_node,
QB_MAP_NOTIFY_INSERTED,
(char*)new_node->key, NULL, new_node->value);
/* Drop @new_node into @list. */
for (level = SKIPLIST_LEVEL_MIN; level <= new_node_level; level++) {
new_node->forward[(size_t)level] = update[(size_t)level]->forward[(size_t)level];
update[(size_t)level]->forward[(size_t)level] = new_node;
}
list->length++;
}
static int32_t
skiplist_rm(struct qb_map *map, const char *key)
{
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *found_node;
struct skiplist_node *cur_node = list->header;
int8_t level = list->level;
int8_t update_level;
skiplist_update_t update;
while ((update_level = level) >= SKIPLIST_LEVEL_MIN) {
struct skiplist_node *fwd_node = cur_node->forward[(size_t)level];
switch (op_search(list, fwd_node, key)) {
case OP_GOTO_NEXT_NODE:
cur_node = fwd_node;
break;
case OP_GOTO_NEXT_LEVEL:
default:
level--;
break;
}
update[(size_t)update_level] = cur_node;
}
/* The immediate forward node should be the matching node... */
found_node = skiplist_node_next(cur_node);
/* ...unless we're at the end of the list or the value doesn't exist. */
if (!found_node || strcmp(found_node->key, key) != 0) {
return QB_FALSE;
}
/* Splice found_node out of list. */
for (level = SKIPLIST_LEVEL_MIN; level <= list->level; level++) {
if (update[level]->forward[level] == found_node) {
update[(size_t)level]->forward[(size_t)level] =
found_node->forward[(size_t)level];
}
}
/* If @found_node is referenced more than once, it means that it is
currently positioned with one or more iterators, therefore it's
likely that @qb_map_iter_next() will be called at least once so
as to progress pass this @found_node. The problem is, original
@found_node->forward may have become referencing successfully
destroyed original successor by the time the progress of such
iterator(s) resumes, possibly causing use-after-free in
@skiplist_node_next().
To solve this, we will grab @cur_node->forward, which has just
been updated accordingly in the above statement, copying it
to @found_node->forward, and repointing @cur_node->forward to
point to @found_node's copy (freeing its original list first).
To prevent freeing the pointed memory behind @cur_node's
back from the @found_node's context, we use the fact that the
iterator can only advance to the next node, without re-examination
of the current one, hence we can afford to abuse @found_node->value
as a flag field when set to our private "special" value that under
no normal circumstance can appear (for being link-time singleton).
In addition, we have to special-case the beginning of the list
(header) preceding @found_node, which can be distinguished with
NULL being used as a key (second allowing condition below). */
if (found_node->refcount > 1 || cur_node->key == NULL) {
for (level = SKIPLIST_LEVEL_MIN; level <= found_node->level; level++) {
found_node->forward[level] = cur_node->forward[level];
}
found_node->level = SKIPLIST_LEVEL_MIN - 1; /* no "forward" drop */
free(cur_node->forward);
cur_node->forward = found_node->forward;
}
skiplist_node_deref(found_node, list);
/* Remove unused levels from @list -- stop removing levels as soon as a
* used level is found. Unused levels can occur if @found_node had the
* highest level.
*/
for (level = list->level; level >= SKIPLIST_LEVEL_MIN; level--) {
if (list->header->forward[(size_t)level])
break;
list->level--;
}
list->length--;
return QB_TRUE;
}
static void *
skiplist_get(struct qb_map *map, const char *key)
{
struct skiplist *list = (struct skiplist *)map;
struct skiplist_node *n = skiplist_lookup(list, key);
if (n) {
return n->value;
}
return NULL;
}
static qb_map_iter_t *
skiplist_iter_create(struct qb_map *map, const char *prefix)
{
struct skiplist_iter *i = malloc(sizeof(struct skiplist_iter));
struct skiplist *list = (struct skiplist *)map;
if (i == NULL) {
return NULL;
}
i->i.m = map;
i->n = list->header;
i->n->refcount++;
return (qb_map_iter_t *) i;
}
static const char *
skiplist_iter_next(qb_map_iter_t * i, void **value)
{
struct skiplist_iter *si = (struct skiplist_iter *)i;
struct skiplist_node *p = si->n;
if (p == NULL) {
return NULL;
}
si->n = skiplist_node_next(p);
if (si->n == NULL) {
skiplist_node_deref(p, (struct skiplist *)i->m);
return NULL;
}
si->n->refcount++;
skiplist_node_deref(p, (struct skiplist *)i->m);
*value = si->n->value;
return si->n->key;
}
static void
skiplist_iter_free(qb_map_iter_t * i)
{
free(i);
}
static size_t
skiplist_count_get(struct qb_map *map)
{
struct skiplist *list = (struct skiplist *)map;
return list->length;
}
qb_map_t *
qb_skiplist_create(void)
{
struct skiplist *sl = malloc(sizeof(struct skiplist));
if (sl == NULL) {
return NULL;
}
srand(time(NULL));
sl->map.put = skiplist_put;
sl->map.get = skiplist_get;
sl->map.rm = skiplist_rm;
sl->map.count_get = skiplist_count_get;
sl->map.iter_create = skiplist_iter_create;
sl->map.iter_next = skiplist_iter_next;
sl->map.iter_free = skiplist_iter_free;
sl->map.destroy = skiplist_destroy;
sl->map.notify_add = skiplist_notify_add;
sl->map.notify_del = skiplist_notify_del;
sl->level = SKIPLIST_LEVEL_MIN;
sl->length = 0;
sl->header = skiplist_header_node_new();
return (qb_map_t *) sl;
}
|