xref: /unit/src/nxt_lvlhsh.h (revision 30:0d768946dbf4)
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     void                      *slot;
86 } nxt_lvlhsh_t;
87 
88 
89 struct nxt_lvlhsh_query_s {
90     uint32_t                  key_hash;
91     uint32_t                  replace;   /* 1 bit */
92     nxt_str_t                 key;
93     void                      *value;
94 
95     const nxt_lvlhsh_proto_t  *proto;
96     void                      *pool;
97 
98     /* Opaque data passed for the test function. */
99     void                      *data;
100 };
101 
102 
103 #define                                                                       \
104 nxt_lvlhsh_is_empty(lh)                                                       \
105     ((lh)->slot == NULL)
106 
107 
108 #define                                                                       \
109 nxt_lvlhsh_init(lh)                                                           \
110     (lh)->slot = NULL
111 
112 /*
113  * nxt_lvlhsh_find() finds a hash element.  If the element has been
114  * found then it is stored in the lhq->value and nxt_lvlhsh_find()
115  * returns NXT_OK.  Otherwise NXT_DECLINED is returned.
116  *
117  * The required nxt_lvlhsh_query_t fields: key_hash, key, proto.
118  */
119 NXT_EXPORT nxt_int_t nxt_lvlhsh_find(nxt_lvlhsh_t *lh, nxt_lvlhsh_query_t *lhq);
120 
121 /*
122  * nxt_lvlhsh_insert() adds a hash element.  If the element already
123  * presents in lvlhsh and the lhq->replace flag is zero, then lhq->value
124  * is updated with the old element and NXT_DECLINED is returned.
125  * If the element already presents in lvlhsh and the lhq->replace flag
126  * is non-zero, then the old element is replaced with the new element.
127  * lhq->value is updated with the old element, and NXT_OK is returned.
128  * If the element is not present in lvlhsh, then it is inserted and
129  * NXT_OK is returned.  The lhq->value is not changed.
130  * On memory allocation failure NXT_ERROR is returned.
131  *
132  * The required nxt_lvlhsh_query_t fields: key_hash, key, proto, replace, value.
133  * The optional nxt_lvlhsh_query_t fields: pool.
134  */
135 NXT_EXPORT nxt_int_t nxt_lvlhsh_insert(nxt_lvlhsh_t *lh,
136     nxt_lvlhsh_query_t *lhq);
137 
138 /*
139  * nxt_lvlhsh_delete() deletes a hash element.  If the element has been
140  * found then it is removed from lvlhsh and is stored in the lhq->value,
141  * and NXT_OK is returned.  Otherwise NXT_DECLINED is returned.
142  *
143  * The required nxt_lvlhsh_query_t fields: key_hash, key, proto.
144  * The optional nxt_lvlhsh_query_t fields: pool.
145  */
146 NXT_EXPORT nxt_int_t nxt_lvlhsh_delete(nxt_lvlhsh_t *lh,
147     nxt_lvlhsh_query_t *lhq);
148 
149 
150 typedef struct {
151     const nxt_lvlhsh_proto_t  *proto;
152 
153     /*
154      * Fields to store current bucket entry position.  They cannot be
155      * combined in a single bucket pointer with number of entries in low
156      * bits, because entry positions are not aligned.  A current level is
157      * stored as key bit path from the root.
158      */
159     uint32_t                  *bucket;
160     uint32_t                  current;
161     uint32_t                  entry;
162     uint32_t                  entries;
163 } nxt_lvlhsh_each_t;
164 
165 
166 NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *le);
167 
168 
169 NXT_EXPORT void *nxt_lvlhsh_alloc(void *data, size_t size, nxt_uint_t nalloc);
170 NXT_EXPORT void nxt_lvlhsh_free(void *data, void *p, size_t size);
171 
172 NXT_EXPORT void *nxt_lvlhsh_pool_alloc(void *ctx, size_t size,
173     nxt_uint_t nalloc);
174 NXT_EXPORT void nxt_lvlhsh_pool_free(void *ctx, void *p, size_t size);
175 
176 
177 #endif /* _NXT_LEVEL_HASH_H_INCLUDED_ */
178