ewah/bitmap: introduce bitmap_word_alloc()
In a following commit we will need to allocate a variable number of bitmap words, instead of always 32, so let's add bitmap_word_alloc() for this purpose. Note that we have to adjust the block growth in bitmap_set(), since a caller could now use an initial size of "0" (we don't plan to do that, but it doesn't hurt to be defensive). Helped-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Dec 18, 2019 at 12:25 UTC
14fbd260442b14813e142f762050313daee1f18d
2 files changed
+10
-4
ewah/bitmap.c
+9
-4
@@ -22,21 +22,26 @@
22
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
23
#define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
24
25
-struct bitmap *bitmap_new(void)
25
+struct bitmap *bitmap_word_alloc(size_t word_alloc)
26
{
27
struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
28
- bitmap->words = xcalloc(32, sizeof(eword_t));
29
- bitmap->word_alloc = 32;
28
+ bitmap->words = xcalloc(word_alloc, sizeof(eword_t));
29
+ bitmap->word_alloc = word_alloc;
30
return bitmap;
31
}
32
33
+struct bitmap *bitmap_new(void)
34
+{
35
+ return bitmap_word_alloc(32);
36
+}
37
+
38
void bitmap_set(struct bitmap *self, size_t pos)
39
{
40
size_t block = EWAH_BLOCK(pos);
41
42
if (block >= self->word_alloc) {
43
size_t old_size = self->word_alloc;
39
- self->word_alloc = block * 2;
44
+ self->word_alloc = block ? block * 2 : 1;
45
REALLOC_ARRAY(self->words, self->word_alloc);
46
memset(self->words + old_size, 0x0,
47
(self->word_alloc - old_size) * sizeof(eword_t));
ewah/ewok.h
+1
@@ -172,6 +172,7 @@ struct bitmap {
172
};
173
174
struct bitmap *bitmap_new(void);
175
+struct bitmap *bitmap_word_alloc(size_t word_alloc);
176
void bitmap_set(struct bitmap *self, size_t pos);
177
int bitmap_get(struct bitmap *self, size_t pos);
178
void bitmap_reset(struct bitmap *self);