diff options
author | Eric Faurot <eric@faurot.net> | 2013-05-10 21:27:23 +0200 |
---|---|---|
committer | Eric Faurot <eric@faurot.net> | 2013-05-10 21:35:06 +0200 |
commit | 737268b2b19a01759ce190d3d4653cd828ae0442 (patch) | |
tree | 725e01fada56c0b777e96e4cd4eaa988eb97c77e /smtpd/tree.c | |
parent | document lmtp to socket (diff) | |
download | OpenSMTPD-737268b2b19a01759ce190d3d4653cd828ae0442.tar.xz OpenSMTPD-737268b2b19a01759ce190d3d4653cd828ae0442.zip |
remember the number of nodes in a struct tree and struct dict.
Diffstat (limited to '')
-rw-r--r-- | smtpd/tree.c | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/smtpd/tree.c b/smtpd/tree.c index ed3bbfa3..8e0b7fee 100644 --- a/smtpd/tree.c +++ b/smtpd/tree.c @@ -38,7 +38,7 @@ struct treeentry { static int treeentry_cmp(struct treeentry *, struct treeentry *); -SPLAY_PROTOTYPE(tree, treeentry, entry, treeentry_cmp); +SPLAY_PROTOTYPE(_tree, treeentry, entry, treeentry_cmp); int tree_check(struct tree *t, uint64_t id) @@ -46,7 +46,7 @@ tree_check(struct tree *t, uint64_t id) struct treeentry key; key.id = id; - return (SPLAY_FIND(tree, t, &key) != NULL); + return (SPLAY_FIND(_tree, &t->tree, &key) != NULL); } void * @@ -56,12 +56,13 @@ tree_set(struct tree *t, uint64_t id, void *data) char *old; key.id = id; - if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) { + if ((entry = SPLAY_FIND(_tree, &t->tree, &key)) == NULL) { if ((entry = malloc(sizeof *entry)) == NULL) err(1, "tree_set: malloc"); entry->id = id; - SPLAY_INSERT(tree, t, entry); + SPLAY_INSERT(_tree, &t->tree, entry); old = NULL; + t->count += 1; } else old = entry->data; @@ -79,8 +80,9 @@ tree_xset(struct tree *t, uint64_t id, void *data) err(1, "tree_xset: malloc"); entry->id = id; entry->data = data; - if (SPLAY_INSERT(tree, t, entry)) + if (SPLAY_INSERT(_tree, &t->tree, entry)) errx(1, "tree_xset(%p, 0x%016"PRIx64 ")", t, id); + t->count += 1; } void * @@ -89,7 +91,7 @@ tree_get(struct tree *t, uint64_t id) struct treeentry key, *entry; key.id = id; - if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) + if ((entry = SPLAY_FIND(_tree, &t->tree, &key)) == NULL) return (NULL); return (entry->data); @@ -101,7 +103,7 @@ tree_xget(struct tree *t, uint64_t id) struct treeentry key, *entry; key.id = id; - if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) + if ((entry = SPLAY_FIND(_tree, &t->tree, &key)) == NULL) errx(1, "tree_get(%p, 0x%016"PRIx64 ")", t, id); return (entry->data); @@ -114,12 +116,13 @@ tree_pop(struct tree *t, uint64_t id) void *data; key.id = id; - if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) + if ((entry = SPLAY_FIND(_tree, &t->tree, &key)) == NULL) return (NULL); data = entry->data; - SPLAY_REMOVE(tree, t, entry); + SPLAY_REMOVE(_tree, &t->tree, entry); free(entry); + t->count -= 1; return (data); } @@ -131,12 +134,13 @@ tree_xpop(struct tree *t, uint64_t id) void *data; key.id = id; - if ((entry = SPLAY_FIND(tree, t, &key)) == NULL) + if ((entry = SPLAY_FIND(_tree, &t->tree, &key)) == NULL) errx(1, "tree_xpop(%p, 0x%016" PRIx64 ")", t, id); data = entry->data; - SPLAY_REMOVE(tree, t, entry); + SPLAY_REMOVE(_tree, &t->tree, entry); free(entry); + t->count -= 1; return (data); } @@ -146,15 +150,17 @@ tree_poproot(struct tree *t, uint64_t *id, void **data) { struct treeentry *entry; - entry = SPLAY_ROOT(t); + entry = SPLAY_ROOT(&t->tree); if (entry == NULL) return (0); if (id) *id = entry->id; if (data) *data = entry->data; - SPLAY_REMOVE(tree, t, entry); + SPLAY_REMOVE(_tree, &t->tree, entry); free(entry); + t->count -= 1; + return (1); } @@ -163,7 +169,7 @@ tree_root(struct tree *t, uint64_t *id, void **data) { struct treeentry *entry; - entry = SPLAY_ROOT(t); + entry = SPLAY_ROOT(&t->tree); if (entry == NULL) return (0); if (id) @@ -179,9 +185,9 @@ tree_iter(struct tree *t, void **hdl, uint64_t *id, void **data) struct treeentry *curr = *hdl; if (curr == NULL) - curr = SPLAY_MIN(tree, t); + curr = SPLAY_MIN(_tree, &t->tree); else - curr = SPLAY_NEXT(tree, t, curr); + curr = SPLAY_NEXT(_tree, &t->tree, curr); if (curr) { *hdl = curr; @@ -202,18 +208,18 @@ tree_iterfrom(struct tree *t, void **hdl, uint64_t k, uint64_t *id, void **data) if (curr == NULL) { if (k == 0) - curr = SPLAY_MIN(tree, t); + curr = SPLAY_MIN(_tree, &t->tree); else { key.id = k; - curr = SPLAY_FIND(tree, t, &key); + curr = SPLAY_FIND(_tree, &t->tree, &key); if (curr == NULL) { - SPLAY_INSERT(tree, t, &key); - curr = SPLAY_NEXT(tree, t, &key); - SPLAY_REMOVE(tree, t, &key); + SPLAY_INSERT(_tree, &t->tree, &key); + curr = SPLAY_NEXT(_tree, &t->tree, &key); + SPLAY_REMOVE(_tree, &t->tree, &key); } } } else - curr = SPLAY_NEXT(tree, t, curr); + curr = SPLAY_NEXT(_tree, &t->tree, curr); if (curr) { *hdl = curr; @@ -232,12 +238,14 @@ tree_merge(struct tree *dst, struct tree *src) { struct treeentry *entry; - while (!SPLAY_EMPTY(src)) { - entry = SPLAY_ROOT(src); - SPLAY_REMOVE(tree, src, entry); - if (SPLAY_INSERT(tree, dst, entry)) + while (!SPLAY_EMPTY(&src->tree)) { + entry = SPLAY_ROOT(&src->tree); + SPLAY_REMOVE(_tree, &src->tree, entry); + if (SPLAY_INSERT(_tree, &dst->tree, entry)) errx(1, "tree_merge: duplicate"); } + dst->count += src->count; + src->count = 0; } static int @@ -250,4 +258,4 @@ treeentry_cmp(struct treeentry *a, struct treeentry *b) return (0); } -SPLAY_GENERATE(tree, treeentry, entry, treeentry_cmp); +SPLAY_GENERATE(_tree, treeentry, entry, treeentry_cmp); |