Skip to content

Commit 14fbd26

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 56d9cbe commit 14fbd26

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

ewah/bitmap.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,26 @@
2222
#define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
2323
#define EWAH_BLOCK(x) (x / BITS_IN_EWORD)
2424

25-
struct bitmap *bitmap_new(void)
25+
struct bitmap *bitmap_word_alloc(size_t word_alloc)
2626
{
2727
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;
3030
return bitmap;
3131
}
3232

33+
struct bitmap *bitmap_new(void)
34+
{
35+
return bitmap_word_alloc(32);
36+
}
37+
3338
void bitmap_set(struct bitmap *self, size_t pos)
3439
{
3540
size_t block = EWAH_BLOCK(pos);
3641

3742
if (block >= self->word_alloc) {
3843
size_t old_size = self->word_alloc;
39-
self->word_alloc = block * 2;
44+
self->word_alloc = block ? block * 2 : 1;
4045
REALLOC_ARRAY(self->words, self->word_alloc);
4146
memset(self->words + old_size, 0x0,
4247
(self->word_alloc - old_size) * sizeof(eword_t));

ewah/ewok.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ struct bitmap {
172172
};
173173

174174
struct bitmap *bitmap_new(void);
175+
struct bitmap *bitmap_word_alloc(size_t word_alloc);
175176
void bitmap_set(struct bitmap *self, size_t pos);
176177
int bitmap_get(struct bitmap *self, size_t pos);
177178
void bitmap_reset(struct bitmap *self);

0 commit comments

Comments
 (0)