Fix conservative GC some more.
[sbcl.git] / src / runtime / hopscotch.h
blobefaf5e40b0860e189be8b2781b652508c218bc92
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
12 #ifndef _HOPSCOTCH_H_
13 #define _HOPSCOTCH_H_
15 struct hopscotch_table {
16 uword_t* keys;
17 unsigned* hops;
18 sword_t* values;
19 sword_t (*get_value)(struct hopscotch_table*,int);
20 uint32_t (*hash)(uword_t);
21 unsigned mask;
22 int hop_range;
23 int count;
24 int threshold; // max count before sizing up
25 int prev_size; // in cells, as specified to hopscotch_create()
26 int mem_size; // in bytes, for zero-filling when done using
27 // Statistics
28 struct { int n_seeks, n_probes; } hit, miss;
29 char value_size; // number of bytes in a value: 0,1,2,4,8
30 char hashfun; // 1 or 2 to pick fast or slow
31 char resized; // set to 1 if sized up since last reset
32 char rehashing; // set to 1 during rehash
35 void hopscotch_init();
36 void hopscotch_create(struct hopscotch_table*,int,int,int,char);
37 void hopscotch_delete(struct hopscotch_table*);
38 int hopscotch_insert(struct hopscotch_table*,uword_t,sword_t);
39 int hopscotch_put(struct hopscotch_table*,uword_t,sword_t);
40 sword_t hopscotch_get(struct hopscotch_table*,uword_t,sword_t);
41 int hopscotch_containsp(struct hopscotch_table*,uword_t);
42 void hopscotch_reset(struct hopscotch_table*);
43 void hopscotch_log_stats(struct hopscotch_table*,char*);
45 uint32_t hopscotch_hmix(uword_t);
47 #define HOPSCOTCH_HASH_FUN_DEFAULT 1
48 #define HOPSCOTCH_HASH_FUN_MIX 2
50 /* This confuses me every time I look at it, so here's an example-
51 * Suppose (unrealistically) that a table has a hop range of 4,
52 * and 16 logical bins. The largest logical bin index (= the table mask)
53 * is 15, and there are 3 (not 4) more cells at the end.
54 * So the largest physical index in which you can probe without
55 * overrunning the key array is (mask + range - 1).
56 * i.e. the neighborhood of logical cell index 15 is {15,16,17,18}.
57 * The total count of physical key cells is (mask + 1) + (range - 1)
58 * where (mask + 1) is the logical bin count.
60 #define hopscotch_max_key_index(table) ((table).mask+(table).hop_range-1)
62 #define for_each_hopscotch_key(indexvar, keyvar, tablevar) \
63 for(indexvar=hopscotch_max_key_index(tablevar) ; indexvar >= 0 ; --indexvar) \
64 if ((keyvar=tablevar.keys[indexvar])!=0)
66 #endif