From fb7dbf3e7a668ab60c158d3d1efc77578ef9db1b Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 22 Feb 2016 17:45:12 -0500 Subject: convert ewah/bitmap code to use xmalloc This code was originally written with the idea that it could be spun off into its own ewah library, and uses the overrideable ewah_malloc to do allocations. We plug in xmalloc as our ewah_malloc, of course. But over the years the ewah code itself has become more entangled with git, and the return value of many ewah_malloc sites is not checked. Let's just drop the level of indirection and use xmalloc and friends directly. This saves a few lines, and will let us adapt these sites to our more advanced malloc helpers. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- ewah/bitmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'ewah/bitmap.c') diff --git a/ewah/bitmap.c b/ewah/bitmap.c index 47ad6747c4..c88daa0aa4 100644 --- a/ewah/bitmap.c +++ b/ewah/bitmap.c @@ -25,8 +25,8 @@ struct bitmap *bitmap_new(void) { - struct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap)); - bitmap->words = ewah_calloc(32, sizeof(eword_t)); + struct bitmap *bitmap = xmalloc(sizeof(struct bitmap)); + bitmap->words = xcalloc(32, sizeof(eword_t)); bitmap->word_alloc = 32; return bitmap; } @@ -38,8 +38,8 @@ void bitmap_set(struct bitmap *self, size_t pos) if (block >= self->word_alloc) { size_t old_size = self->word_alloc; self->word_alloc = block * 2; - self->words = ewah_realloc(self->words, - self->word_alloc * sizeof(eword_t)); + self->words = xrealloc(self->words, + self->word_alloc * sizeof(eword_t)); memset(self->words + old_size, 0x0, (self->word_alloc - old_size) * sizeof(eword_t)); @@ -102,7 +102,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah) while (ewah_iterator_next(&blowup, &it)) { if (i >= bitmap->word_alloc) { bitmap->word_alloc *= 1.5; - bitmap->words = ewah_realloc( + bitmap->words = xrealloc( bitmap->words, bitmap->word_alloc * sizeof(eword_t)); } @@ -134,7 +134,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other) if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words = ewah_realloc(self->words, + self->words = xrealloc(self->words, self->word_alloc * sizeof(eword_t)); memset(self->words + original_size, 0x0, (self->word_alloc - original_size) * sizeof(eword_t)); -- cgit v1.2.1 From 08c95df8faa25ab4c9ad3da45bc12abb9274d343 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 22 Feb 2016 17:45:15 -0500 Subject: ewah: convert to REALLOC_ARRAY, etc Now that we're built around xmalloc and friends, we can use helpers like REALLOC_ARRAY, ALLOC_GROW, and so on to make the code shorter and protect against integer overflow. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- ewah/bitmap.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) (limited to 'ewah/bitmap.c') diff --git a/ewah/bitmap.c b/ewah/bitmap.c index c88daa0aa4..7103ceefbf 100644 --- a/ewah/bitmap.c +++ b/ewah/bitmap.c @@ -17,7 +17,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include "git-compat-util.h" +#include "cache.h" #include "ewok.h" #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD)) @@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos) if (block >= self->word_alloc) { size_t old_size = self->word_alloc; self->word_alloc = block * 2; - self->words = xrealloc(self->words, - self->word_alloc * sizeof(eword_t)); - + REALLOC_ARRAY(self->words, self->word_alloc); memset(self->words + old_size, 0x0, (self->word_alloc - old_size) * sizeof(eword_t)); } @@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah) ewah_iterator_init(&it, ewah); while (ewah_iterator_next(&blowup, &it)) { - if (i >= bitmap->word_alloc) { - bitmap->word_alloc *= 1.5; - bitmap->words = xrealloc( - bitmap->words, bitmap->word_alloc * sizeof(eword_t)); - } - + ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc); bitmap->words[i++] = blowup; } @@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other) if (self->word_alloc < other_final) { self->word_alloc = other_final; - self->words = xrealloc(self->words, - self->word_alloc * sizeof(eword_t)); + REALLOC_ARRAY(self->words, self->word_alloc); memset(self->words + original_size, 0x0, (self->word_alloc - original_size) * sizeof(eword_t)); } -- cgit v1.2.1