提交 f63ea4e9 编写于 作者: V Vladimir Sementsov-Ogievskiy 提交者: Max Reitz

hbitmap: improve dirty iter

Make dirty iter resistant to resetting bits in corresponding HBitmap.
Signed-off-by: NVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: NMax Reitz <mreitz@redhat.com>
Reviewed-by: NJohn Snow <jsnow@redhat.com>
Message-id: 20170628120530.31251-4-vsementsov@virtuozzo.com
Signed-off-by: NMax Reitz <mreitz@redhat.com>
上级 b348c262
...@@ -256,10 +256,9 @@ void hbitmap_free(HBitmap *hb); ...@@ -256,10 +256,9 @@ void hbitmap_free(HBitmap *hb);
* the lowest-numbered bit that is set in @hb, starting at @first. * the lowest-numbered bit that is set in @hb, starting at @first.
* *
* Concurrent setting of bits is acceptable, and will at worst cause the * Concurrent setting of bits is acceptable, and will at worst cause the
* iteration to miss some of those bits. Resetting bits before the current * iteration to miss some of those bits.
* position of the iterator is also okay. However, concurrent resetting of *
* bits can lead to unexpected behavior if the iterator has not yet reached * The concurrent resetting of bits is OK.
* those bits.
*/ */
void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first); void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first);
...@@ -298,24 +297,7 @@ void hbitmap_free_meta(HBitmap *hb); ...@@ -298,24 +297,7 @@ void hbitmap_free_meta(HBitmap *hb);
* Return the next bit that is set in @hbi's associated HBitmap, * Return the next bit that is set in @hbi's associated HBitmap,
* or -1 if all remaining bits are zero. * or -1 if all remaining bits are zero.
*/ */
static inline int64_t hbitmap_iter_next(HBitmapIter *hbi) int64_t hbitmap_iter_next(HBitmapIter *hbi);
{
unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1];
int64_t item;
if (cur == 0) {
cur = hbitmap_iter_skip_words(hbi);
if (cur == 0) {
return -1;
}
}
/* The next call will resume work from the next bit. */
hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
return item << hbi->granularity;
}
/** /**
* hbitmap_iter_next_word: * hbitmap_iter_next_word:
......
...@@ -106,8 +106,9 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi) ...@@ -106,8 +106,9 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
unsigned long cur; unsigned long cur;
do { do {
cur = hbi->cur[--i]; i--;
pos >>= BITS_PER_LEVEL; pos >>= BITS_PER_LEVEL;
cur = hbi->cur[i] & hb->levels[i][pos];
} while (cur == 0); } while (cur == 0);
/* Check for end of iteration. We always use fewer than BITS_PER_LONG /* Check for end of iteration. We always use fewer than BITS_PER_LONG
...@@ -139,6 +140,26 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi) ...@@ -139,6 +140,26 @@ unsigned long hbitmap_iter_skip_words(HBitmapIter *hbi)
return cur; return cur;
} }
int64_t hbitmap_iter_next(HBitmapIter *hbi)
{
unsigned long cur = hbi->cur[HBITMAP_LEVELS - 1] &
hbi->hb->levels[HBITMAP_LEVELS - 1][hbi->pos];
int64_t item;
if (cur == 0) {
cur = hbitmap_iter_skip_words(hbi);
if (cur == 0) {
return -1;
}
}
/* The next call will resume work from the next bit. */
hbi->cur[HBITMAP_LEVELS - 1] = cur & (cur - 1);
item = ((uint64_t)hbi->pos << BITS_PER_LEVEL) + ctzl(cur);
return item << hbi->granularity;
}
void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first) void hbitmap_iter_init(HBitmapIter *hbi, const HBitmap *hb, uint64_t first)
{ {
unsigned i, bit; unsigned i, bit;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册