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