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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 22, 2016 at 17:45 UTC 08c95df8faa25ab4c9ad3da45bc12abb9274d343
3 files changed +8 -19
ewah/bitmap.c
+4 -12
@@ -17,7 +17,7 @@
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 -#include "git-compat-util.h"
20 +#include "cache.h"
21 #include "ewok.h"
22
23 #define EWAH_MASK(x) ((eword_t)1 << (x % BITS_IN_EWORD))
@@ -38,9 +38,7 @@ void bitmap_set(struct bitmap *self, size_t pos)
38 if (block >= self->word_alloc) {
39 size_t old_size = self->word_alloc;
40 self->word_alloc = block * 2;
41 - self->words = xrealloc(self->words,
42 - self->word_alloc * sizeof(eword_t));
43 -
41 + REALLOC_ARRAY(self->words, self->word_alloc);
42 memset(self->words + old_size, 0x0,
43 (self->word_alloc - old_size) * sizeof(eword_t));
44 }
@@ -100,12 +98,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
98 ewah_iterator_init(&it, ewah);
99
100 while (ewah_iterator_next(&blowup, &it)) {
103 - if (i >= bitmap->word_alloc) {
104 - bitmap->word_alloc *= 1.5;
105 - bitmap->words = xrealloc(
106 - bitmap->words, bitmap->word_alloc * sizeof(eword_t));
107 - }
108 -
101 + ALLOC_GROW(bitmap->words, i + 1, bitmap->word_alloc);
102 bitmap->words[i++] = blowup;
103 }
104
@@ -134,8 +127,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
127
128 if (self->word_alloc < other_final) {
129 self->word_alloc = other_final;
137 - self->words = xrealloc(self->words,
138 - self->word_alloc * sizeof(eword_t));
130 + REALLOC_ARRAY(self->words, self->word_alloc);
131 memset(self->words + original_size, 0x0,
132 (self->word_alloc - original_size) * sizeof(eword_t));
133 }
ewah/ewah_bitmap.c
+2 -3
@@ -39,8 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
39 return;
40
41 self->alloc_size = new_size;
42 - self->buffer = xrealloc(self->buffer,
43 - self->alloc_size * sizeof(eword_t));
42 + REALLOC_ARRAY(self->buffer, self->alloc_size);
43 self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
44 }
45
@@ -283,8 +282,8 @@ struct ewah_bitmap *ewah_new(void)
282 struct ewah_bitmap *self;
283
284 self = xmalloc(sizeof(struct ewah_bitmap));
286 - self->buffer = xmalloc(32 * sizeof(eword_t));
285 self->alloc_size = 32;
286 + ALLOC_ARRAY(self->buffer, self->alloc_size);
287
288 ewah_clear(self);
289 return self;
ewah/ewah_io.c
+2 -4
@@ -134,8 +134,7 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
134 self->buffer_size = self->alloc_size = get_be32(ptr);
135 ptr += sizeof(uint32_t);
136
137 - self->buffer = xrealloc(self->buffer,
138 - self->alloc_size * sizeof(eword_t));
137 + REALLOC_ARRAY(self->buffer, self->alloc_size);
138
139 /*
140 * Copy the raw data for the bitmap as a whole chunk;
@@ -177,8 +176,7 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)
176 return -1;
177
178 self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
180 - self->buffer = xrealloc(self->buffer,
181 - self->alloc_size * sizeof(eword_t));
179 + REALLOC_ARRAY(self->buffer, self->alloc_size);
180
181 /** 64 bit x N -- compressed words */
182 buffer = self->buffer;