summaryrefslogtreecommitdiff
path: root/src/libknot/hash/cuckoo-hash-table.c
blob: 50a7a0f5b7d77aa977b796d4953c3e7639b7c8e6 (plain)
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
/*  Copyright (C) 2011 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>     /* defines uint32_t etc */
#include <assert.h>
#include <pthread.h>
#include <math.h>

#include <urcu.h>

#include "util/utils.h"
#include "common.h"
#include "util/debug.h"
#include "hash/cuckoo-hash-table.h"
#include "hash/hash-functions.h"
#include "common/dynamic-array.h"

/*----------------------------------------------------------------------------*/
/* Macros and inline functions                                                */
/*----------------------------------------------------------------------------*/

/*!
 * \brief Default size table holding information about used hash table cells
 *        when hashing.
 */
#define RELOCATIONS_DEFAULT 200

/*!
 * \brief Maximum size table holding information about used hash table cells
 *        when hashing (just for debug issues).
 */
#define RELOCATIONS_MAX 1000

/*!
 * \brief Macro for hashing the given key using the universal system.
 *
 * \param system Universal system to use for the hashing.
 * \param key Key to hash.
 * \param length Size of the key in bytes.
 * \param exp Exponent of the hash table size (the size is a power of 2).
 * \param table Hash table index.
 * \param gen Universal system generation.
 *
 * \return Hashed key.
 */
#define HASH(system, key, length, exp, gen, table) \
	us_hash(system, fnv_32_buf(key, length, FNV1_32_INIT), exp, table, gen)

/*!
 * \brief Approximate ratio of hash table size to number of hashed items when 2
 *        tables are used.
 */
static const float SIZE_RATIO_2 = 2;

/*!
 * \brief Approximate ratio of hash table size to number of hashed items when 3
 *        tables are used.
 */
static const float SIZE_RATIO_3 = 1.15;

/*!
 * \brief Approximate ratio of hash table size to number of hashed items when 4
 *        tables are used.
 */
static const float SIZE_RATIO_4 = 1.08;

/*----------------------------------------------------------------------------*/

/*! \brief Flag marking the generation of hash table or its item to be 1. */
static const uint8_t FLAG_GENERATION1     = 0x1; // 00000001
/*! \brief Flag marking the generation of hash table or its item to be 2. */
static const uint8_t FLAG_GENERATION2     = 0x2; // 00000010
/*! \brief Flag marking both generations. */
static const uint8_t FLAG_GENERATION_BOTH = 0x3; // 00000011

/*! \brief Flag used to mark the table when it's being rehashed. */
static const uint8_t FLAG_REHASH          = 0x4; // 00000100

/*----------------------------------------------------------------------------*/
/*! \brief Clears the table / item flags. */
static inline void CLEAR_FLAGS(uint8_t *flags)
{
	*flags = (uint8_t)0x0;
}

/*! \brief Returns the generation stored in the flags. */
static inline uint8_t GET_GENERATION(uint8_t flags)
{
	return (flags & FLAG_GENERATION_BOTH);
}

/*! \brief Checks if the generation stored in both flags are the same. */
static inline int EQUAL_GENERATIONS(uint8_t flags1, uint8_t flags2)
{
	return (GET_GENERATION(flags1) == GET_GENERATION(flags2));
}

/*! \brief Checks if the generation stored in the flags is 1. */
static inline int IS_GENERATION1(uint8_t flags)
{
	return ((flags & FLAG_GENERATION1) != 0);
}

/*! \brief Sets the generation stored in the flags to 1. */
static inline void SET_GENERATION1(uint8_t *flags)
{
	*flags = ((*flags) & ~FLAG_GENERATION2) | FLAG_GENERATION1;
}

/*! \brief Checks if the generation stored in the flags is 2. */
static inline int IS_GENERATION2(uint8_t flags)
{
	return ((flags & FLAG_GENERATION2) != 0);
}

/*! \brief Sets the generation stored in the flags to 2. */
static inline void SET_GENERATION2(uint8_t *flags)
{
	*flags = ((*flags) & ~FLAG_GENERATION1) | FLAG_GENERATION2;
}

/*! \brief Sets the generation stored in the flags to the given generation. */
static inline void SET_GENERATION(uint8_t *flags, uint8_t generation)
{
	*flags = ((*flags) & ~FLAG_GENERATION_BOTH) | generation;
}

/*! \brief Sets the generation stored in the flags to the next one (cyclic). */
static inline uint8_t SET_NEXT_GENERATION(uint8_t *flags)
{
	return ((*flags) ^= FLAG_GENERATION_BOTH);
}

/*! \brief Returns the next generation to the one stored in flags (cyclic). */
static inline uint8_t NEXT_GENERATION(uint8_t flags)
{
	return ((flags & FLAG_GENERATION_BOTH) ^ FLAG_GENERATION_BOTH);
}

/*! \brief Sets the rehashing flag to the flags. */
static inline void SET_REHASHING_ON(uint8_t *flags)
{
	*flags = (*flags | FLAG_REHASH);
}

/*! \brief Removes the rehashing flag from the flags. */
static inline void SET_REHASHING_OFF(uint8_t *flags)
{
	*flags = (*flags & ~FLAG_REHASH);
}

/*! \brief Checks if the rehashing flag is set in the flags. */
static inline int IS_REHASHING(uint8_t flags)
{
	return ((flags & FLAG_REHASH) != 0);
}

/*----------------------------------------------------------------------------*/
/* Private functions                                                          */
/*----------------------------------------------------------------------------*/
/*!
 * \brief Returns the exponent of the nearest larger power of two.
 */
