Skip to content

Commit 3b1ca60

Browse files
ttaylorrgitster
authored andcommitted
ewah/ewah_bitmap.c: avoid open-coding ALLOC_GROW()
'ewah/ewah_bitmap.c:buffer_grow()' is responsible for growing the buffer used to store the bits of an EWAH bitmap. It is essentially doing the same task as the 'ALLOC_GROW()' macro, so use that instead. This simplifies the callers of 'buffer_grow()', who no longer have to ask for a specific size, but rather specify how much of the buffer they need. They also no longer need to guard 'buffer_grow()' behind an if statement, since 'ALLOC_GROW()' (and, by extension, 'buffer_grow()') is a noop if the buffer is already large enough. But, the most significant change is that this fixes a bug when calling buffer_grow() with both 'alloc_size' and 'new_size' set to 1. In this case, truncating integer math will leave the new size set to 1, causing the buffer to never grow. Instead, let alloc_nr() handle this, which asks for '(new_size + 16) * 3 / 2' instead of 'new_size * 3 / 2'. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent e31aba4 commit 3b1ca60

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

ewah/ewah_bitmap.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "git-compat-util.h"
2020
#include "ewok.h"
2121
#include "ewok_rlw.h"
22+
#include "cache.h"
2223

2324
static inline size_t min_size(size_t a, size_t b)
2425
{
@@ -33,20 +34,13 @@ static inline size_t max_size(size_t a, size_t b)
3334
static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
3435
{
3536
size_t rlw_offset = (uint8_t *)self->rlw - (uint8_t *)self->buffer;
36-
37-
if (self->alloc_size >= new_size)
38-
return;
39-
40-
self->alloc_size = new_size;
41-
REALLOC_ARRAY(self->buffer, self->alloc_size);
37+
ALLOC_GROW(self->buffer, new_size, self->alloc_size);
4238
self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
4339
}
4440

4541
static inline void buffer_push(struct ewah_bitmap *self, eword_t value)
4642
{
47-
if (self->buffer_size + 1 >= self->alloc_size)
48-
buffer_grow(self, self->buffer_size * 3 / 2);
49-
43+
buffer_grow(self, self->buffer_size + 1);
5044
self->buffer[self->buffer_size++] = value;
5145
}
5246

@@ -137,8 +131,7 @@ void ewah_add_dirty_words(
137131

138132
rlw_set_literal_words(self->rlw, literals + can_add);
139133

140-
if (self->buffer_size + can_add >= self->alloc_size)
141-
buffer_grow(self, (self->buffer_size + can_add) * 3 / 2);
134+
buffer_grow(self, self->buffer_size + can_add);
142135

143136
if (negate) {
144137
size_t i;

0 commit comments

Comments
 (0)