Skip to content

Commit fb7dbf3

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b1ddfb9 commit fb7dbf3

File tree

4 files changed

+11
-30
lines changed

4 files changed

+11
-30
lines changed

ewah/bitmap.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
struct bitmap *bitmap_new(void)
2727
{
28-
struct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap));
29-
bitmap->words = ewah_calloc(32, sizeof(eword_t));
28+
struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
29+
bitmap->words = xcalloc(32, sizeof(eword_t));
3030
bitmap->word_alloc = 32;
3131
return bitmap;
3232
}
@@ -38,8 +38,8 @@ void bitmap_set(struct bitmap *self, size_t pos)
3838
if (block >= self->word_alloc) {
3939
size_t old_size = self->word_alloc;
4040
self->word_alloc = block * 2;
41-
self->words = ewah_realloc(self->words,
42-
self->word_alloc * sizeof(eword_t));
41+
self->words = xrealloc(self->words,
42+
self->word_alloc * sizeof(eword_t));
4343

4444
memset(self->words + old_size, 0x0,
4545
(self->word_alloc - old_size) * sizeof(eword_t));
@@ -102,7 +102,7 @@ struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah)
102102
while (ewah_iterator_next(&blowup, &it)) {
103103
if (i >= bitmap->word_alloc) {
104104
bitmap->word_alloc *= 1.5;
105-
bitmap->words = ewah_realloc(
105+
bitmap->words = xrealloc(
106106
bitmap->words, bitmap->word_alloc * sizeof(eword_t));
107107
}
108108

@@ -134,7 +134,7 @@ void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
134134

135135
if (self->word_alloc < other_final) {
136136
self->word_alloc = other_final;
137-
self->words = ewah_realloc(self->words,
137+
self->words = xrealloc(self->words,
138138
self->word_alloc * sizeof(eword_t));
139139
memset(self->words + original_size, 0x0,
140140
(self->word_alloc - original_size) * sizeof(eword_t));

ewah/ewah_bitmap.c

+3-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
3939
return;
4040

4141
self->alloc_size = new_size;
42-
self->buffer = ewah_realloc(self->buffer,
42+
self->buffer = xrealloc(self->buffer,
4343
self->alloc_size * sizeof(eword_t));
4444
self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
4545
}
@@ -282,11 +282,8 @@ struct ewah_bitmap *ewah_new(void)
282282
{
283283
struct ewah_bitmap *self;
284284

285-
self = ewah_malloc(sizeof(struct ewah_bitmap));
286-
if (self == NULL)
287-
return NULL;
288-
289-
self->buffer = ewah_malloc(32 * sizeof(eword_t));
285+
self = xmalloc(sizeof(struct ewah_bitmap));
286+
self->buffer = xmalloc(32 * sizeof(eword_t));
290287
self->alloc_size = 32;
291288

292289
ewah_clear(self);

ewah/ewah_io.c

+2-8
Original file line numberDiff line numberDiff line change
@@ -134,12 +134,9 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
134134
self->buffer_size = self->alloc_size = get_be32(ptr);
135135
ptr += sizeof(uint32_t);
136136

137-
self->buffer = ewah_realloc(self->buffer,
137+
self->buffer = xrealloc(self->buffer,
138138
self->alloc_size * sizeof(eword_t));
139139

140-
if (!self->buffer)
141-
return -1;
142-
143140
/*
144141
* Copy the raw data for the bitmap as a whole chunk;
145142
* if we're in a little-endian platform, we'll perform
@@ -180,12 +177,9 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)
180177
return -1;
181178

182179
self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
183-
self->buffer = ewah_realloc(self->buffer,
180+
self->buffer = xrealloc(self->buffer,
184181
self->alloc_size * sizeof(eword_t));
185182

186-
if (!self->buffer)
187-
return -1;
188-
189183
/** 64 bit x N -- compressed words */
190184
buffer = self->buffer;
191185
words_left = self->buffer_size;

ewah/ewok.h

-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,6 @@
2020
#ifndef __EWOK_BITMAP_H__
2121
#define __EWOK_BITMAP_H__
2222

23-
#ifndef ewah_malloc
24-
# define ewah_malloc xmalloc
25-
#endif
26-
#ifndef ewah_realloc
27-
# define ewah_realloc xrealloc
28-
#endif
29-
#ifndef ewah_calloc
30-
# define ewah_calloc xcalloc
31-
#endif
32-
3323
struct strbuf;
3424
typedef uint64_t eword_t;
3525
#define BITS_IN_EWORD (sizeof(eword_t) * 8)

0 commit comments

Comments
 (0)