static uint get_larger_exp(uint n)
{
	uint res = 0;
	while (hashsize(++res) < n) {}

	return res;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Counts the ideal table count and the exponent of those tables' sizes.
 *
 * Only 3 or 4 hash tables are considered. The setup in which less items are
 * wasted is recommended.
 *
 * \param items Number of items to hash.
 * \param table_count Recommended number of tables will be saved here.
 *
 * \return Exponent of the tables' sizes.
 */
static uint get_table_exp_and_count(uint items, uint *table_count)
{
	// considering only 3 or 4 tables
	int exp3 = get_larger_exp((items * SIZE_RATIO_3) / 3);
	int exp4 = get_larger_exp(items * SIZE_RATIO_4) - 2;
	
	if (exp4 < 0) {
		exp4 = 1;
	}

	dbg_ck("Determining ideal table size...\n");
	dbg_ck("\tNumber of items: %u\n", items);
	dbg_ck("\tThree tables: size of one table: %u, total size: %u\n",
	         hashsize(exp3), 3 * hashsize(exp3));
	dbg_ck("\tFour tables: size of one table: %u, total size: %u\n",
	         hashsize(exp4), 4 * hashsize(exp4));

	// we need exponent at least 1 (this is quite ugly..)
	if (exp3 == 0) {
		exp3 = 1;
	}
	if (exp4 == 0) {
		exp4 = 1;
	}
	
	if (exp3 >= 32 || exp4 >= 32) {
		return 0;
	}
	
	if (((hashsize(exp3) * 3) - (items)) < ((hashsize(exp4) * 4) - items)) {
		*table_count = 3;
		return exp3;
	} else {
		*table_count = 4;
		return exp4;
	}
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Counts the maximum effective item count based on size of the tables.
 *
 * For 3 tables, the effective utilization should be around 91%.
 * For 4 tables it is 97%.
 *
 * See Fotakis, Dimitris, et al. - Space Efficient Hash Tables with Worst Case
 * Constant Access Time. CiteSeerX. 2003
 * http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.14.5337
 */
static uint get_max_table_items(uint table_count, int table_exponent)
{
	assert(table_count == 3 || table_count == 4);
	
	float coef;
	
	if (table_count == 3) {
		coef = 0.91;
	} else {
		coef = 0.97;
	}
	
	return (uint)floor((table_count * hashsize(table_exponent)) * coef);
}

/*----------------------------------------------------------------------------*/

static int ck_is_full(const ck_hash_table_t *table)
{
	return (table->items >= get_max_table_items(table->table_count, 
	                                            table->table_size_exp));
}

/*----------------------------------------------------------------------------*/

static int ck_stash_is_full(const ck_hash_table_t *table)
{
	return (table->items_in_stash >= STASH_SIZE_MAX);
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Clears the given item by assigning a NULL pointer to it.
 */
static inline void ck_clear_item(ck_hash_table_item_t **item)
{
	*item = NULL;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Insert given contents to the hash table item.
 */
static void ck_fill_item(const char *key, size_t key_length, void *value,
                         uint generation, ck_hash_table_item_t *item)
{
	// must allocate new space for key and value, otherwise it will be lost!
	item->key = key;
	item->key_length = key_length;
	item->value = value;
	CLEAR_FLAGS(&item->timestamp);
	item->timestamp = generation;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Swaps two hash table items.
 */
static inline void ck_swap_items(ck_hash_table_item_t **item1,
                                 ck_hash_table_item_t **item2)
{
	ck_hash_table_item_t *tmp = *item1;
	*item1 = *item2;
	*item2 = tmp;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Sets the \a item pointer to the \a to pointer.
 */
static inline void ck_put_item(ck_hash_table_item_t **to,
                               ck_hash_table_item_t *item)
{
	*to = item;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Checks if the hash was already used twice.
 *
 * If yes, it means we entered a loop in the hashing process, so we must stop.
 * Otherwise it remembers that we used the hash.
 *
 * \note According to Kirsch, et al. a check that at most one hash was used
 *       twice should be sufficient. We will retain our version for now.
 *
 * \param used Array of used table indices (hashes).
 * \param hash Hash to check.
 *
 * \retval -1 if the hash was already used twice.
 * \retval -2 if an error occured.
 * \retval 0 if the hash was not used twice yet.
 */
static uint ck_check_used_twice(da_array_t *used, uint32_t hash)
{
	uint i = 0, found = 0;
	while (i < da_get_count(used) && found < 2) {
		if (((uint *)(da_get_items(used)))[i] == hash) {
			++found;
		}
		++i;
	}

	if (found == 2) {
		dbg_ck_hash("Hashing entered infinite loop.\n");
		return -1;
	} else {
		if (da_reserve(used, 1) < 0) {
			ERR_ALLOC_FAILED;
			return -2;
		}
		((uint *)da_get_items(used))[da_get_count(used)] = hash;
		da_occupy(used, 1);
		assert(da_get_count(used) < RELOCATIONS_MAX);
		return 0;
	}
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Compares the key of item with the given key.
 *
 * \param item Item to compare with.
 * \param key Key to compare.
 * \param length Size of the key in bytes.
 *
 * \return <> 0 if the keys match.
 * \return 0 if they don't.
 */
static inline uint ck_items_match(const ck_hash_table_item_t *item,
                                  const char *key, size_t length)
{
	assert(item != NULL);

	return (length == item->key_length
	        && (strncmp(item->key, key, length) == 0));
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Switches the given table number to a randomly chosen other table
 *        number.
 */
static inline void ck_next_table(uint *table, uint table_count)
{
	uint next;
	while ((*table) == (next = knot_quick_rand() % table_count)) {}
	*table = next;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Tries to find the given key in the hash table's stash.
 *
 * \param table Hash table to search in.
 * \param key Key to find.
 * \param length Size of the key in bytes.
 *
 * \return Hash table item matching the key or NULL if not found in the stash.
 */
static ck_hash_table_item_t **ck_find_in_stash(const ck_hash_table_t *table,
                                               const char *key, uint length)
{
	ck_stash_item_t *item = table->stash;
	while (item != NULL) {
		/*! \todo Can the item be NULL?
		 *        Sometimes it crashed on assert in ck_items_match(),
		 *        But I'm not sure if this may happen or if the
		 *        semantics of the stash are that all items must be
		 *        non-NULL.
		 */
		if (item->item && ck_items_match(item->item, key, length)) {
			dbg_ck("Comparing item in stash (key: %.*s (size %zu))"
			         "with searched item (key %.*s (size %u)).\n",
			         (int)item->item->key_length, item->item->key,
			         item->item->key_length, (int)length, key,
			         length);
			return &item->item;
		}
		item = item->next;
	}

	return NULL;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Tries to find item with given key using hash functions from the given
 *        generation.
 *
 * \param table Hash table to search in.
 * \param key Key to find.
 * \param length Size of the key in bytes.
 * \param generation Generation of items (table) to use. Items having other
 *                   generation are ignored.
 */
static ck_hash_table_item_t **ck_find_gen(const ck_hash_table_t *table,
                                          const char *key,
                                          size_t length, uint8_t generation)
{
	uint32_t hash;
	dbg_ck("Finding item in generation: %u\n", generation);

	// check hash tables
	for (uint t = 0; t < table->table_count; ++t) {
		hash = HASH(&table->hash_system, key, length,
		            table->table_size_exp, generation, t);

		dbg_ck("Hash: %u, key: %.*s\n", hash, (int)length, key);
		dbg_ck("Table %d, hash: %u, item: %p\n", t + 1, hash,
		         table->tables[t][hash]);
		if (table->tables[t][hash] != NULL) {
			dbg_ck("Table %u, key: %.*s, value: %p, key "
			         "length: %zu\n",
			         t + 1, (int)table->tables[t][hash]->key_length,
			         table->tables[t][hash]->key,
			         table->tables[t][hash]->value,
			         table->tables[t][hash]->key_length);
		}

		if (table->tables[t][hash] &&
		    ck_items_match(table->tables[t][hash], key, length)) {
			// found
			return &table->tables[t][hash];
		}
	}

	// try to find in stash
	dbg_ck("Searching in stash...\n");

	ck_hash_table_item_t **found =
	        ck_find_in_stash(table, key, length);

	dbg_ck("Found pointer: %p\n", found);
	if (found != NULL) {
		dbg_ck("Stash, key: %.*s, value: %p, key length: %zu\n",
		         (int)(*found)->key_length, (*found)->key,
	                 (*found)->value, (*found)->key_length);
	}

	// ck_find_in_buffer returns NULL if not found, otherwise pointer to
	// item
	return found;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Finds item with given key and returns non-constant pointer to pointer
 *        to the appropriate hash table item.
 *
 * \param table Hash table to search in.
 * \param key Key to find.
 * \param length Size of the key in bytes.
 */
static ck_hash_table_item_t **ck_find_item_nc(const ck_hash_table_t *table,
                                              const char *key, size_t length)
{
	// get the generation of the table so that we use the same value
	uint8_t generation = table->generation;

	// find item using the table generation's hash functions
	ck_hash_table_item_t **found = ck_find_gen(table, key, length,
	                                           GET_GENERATION(generation));
	// if rehashing is in progress, try the next generation's functions
	if (!found && IS_REHASHING(generation)) {
		found = ck_find_gen(table, key, length,
		                    NEXT_GENERATION(generation));
	}

	return found;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Hashes the given item using the given generation.
 *
 * \param table Hash table where to put the item.
 * \param to_hash In: Item to hash. Out: NULL if successful, item that failed
 *                to hash if not.
 * \param free Free place where to put the last moved item when the hasing
 *             is unsuccessful.
 * \param generation Generation of items (table) to be used for hashing.
 *
 * \retval 0 if successful and no loop occured.
 * \retval 1 if a loop occured and the item was inserted to the \a free place.
 */
static int ck_hash_item(ck_hash_table_t *table, ck_hash_table_item_t **to_hash,
                        ck_hash_table_item_t **free, uint8_t generation)
{
	da_array_t used[table->table_count];
	for (uint i = 0; i < table->table_count; ++i) {
		da_initialize(&used[i], RELOCATIONS_DEFAULT, sizeof(uint));
	}

	// hash until empty cell is encountered or until loop appears

	dbg_ck_hash("Hashing key: %.*s of size %zu.\n",
	              (int)(*to_hash)->key_length, (*to_hash)->key,
	              (*to_hash)->key_length);

	uint next_table = 0;

	uint32_t hash = HASH(&table->hash_system, (*to_hash)->key,
	                     (*to_hash)->key_length, table->table_size_exp,
	                     generation, next_table);

	dbg_ck_hash("New hash: %u.\n", hash);
	assert(hash < hashsize(table->table_size_exp));

	((uint *)da_get_items(&used[next_table]))
	[da_get_count(&used[next_table])] = hash;
	ck_hash_table_item_t **next = &table->tables[next_table][hash];
	dbg_ck_hash("Item to be moved: %p, place in table: %p\n",
	              *next, next);
	ck_hash_table_item_t **moving = to_hash;

	int loop = 0;

	while (*next != NULL) {
		dbg_ck_hash("Swapping items to hash: %p and Moving: %p\n",
		              to_hash, moving);
		ck_swap_items(to_hash, moving); // first time it's unnecessary

		// set the generation of the inserted item to the next
		SET_GENERATION(&(*moving)->timestamp, generation);

		moving = next;

		dbg_ck_hash("Moving item from table %u, key: %.*s, hash %u ",
		              next_table + 1, (int)(*moving)->key_length,
		              (*moving)->key, hash);

		// if rehashing and the 'next' item is from the old generation,
		// start from table 1
		if (generation != table->generation &&
		    EQUAL_GENERATIONS((*next)->timestamp, table->generation)) {
			next_table = 0;
		} else {
			ck_next_table(&next_table, table->table_count);
		}

		hash = HASH(&table->hash_system, (*next)->key,
		            (*next)->key_length, table->table_size_exp,
		            generation, next_table);

		next = &table->tables[next_table][hash];

		dbg_ck_hash("to table %u, hash %u, item: %p, place: %p\n",
		              next_table + 1, hash, *next, next);

		if ((*next) != NULL) {
			dbg_ck_hash("Table %u, hash: %u, key: %.*s\n",
			              next_table + 1, hash,
			              (int)(*next)->key_length, (*next)->key);
		}

		// check if this cell wasn't already used in this item's hashing
		if (ck_check_used_twice(&used[next_table], hash) != 0) {
			next = free;
			loop = -1;
			break;
		}
	}

	dbg_ck_hash("Putting pointer %p (*moving) to item %p (next).\n",
	              *moving, next);

	ck_put_item(next, *moving);
	// set the new generation for the inserted item
	SET_GENERATION(&(*next)->timestamp, generation);
	dbg_ck_hash("Putting pointer %p (*old) to item %p (moving).\n",
	              *to_hash, moving);

	ck_put_item(moving, *to_hash);

	// set the new generation for the inserted item
	SET_GENERATION(&(*moving)->timestamp, generation);
	*to_hash = NULL;

	for (uint i = 0; i < table->table_count; ++i) {
		da_destroy(&used[i]);
	}

	return loop;
}

/*----------------------------------------------------------------------------*/

static void ck_rollback_rehash(ck_hash_table_t *table)
{
	// set old generation in tables
	for (int i = 0; i < hashsize(table->table_size_exp); ++i) {
		// no need for locking - timestamp is not used in lookup
		// and two paralel insertions (and thus rehashings) are
		// impossible
		for (uint t = 0; t < table->table_count; ++t) {
			if (table->tables[t][i] != NULL) {
				SET_GENERATION(&table->tables[t][i]->timestamp,
				               table->generation);
			}
		}
	}

	// set old generation in stash
	ck_stash_item_t *item = table->stash;
	while (item != NULL) {
		assert(item->item != NULL);
		SET_GENERATION(&item->item->timestamp, table->generation);
	}
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Adds the given item to the hash table's stash.
 *
 * \param table Hash table to add the item to.
 * \param item Item to add.
 *
 * \retval 0 if successful.
 * \retval -1 if an error occured.
 */
int ck_add_to_stash(ck_hash_table_t *table, ck_hash_table_item_t *item)
{
	ck_stash_item_t *new_item
		= (ck_stash_item_t *)malloc(sizeof(ck_stash_item_t));
	if (new_item == NULL) {
		ERR_ALLOC_FAILED;
		return -1;
	}

	new_item->item = item;
	new_item->next = table->stash;
	table->stash = new_item;

	dbg_ck_hash("First item in stash (now inserted): key: %.*s (size %zu)"
	              ", value: %p\n", (int)table->stash->item->key_length,
	              table->stash->item->key, table->stash->item->key_length,
	              table->stash->item->value);
	
	// increase count of items in stash
	++table->items_in_stash;
	
	return 0;
}

/*----------------------------------------------------------------------------*/

static int ck_new_table(ck_hash_table_item_t ***table, int exp)
{
	*table = (ck_hash_table_item_t **)
	          malloc(hashsize(exp) * sizeof(ck_hash_table_item_t *));
	if (*table == NULL) {
		ERR_ALLOC_FAILED;
		return -1;
	}

	// set to 0
	memset(*table, 0, hashsize(exp) * sizeof(ck_hash_table_item_t *));
	
	return 0;
}

/*----------------------------------------------------------------------------*/
/* Public functions                                                           */
/*----------------------------------------------------------------------------*/

ck_hash_table_t *ck_create_table(uint items)
{
	ck_hash_table_t *table =
			(ck_hash_table_t *)malloc(sizeof(ck_hash_table_t));

	if (table == NULL) {
		ERR_ALLOC_FAILED;
		return NULL;
	}
	
	memset(table, 0, sizeof(ck_hash_table_t));

	// determine ideal size of one table in powers of 2 and save the
	// exponent
	table->table_size_exp = get_table_exp_and_count(items,
	                                                &table->table_count);
	assert(table->table_size_exp <= 32);
	
	if (table->table_size_exp == 0) {
		dbg_ck("Failed to count exponent of the hash table.\n");
		free(table);
		return NULL;
	}

	dbg_ck("Creating hash table for %u items.\n", items);
	dbg_ck("Exponent: %u, number of tables: %u\n ",
		 table->table_size_exp, table->table_count);
	dbg_ck("Table size: %u items, each %zu bytes, total %zu bytes\n",
	         hashsize(table->table_size_exp),
	         sizeof(ck_hash_table_item_t *),
	         hashsize(table->table_size_exp)
	           * sizeof(ck_hash_table_item_t *));

	// create tables
	for (uint t = 0; t < table->table_count; ++t) {
		dbg_ck("Creating table %u...\n", t);
		if (ck_new_table(&table->tables[t], table->table_size_exp) 
		    != 0) {
			for (uint i = 0; i < t; ++i) {
				free(table->tables[i]);
			}
			free(table);
			return NULL;
		}
	}

	assert(table->stash == NULL);
	assert(table->hashed == NULL);
	assert(table->items == 0);
	assert(table->items_in_stash == 0);
	assert(table->table_count == MAX_TABLES
	       || table->tables[table->table_count] == NULL);

	// initialize rehash/insert mutex
	pthread_mutex_init(&table->mtx_table, NULL);

	// set the generation to 1 and initialize the universal system
	CLEAR_FLAGS(&table->generation);
	SET_GENERATION1(&table->generation);

	us_initialize(&table->hash_system);

	return table;
}

/*----------------------------------------------------------------------------*/

void ck_destroy_table(ck_hash_table_t **table, void (*dtor_value)(void *value),
                      int delete_key)
{
	assert(table);
	assert(*table);
	pthread_mutex_lock(&(*table)->mtx_table);

	// destroy items in tables
	for (uint i = 0; i < hashsize((*table)->table_size_exp); ++i) {
		for (uint t = 0; t < (*table)->table_count; ++t) {
			if ((*table)->tables[t][i] != NULL) {
				if (dtor_value) {
					dtor_value(
					  (*table)->tables[t][i]->value);
				}
				if (delete_key != 0) {
					free(
					  (void *)(*table)->tables[t][i]->key);
				}
				free((void *)(*table)->tables[t][i]);
			}
		}
	}

	// destroy items in stash
//	ck_hash_table_item_t **stash =
//	        ((ck_hash_table_item_t **)(da_get_items(&(*table)->stash)));
//	for (uint i = 0; i < da_get_count(&(*table)->stash); ++i) {
//		assert(stash[i] != NULL);
//		if (dtor_value) {
//			dtor_value(stash[i]->value);
//		}
//		if (delete_key != 0) {
//			free((void *)stash[i]->key);
//		}
//		free((void *)stash[i]);
//	}
	ck_stash_item_t *item = (*table)->stash;
	while (item != NULL) {
		// disconnect the item
		(*table)->stash = item->next;

		/*! \todo Check item semantics. (#1688) */
		if (item->item != NULL) {
			if (dtor_value) {
				dtor_value(item->item->value);
			}
			if (delete_key) {
				free((void *)item->item->key);
			}

			free((void *)item->item);
		}

		free(item);
		item = (*table)->stash;
	}

	// deallocate tables
	for (uint t = 0; t < (*table)->table_count; ++t) {
		free((*table)->tables[t]);
	}
	// destroy stash
//	da_destroy(&(*table)->stash);

	pthread_mutex_unlock(&(*table)->mtx_table);
	// destroy mutex, assuming that here noone will lock the mutex again
	pthread_mutex_destroy(&(*table)->mtx_table);

	free(*table);
	(*table) = NULL;
}

void ck_table_free(ck_hash_table_t **table)
{
	if (table == NULL || *table == NULL) {
		return;
	}
	
	pthread_mutex_lock(&(*table)->mtx_table);

	ck_stash_item_t *item = (*table)->stash;
	while (item != NULL) {
		// disconnect the item
		(*table)->stash = item->next;
		free(item);
		item = (*table)->stash;
	}

	// deallocate tables
	for (uint t = 0; t < (*table)->table_count; ++t) {
		free((*table)->tables[t]);
	}

	pthread_mutex_unlock(&(*table)->mtx_table);
	pthread_mutex_destroy(&(*table)->mtx_table);

	free(*table);
	(*table) = NULL;
}

int ck_resize_table(ck_hash_table_t *table)
{
	dbg_ck("Resizing hash table.\n");
	
	/*
	 * Easiest is just to increment the exponent, resulting in doubling
	 * the table sizes. This is not very memory-effective, but should do
	 * the job.
	 */
	
	if (table->table_size_exp == 31) {
		dbg_ck("Hash tables achieved max size (exponent 31).\n");
		return -1;
	}
	
	ck_hash_table_item_t **tables_new[MAX_TABLES];
	ck_hash_table_item_t **tables_old[MAX_TABLES];
	int exp_new = table->table_size_exp + 1;
	
	dbg_ck("New tables exponent: %d\n", exp_new);
	
	for (int t = 0; t < table->table_count; ++t) {
		if (ck_new_table(&tables_new[t], exp_new) != 0) {
			dbg_ck("Failed to create new table.\n");
			for (int i = 0; i < t; ++i) {
				free(tables_new[i]);
			}
			return -1;
		}
	}
	
	dbg_ck("Created new tables, copying data to them.\n");
	
	for (int t = 0; t < table->table_count; ++t) {
		size_t old_size = hashsize(table->table_size_exp) 
		                  * sizeof(ck_hash_table_item_t *);
		
		// copy the old table items
		dbg_ck("Copying to: %p, from %p, size: %zu\n",
		         tables_new[t], table->tables[t], old_size);
		memcpy(tables_new[t], table->tables[t], old_size);
		// set the rest to 0
		dbg_ck("Setting to 0 from %p, size %zu\n",
		         tables_new[t] + hashsize(table->table_size_exp),
		         (hashsize(exp_new) * sizeof(ck_hash_table_item_t *))
		         - old_size);
		memset(tables_new[t] + hashsize(table->table_size_exp), 0, 
		       (hashsize(exp_new) * sizeof(ck_hash_table_item_t *))
		       - old_size);
	}
	
	dbg_ck("Done, switching the tables and running rehash.\n");
	
	
	memcpy(tables_old, table->tables, 
	       MAX_TABLES * sizeof(ck_hash_table_item_t **));
	memcpy(table->tables, tables_new, 
	       MAX_TABLES * sizeof(ck_hash_table_item_t **));
	
	table->table_size_exp = exp_new;
	
	// delete the old tables
	for (int t = 0; t < table->table_count; ++t) {
		free(tables_old[t]);
	}
	
	return ck_rehash(table);
	//return 0;
}

int ck_insert_item(ck_hash_table_t *table, const char *key,
                   size_t length, void *value)
{
	// lock mutex to avoid write conflicts
	pthread_mutex_lock(&table->mtx_table);

	assert(value != NULL);

	dbg_ck_hash("Inserting item with key: %.*s.\n", (int)length, key);
	dbg_ck_hash_hex(key, length);
	dbg_ck_hash("\n");

	// create item structure and fill in the given data, key won't be copied
	ck_hash_table_item_t *new_item =
	        (ck_hash_table_item_t *)malloc((sizeof(ck_hash_table_item_t)));
	ck_fill_item(key, length, value, GET_GENERATION(table->generation),
	             new_item);
	
	// check if the table is not full; if yes, resize and rehash!
	if (ck_is_full(table)) {
		dbg_ck("Table is full, resize needed.\n");
		if (ck_resize_table(table) != 0) {
			dbg_ck("Failed to resize hash table!\n");
			free(new_item);
			pthread_mutex_unlock(&table->mtx_table);
			return -1;
		}
	}

	// there should be at least 2 free places
	//assert(da_try_reserve(&table->stash, 2) == 0);
	//da_reserve(&table->stash, 1);
	ck_hash_table_item_t *free_place = NULL;
	if (ck_hash_item(table, &new_item, &free_place,
	                 table->generation) != 0) {

		dbg_ck("Adding item with key %.*s to stash.\n",
		         (int)free_place->key_length, free_place->key);

		// maybe some limit on the stash and rehash if full
		if (ck_add_to_stash(table, free_place) != 0) {
			dbg_ck_hash("Could not add item to stash!!\n");
			assert(0);
		}
		
		if (ck_stash_is_full(table)) {
			dbg_ck("Stash is full, resize needed.\n");
			if (ck_resize_table(table) != 0) {
				dbg_ck("Failed to resize hash table!\n");
				/*! \todo Shouldn't 'new_item' be freed? */
				pthread_mutex_unlock(&table->mtx_table);
				return -1;
			}
		}
	}

	++table->items;
	pthread_mutex_unlock(&table->mtx_table);
	return 0;
}

/*----------------------------------------------------------------------------*/

const ck_hash_table_item_t *ck_find_item(const ck_hash_table_t *table,
                                         const char *key, size_t length)
{
	dbg_ck("ck_find_item(), key: %.*s, size: %zu\n",
	         (int)length, key, length);

	ck_hash_table_item_t **found = ck_find_item_nc(table, key, length);

	return (found == NULL) ? NULL : rcu_dereference(*found);
}

/*----------------------------------------------------------------------------*/

int ck_update_item(const ck_hash_table_t *table, const char *key, size_t length,
                   void *new_value, void (*dtor_value)(void *value))
{
	rcu_read_lock();	// is needed?

	assert(new_value != NULL);

	ck_hash_table_item_t **item = ck_find_item_nc(table, key, length);

	if (item == NULL || (*item) == NULL) {
		return -1;
	}

	void *old = rcu_xchg_pointer(&(*item)->value, new_value);
	rcu_read_unlock();

	synchronize_rcu();
	if (dtor_value) {
		dtor_value(old);
	}

	return 0;
}

/*----------------------------------------------------------------------------*/

int ck_delete_item(const ck_hash_table_t *table, const char *key, size_t length,
                   void (*dtor_value)(void *value), int delete_key)
{
	rcu_read_lock();	// is needed?
	ck_hash_table_item_t **place = ck_find_item_nc(table, key, length);

	if (place == NULL) {
		return -1;
	}

	ck_hash_table_item_t *item = *place;

	assert(item != NULL);

	ck_put_item(place, NULL);
	rcu_read_unlock();

	synchronize_rcu();
	if (dtor_value) {
		dtor_value(item->value);
	}
	item->value = NULL;
	if (delete_key != 0) {
		free((void *)item->key);
	}
	free(item);

	return 0;
}

/*----------------------------------------------------------------------------*/

ck_hash_table_item_t *ck_remove_item(ck_hash_table_t *table, const char *key, 
                                     size_t length)
{
	ck_hash_table_item_t **place = ck_find_item_nc(table, key, length);
	if (place == NULL) {
		return NULL;
	}
	
	ck_hash_table_item_t *item = *place;
	*place = NULL;
	return item;
}

/*----------------------------------------------------------------------------*/

int ck_shallow_copy(const ck_hash_table_t *from, ck_hash_table_t **to)
{
	if (from == NULL || to == NULL) {
		return -1;
	}

	*to = (ck_hash_table_t *)malloc(sizeof(ck_hash_table_t));

	if (*to == NULL) {
		ERR_ALLOC_FAILED;
		return -2;
	}
	memset(*to, 0, sizeof(ck_hash_table_t));

	// copy table count and table size exponent
	(*to)->table_size_exp = from->table_size_exp;
	(*to)->table_count = from->table_count;
	assert((*to)->table_size_exp <= 32);

	dbg_ck("Creating hash table for %u items.\n", from->table_count);
	dbg_ck("Exponent: %u, number of tables: %u\n ",
		 (*to)->table_size_exp, (*to)->table_count);
	dbg_ck("Table size: %u items, each %zu bytes, total %zu bytes\n",
	         hashsize((*to)->table_size_exp),
	         sizeof(ck_hash_table_item_t *),
	         hashsize((*to)->table_size_exp)
	           * sizeof(ck_hash_table_item_t *));

	// create tables
	for (uint t = 0; t < (*to)->table_count; ++t) {
		dbg_ck("Creating table %u...\n", t);
		(*to)->tables[t] = (ck_hash_table_item_t **)malloc(
		                        hashsize((*to)->table_size_exp)
		                        * sizeof(ck_hash_table_item_t *));
		if ((*to)->tables[t] == NULL) {
			ERR_ALLOC_FAILED;
			for (uint i = 0; i < t; ++i) {
				free((*to)->tables[i]);
			}
			free(*to);
			return -2;
		}

		// copy the table
		memcpy((*to)->tables[t], from->tables[t],
		       hashsize((*to)->table_size_exp)
		           * sizeof(ck_hash_table_item_t *));
	}

	// copy the stash - we must explicitly copy each stash item, but do not
	// copy the ck_hash_table_item_t within them.
	ck_stash_item_t *si = from->stash;
	ck_stash_item_t **pos = &(*to)->stash;
	dbg_ck_verb("Copying hash table stash.\n");
	while (si != NULL) {
		ck_stash_item_t *si_new = (ck_stash_item_t *)
		                           malloc(sizeof(ck_stash_item_t));
		if (si_new == NULL) {
			ERR_ALLOC_FAILED;
			// delete tables
			for (uint i = 0; i < (*to)->table_count; ++i) {
				free((*to)->tables[i]);
			}
			// delete created stash items
			si_new = (*to)->stash;
			while (si_new != NULL) {
				ck_stash_item_t *prev = si_new;
				si_new = si_new->next;
				free(prev);
			}
			free(*to);
			return -2;
		}

		dbg_ck("Copying stash item: %p with item %p, ", si, si->item);
		dbg_ck("key: %.*s\n", (int)si->item->key_length, si->item->key);

		si_new->item = si->item;
		*pos = si_new;
		pos = &si_new->next;
		si = si->next;


		dbg_ck("Old stash item: %p with item %p, ", si,
		       ((si == NULL) ? NULL : si->item));
		if (si != NULL) {
			dbg_ck("key: %.*s\n", (int)si->item->key_length, si->item->key);
		} else {
			dbg_ck("\n");
		}
		dbg_ck("New stash item: %p with item %p, ", si_new,
		       si_new->item);
		dbg_ck("key: %.*s\n", (int)si_new->item->key_length, 
		       si_new->item->key);
	}
	
	*pos = NULL;
	
	// there should be no item being hashed right now
	/*! \todo This operation should not be done while inserting / rehashing. 
	 */
	assert(from->hashed == NULL);
	(*to)->hashed = NULL;
	
	// initialize rehash/insert mutex
	pthread_mutex_init(&(*to)->mtx_table, NULL);

	// copy the generation
	(*to)->generation = from->generation;
	
	// copy the hash functions
	memcpy(&(*to)->hash_system, &from->hash_system, sizeof(us_system_t));
	
	return 0;
}

/*----------------------------------------------------------------------------*/

static int ck_copy_items(ck_hash_table_item_t **from,
                         ck_hash_table_item_t **to, uint32_t count)
{
	assert(from != NULL);
	assert(to != NULL);

	for (int i = 0; i < count; ++i) {
		if (from[i] != NULL) {
			to[i] = (ck_hash_table_item_t *)
				malloc(sizeof(ck_hash_table_item_t));

			if (to[i] == NULL) {
				return -2;
			}

			memcpy(to[i], from[i], sizeof(ck_hash_table_item_t));
		} else {
			to[i] = NULL;
		}
	}

	return 0;
}

/*----------------------------------------------------------------------------*/

void ck_deep_copy_cleanup(ck_hash_table_t *table, int table_count)
{
	// free tables with their items
	for (int t = 0; t < table_count; ++t) {
		for (int i = 0; i < hashsize(table->table_size_exp); ++i) {
			free(table->tables[t][i]);
		}
		free(table->tables[t]);
	}

	// free stash items with hash table items in them
	ck_stash_item_t *si = table->stash;
	ck_stash_item_t *to_free;
	while (si != NULL) {
		to_free = si;
		si = si->next;
		free(to_free->item);
		free(to_free);
	}

	free(table);
}

/*----------------------------------------------------------------------------*/

int ck_deep_copy(ck_hash_table_t *from, ck_hash_table_t **to)
{
	if (from == NULL || to == NULL) {
		return -1;
	}

	dbg_ck("Allocating new table...\n");
	*to = (ck_hash_table_t *)malloc(sizeof(ck_hash_table_t));

	if (*to == NULL) {
		ERR_ALLOC_FAILED;
		return -2;
	}
	memset(*to, 0, sizeof(ck_hash_table_t));

	// copy table count and table size exponent
	(*to)->table_size_exp = from->table_size_exp;
	(*to)->table_count = from->table_count;
	assert((*to)->table_size_exp <= 32);

	dbg_ck("Creating hash table for %u items.\n", from->table_count);
	dbg_ck("Exponent: %u, number of tables: %u\n ",
		 (*to)->table_size_exp, (*to)->table_count);
	dbg_ck("Table size: %u items, each %zu bytes, total %zu bytes\n",
	         hashsize((*to)->table_size_exp),
	         sizeof(ck_hash_table_item_t *),
	         hashsize((*to)->table_size_exp)
	           * sizeof(ck_hash_table_item_t *));

	// create tables
	for (uint t = 0; t < (*to)->table_count; ++t) {
		dbg_ck("Creating table %u...\n", t);
		(*to)->tables[t] = (ck_hash_table_item_t **)malloc(
		                        hashsize((*to)->table_size_exp)
		                        * sizeof(ck_hash_table_item_t *));
		if ((*to)->tables[t] == NULL) {
			ERR_ALLOC_FAILED;
			for (uint i = 0; i < t; ++i) {
				free((*to)->tables[i]);
			}
			free(*to);
			return -2;
		}

		// copy the table with all hash table items
		dbg_ck("Copying table %u...\n", t);
		int ret = ck_copy_items(from->tables[t], (*to)->tables[t],
		                        hashsize((*to)->table_size_exp));
		if (ret != 0) {
			dbg_ck("Failed!\n");
			// free all tables created until now
			ck_deep_copy_cleanup(*to, t);
			return ret;
		}
	}

	// copy the stash - we must explicitly copy each stash item,
	// together with the hash table item stored in it
	ck_stash_item_t *si = from->stash;
	ck_stash_item_t **pos = &(*to)->stash;
	dbg_ck_verb("Copying hash table stash.\n");
	while (si != NULL) {
		ck_stash_item_t *si_new = (ck_stash_item_t *)
		                           malloc(sizeof(ck_stash_item_t));
		if (si_new == NULL) {
			ERR_ALLOC_FAILED;
			ck_deep_copy_cleanup(*to, (*to)->table_count);
			return -2;
		}

		dbg_ck("Copying stash item: %p with item %p, ", si, si->item);
//		dbg_ck("key: %.*s\n", (int)si->item->key_length, si->item->key);

		if (si->item == NULL) {
			si_new->item = NULL;
			si_new->next = NULL;
		} else {
			si_new->item = (ck_hash_table_item_t *)
					malloc(sizeof(ck_hash_table_item_t));

			if (si_new->item == NULL) {
				ERR_ALLOC_FAILED;
				free(si_new);
				ck_deep_copy_cleanup(*to, (*to)->table_count);
				return -2;
			}

			memcpy(si_new->item, si->item,
			       sizeof(ck_hash_table_item_t));
			si_new->next = NULL;
		}

		*pos = si_new;
		pos = &si_new->next;
		si = si->next;

dbg_ck_exec(
		dbg_ck("Old stash item: %p with item %p, ", si,
		       ((si == NULL) ? NULL : si->item));
		if (si != NULL && si->item != NULL) {
			dbg_ck("key: %.*s\n", (int)si->item->key_length,
			       si->item->key);
		} else {
			dbg_ck("\n");
		}
		dbg_ck("New stash item: %p with item %p, ", si_new,
		       (si_new) ? si_new->item : NULL);

		assert(si_new != NULL);
		assert(si_new->item != NULL);
		dbg_ck("key: %.*s\n", (int)si_new->item->key_length,
		       si_new->item->key);
);
	}

	*pos = NULL;

	// there should be no item being hashed right now
	/*! \todo This operation should not be done while inserting / rehashing.
	 */
	assert(from->hashed == NULL);
	(*to)->hashed = NULL;

	// initialize rehash/insert mutex
	pthread_mutex_init(&(*to)->mtx_table, NULL);

	// copy the generation
	(*to)->generation = from->generation;

	// copy the hash functions
	memcpy(&(*to)->hash_system, &from->hash_system, sizeof(us_system_t));

	return 0;
}

/*----------------------------------------------------------------------------*/

int ck_apply(ck_hash_table_t *table,
             void (*function)(ck_hash_table_item_t *item, void *data), 
             void *data)
{
	if (table == NULL || function == NULL) {
		return -1;
	}
	
	/*! \todo Ensure that no insertion nor rehash is made during applying.*/
	
	// apply the function to all items in all tables
	for (int t = 0; t < table->table_count; ++t) {
		for (int i = 0; i < hashsize(table->table_size_exp); ++i) {
			function(table->tables[t][i], data);
		}
	}
	
	// apply the function to the stash items
	ck_stash_item_t *si = table->stash;
	while (si != NULL) {
		function(si->item, data);
		si = si->next;
	}
	
	return 0;
}

/*----------------------------------------------------------------------------*/

int ck_rehash(ck_hash_table_t *table)
{
	dbg_ck_hash("Rehashing items in table.\n");
	SET_REHASHING_ON(&table->generation);

	ck_stash_item_t *free_stash_items = NULL;

	do {
		// 1) Rehash items from stash
		dbg_ck_rehash("Rehashing items from stash.\n");
		ck_stash_item_t *item = table->stash;
		ck_stash_item_t **item_place = &table->stash;
		// terminate when at the end; this way the newly added items
		// (added to the beginning) will be properly ignored
		while (item != NULL) {
			dbg_ck_rehash("Rehashing item with "
			  "key (length %zu): %.*s, generation: %hu, "
			  "table generation: %hu.\n", item->item->key_length,
			  (int)item->item->key_length, item->item->key,
			  GET_GENERATION(
				item->item->timestamp),
			  GET_GENERATION(table->generation));

			// put the hashed item to the prepared space
			table->hashed = item->item;
			item->item = NULL;
			// we may use the place in the stash item as the free
			// place for rehashing
			if (ck_hash_item(table, &table->hashed, &item->item,
			             NEXT_GENERATION(table->generation)) != 0) {
				// the free place was used
				assert(item->item != NULL);
				// we may leave the item there (in the stash)
				assert(EQUAL_GENERATIONS(item->item->timestamp,
				           NEXT_GENERATION(table->generation)));
				//assert(item->item == table->hashed);

				item_place = &item->next;
				item = item->next;
			} else {
				// the free place should be free
				assert(item->item == NULL);
				// and the item should be hashed too
//				assert(table->hashed == NULL);

				// fix the pointer from the previous hash item
				*item_place = item->next;
				// and do not change the item place pointer

				// put the stash item into list of free stash
				// items
				item->next = free_stash_items;
				free_stash_items = item;

				item = *item_place;
			}
		}

		// 2) Rehash items from tables

		// in case of failure, save the item in a temp variable
		// which will be put to the stash
		ck_hash_table_item_t *free = NULL;
		assert(table->hashed == NULL);
//		ck_hash_table_item_t *old = table->hashed;

		for (uint t = 0; t < table->table_count; ++t) {
			uint rehashed = 0;

			dbg_ck_rehash("Rehashing table %d.\n", t);

			while (rehashed < hashsize(table->table_size_exp)) {

				// if item's generation is the new generation,
				// skip
				if (table->tables[t][rehashed] == NULL
				    || !(EQUAL_GENERATIONS(
				          table->tables[t][rehashed]->timestamp,
				          table->generation))) {
					dbg_ck_rehash("Skipping item.\n");
					++rehashed;
					continue;
				}

				dbg_ck_rehash("Rehashing item with hash %u, "
				  "key (length %zu): %.*s, generation: %hu, "
				  "table generation: %hu.\n", rehashed,
				  table->tables[t][rehashed]->key_length,
				  (int)(table->tables[t][rehashed]->key_length),
				  table->tables[t][rehashed]->key,
				  GET_GENERATION(
					table->tables[t][rehashed]->timestamp),
				  GET_GENERATION(table->generation));

				// otherwise copy the item for rehashing
				ck_put_item(&table->hashed, table->tables[t][rehashed]);
				// clear the place so that this item will not
				// get rehashed again
				ck_clear_item(&table->tables[t][rehashed]);

				dbg_ck_rehash("Table generation: %hu, next "
				            "generation: %hu.\n",
				            GET_GENERATION(table->generation),
				            NEXT_GENERATION(table->generation));

				if (ck_hash_item(table, &table->hashed, &free,
				     NEXT_GENERATION(table->generation)) != 0) {
					// loop occured
					dbg_ck_hash("Hashing entered a loop."
						      "\n");
					dbg_ck_rehash("Item with key %.*s "
					  "inserted into the free slot.\n",
					  free->key_length, free->key);

					//assert(old == free);

					// put the item into the stash, but
					// try the free stash items first
					if (free_stash_items != NULL) {
						// take first
						ck_stash_item_t *item =
							free_stash_items;
						free_stash_items = item->next;

						item->item = free;
						item->next = table->stash;
						table->stash = item;
					} else {
						if (ck_add_to_stash(table, free)
						    != 0) {
							ck_rollback_rehash(
								table);
						}
					}

					free = NULL;
					table->hashed = NULL;
				}
				++rehashed;
			}
		}

		dbg_ck_rehash("Old table generation: %u\n",
		                GET_GENERATION(table->generation));
		// rehashing completed, switch generation of the table
		SET_NEXT_GENERATION(&table->generation);
		dbg_ck_rehash("New table generation: %u\n",
		                GET_GENERATION(table->generation));
		// generate new hash functions for the old generation
		dbg_ck_rehash("Generating coeficients for generation: %u\n",
		                NEXT_GENERATION(table->generation));
		us_next(&table->hash_system,
		        NEXT_GENERATION(table->generation));

	} while (false /*! \todo Add proper condition!! */);

	SET_REHASHING_OFF(&table->generation);

	assert(table->hashed == NULL);


	while (free_stash_items != NULL) {
		ck_stash_item_t *item = free_stash_items;
		free_stash_items = item->next;
		assert(item->item == NULL);
		free(item);
	}

	return 0;
}

/*----------------------------------------------------------------------------*/
/*!
 * \brief Rehashes the whole table.
 *
 * \param table Hash table to be rehashed.
 *
 * \note While rehashing no item should be inserted as it will result in a
 *       deadlock.
 *
 * \retval 0 No error.
 * \retval -1 Rehashing failed. Some items may have been already moved and the
 *            rehashing flag remains set.
 *
 * \todo What if the stash is reallocated during ck_hash_item()? We'd be using
 *       the old stash for saving items! The old stash would not get deallocated
 *       (due to RCU - maybe put some rcu_read_lock() here), but the item
 *       would not be saved into the new stash!
 *       Maybe add a function for getting a pointer to particular item from
 *       the dynamic array and protect it using rcu_read_lock().
 *       Other option: Do not use pointer to an item in stash in the call to
 *       ck_hash_item(). Use some new place & put the item to the stash
 *       afterwards, protecting it using rcu_read_lock() and rcu_assign_pointer.
 */
//int ck_rehash(ck_hash_table_t *table)
//{
//	dbg_ck_rehash("Rehashing items in table.\n");
//	SET_REHASHING_ON(&table->generation);

//	// we already have functions for the next generation, begin rehashing
//	// we wil use the last item in the buffer as free cell for hashing
//	assert(da_try_reserve(&table->stash, 1) == 0);
//	ck_hash_table_item_t *old = (ck_hash_table_item_t *)
//	                          (malloc(sizeof(ck_hash_table_item_t)));

//	do {
//		dbg_ck_hash("Rehash!\n");

//		if (da_get_count(&table->stash) > STASH_SIZE) {
//			dbg_ck_hash("STASH RESIZED!!! (new stash size: %d)\n",
//			              da_get_count(&table->stash));
//		}

//		// rehash items from stash, starting from the last old item
//		int stash_i = da_get_count(&table->stash) - 1;
//		while (stash_i >= 0) {
//			// if item's generation is the new generation, skip
//			if (STASH_ITEMS(&table->stash)[stash_i] == NULL
//			    || !(EQUAL_GENERATIONS(STASH_ITEMS(&table->stash)
//			                            [stash_i]->timestamp,
//			                            table->generation))) {
//				dbg_ck_rehash("Skipping item.\n");
//				--stash_i;
//				continue;
//			}

//			dbg_ck_rehash("Rehashing item from buffer position %u"
//			                ", key (length %u): %.*s, generation: "
//			                "%hu, table generation: %hu.\n",
//			   stash_i,
//			   STASH_ITEMS(&table->stash)[stash_i]->key_length,
//			   (int)STASH_ITEMS(&table->stash)[stash_i]->key_length,
//			   STASH_ITEMS(&table->stash)[stash_i]->key,
//			   GET_GENERATION(
//				STASH_ITEMS(&table->stash)[stash_i]->timestamp),
//			   GET_GENERATION(table->generation));

//			// otherwise copy the item for rehashing
//			ck_put_item(&old, STASH_ITEMS(&table->stash)[stash_i]);
//			// clear the place so that this item will not get
//			// rehashed again
//			ck_clear_item(&STASH_ITEMS(&table->stash)[stash_i]);
//			da_release(&table->stash, 1);

//			// there should be at least one place in the stash
//			assert(da_try_reserve(&table->stash, 1) == 0);
//			da_reserve(&table->stash, 1);

//			assert(STASH_ITEMS(&table->stash)[stash_i] == NULL);

//			// and start rehashing
//			if (ck_hash_item(table, &old,
//			             &STASH_ITEMS(&table->stash)[stash_i],
//			             NEXT_GENERATION(table->generation)) != 0) {
//				// loop occured
//				dbg_ck_hash("Hashing entered a loop.\n");

//				dbg_ck_rehash("Item with key %.*s inserted "
//					"into the stash on position %d.\n",
//					STASH_ITEMS(&table->stash)
//						[stash_i]->key_length,
//					STASH_ITEMS(&table->stash)
//						[stash_i]->key,
//					da_get_count(&table->stash));

//				// hashing unsuccessful, the item was inserted
//				// into the stash
//				da_occupy(&table->stash, 1);
//				assert(STASH_ITEMS(&table->stash)[stash_i]
//				       != NULL);

//				// if only one place left, resize the stash
//				// TODO: Why???
//				if (da_reserve(&table->stash, 2) < 0) {
//					// stash could not be resized => !!!
//					dbg_ck_hash("Failed to rehash items "
//					              "from "
//					  "table, no other rehash possible!\n");
//					// so rollback
//					ck_rollback_rehash(table);
//					// clear the 'old' item
//					ck_clear_item(&old);
//					return -1;
//				}
//			}

//			// clear the 'old' item
//			ck_clear_item(&old);
//			// decrement the index
//			--stash_i;
//		}

//		uint i = 0;
//		while (i < da_get_count(&table->stash)) {
//			assert(STASH_ITEMS(&table->stash)[i] != NULL);
//			++i;
//		}
//		dbg_ck_hash("OK\n");
//		assert(da_try_reserve(&table->stash, 1) == 0);
//		assert(STASH_ITEMS(&table->stash)[da_get_count(&table->stash)]
//		       == NULL);

//		// rehash items from hash tables
//		for (uint t = TABLE_FIRST;
//		     t <= TABLE_LAST(table->table_count); ++t) {
//			dbg_ck_rehash("Rehashing items from table %d.\n",
//			                t + 1);
//			uint rehashed = 0;

//			while (rehashed < hashsize(table->table_size_exp)) {

//				// if item's generation is the new generation,
//				// skip
//				if (table->tables[t][rehashed] == NULL
//				    || !(EQUAL_GENERATIONS(
//				          table->tables[t][rehashed]->timestamp,
//				          table->generation))) {
//					dbg_ck_rehash("Skipping item.\n");
//					++rehashed;
//					continue;
//				}

//				dbg_ck_rehash("Rehashing item with hash %u, "
//				  "key (length %u): %.*s, generation: %hu, "
//				  "table generation: %hu.\n", rehashed,
//				  table->tables[t][rehashed]->key_length,
//				  (int)(table->tables[t][rehashed]->key_length),
//				  table->tables[t][rehashed]->key,
//				  GET_GENERATION(
//					table->tables[t][rehashed]->timestamp),
//				  GET_GENERATION(table->generation));

//				// otherwise copy the item for rehashing
//				ck_put_item(&old, table->tables[t][rehashed]);
//				// clear the place so that this item will not
//				// get rehashed again
//				ck_clear_item(&table->tables[t][rehashed]);

//				dbg_ck_rehash("Table generation: %hu, next "
//				            "generation: %hu.\n",
//				            GET_GENERATION(table->generation),
//				            NEXT_GENERATION(table->generation));

//				// and start rehashing
//				assert(&old != &STASH_ITEMS(&table->stash)[
//				               da_get_count(&table->stash)]);
//				assert(da_try_reserve(&table->stash, 1) == 0);
//				da_reserve(&table->stash, 1);

//				if (ck_hash_item(table, &old,
//				     &STASH_ITEMS(&table->stash)[
//				       da_get_count(&table->stash)],
//				     NEXT_GENERATION(table->generation)) != 0) {
//					// loop occured
//					dbg_ck_hash("Hashing entered a loop."
//						      "\n");
//					dbg_ck_rehash("Item with key %.*s "
//					  "inserted into the stash on position "
//					  "%d.\n", STASH_ITEMS(&table->stash)[
//					      da_get_count(&table->stash)]
//					         ->key_length,
//					  STASH_ITEMS(&table->stash)[
//					      da_get_count(&table->stash)]->key,
//					  da_get_count(&table->stash));

//					assert(STASH_ITEMS(&table->stash)[
//					  da_get_count(&table->stash)] != NULL);
//					// loop occured, the item is already at
//					// its new place in the buffer, so just
//					// increment the index
//					da_occupy(&table->stash, 1);

//					// if only one place left, resize the
//					// stash TODO: Why?
//					if (da_reserve(&table->stash, 2) < 0) {
//						// stash could not be resized
//						dbg_ck_hash("Failed to rehash"
//						  " items from table, no other "
//						  "rehash possible!\n");
//						// so rollback
//						ck_rollback_rehash(table);
//						// clear the 'old' item
//						ck_clear_item(&old);
//						return -1;
//					}
//				}
//				++rehashed;
//			}
//		}

//		dbg_ck_rehash("Old table generation: %u\n",
//		                GET_GENERATION(table->generation));
//		// rehashing completed, switch generation of the table
//		SET_NEXT_GENERATION(&table->generation);
//		dbg_ck_rehash("New table generation: %u\n",
//		                GET_GENERATION(table->generation));
//		// generate new hash functions for the old generation
//		dbg_ck_rehash("Generating coeficients for generation: %u\n",
//		                NEXT_GENERATION(table->generation));
//		us_next(NEXT_GENERATION(table->generation));

//		// repeat rehashing while there are more items in the stash than
//		// its initial size
//		if (da_get_count(&table->stash) > STASH_SIZE) {
//			dbg_ck_rehash("Rehashing again!\n");
//		}
//	} while (da_get_count(&table->stash) > STASH_SIZE);

//	SET_REHASHING_OFF(&table->generation);

//	return 0;
//}

/*----------------------------------------------------------------------------*/

void ck_dump_table(const ck_hash_table_t *table)
{
#ifdef CUCKOO_DEBUG
	uint i = 0;
	dbg_ck("----------------------------------------------\n");
	dbg_ck("Hash table dump:\n\n");
	dbg_ck("Size of each table: %u\n\n", hashsize(table->table_size_exp));

	for (uint t = 0; t < table->table_count; ++t) {
		dbg_ck("Table %d:\n", t + 1);

		for (i = 0; i < hashsize(table->table_size_exp); i++) {
			dbg_ck("Hash: %u, Key: %.*s, Value: %p.\n", i,
			         (int)(table->tables[t])[i]->key_length,
			         (table->tables[t])[i]->key,
			         (table->tables[t])[i]->value);
		}
	}

	dbg_ck("Stash:\n");
//	for (i = 0; i < da_get_count(&table->stash); ++i) {
//		dbg_ck("Index: %u, Key: %.*s Value: %p.\n", i,
//		         ((ck_hash_table_item_t **)
//		             da_get_items(&table->stash))[i]->key_length,
//		         ((ck_hash_table_item_t **)
//		             da_get_items(&table->stash))[i]->key,
//		         ((ck_hash_table_item_t **)
//		             da_get_items(&table->stash))[i]->value);
//	}
	ck_stash_item_t *item = table->stash;
	while (item != NULL) {
		dbg_ck("Hash: %u, Key: %.*s, Value: %p.\n", i,
			 (int)item->item->key_length, item->item->key,
			 item->item->value);
		item = item->next;
	}

	dbg_ck("\n");
#endif
}