dict.c 15 KB
Newer Older
1 2
/*
 * This file is part of ltrace.
Petr Machata's avatar
Petr Machata committed
3
 * Copyright (C) 2012, 2013, 2014, 2015 Petr Machata, Red Hat Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 */

Juan Cespedes's avatar
Juan Cespedes committed
21
#include <string.h>
22 23 24
#include <stdlib.h>
#include <stdio.h>
#include "dict.h"
Juan Cespedes's avatar
Juan Cespedes committed
25

26 27 28 29
struct status_bits {
	unsigned char taken : 1;
	unsigned char erased : 1;
};
Juan Cespedes's avatar
Juan Cespedes committed
30

31 32 33 34 35
static struct status_bits *
bitp(struct dict *dict, size_t n)
{
	return VECT_ELEMENT(&dict->status, struct status_bits, n);
}
Juan Cespedes's avatar
Juan Cespedes committed
36

37 38 39 40 41 42 43 44 45
void
dict_init(struct dict *dict,
	  size_t key_size, size_t value_size,
	  size_t (*hash1)(const void *),
	  int (*eq)(const void *, const void *),
	  size_t (*hash2)(size_t))
{
	assert(hash1 != NULL);
	assert(eq != NULL);
Juan Cespedes's avatar
Juan Cespedes committed
46

47 48 49 50 51 52 53 54 55
	vect_init(&dict->keys, key_size);
	vect_init(&dict->values, value_size);
	VECT_INIT(&dict->status, struct status_bits);
	dict->size = 0;

	dict->hash1 = hash1;
	dict->hash2 = hash2;
	dict->eq = eq;
}
Juan Cespedes's avatar
Juan Cespedes committed
56

57 58 59 60 61 62 63
struct clone_data {
	struct dict *target;
	int (*clone_key)(void *tgt, const void *src, void *data);
	int (*clone_value)(void *tgt, const void *src, void *data);
	void (*dtor_key)(void *tgt, void *data);
	void (*dtor_value)(void *tgt, void *data);
	void *data;
Juan Cespedes's avatar
Juan Cespedes committed
64 65
};

