diff options
author | David Herrmann <dh.herrmann@googlemail.com> | 2015-07-16 15:12:26 +0200 |
---|---|---|
committer | David Herrmann <dh.herrmann@googlemail.com> | 2015-07-16 15:12:26 +0200 |
commit | 2ec7c4279e4cbbe668c5dfb0ab447b74deaa294b (patch) | |
tree | 27006a5c9063902b387f609920406abac3299976 | |
parent | 18458807574ec84f2bba4597ea5f8c925037d5f4 (diff) | |
parent | cb57dd41595adddb08095298bb1ed258c8ea4877 (diff) |
Merge pull request #602 from teg/bitmap-iterator
bitmap: use external iterator
-rw-r--r-- | src/basic/bitmap.c | 20 | ||||
-rw-r--r-- | src/basic/bitmap.h | 8 | ||||
-rw-r--r-- | src/resolve/resolved-dns-packet.c | 3 | ||||
-rw-r--r-- | src/resolve/resolved-dns-rr.c | 3 | ||||
-rw-r--r-- | src/test/test-bitmap.c | 5 |
5 files changed, 17 insertions, 22 deletions
diff --git a/src/basic/bitmap.c b/src/basic/bitmap.c index d865e2fc6e..d559be1bbb 100644 --- a/src/basic/bitmap.c +++ b/src/basic/bitmap.c @@ -27,7 +27,6 @@ struct Bitmap { long long unsigned *bitmaps; size_t n_bitmaps; size_t bitmaps_allocated; - unsigned next_entry; }; /* Bitmaps are only meant to store relatively small numbers @@ -149,22 +148,15 @@ void bitmap_clear(Bitmap *b) { b->bitmaps[i] = 0; } -void bitmap_rewind(Bitmap *b) { - if (!b) - return; - - b->next_entry = 0; -} - -bool bitmap_next(Bitmap *b, unsigned *n) { +bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n) { long long bitmask; unsigned offset, rem; - if (!b && b->next_entry == BITMAP_END) + if (!b && i->idx == BITMAP_END) return false; - offset = BITMAP_NUM_TO_OFFSET(b->next_entry); - rem = BITMAP_NUM_TO_REM(b->next_entry); + offset = BITMAP_NUM_TO_OFFSET(i->idx); + rem = BITMAP_NUM_TO_REM(i->idx); bitmask = 1 << rem; for (; offset < b->n_bitmaps; offset ++) { @@ -172,7 +164,7 @@ bool bitmap_next(Bitmap *b, unsigned *n) { for (; bitmask; bitmask <<= 1, rem ++) { if (b->bitmaps[offset] & bitmask) { *n = BITMAP_OFFSET_TO_NUM(offset, rem); - b->next_entry = *n + 1; + i->idx = *n + 1; return true; } @@ -183,7 +175,7 @@ bool bitmap_next(Bitmap *b, unsigned *n) { bitmask = 1; } - b->next_entry = BITMAP_END; + i->idx = BITMAP_END; return false; } diff --git a/src/basic/bitmap.h b/src/basic/bitmap.h index 92bee51a2c..2874bc99f7 100644 --- a/src/basic/bitmap.h +++ b/src/basic/bitmap.h @@ -22,6 +22,7 @@ ***/ #include "macro.h" +#include "hashmap.h" typedef struct Bitmap Bitmap; @@ -37,13 +38,12 @@ bool bitmap_isset(Bitmap *b, unsigned n); bool bitmap_isclear(Bitmap *b); void bitmap_clear(Bitmap *b); -void bitmap_rewind(Bitmap *b); -bool bitmap_next(Bitmap *b, unsigned *n); +bool bitmap_iterate(Bitmap *b, Iterator *i, unsigned *n); bool bitmap_equal(Bitmap *a, Bitmap *b); -#define BITMAP_FOREACH(n, b) \ - for (bitmap_rewind(b); bitmap_next((b), &(n)); ) +#define BITMAP_FOREACH(n, b, i) \ + for ((i).idx = 0; bitmap_iterate((b), &(i), (unsigned*)&(n)); ) DEFINE_TRIVIAL_CLEANUP_FUNC(Bitmap*, bitmap_free); diff --git a/src/resolve/resolved-dns-packet.c b/src/resolve/resolved-dns-packet.c index e44d3926d9..b1cde4ab35 100644 --- a/src/resolve/resolved-dns-packet.c +++ b/src/resolve/resolved-dns-packet.c @@ -536,6 +536,7 @@ fail: } static int dns_packet_append_types(DnsPacket *p, Bitmap *types, size_t *start) { + Iterator i; uint8_t window = 0; uint8_t len = 0; uint8_t bitmaps[32] = {}; @@ -548,7 +549,7 @@ static int dns_packet_append_types(DnsPacket *p, Bitmap *types, size_t *start) { saved_size = p->size; - BITMAP_FOREACH(n, types) { + BITMAP_FOREACH(n, types, i) { uint8_t entry; assert(n <= 0xffff); diff --git a/src/resolve/resolved-dns-rr.c b/src/resolve/resolved-dns-rr.c index e9907eabc0..859b3f7339 100644 --- a/src/resolve/resolved-dns-rr.c +++ b/src/resolve/resolved-dns-rr.c @@ -527,10 +527,11 @@ static int format_timestamp_dns(char *buf, size_t l, time_t sec) { static char *format_types(Bitmap *types) { _cleanup_strv_free_ char **strv = NULL; _cleanup_free_ char *str = NULL; + Iterator i; unsigned type; int r; - BITMAP_FOREACH(type, types) { + BITMAP_FOREACH(type, types, i) { if (dns_type_to_string(type)) { r = strv_extend(&strv, strdup(dns_type_to_string(type))); if (r < 0) diff --git a/src/test/test-bitmap.c b/src/test/test-bitmap.c index 304888beb9..77db784a94 100644 --- a/src/test/test-bitmap.c +++ b/src/test/test-bitmap.c @@ -21,6 +21,7 @@ int main(int argc, const char *argv[]) { _cleanup_bitmap_free_ Bitmap *b = NULL; + Iterator it; unsigned n = (unsigned) -1, i = 0; b = bitmap_new(); @@ -61,7 +62,7 @@ int main(int argc, const char *argv[]) { assert_se(bitmap_set(b, 1) == 0); assert_se(bitmap_set(b, 256) == 0); - BITMAP_FOREACH(n, b) { + BITMAP_FOREACH(n, b, it) { assert_se(n == i); if (i == 0) i = 1; @@ -75,7 +76,7 @@ int main(int argc, const char *argv[]) { i = 0; - BITMAP_FOREACH(n, b) { + BITMAP_FOREACH(n, b, it) { assert_se(n == i); if (i == 0) i = 1; |