-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhattrie64d.c
2019 lines (1584 loc) · 43.2 KB
/
hattrie64d.c
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
// Author Karl Malbrain, malbrain@cal.berkeley.edu
// Implement Simplified HAT-trie w/associated data areas,
// and bi-directional cursors
// Adapted from the ideas of Douglas Baskins of HP
// and Dr. Askitis.
// The ASKITIS benchmarking option was implemented with
// assistance from Dr. Nikolas Askitis (www.naskitis.com).
// functions:
// hat_open: open a new hat array returning a hat object.
// hat_close: close an open hat array, freeing all memory.
// hat_data: allocate data memory within hat array for external use.
// hat_cell: insert a string into the HAT tree, return associated data addr.
// hat_cursor: return a sort cursor for the HAT tree. Free with free().
// hat_key: return the key from the HAT trie at the current cursor location.
// hat_nxt: move the cursor to the next key in the HAT trie, return TRUE/FALSE.
// hat_prv: move the cursor to the prev key in the HAT trie, return TRUE/FALSE.
// hat_start: move the cursor to the first key >= given key, return TRUE/FALSE.
// hat_last: move the cursor to the last key in the HAT trie, return TRUE/FALSE
// hat_slot: return the pointer to the associated data area for cursor.
#ifdef linux
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define __USE_FILE_OFFSET64
#include <endian.h>
#else
#ifdef __BIG_ENDIAN__
#ifndef BYTE_ORDER
#define BYTE_ORDER 4321
#endif
#else
#ifndef BYTE_ORDER
#define BYTE_ORDER 1234
#endif
#endif
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 4321
#endif
#endif
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#if defined(_WIN32)
typedef unsigned short ushort;
#endif
typedef unsigned char uchar;
typedef unsigned int uint;
#define PRIuint "u"
#if defined(__LP64__) || \
defined(__x86_64__) || \
defined(__amd64__) || \
defined(_WIN64) || \
defined(__sparc64__) || \
defined(__arch64__) || \
defined(__powerpc64__) || \
defined (__s390x__)
// defines for 64 bit
typedef unsigned long long HatSlot;
#define HAT_slot_size 8
#define PRIhatvalue "llu"
#else
// defines for 32 bit
typedef uint HatSlot;
#define HAT_slot_size 4
#define PRIhatvalue "u"
#endif
#define HAT_mask (~(HatSlot)0x07)
#define HAT_type ((HatSlot)0x07)
#define HAT_node_size 16
typedef struct {
HatSlot array[0]; // hash array of pail arrays
} HatPail;
typedef struct {
uint count;
HatSlot slots[0];
} HatBucket;
#define HAT_cache_line 8 // allocation granularity is 8 bytes
#include <assert.h>
#include <stdio.h>
unsigned long long MaxMem = 0;
unsigned long long Searches = 0;
unsigned long long Probes = 0;
unsigned long long Bucket = 0;
unsigned long long Pail = 0;
unsigned long long Radix = 0;
unsigned long long Small = 0;
// void hat_abort (char *msg) __attribute__ ((noreturn)); // Tell static analyser that this function will not return
void hat_abort (char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
// allow room for 64K bucket slots and HatSeg structure
#define HAT_seg (65536 * HAT_slot_size + 32)
enum HAT_types {
HAT_radix = 0, // radix nodes
HAT_bucket = 1, // bucket nodes
HAT_array = 2, // linear array nodes
HAT_pail = 3, // hashed linear array nodes
HAT_1 = 4,
HAT_2 = 5,
HAT_3 = 6,
HAT_4 = 7,
HAT_6 = 8,
HAT_8 = 9,
HAT_10 = 10,
HAT_12 = 11,
HAT_14 = 12,
HAT_16 = 13,
HAT_24 = 14,
HAT_32 = 15,
};
uint HatSize[32] = {
(HAT_slot_size * 128), // HAT_radix node size
(sizeof(HatBucket)), // HAT_bucket node size
(0), // HAT_array node size below
(sizeof(HatPail)), // HAT_pail node size
(1 * HAT_node_size), // HAT_1 array size
(2 * HAT_node_size), // HAT_2 array size
(3 * HAT_node_size), // HAT_3 array size
(4 * HAT_node_size), // HAT_4 array size
(6 * HAT_node_size), // HAT_6 array size
(8 * HAT_node_size), // HAT_8 array size
(10 * HAT_node_size), // HAT_10 array size
(12 * HAT_node_size), // HAT_12 array size
(14 * HAT_node_size), // HAT_14 array size
(16 * HAT_node_size), // HAT_16 array size
(24 * HAT_node_size), // HAT_24 array size
(32 * HAT_node_size), // HAT_32 array size
};
uint HatBucketSlots = 2047;
uint HatBucketMax = 65536;
uint HatPailMax = 127;
uchar HatMax = HAT_32;
typedef struct {
void *seg; // next used allocator
uint next; // next available offset
} HatSeg;
typedef struct {
void **reuse[32]; // reuse hat blocks
int counts[32]; // hat block counters
HatSeg *seg; // current hat allocator
uint bootlvl; // cascaded radix nodes in root
uint aux; // auxilliary bytes per key
HatSlot root[0]; // base root of hat array
} Hat;
typedef struct {
ushort nxt; // next key array allocation
uchar type; // type of base node
uchar cnt; // next data area allocation
uchar keys[0]; // keys byte array
} HatBase;
typedef struct {
uchar *key; // pointer to key string
void *slot; // user data area
} HatSort;
typedef struct {
int cnt; // number of bucket keys
int idx; // current bucket index
short top; // current stack top
ushort aux; // number of aux bytes per key
int rootlvl; // number of root levels
uint maxroot; // count of root array slots
uint rootscan; // triple root scan index
HatSlot next[256]; // radix node stack
uchar scan[256]; // radix node scan index stack
HatSort keys[0]; // sorted array for bucket
} HatCursor;
int hat_nxt (HatCursor *cursor);
// ternery quick sort of cursor's keys
// modelled after R Sedgewick's
// "Quicksort with 3-way partitioning"
vecswap (int i, int j, int n, HatSort *x)
{
HatSort swap[1];
while( n-- ) {
*swap = x[i];
x[i++] = x[j];
x[j++] = *swap;
}
}
void hat_qsort (HatSort *x, int n, uchar o)
{
ushort skip, skipb, skipc, len;
uchar pivot, chb, chc, *key;
int a, b, c, d, r;
HatSort swap[1];
while( n > 10 ) {
a = rand () % n;
*swap = x[0];
x[0] = x[a];
x[a] = *swap;
len = x[0].key[0];
if( len & 0x80 )
len &= 0x7f, len += x[0].key[1] << 7, skip = 2;
else
skip = 1;
if( len > o )
pivot = x[0].key[o+skip];
else
pivot = 0;
a = b = 1;
c = d = n - 1;
while( 1 ) {
while( b <= c ) {
len = x[b].key[0];
if( len & 0x80 )
len &= 0x7f, len += x[b].key[1] << 7, skip = 2;
else
skip = 1;
if( len > o )
chb = x[b].key[o+skip];
else
chb = 0;
if( chb > pivot )
break;
if( chb == pivot ) {
*swap = x[a];
x[a++] = x[b];
x[b] = *swap;
}
b += 1;
}
while( b <= c ) {
len = x[c].key[0];
if( len & 0x80 )
len &= 0x7f, len += x[c].key[1] << 7, skip = 2;
else
skip = 1;
if( len > o )
chc = x[c].key[o+skip];
else
chc = 0;
if( chc < pivot )
break;
if( chc == pivot ) {
*swap = x[c];
x[c] = x[d];
x[d--] = *swap;
}
c -= 1;
}
if( b > c )
break;
*swap = x[b];
x[b++] = x[c];
x[c--] = *swap;
}
r = a < b-a ? a : b-a;
vecswap (0, b-r, r, x);
r = d-c < n-d-1 ? d-c : n-d-1;
vecswap (b, n-r, r, x);
if( r = d - c )
hat_qsort (x + n - r, r, o);
if( r = b - a )
hat_qsort (x, r, o);
len = x[r].key[0];
if( len & 0x80 )
len &= 0x7f, len += x[r].key[1] << 7;
if( len == o )
return;
n += a - d - 1;
x += r;
o += 1;
}
if( n > 1 ) {
a = 0;
while( ++a < n )
for( b = a; b > 0; b-- ) {
chb = x[b-1].key[0];
if( chb & 0x80 )
chb &= 0x7f, chb += x[b-1].key[1] << 7, skipb = 2;
else
skipb = 1;
chc = x[b].key[0];
if( chc & 0x80 )
chc &= 0x7f, chc += x[b].key[1] << 7, skipc = 2;
else
skipc = 1;
r = o;
d = 0;
while( r < chb && r < chc )
if( d = x[b-1].key[r+skipb] - x[b].key[r+skipc] )
break;
else
r++;
if( d > 0 || d == 0 && chb > chc ) {
*swap = x[b];
x[b] = x[b-1];
x[b-1] = *swap;
}
}
}
}
// strip out pointers from HAT_array node
// to elements of the sorted array
int hat_strip_array (HatCursor *cursor, HatSlot node, HatSort *list)
{
HatBase *base = (HatBase *)(node & HAT_mask);
uint size = HatSize[base->type];
ushort tst = 0;
ushort cnt = 0;
ushort len;
while( tst < base->nxt ) {
list[cnt].slot = (uchar *)base + size - (cnt+1) * cursor->aux;
list[cnt].key = base->keys + tst;
len = base->keys[tst++];
if( len & 0x80 )
len &= 0x7f, len += base->keys[tst++] << 7;
tst += len;
cnt++;
}
return cnt;
}
int hat_strip_pail (HatCursor *cursor, HatSlot node, HatSort *list)
{
HatPail *pail = (HatPail *)(node & HAT_mask);
uint total = 0;
int idx;
for( idx = 0; idx < HatPailMax; idx++ )
if( pail->array[idx] )
total += hat_strip_array (cursor, pail->array[idx], list);
return total;
}
// sort current bucket into cursor array
// find and sort current node entry
// either Bucket or Array
void hat_sort (HatCursor *cursor)
{
HatBucket *bucket;
uint off, idx;
uchar len, ch;
uint cnt;
switch( cursor->next[cursor->top] & HAT_type ) {
case HAT_array:
cursor->cnt = hat_strip_array (cursor, cursor->next[cursor->top], cursor->keys);
break;
case HAT_pail:
cursor->cnt = hat_strip_pail (cursor, cursor->next[cursor->top], cursor->keys);
break;
case HAT_bucket:
bucket = (HatBucket *)(cursor->next[cursor->top] & HAT_mask);
cursor->cnt = 0;
for( idx = 0; idx < HatBucketSlots; idx++ )
switch( bucket->slots[idx] & HAT_type ) {
case HAT_array:
cursor->cnt += hat_strip_array (cursor, bucket->slots[idx], cursor->keys + cursor->cnt);
continue;
case HAT_pail:
cursor->cnt += hat_strip_pail (cursor, bucket->slots[idx], cursor->keys + cursor->cnt);
continue;
}
break;
}
hat_qsort (cursor->keys, cursor->cnt, 0);
}
int hat_greater (HatCursor *cursor, uchar *buff, uint max)
{
ushort len;
ushort tst;
// find first key >= given key
for( cursor->idx = 0; cursor->idx < cursor->cnt; cursor->idx++ ) {
tst = 1;
len = cursor->keys[cursor->idx].key[0];
if( len & 0x80 )
len &= 0x7f, len += cursor->keys[cursor->idx].key[1] << 7, tst = 2;
if( memcmp (cursor->keys[cursor->idx].key + tst, buff, len > max ? max : len) )
continue;
if( len >= max )
return 1;
}
// given key > every key in bucket
return hat_nxt (cursor);
}
// open new sort cursor into collection
void *hat_cursor (Hat *hat)
{
HatCursor *cursor;
uint size;
size = sizeof(HatCursor) + HatBucketMax * sizeof(HatSort);
cursor = malloc (size);
memset (cursor, 0, size);
cursor->next[0] = (HatSlot)hat->root;
cursor->aux = hat->aux;
cursor->maxroot = 1;
for( cursor->rootlvl = 0; cursor->rootlvl < hat->bootlvl; cursor->rootlvl++ )
cursor->maxroot *= 128;
return cursor;
}
void *hat_start (HatCursor *cursor, uchar *buff, uint max)
{
HatSlot *radix, *root;
HatSlot next;
uint off = 0;
uint idx;
uchar ch;
if( max > 255 )
max = 255;
for( idx = 0; idx < cursor->rootlvl; idx++ ) {
cursor->rootscan *= 128;
if( off < max )
cursor->rootscan += buff[off++];
}
// find first root >= given key
root = (HatSlot *)(cursor->next[0]);
cursor->top = 0;
if( next = root[cursor->rootscan] ) {
cursor->next[++cursor->top] = next;
loop:
if( (cursor->next[cursor->top] & HAT_type) == HAT_radix ) {
if( max > off )
ch = buff[off++];
else
ch = 0;
radix = (HatSlot *)(cursor->next[cursor->top] & HAT_mask);
while( ch < 128 )
if( radix[ch] ) {
cursor->scan[cursor->top] = ch;
cursor->next[++cursor->top] = radix[ch];
goto loop;
} else
max = 0, ch++;
// given key > every key
if( hat_nxt (cursor) )
return cursor;
free (cursor);
return NULL;
}
hat_sort (cursor);
cursor->idx = 0;
if( hat_greater (cursor, buff + off, max - off) )
return cursor;
free (cursor);
return NULL;
}
// scan to next occupied root
cursor->top++;
if( hat_nxt (cursor) )
return cursor;
free (cursor);
return NULL;
}
// return user area slot address at given cursor location
void *hat_slot (HatCursor *cursor)
{
return cursor->keys[cursor->idx].slot;
}
// advance cursor to next key
// returning false if EOT
int hat_nxt (HatCursor *cursor)
{
HatSlot *radix;
uint idx, max;
uchar ch;
// any keys left in current sorted array?
if( ++cursor->idx < cursor->cnt )
return 1;
// move thru radix nodes
// slot zero is the triple root
while( --cursor->top >= 0 ) {
radix = (HatSlot *)(cursor->next[cursor->top] & HAT_mask);
if( !cursor->top )
max = cursor->maxroot;
else
max = 128;
if( cursor->top )
idx = cursor->scan[cursor->top];
else
idx = cursor->rootscan;
while( ++idx < max )
if( radix[idx] ) {
if( cursor->top )
cursor->scan[cursor->top] = idx;
else
cursor->rootscan = idx;
cursor->next[++cursor->top] = radix[idx];
loop:
if( (cursor->next[cursor->top] & HAT_type) == HAT_radix ) {
radix = (HatSlot *)(cursor->next[cursor->top] & HAT_mask);
for( ch = 0; ch < 128; ch++ )
if( radix[ch] ) {
cursor->scan[cursor->top] = ch;
cursor->next[++cursor->top] = radix[ch];
goto loop;
}
}
hat_sort (cursor);
cursor->idx = 0;
return 1;
}
}
return 0;
}
// advance cursor to previous key
// returning false if BOI
int hat_prv (HatCursor *cursor)
{
HatSlot *radix;
uint idx, max;
uchar ch;
// any keys left in current sorted array?
if( cursor->idx )
return cursor->idx--, 1;
// move down thru radix nodes
// slot zero is the triple root
while( --cursor->top >= 0 ) {
radix = (HatSlot *)(cursor->next[cursor->top] & HAT_mask);
if( cursor->top )
idx = cursor->scan[cursor->top];
else
idx = cursor->rootscan;
while( idx-- )
if( radix[idx] ) {
if( cursor->top )
cursor->scan[cursor->top] = idx;
else
cursor->rootscan = idx;
cursor->next[++cursor->top] = radix[idx];
loop:
if( (cursor->next[cursor->top] & HAT_type) == HAT_radix ) {
radix = (HatSlot *)(cursor->next[cursor->top] & HAT_mask);
for( ch = 128; ch-- > 0; )
if( radix[ch] ) {
cursor->scan[cursor->top] = ch;
cursor->next[++cursor->top] = radix[ch];
goto loop;
}
}
hat_sort (cursor);
cursor->idx = cursor->cnt - 1;
return 1;
}
}
return 0;
}
// advance cursor to last key in the trie
// returning false if tree is empty
int hat_last (HatCursor *cursor)
{
HatSlot *radix, next, *root;
uint idx, max;
uchar ch;
// find last root
// or return if tree is empty
cursor->rootscan = cursor->maxroot;
root = (HatSlot *)(cursor->next[0]);
cursor->top = 0;
while( cursor->rootscan )
if( next = root[--cursor->rootscan] )
break;
else if( !cursor->rootscan )
return 0;
cursor->next[++cursor->top] = next;
loop:
if( (cursor->next[cursor->top] & HAT_type) == HAT_radix ) {
radix = (HatSlot *)(cursor->next[cursor->top] & HAT_mask);
ch = 128;
while( ch-- )
if( radix[ch] ) {
cursor->scan[cursor->top] = ch;
cursor->next[++cursor->top] = radix[ch];
goto loop;
}
}
hat_sort (cursor);
cursor->idx = cursor->cnt - 1;
return 1;
}
// return key at current cursor location
uint hat_key (HatCursor *cursor, uchar *buff, uint max)
{
int idx, scan, len;
uchar *key, ch;
uint off = 0;
max--; // leave room for terminator
// is cursor at EOF?
if( cursor->top < 0 ) {
if( max )
buff[0] = 0;
return 0;
}
// fill in from triple root
// and cascaded radix nodes
for( idx = 0; idx < cursor->top; idx++ )
if( !idx ) {
for( scan = cursor->rootlvl; scan--; )
if( ch = (cursor->rootscan >> scan * 7) & 0x7F )
if( off < max )
buff[off++] = ch;
} else if( off < max )
if( ch = cursor->scan[idx] ) // skip slot zero
buff[off++] = ch;
// pull rest of key from current entry in sorted array
key = cursor->keys[cursor->idx].key;
len = *key++;
if( len & 0x80 )
len &= 0x7f, len += *key++ << 7;
while( len-- && off < max )
buff[off++] = *key++;
buff[off] = 0;
return off;
}
// allocate hat node
void *hat_alloc (Hat *hat, uint type)
{
uint amt, idx, round;
HatSeg *seg;
void *block;
amt = HatSize[type];
hat->counts[type]++;
if( amt & (HAT_cache_line - 1) )
amt |= (HAT_cache_line - 1), amt += 1;
// see if free block is already available
if( (block = hat->reuse[type]) ) {
hat->reuse[type] = *(void **)block;
memset (block, 0, amt);
return (void *)block;
}
if( hat->seg->next + amt > HAT_seg ) {
if( (seg = malloc (HAT_seg)) ) {
seg->next = sizeof(*seg);
seg->seg = hat->seg;
hat->seg = seg;
if( round = (HatSlot)seg & (HAT_cache_line - 1) )
seg->next += HAT_cache_line - round;
} else {
hat_abort("Out of virtual memory");
}
MaxMem += HAT_seg;
}
block = (void *)((uchar *)hat->seg + hat->seg->next);
hat->seg->next += amt;
memset (block, 0, amt);
return block;
}
void *hat_data (Hat *hat, uint amt)
{
HatSeg *seg;
void *block;
uint round;
if( amt & (HAT_cache_line - 1))
amt |= (HAT_cache_line - 1), amt += 1;
if( hat->seg->next + amt > HAT_seg ) {
if( (seg = malloc (HAT_seg)) ) {
seg->next = sizeof(*seg);
seg->seg = hat->seg;
hat->seg = seg;
if( round = (HatSlot)seg & (HAT_cache_line - 1) )
seg->next += HAT_cache_line - round;
} else {
hat_abort("Out of virtual memory");
}
MaxMem += HAT_seg;
}
block = (void *)((uchar *)hat->seg + hat->seg->next);
hat->seg->next += amt;
memset (block, 0, amt);
return block;
}
void hat_free (Hat *hat, void *block, int type)
{
*((void **)(block)) = hat->reuse[type];
hat->reuse[type] = (void **)block;
hat->counts[type]--;
return;
}
// open hat object
// call with number of radix levels to boot into root
// and number of auxilliary user bytes to assign to each key
void *hat_open (int boot, int aux)
{
uint amt, size = HAT_slot_size, round;
HatSeg *seg;
Hat *hat;
int idx;
for( idx = 0; idx < boot; idx++ )
size *= 128;
amt = sizeof(Hat) + size;
if( amt & (HAT_cache_line - 1) )
amt |= HAT_cache_line - 1, amt++;
if( (seg = malloc(amt + HAT_seg)) ) {
seg->next = sizeof(*seg);
seg->seg = NULL;
if( round = (HatSlot)seg & (HAT_cache_line - 1) )
seg->next += HAT_cache_line - round;
} else {
hat_abort ("No virtual memory");
}
MaxMem += amt + HAT_seg;
hat = (Hat *)((uchar *)seg + HAT_seg);
memset(hat, 0, amt);
hat->bootlvl = boot;
hat->aux = aux;
hat->seg = seg;
if( !boot )
*hat->root = (HatSlot)hat_alloc (hat, HAT_bucket) | HAT_bucket;
return hat;
}
void hat_close (Hat *hat)
{
HatSeg *seg, *nxt = hat->seg;
while( (seg = nxt) )
nxt = seg->seg, free (seg);
}
// compute hash code for key
uint hat_code (uchar *buff, uint max)
{
uint hash = max;
while( max-- )
hash += (hash << 5) + (hash >> 27) + *buff++;
return hash;
}
void *hat_add_array (Hat *hat, HatSlot *parent, uchar *buff, uint amt, int pail);
void *hat_new_array (Hat *hat, HatSlot *parent, uchar *buff, uint amt);
// add new key to existing HAT_pail node
// return auxilliary area pointer, or
// NULL if it doesn't fit PAIL array
void *hat_add_pail (Hat *hat, HatSlot *parent, uchar *buff, uint amt)
{
HatPail *pail = (HatPail *)(*parent & HAT_mask);
uint slot = hat_code (buff, amt) % HatPailMax;
void *cell;
if( !pail->array[slot] )
return hat_new_array (hat, &pail->array[slot], buff, amt);
// does room exist in slot?
if( cell = hat_add_array (hat, &pail->array[slot], buff, amt, 0) )
return cell;
return NULL;
}
// create new HAT_pail node
// from full HAT array node
// by bursting it
void *hat_new_pail (Hat *hat, HatSlot *parent, uchar *buff, uint amt)
{
HatBase *base = (HatBase *)(*parent & HAT_mask);
ushort tst = 0, len, cnt = 0;
HatPail *pail;
uchar *cell;
uint code;
// strip array node keys into HAT_pail structure
pail = hat_alloc (hat, HAT_pail);
*parent = (HatSlot)pail | HAT_pail;
// burst array node into new PAIL node
while( tst < base->nxt ) {
len = base->keys[tst++];
if( len & 0x80 )
len &= 0x7f, len += base->keys[tst++] << 7;
code = hat_code (base->keys + tst, len) % HatPailMax;
if( pail->array[code] ) {
cell = hat_add_array (hat, &pail->array[code], base->keys + tst, len, 0);
if( hat->aux )
memcpy(cell, (uchar *)base + HatSize[base->type] - (cnt + 1) * hat->aux, hat->aux);
} else {
cell = hat_new_array (hat, &pail->array[code], base->keys + tst, len);
if( hat->aux )
memcpy (cell, (uchar *)base + HatSize[base->type] - (cnt + 1) * hat->aux, hat->aux);
}
tst += len;
cnt++;
}
hat_free (hat, base, base->type);
return hat_add_pail (hat, parent, buff, amt);
}
// promote full array nodes to next larger size
// if configured, overflow to HAT_pail node
void *hat_promote (Hat *hat, HatSlot *parent, uchar *buff, int amt, int pail)
{
HatBase *base = (HatBase *)(*parent & HAT_mask);
uchar *oldslots, *newslots;
ushort tst, len, skip;
uint type, oldtype;
HatBase *newbase;
if( amt > 0x7f )
skip = 2;
else
skip = 1;
oldtype = type = base->type;
oldslots = (uchar *)base + HatSize[type];
// calculate new array node big enough to contain keys
// and associated slots