66
static enum callback_status
67
clone_cb(void *key, void *value, void *data)
Petr Machata's avatar
Petr Machata committed
68
{
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	struct clone_data *clone_data = data;

	char nkey[clone_data->target->keys.elt_size];
	if (clone_data->clone_key == NULL)
		memmove(nkey, key, sizeof(nkey));
	else if (clone_data->clone_key(&nkey, key, clone_data->data) < 0)
		return CBS_STOP;

	char nvalue[clone_data->target->values.elt_size];
	if (clone_data->clone_value == NULL) {
		memmove(nvalue, value, sizeof(nvalue));
	} else if (clone_data->clone_value(&nvalue, value,
					 clone_data->data) < 0) {
	fail:
		if (clone_data->clone_key != NULL)
			clone_data->dtor_key(&nkey, clone_data->data);
		return CBS_STOP;
Juan Cespedes's avatar
Juan Cespedes committed
86
	}
87 88 89 90 91

	if (dict_insert(clone_data->target, nkey, nvalue) < 0) {
		if (clone_data->clone_value != NULL)
			clone_data->dtor_value(&nvalue, clone_data->data);
		goto fail;
Juan Cespedes's avatar
Juan Cespedes committed
92
	}
93 94

	return CBS_CONT;
Juan Cespedes's avatar
Juan Cespedes committed
95 96
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
int
dict_clone(struct dict *target, const struct dict *source,
	   int (*clone_key)(void *tgt, const void *src, void *data),
	   void (*dtor_key)(void *tgt, void *data),
	   int (*clone_value)(void *tgt, const void *src, void *data),
	   void (*dtor_value)(void *tgt, void *data),
	   void *data)
{
	assert((clone_key != NULL) == (dtor_key != NULL));
	assert((clone_value != NULL) == (dtor_value != NULL));

	dict_init(target, source->keys.elt_size, source->values.elt_size,
		  source->hash1, source->eq, source->hash2);
	struct clone_data clone_data = {
		target, clone_key, clone_value, dtor_key, dtor_value, data
	};
	if (dict_each((struct dict *)source, NULL,
		      clone_cb, &clone_data) != NULL) {
		dict_destroy(target, dtor_key, dtor_value, data);
		return -1;
Juan Cespedes's avatar
Juan Cespedes committed
117
	}
118 119 120 121 122 123 124
	return 0;
}

size_t
dict_size(const struct dict *dict)
{
	return dict->size;
Juan Cespedes's avatar
Juan Cespedes committed
125 126
}

127
int
128 129 130 131
dict_empty(const struct dict *dict)
{
	return dict->size == 0;
}
132

133 134 135 136 137
struct destroy_data {
	void (*dtor_key)(void *tgt, void *data);
	void (*dtor_value)(void *tgt, void *data);
	void *data;
};
138

139
static enum callback_status
140 141 142 143 144 145 146 147 148
destroy_cb(void *key, void *value, void *data)
{
	struct destroy_data *destroy_data = data;
	if (destroy_data->dtor_key)
		destroy_data->dtor_key(key, destroy_data->data);
	if (destroy_data->dtor_value)
		destroy_data->dtor_value(value, destroy_data->data);
	return CBS_CONT;
}
Juan Cespedes's avatar
Juan Cespedes committed
149

150 151 152 153 154 155
void
dict_destroy(struct dict *dict,
	     void (*dtor_key)(void *tgt, void *data),
	     void (*dtor_value)(void *tgt, void *data),
	     void *data)
{
Petr Machata's avatar
Petr Machata committed
156 157 158
	/* Some slots are unused (the corresponding keys and values
	 * are uninitialized), so we can't call dtors for them.
	 * Iterate DICT instead.  */
159 160 161 162 163
	if (dtor_key != NULL || dtor_value != NULL) {
		struct destroy_data destroy_data = {
			dtor_key, dtor_value, data
		};
		dict_each(dict, NULL, destroy_cb, &destroy_data);
Juan Cespedes's avatar
Juan Cespedes committed
164 165
	}

166 167 168 169
	vect_destroy(&dict->keys, NULL, NULL);
	vect_destroy(&dict->values, NULL, NULL);
	vect_destroy(&dict->status, NULL, NULL);
}
Juan Cespedes's avatar
Juan Cespedes committed
170

171 172 173 174 175
static size_t
default_secondary_hash(size_t pos)
{
	return pos % 97 + 1;
}
Juan Cespedes's avatar
Juan Cespedes committed
176

Petr Machata's avatar
Petr Machata committed
177 178 179 180 181 182
static size_t
small_secondary_hash(size_t pos)
{
	return 1;
}

183 184 185 186
static inline size_t
n(struct dict *dict)
{
	return vect_size(&dict->keys);
Juan Cespedes's avatar
Juan Cespedes committed
187 188
}

189 190 191 192 193
static inline size_t (*
hash2(struct dict *dict))(size_t)
{
	if (dict->hash2 != NULL)
		return dict->hash2;
Petr Machata's avatar
Petr Machata committed
194 195
	else if (n(dict) < 100)
		return small_secondary_hash;
196 197
	else
		return default_secondary_hash;
Petr Machata's avatar
Petr Machata committed
198 199
}

200 201
static void *
getkey(struct dict *dict, size_t pos)
Petr Machata's avatar
Petr Machata committed
202
{
203 204 205
	return ((unsigned char *)dict->keys.data)
		+ dict->keys.elt_size * pos;
}
Juan Cespedes's avatar
Juan Cespedes committed
206

207 208 209 210 211 212
static void *
getvalue(struct dict *dict, size_t pos)
{
	return ((unsigned char *)dict->values.data)
		+ dict->values.elt_size * pos;
}
213

214 215 216 217
static size_t
find_slot(struct dict *dict, const void *key,
	  int *foundp, int *should_rehash, size_t *pi)
{
218
	assert(n(dict) > 0);
219 220 221 222 223 224 225 226 227 228 229 230 231 232
	size_t pos = dict->hash1(key) % n(dict);
	size_t pos0 = -1;
	size_t d = hash2(dict)(pos);
	size_t i = 0;
	*foundp = 0;

	/* We skip over any taken or erased slots.  But we remember
	 * the first erased that we find, and if we don't find the key
	 * later, we return that position.  */
	for (; bitp(dict, pos)->taken || bitp(dict, pos)->erased;
	     pos = (pos + d) % n(dict)) {
		if (pos0 == (size_t)-1 && bitp(dict, pos)->erased)
			pos0 = pos;

Petr Machata's avatar
Petr Machata committed
233 234
		/* If there is a loop, but we've seen an erased
		 * element, take that one.  Otherwise give up.  */
Petr Machata's avatar
Petr Machata committed
235
		if (++i > n(dict)) {
Petr Machata's avatar
Petr Machata committed
236 237 238 239
			if (pos0 != (size_t)-1)
				break;
			return (size_t)-1;
		}
240

241 242 243
		if (bitp(dict, pos)->taken
		    && dict->eq(getkey(dict, pos), key)) {
			*foundp = 1;
Juan Cespedes's avatar
Juan Cespedes committed
244 245 246
			break;
		}
	}
247 248 249 250 251 252 253 254 255 256 257 258

	if (!*foundp && pos0 != (size_t)-1)
		pos = pos0;

	/* If the hash table degraded into a linked list, request a
	 * rehash.  */
	if (should_rehash != NULL)
		*should_rehash = i > 10 && i > n(dict) / 10;

	if (pi != NULL)
		*pi = i;
	return pos;
Juan Cespedes's avatar
Juan Cespedes committed
259 260
}

261
static enum callback_status
262 263 264 265 266 267 268 269
rehash_move(void *key, void *value, void *data)
{
	if (dict_insert(data, key, value) < 0)
		return CBS_STOP;
	else
		return CBS_CONT;
}

270
static int
271 272
rehash(struct dict *dict, size_t nn)
{
Petr Machata's avatar
Petr Machata committed
273
	assert(nn != n(dict));
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
	int ret = -1;

	struct dict tmp;
	dict_init(&tmp, dict->keys.elt_size, dict->values.elt_size,
		  dict->hash1, dict->eq, dict->hash2);

	/* To honor all invariants (so that we can safely call
	 * dict_destroy), we first make a request to _reserve_ enough
	 * room in all vectors.  This has no observable effect on
	 * contents of vectors.  */
	if (vect_reserve(&tmp.keys, nn) < 0
	    || vect_reserve(&tmp.values, nn) < 0
	    || vect_reserve(&tmp.status, nn) < 0)
		goto done;

	/* Now that we know that there is enough size in vectors, we
	 * simply bump the size.  */
	tmp.keys.size = nn;
	tmp.values.size = nn;
	size_t old_size = tmp.status.size;
	tmp.status.size = nn;
	memset(VECT_ELEMENT(&tmp.status, struct status_bits, old_size),
	       0, (tmp.status.size - old_size) * tmp.status.elt_size);

	/* At this point, TMP is once more an empty dictionary with NN
	 * slots.  Now move stuff from DICT to TMP.  */
	if (dict_each(dict, NULL, rehash_move, &tmp) != NULL)
		goto done;

	/* And now swap contents of DICT and TMP, and we are done.  */
	{
		struct dict tmp2 = *dict;
		*dict = tmp;
		tmp = tmp2;
	}
Juan Cespedes's avatar
Juan Cespedes committed
309

310
	ret = 0;
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
done:
	/* We only want to release the containers, not the actual data
	 * that they hold, so it's fine if we don't pass any dtor.  */
	dict_destroy(&tmp, NULL, NULL, NULL);
	return ret;

}

static const size_t primes[] = {
	13, 31, 61, 127, 251, 509, 1021, 2039, 4093,
	8191, 16381, 32749, 65521, 130981, 0
};

static size_t
larger_size(size_t current)
{
	if (current == 0)
		return primes[0];

	if (current < primes[sizeof(primes)/sizeof(*primes) - 2]) {
		size_t i;
		for (i = 0; primes[i] != 0; ++i)
			if (primes[i] > current)
				return primes[i];
		abort();
Juan Cespedes's avatar
Juan Cespedes committed
337
	}
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

	/* We ran out of primes, so invent a new one.  The following
	 * gives primes until about 17M elements (and then some more
	 * later).  */
	return 2 * current + 6585;
}

static size_t
smaller_size(size_t current)
{
	if (current <= primes[0])
		return primes[0];

	if (current <= primes[sizeof(primes)/sizeof(*primes) - 2]) {
		size_t i;
		size_t prev = 0;
		for (i = 0; primes[i] != 0; ++i) {
			if (primes[i] >= current)
				return prev;
			prev = primes[i];
Juan Cespedes's avatar
Juan Cespedes committed
358
		}
359
		abort();
Juan Cespedes's avatar
Juan Cespedes committed
360
	}
361 362

	return (current - 6585) / 2;
Juan Cespedes's avatar
Juan Cespedes committed
363 364
}

365 366 367 368 369 370 371 372 373 374 375
int
dict_insert(struct dict *dict, void *key, void *value)
{
	if (n(dict) == 0 || dict->size > 0.7 * n(dict))
	rehash:
		if (rehash(dict, larger_size(n(dict))) < 0)
			return -1;

	int found;
	int should_rehash;
	size_t slot_n = find_slot(dict, key, &found, &should_rehash, NULL);
Petr Machata's avatar
Petr Machata committed
376 377
	if (slot_n == (size_t)-1)
		return -1;
378 379
	if (found)
		return 1;
Petr Machata's avatar
Petr Machata committed
380
	assert(!bitp(dict, slot_n)->taken);
381 382 383 384 385 386 387 388 389 390 391 392 393

	/* If rehash was requested, do that, and retry.  But just live
	 * with it for apparently sparse tables.  No resizing can fix
	 * a rubbish hash.  */
	if (should_rehash && dict->size > 0.3 * n(dict))
		goto rehash;

	memmove(getkey(dict, slot_n), key, dict->keys.elt_size);
	memmove(getvalue(dict, slot_n), value, dict->values.elt_size);

	bitp(dict, slot_n)->taken = 1;
	bitp(dict, slot_n)->erased = 0;
	++dict->size;
Juan Cespedes's avatar
Juan Cespedes committed
394

395 396 397 398 399
	return 0;
}

void *
dict_find(struct dict *dict, const void *key)
Petr Machata's avatar
Petr Machata committed
400
{
401 402
	if (dict->size == 0)
		return NULL;
403
	assert(n(dict) > 0);
Juan Cespedes's avatar
Juan Cespedes committed
404

405 406 407 408 409 410
	int found;
	size_t slot_n = find_slot(dict, key, &found, NULL, NULL);
	if (found)
		return getvalue(dict, slot_n);
	else
		return NULL;
Juan Cespedes's avatar
Juan Cespedes committed
411 412
}

413
int
414 415 416 417
dict_erase(struct dict *dict, const void *key,
	   void (*dtor_key)(void *tgt, void *data),
	   void (*dtor_value)(void *tgt, void *data),
	   void *data)
Petr Machata's avatar
Petr Machata committed
418
{
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	int found;
	size_t i;
	size_t slot_n = find_slot(dict, key, &found, NULL, &i);
	if (!found)
		return -1;

	if (dtor_key != NULL)
		dtor_key(getkey(dict, slot_n), data);
	if (dtor_value != NULL)
		dtor_value(getvalue(dict, slot_n), data);

	bitp(dict, slot_n)->taken = 0;
	bitp(dict, slot_n)->erased = 1;
	--dict->size;

	if (dict->size < 0.3 * n(dict)) {
Petr Machata's avatar
Petr Machata committed
435 436 437 438
		size_t smaller = smaller_size(n(dict));
		if (smaller != n(dict))
			/* Don't mind if it fails when shrinking.  */
			rehash(dict, smaller);
439 440 441
	}

	return 0;
Juan Cespedes's avatar
Juan Cespedes committed
442 443
}

444 445 446
void *
dict_each(struct dict *dict, void *start_after,
	  enum callback_status (*cb)(void *, void *, void *), void *data)
Petr Machata's avatar
Petr Machata committed
447
{
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	size_t i;
	if (start_after != NULL)
		i = ((start_after - dict->keys.data) / dict->keys.elt_size) + 1;
	else
		i = 0;

	for (; i < dict->keys.size; ++i)
		if (bitp(dict, i)->taken && !bitp(dict, i)->erased) {
			void *key = getkey(dict, i);
			if (cb(key, getvalue(dict, i), data) != CBS_CONT)
				return key;
		}

	return NULL;
}

size_t
dict_hash_int(const int *key)
{
Petr Machata's avatar
Petr Machata committed
467
	return (size_t)(*key * 2654435761U);
Juan Cespedes's avatar
Juan Cespedes committed
468 469
}

470
int
471
dict_eq_int(const int *key1, const int *key2)
Petr Machata's avatar
Petr Machata committed
472
{
473
	return *key1 == *key2;
Juan Cespedes's avatar
Juan Cespedes committed
474
}
475

476 477 478
size_t
dict_hash_uint64(const uint64_t *key)
{
479 480 481
	int const a = (int) *key;
	int const b = (int) (*key >> 32);
	return dict_hash_int (&a) ^ dict_hash_int (&b);
482 483 484 485 486 487 488 489
}

int
dict_eq_uint64(const uint64_t *key1, const uint64_t *key2)
{
	return *key1 == *key2;
}

490 491
size_t
dict_hash_string(const char **key)
492
{
493 494 495 496 497 498
	size_t h = 5381;
	const char *str = *key;
	while (*str != 0)
		h = h * 33 ^ *str++;
	return h;
}
499

500 501 502 503 504
int
dict_eq_string(const char **key1, const char **key2)
{
	return strcmp(*key1, *key2) == 0;
}
505

Petr Machata's avatar
Petr Machata committed
506 507 508 509 510 511 512 513 514 515 516 517 518
void
dict_dtor_string(const char **key, void *data)
{
	free((char *)*key);
}

int
dict_clone_string(const char **tgt, const char **src, void *data)
{
	*tgt = strdup(*src);
	return *tgt != NULL ? 0 : -1;
}

519 520 521 522 523 524 525 526 527 528
#ifdef TEST
static enum callback_status
dump(int *key, int *value, void *data)
{
	char *seen = data;
	assert(seen[*key] == 0);
	seen[*key] = 1;
	assert(*value == *key * 2 + 1);
	return CBS_STOP;
}
529

530 531 532 533 534
static size_t
dict_hash_int_silly(const int *key)
{
	return *key % 10;
}
535

536 537 538 539 540 541 542 543 544
static void
verify(struct dict *di, size_t len, char *seen)
{
	size_t ct = 0;
	int *it;
	for (it = NULL; (it = DICT_EACH(di, int, int, it, dump, seen)) != NULL;)
		ct++;
	assert(ct == len);
	memset(seen, 0, len);
545
}
546

547 548
static enum callback_status
fill_keys(int *key, int *value, void *data)
549
{
550 551 552 553
	int *array = data;
	array[++array[0]] = *key;
	return CBS_CONT;
}
554

555 556
static void
test1(void)
557
{
558 559 560 561 562 563 564 565 566
	struct dict di;
	DICT_INIT(&di, int, int, dict_hash_int, dict_eq_int, NULL);

	char seen[100000] = {};
	size_t i;
	for (i = 0; i < sizeof(seen); ++i) {
		int key = i;
		int value = 2 * i + 1;
		DICT_INSERT(&di, &key, &value);
567
		int *valp = DICT_FIND_REF(&di, &key, int);
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
		assert(valp != NULL);
		assert(*valp == value);
		assert(dict_size(&di) == i + 1);
	}

	verify(&di, sizeof(seen), seen);

	struct dict d2;
	DICT_CLONE(&d2, &di, int, int, NULL, NULL, NULL, NULL, NULL);
	DICT_DESTROY(&di, int, int, NULL, NULL, NULL);
	verify(&d2, sizeof(seen), seen);

	/* Now we try to gradually erase all elements.  We can't erase
	 * inside a DICT_EACH call, so copy first keys to a separate
	 * memory area first.  */
	int keys[d2.size + 1];
	size_t ct = 0;
	keys[0] = 0;
	DICT_EACH(&d2, int, int, NULL, fill_keys, keys);
	for (i = 0; i < (size_t)keys[0]; ++i) {
		assert(DICT_ERASE(&d2, &keys[i + 1], int,
				  NULL, NULL, NULL) == 0);
		++ct;
	}
	assert(ct == sizeof(seen));
	DICT_DESTROY(&d2, int, int, NULL, NULL, NULL);
594 595
}

596 597
static void
test_erase(void)
598
{
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
	int i;

	/* To test erase, we need a relatively bad hash function, so
	 * that there are some overlapping chains in the table.  */
	struct dict d2;
	DICT_INIT(&d2, int, int, dict_hash_int_silly, dict_eq_int, NULL);
	const int limit = 500;
	for (i = 0; i < limit; ++i) {
		int key = 2 * i + 1;
		int value = 2 * key + 1;
		DICT_INSERT(&d2, &key, &value);
	}

	/* Now we try to delete each of the keys, and verify that none
	 * of the chains was broken.  */
	for (i = 0; i < limit; ++i) {
		struct dict copy;
		DICT_CLONE(&copy, &d2, int, int, NULL, NULL, NULL, NULL, NULL);
		int key = 2 * i + 1;
		DICT_ERASE(&copy, &key, int, NULL, NULL, NULL);
		assert(dict_size(&copy) == dict_size(&d2) - 1);

		int j;
		for (j = 0; j < limit; ++j) {
			key = 2 * j + 1;
624
			int *valp = DICT_FIND_REF(&copy, &key, int);
625 626 627 628 629 630 631 632 633 634 635
			if (i != j) {
				assert(valp != NULL);
				assert(*valp == 2 * key + 1);
			} else {
				assert(valp == NULL);
			}
		}

		DICT_DESTROY(&copy, int, int, NULL, NULL, NULL);
	}
	DICT_DESTROY(&d2, int, int, NULL, NULL, NULL);
636 637
}

638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
static void
test_erase_find(void)
{
	/* Degenerate case for erase followed by find, considering 2 colliding keys.
	   Erase 1st inserted key, then try finding 2nd.
	 */
	struct dict d2;
	DICT_INIT(&d2, int, int, dict_hash_int_silly, dict_eq_int, NULL);
	int key;
	int value;

	key = 0, value = 0;
	DICT_INSERT(&d2, &key, &value);
	key = 10, value = 10;
	DICT_INSERT(&d2, &key, &value);

	key = 10;
	assert (DICT_FIND_REF(&d2, &key, int) != NULL);

	key = 0;
	DICT_ERASE(&d2, &key, int, NULL, NULL, NULL);

	key = 10;
	assert (DICT_FIND_REF(&d2, &key, int) != NULL);

	DICT_DESTROY(&d2, int, int, NULL, NULL, NULL);
}

666
int main(int argc, char *argv[])
667
{
668 669
	test1();
	test_erase();
670
	test_erase_find();
671
	return 0;
672
}
673 674

#endif