Browse Source

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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
maint
Jeff King 9 years ago committed by Junio C Hamano
parent
commit
fb7dbf3e7a
  1. 12
      ewah/bitmap.c
  2. 9
      ewah/ewah_bitmap.c
  3. 10
      ewah/ewah_io.c
  4. 10
      ewah/ewok.h

12
ewah/bitmap.c

@ -25,8 +25,8 @@


struct bitmap *bitmap_new(void) struct bitmap *bitmap_new(void)
{ {
struct bitmap *bitmap = ewah_malloc(sizeof(struct bitmap)); struct bitmap *bitmap = xmalloc(sizeof(struct bitmap));
bitmap->words = ewah_calloc(32, sizeof(eword_t)); bitmap->words = xcalloc(32, sizeof(eword_t));
bitmap->word_alloc = 32; bitmap->word_alloc = 32;
return bitmap; return bitmap;
} }
@ -38,8 +38,8 @@ void bitmap_set(struct bitmap *self, size_t pos)
if (block >= self->word_alloc) { if (block >= self->word_alloc) {
size_t old_size = self->word_alloc; size_t old_size = self->word_alloc;
self->word_alloc = block * 2; self->word_alloc = block * 2;
self->words = ewah_realloc(self->words, self->words = xrealloc(self->words,
self->word_alloc * sizeof(eword_t)); self->word_alloc * sizeof(eword_t));


memset(self->words + old_size, 0x0, memset(self->words + old_size, 0x0,
(self->word_alloc - old_size) * sizeof(eword_t)); (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)) { while (ewah_iterator_next(&blowup, &it)) {
if (i >= bitmap->word_alloc) { if (i >= bitmap->word_alloc) {
bitmap->word_alloc *= 1.5; bitmap->word_alloc *= 1.5;
bitmap->words = ewah_realloc( bitmap->words = xrealloc(
bitmap->words, bitmap->word_alloc * sizeof(eword_t)); 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) { if (self->word_alloc < other_final) {
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)); self->word_alloc * sizeof(eword_t));
memset(self->words + original_size, 0x0, memset(self->words + original_size, 0x0,
(self->word_alloc - original_size) * sizeof(eword_t)); (self->word_alloc - original_size) * sizeof(eword_t));

9
ewah/ewah_bitmap.c

@ -39,7 +39,7 @@ static inline void buffer_grow(struct ewah_bitmap *self, size_t new_size)
return; return;


self->alloc_size = new_size; self->alloc_size = new_size;
self->buffer = ewah_realloc(self->buffer, self->buffer = xrealloc(self->buffer,
self->alloc_size * sizeof(eword_t)); self->alloc_size * sizeof(eword_t));
self->rlw = self->buffer + (rlw_offset / sizeof(eword_t)); self->rlw = self->buffer + (rlw_offset / sizeof(eword_t));
} }
@ -282,11 +282,8 @@ struct ewah_bitmap *ewah_new(void)
{ {
struct ewah_bitmap *self; struct ewah_bitmap *self;


self = ewah_malloc(sizeof(struct ewah_bitmap)); self = xmalloc(sizeof(struct ewah_bitmap));
if (self == NULL) self->buffer = xmalloc(32 * sizeof(eword_t));
return NULL;

self->buffer = ewah_malloc(32 * sizeof(eword_t));
self->alloc_size = 32; self->alloc_size = 32;


ewah_clear(self); ewah_clear(self);

10
ewah/ewah_io.c

@ -134,12 +134,9 @@ int ewah_read_mmap(struct ewah_bitmap *self, const void *map, size_t len)
self->buffer_size = self->alloc_size = get_be32(ptr); self->buffer_size = self->alloc_size = get_be32(ptr);
ptr += sizeof(uint32_t); ptr += sizeof(uint32_t);


self->buffer = ewah_realloc(self->buffer, self->buffer = xrealloc(self->buffer,
self->alloc_size * sizeof(eword_t)); self->alloc_size * sizeof(eword_t));


if (!self->buffer)
return -1;

/* /*
* Copy the raw data for the bitmap as a whole chunk; * Copy the raw data for the bitmap as a whole chunk;
* if we're in a little-endian platform, we'll perform * if we're in a little-endian platform, we'll perform
@ -180,12 +177,9 @@ int ewah_deserialize(struct ewah_bitmap *self, int fd)
return -1; return -1;


self->buffer_size = self->alloc_size = (size_t)ntohl(word_count); self->buffer_size = self->alloc_size = (size_t)ntohl(word_count);
self->buffer = ewah_realloc(self->buffer, self->buffer = xrealloc(self->buffer,
self->alloc_size * sizeof(eword_t)); self->alloc_size * sizeof(eword_t));


if (!self->buffer)
return -1;

/** 64 bit x N -- compressed words */ /** 64 bit x N -- compressed words */
buffer = self->buffer; buffer = self->buffer;
words_left = self->buffer_size; words_left = self->buffer_size;

10
ewah/ewok.h

@ -20,16 +20,6 @@
#ifndef __EWOK_BITMAP_H__ #ifndef __EWOK_BITMAP_H__
#define __EWOK_BITMAP_H__ #define __EWOK_BITMAP_H__


#ifndef ewah_malloc
# define ewah_malloc xmalloc
#endif
#ifndef ewah_realloc
# define ewah_realloc xrealloc
#endif
#ifndef ewah_calloc
# define ewah_calloc xcalloc
#endif

struct strbuf; struct strbuf;
typedef uint64_t eword_t; typedef uint64_t eword_t;
#define BITS_IN_EWORD (sizeof(eword_t) * 8) #define BITS_IN_EWORD (sizeof(eword_t) * 8)

Loading…
Cancel
Save