nxt_lvlhsh_each() refactoring and nxt_lvlhsh_each_init().

This commit is contained in:
Igor Sysoev
2018-03-29 16:35:42 +03:00
parent 5a9c23e2b4
commit 5177b085b1
5 changed files with 34 additions and 24 deletions

View File

@@ -1262,8 +1262,7 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start,
object->count = count; object->count = count;
member = object->members; member = object->members;
nxt_memzero(&lhe, sizeof(nxt_lvlhsh_each_t)); nxt_lvlhsh_each_init(&lhe, &nxt_conf_object_hash_proto);
lhe.proto = &nxt_conf_object_hash_proto;
for ( ;; ) { for ( ;; ) {
element = nxt_lvlhsh_each(&hash, &lhe); element = nxt_lvlhsh_each(&hash, &lhe);

View File

@@ -782,11 +782,13 @@ nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe)
} }
if (!nxt_lvlhsh_is_bucket(slot)) { if (!nxt_lvlhsh_is_bucket(slot)) {
lhe->current = 0;
goto level; goto level;
} }
lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot); lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot);
lhe->entries = nxt_lvlhsh_bucket_entries(lhe->proto, slot); lhe->entries = nxt_lvlhsh_bucket_entries(lhe->proto, slot);
lhe->entry = 0;
} }
return nxt_lvlhsh_bucket_each(lhe); return nxt_lvlhsh_bucket_each(lhe);

View File

@@ -99,6 +99,21 @@ struct nxt_lvlhsh_query_s {
}; };
typedef struct {
const nxt_lvlhsh_proto_t *proto;
/*
* Fields to store current bucket entry position. They cannot be
* combined in a single bucket pointer with number of entries in low
* bits, because entry positions are not aligned. A current level is
* stored as key bit path from the root.
*/
uint32_t *bucket;
uint32_t current;
uint32_t entry;
uint32_t entries;
} nxt_lvlhsh_each_t;
#define \ #define \
nxt_lvlhsh_is_empty(lh) \ nxt_lvlhsh_is_empty(lh) \
((lh)->slot == NULL) ((lh)->slot == NULL)
@@ -145,24 +160,21 @@ NXT_EXPORT nxt_int_t nxt_lvlhsh_insert(nxt_lvlhsh_t *lh,
NXT_EXPORT nxt_int_t nxt_lvlhsh_delete(nxt_lvlhsh_t *lh, NXT_EXPORT nxt_int_t nxt_lvlhsh_delete(nxt_lvlhsh_t *lh,
nxt_lvlhsh_query_t *lhq); nxt_lvlhsh_query_t *lhq);
/*
* nxt_lvlhsh_each_init() initializes iterator.
* It must be called before the first nxt_lvlhsh_each() call.
*/
#define nxt_lvlhsh_each_init(lhe, _proto) \
do { \
(lhe)->proto = _proto; \
(lhe)->bucket = NULL; \
} while (0)
typedef struct { /*
const nxt_lvlhsh_proto_t *proto; * nxt_lvlhsh_each() iterates over a lvlhsh.
* It returns NULL if there is no more elements.
/* */
* Fields to store current bucket entry position. They cannot be NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe);
* combined in a single bucket pointer with number of entries in low
* bits, because entry positions are not aligned. A current level is
* stored as key bit path from the root.
*/
uint32_t *bucket;
uint32_t current;
uint32_t entry;
uint32_t entries;
} nxt_lvlhsh_each_t;
NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *le);
/* /*
* nxt_lvlhsh_peek() is used to iterate over a lvlhsh during the lvlhsh * nxt_lvlhsh_peek() is used to iterate over a lvlhsh during the lvlhsh

View File

@@ -1834,9 +1834,7 @@ nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i)
nxt_process_t * nxt_process_t *
nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe) nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe)
{ {
nxt_memzero(lhe, sizeof(nxt_lvlhsh_each_t)); nxt_lvlhsh_each_init(lhe, &lvlhsh_processes_proto);
lhe->proto = &lvlhsh_processes_proto;
return nxt_runtime_process_next(rt, lhe); return nxt_runtime_process_next(rt, lhe);
} }

View File

@@ -187,8 +187,7 @@ nxt_lvlhsh_test(nxt_thread_t *thr, nxt_uint_t n, nxt_bool_t use_pool)
} }
} }
nxt_memzero(&lhe, sizeof(nxt_lvlhsh_each_t)); nxt_lvlhsh_each_init(&lhe, proto);
lhe.proto = proto;
for (i = 0; i < n + 1; i++) { for (i = 0; i < n + 1; i++) {
if (nxt_lvlhsh_each(&lh, &lhe) == NULL) { if (nxt_lvlhsh_each(&lh, &lhe) == NULL) {