add isl_calloc_type
[platform/upstream/isl.git] / include / isl_hash.h
1 #ifndef ISL_HASH_H
2 #define ISL_HASH_H
3
4 #include <isl_stdint.h>
5
6 #if defined(__cplusplus)
7 extern "C" {
8 #endif
9
10 #define isl_hash_init()         (2166136261u)
11 #define isl_hash_byte(h,b)      do {                                    \
12                                         h *= 16777619;                  \
13                                         h ^= b;                         \
14                                 } while(0)
15 #define isl_hash_hash(h,h2)                                             \
16         do {                                                            \
17                 isl_hash_byte(h, (h2) & 0xFF);                          \
18                 isl_hash_byte(h, ((h2) >> 8) & 0xFF);                   \
19                 isl_hash_byte(h, ((h2) >> 16) & 0xFF);                  \
20                 isl_hash_byte(h, ((h2) >> 24) & 0xFF);                  \
21         } while(0)
22 #define isl_hash_bits(h,bits)                                           \
23         ((bits) == 32) ? (h) :                                          \
24         ((bits) >= 16) ?                                                \
25               ((h) >> (bits)) ^ ((h) & (((uint32_t)1 << (bits)) - 1)) : \
26               (((h) >> (bits)) ^ (h)) & (((uint32_t)1 << (bits)) - 1)
27
28 uint32_t isl_hash_string(uint32_t hash, const char *s);
29
30 struct isl_hash_table_entry
31 {
32         uint32_t  hash;
33         void     *data;
34 };
35
36 struct isl_hash_table {
37         int    bits;
38         int    n;
39         struct isl_hash_table_entry *entries;
40 };
41
42 struct isl_ctx;
43
44 int isl_hash_table_init(struct isl_ctx *ctx, struct isl_hash_table *table,
45                         int init_bits);
46 void isl_hash_table_clear(struct isl_hash_table *table);
47 struct isl_hash_table_entry *isl_hash_table_find(struct isl_ctx *ctx,
48                                 struct isl_hash_table *table,
49                                 uint32_t key_hash,
50                                 int (*eq)(const void *entry, const void *val),
51                                 const void *val, int reserve);
52 void isl_hash_table_remove(struct isl_ctx *ctx,
53                                 struct isl_hash_table *table,
54                                 struct isl_hash_table_entry *entry);
55
56 #if defined(__cplusplus)
57 }
58 #endif
59
60 #endif