xref: /unit/src/nxt_lvlhsh.h (revision 0:a63ceefd6ab0)
1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) NGINX, Inc.
5  */
6 
7 #ifndef _NXT_LEVEL_HASH_H_INCLUDED_
8 #define _NXT_LEVEL_HASH_H_INCLUDED_
9 
10 
11 typedef struct nxt_lvlhsh_query_s  nxt_lvlhsh_query_t;
12 
13 typedef nxt_int_t (*nxt_lvlhsh_test_t)(nxt_lvlhsh_query_t *lhq, void *data);
14 typedef void *(*nxt_lvlhsh_alloc_t)(void *ctx, size_t size, nxt_uint_t nalloc);
15 typedef void (*nxt_lvlhsh_free_t)(void *ctx, void *p, size_t size);
16 
17 
18 #if (NXT_64BIT)
19 
20 #define NXT_LVLHSH_DEFAULT_BUCKET_SIZE  128
21 #define NXT_LVLHSH_ENTRY_SIZE           3
22 #define NXT_LVLHSH_BATCH_ALLOC          16
23 
24 /* 3 is shift of 64-bit pointer. */
25 #define NXT_LVLHSH_MEMALIGN_SHIFT       (NXT_MAX_MEMALIGN_SHIFT - 3)
26 
27 #else
28 
29 #define NXT_LVLHSH_DEFAULT_BUCKET_SIZE  64
30 #define NXT_LVLHSH_ENTRY_SIZE           2
31 #define NXT_LVLHSH_BATCH_ALLOC          8
32 
33 /* 2 is shift of 32-bit pointer. */
34 #define NXT_LVLHSH_MEMALIGN_SHIFT       (NXT_MAX_MEMALIGN_SHIFT - 2)
35 
36 #endif
37 
38 
39 #if (NXT_LVLHSH_MEMALIGN_SHIFT < 10)
40 #define NXT_LVLHSH_MAX_MEMALIGN_SHIFT   NXT_LVLHSH_MEMALIGN_SHIFT
41 #else
42 #define NXT_LVLHSH_MAX_MEMALIGN_SHIFT   10
43 #endif
44 
45 
46 #define NXT_LVLHSH_BUCKET_END(bucket_size)                                    \
47     (((bucket_size) - sizeof(void *))                                         \
48         / (NXT_LVLHSH_ENTRY_SIZE * sizeof(uint32_t))                          \
49      * NXT_LVLHSH_ENTRY_SIZE)
50 
51 
52 #define NXT_LVLHSH_BUCKET_SIZE(bucket_size)                                   \
53     NXT_LVLHSH_BUCKET_END(bucket_size), bucket_size, (bucket_size - 1)
54 
55 
56 #define NXT_LVLHSH_DEFAULT                                                    \
57     NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE),                   \
58     { 4, 4, 4, 4, 4, 4, 4, 0 }
59 
60 
61 #define NXT_LVLHSH_LARGE_SLAB                                                 \
62     NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE),                   \
63     { 10, 4, 4, 4, 4, 4, 4, 0 }
64 
65 
66 #define NXT_LVLHSH_LARGE_MEMALIGN                                             \
67     NXT_LVLHSH_BUCKET_SIZE(NXT_LVLHSH_DEFAULT_BUCKET_SIZE),                   \
68     { NXT_LVLHSH_MAX_MEMALIGN_SHIFT, 4, 4, 4, 4, 0, 0, 0 }
69 
70 
71 typedef struct {
72     uint32_t                  bucket_end;
73     uint32_t                  bucket_size;
74     uint32_t                  bucket_mask;
75     uint8_t                   shift[8];
76     uint32_t                  nalloc;
77 
78     nxt_lvlhsh_test_t         test;
79     nxt_lvlhsh_alloc_t        alloc;
80     nxt_lvlhsh_free_t         free;
81 } nxt_lvlhsh_proto_t;
82 
83 
84 typedef struct {
85     nxt_lvlhsh_test_t         test;
86     nxt_lvlhsh_alloc_t        alloc;
87     nxt_lvlhsh_free_t         free;
88 
89     /* The maximum allowed aligned shift. */
90     uint32_t                  max_shift;
91     uint32_t                  nalloc;
92 } nxt_lvlhsh_ctx_t;
93 
94 
95 typedef struct {
96     void                      *slot;
97 } nxt_lvlhsh_t;
98 
99 
100 struct nxt_lvlhsh_query_s {
101     uint32_t                  key_hash;
102     uint32_t                  replace;   /* 1 bit */
103     nxt_str_t                 key;
104     void                      *value;
105 
106     const nxt_lvlhsh_proto_t  *proto;
107     void                      *pool;
108 
109     /* Opaque data passed for the test function. */
110     void                      *data;
111 };
112 
113 
114 #define                                                                       \
115 nxt_lvlhsh_is_empty(lh)                                                       \
116     ((lh)->slot == NULL)
117 
118 
119 #define                                                                       \
120 nxt_lvlhsh_init(lh)                                                           \
121     (lh)->slot = NULL
122 
123 /*
124  * nxt_lvlhsh_find() finds a hash element.  If the element has been
125  * found then it is stored in the lhq->value and nxt_lvlhsh_find()
126  * returns NXT_OK.  Otherwise NXT_DECLINED is returned.
127  *
128  * The required nxt_lvlhsh_query_t fields: key_hash, key, proto.
129  */
130 NXT_EXPORT nxt_int_t nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq);
131 
132 /*
133  * nxt_lvlhsh_insert() adds a hash element.  If the element already
134  * presents in lvlhsh and the lhq->replace flag is zero, then lhq->value
135  * is updated with the old element and NXT_DECLINED is returned.
136  * If the element already presents in lvlhsh and the lhq->replace flag
137  * is non-zero, then the old element is replaced with the new element.
138  * lhq->value is updated with the old element, and NXT_OK is returned.
139  * If the element is not present in lvlhsh, then it is inserted and
140  * NXT_OK is returned.  The lhq->value is not changed.
141  * On memory allocation failure NXT_ERROR is returned.
142  *
143  * The required nxt_lvlhsh_query_t fields: key_hash, key, proto, replace, value.
144  * The optional nxt_lvlhsh_query_t fields: pool.
145  */
146 NXT_EXPORT nxt_int_t nxt_lvlhsh_insert(nxt_lvlhsh_t *lh,
147     nxt_lvlhsh_query_t *lhq);
148 
149 /*
150  * nxt_lvlhsh_delete() deletes a hash element.  If the element has been
151  * found then it is removed from lvlhsh and is stored in the lhq->value,
152  * and NXT_OK is returned.  Otherwise NXT_DECLINED is returned.
153  *
154  * The required nxt_lvlhsh_query_t fields: key_hash, key, proto.
155  * The optional nxt_lvlhsh_query_t fields: pool.
156  */
157 NXT_EXPORT nxt_int_t nxt_lvlhsh_delete(nxt_lvlhsh_t *lh,
158     nxt_lvlhsh_query_t *lhq);
159 
160 
161 typedef struct {
162     const nxt_lvlhsh_proto_t  *proto;
163 
164     /*
165      * Fields to store current bucket entry position.  They cannot be
166      * combined in a single bucket pointer with number of entries in low
167      * bits, because entry positions are not aligned.  A current level is
168      * stored as key bit path from the root.
169      */
170     uint32_t                  *bucket;
171     uint32_t                  current;
172     uint32_t                  entry;
173     uint32_t                  entries;
174 } nxt_lvlhsh_each_t;
175 
176 
177 NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *le);
178 
179 
180 NXT_EXPORT void *nxt_lvlhsh_alloc(void *data, size_t size, nxt_uint_t nalloc);
181 NXT_EXPORT void nxt_lvlhsh_free(void *data, void *p, size_t size);
182 
183 NXT_EXPORT void *nxt_lvlhsh_pool_alloc(void *ctx, size_t size,
184     nxt_uint_t nalloc);
185 NXT_EXPORT void nxt_lvlhsh_pool_free(void *ctx, void *p, size_t size);
186 
187 
188 #endif /* _NXT_LEVEL_HASH_H_INCLUDED_ */
189