summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2018-03-29 16:35:42 +0300
committerIgor Sysoev <igor@sysoev.ru>2018-03-29 16:35:42 +0300
commit5177b085b13be78d216441d21480caf81eb968c2 (patch)
treeea3c80a789a295d072fd339d10b23515d6dea549
parent5a9c23e2b4ff39ee4c25f67ece56e3d441edfeed (diff)
downloadunit-5177b085b13be78d216441d21480caf81eb968c2.tar.gz
unit-5177b085b13be78d216441d21480caf81eb968c2.tar.bz2
nxt_lvlhsh_each() refactoring and nxt_lvlhsh_each_init().
-rw-r--r--src/nxt_conf.c3
-rw-r--r--src/nxt_lvlhsh.c2
-rw-r--r--src/nxt_lvlhsh.h46
-rw-r--r--src/nxt_runtime.c4
-rw-r--r--src/test/nxt_lvlhsh_test.c3
5 files changed, 34 insertions, 24 deletions
diff --git a/src/nxt_conf.c b/src/nxt_conf.c
index 684135cf..6d5d3382 100644
--- a/src/nxt_conf.c
+++ b/src/nxt_conf.c
@@ -1262,8 +1262,7 @@ nxt_conf_json_parse_object(nxt_mp_t *mp, nxt_conf_value_t *value, u_char *start,
object->count = count;
member = object->members;
- nxt_memzero(&lhe, sizeof(nxt_lvlhsh_each_t));
- lhe.proto = &nxt_conf_object_hash_proto;
+ nxt_lvlhsh_each_init(&lhe, &nxt_conf_object_hash_proto);
for ( ;; ) {
element = nxt_lvlhsh_each(&hash, &lhe);
diff --git a/src/nxt_lvlhsh.c b/src/nxt_lvlhsh.c
index 864d3007..ec433341 100644
--- a/src/nxt_lvlhsh.c
+++ b/src/nxt_lvlhsh.c
@@ -782,11 +782,13 @@ nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe)
}
if (!nxt_lvlhsh_is_bucket(slot)) {
+ lhe->current = 0;
goto level;
}
lhe->bucket = nxt_lvlhsh_bucket(lhe->proto, slot);
lhe->entries = nxt_lvlhsh_bucket_entries(lhe->proto, slot);
+ lhe->entry = 0;
}
return nxt_lvlhsh_bucket_each(lhe);
diff --git a/src/nxt_lvlhsh.h b/src/nxt_lvlhsh.h
index 07342d44..7127c0d0 100644
--- a/src/nxt_lvlhsh.h
+++ b/src/nxt_lvlhsh.h
@@ -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 \
nxt_lvlhsh_is_empty(lh) \
((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_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;
-
- /*
- * 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;
-
-
-NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *le);
+/*
+ * nxt_lvlhsh_each() iterates over a lvlhsh.
+ * It returns NULL if there is no more elements.
+ */
+NXT_EXPORT void *nxt_lvlhsh_each(nxt_lvlhsh_t *lh, nxt_lvlhsh_each_t *lhe);
/*
* nxt_lvlhsh_peek() is used to iterate over a lvlhsh during the lvlhsh
diff --git a/src/nxt_runtime.c b/src/nxt_runtime.c
index a72bf945..e48f8b58 100644
--- a/src/nxt_runtime.c
+++ b/src/nxt_runtime.c
@@ -1834,9 +1834,7 @@ nxt_process_use(nxt_task_t *task, nxt_process_t *process, int i)
nxt_process_t *
nxt_runtime_process_first(nxt_runtime_t *rt, nxt_lvlhsh_each_t *lhe)
{
- nxt_memzero(lhe, sizeof(nxt_lvlhsh_each_t));
-
- lhe->proto = &lvlhsh_processes_proto;
+ nxt_lvlhsh_each_init(lhe, &lvlhsh_processes_proto);
return nxt_runtime_process_next(rt, lhe);
}
diff --git a/src/test/nxt_lvlhsh_test.c b/src/test/nxt_lvlhsh_test.c
index fca6ed30..3dc56076 100644
--- a/src/test/nxt_lvlhsh_test.c
+++ b/src/test/nxt_lvlhsh_test.c
@@ -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));
- lhe.proto = proto;
+ nxt_lvlhsh_each_init(&lhe, proto);
for (i = 0; i < n + 1; i++) {
if (nxt_lvlhsh_each(&lh, &lhe) == NULL) {