diff --git a/src/bloomflex.cc b/src/bloomflex.cc index d60b5347..9feb0d12 100644 --- a/src/bloomflex.cc +++ b/src/bloomflex.cc @@ -35,6 +35,7 @@ #include #include "bloomflex.h" #include "pseudo_rng.h" +#include "util.h" auto bloomflex_patterns_generate(struct bloomflex_s * b) -> void @@ -64,7 +65,7 @@ auto bloomflex_init(uint64_t size, unsigned int k) -> struct bloomflex_s * constexpr unsigned int multiplier {16}; // multiply by 65,536 constexpr unsigned int divider {3}; // divide by 8 - auto * b = new struct bloomflex_s; + auto * b = static_cast(xmalloc(sizeof(struct bloomflex_s))); b->size = size >> divider; b->pattern_shift = multiplier; @@ -72,10 +73,10 @@ auto bloomflex_init(uint64_t size, unsigned int k) -> struct bloomflex_s * b->pattern_mask = b->pattern_count - 1; b->pattern_k = k; - b->patterns = new uint64_t[b->pattern_count]; + b->patterns = static_cast(xmalloc(b->pattern_count * sizeof(uint64_t))); bloomflex_patterns_generate(b); - b->bitmap = new uint64_t[size]; + b->bitmap = static_cast(xmalloc(size)); memset(b->bitmap, UINT8_MAX, size); return b; @@ -84,10 +85,7 @@ auto bloomflex_init(uint64_t size, unsigned int k) -> struct bloomflex_s * auto bloomflex_exit(struct bloomflex_s * b) -> void { - delete [] b->bitmap; - b->bitmap = nullptr; - delete [] b->patterns; - b->patterns = nullptr; - delete b; - b = nullptr; + xfree(b->bitmap); + xfree(b->patterns); + xfree(b); } diff --git a/src/util.cc b/src/util.cc index 6d1a00cf..2b55140c 100644 --- a/src/util.cc +++ b/src/util.cc @@ -28,6 +28,7 @@ static const char * progress_prompt; static uint64_t progress_next; static uint64_t progress_size; static uint64_t progress_chunk; +constexpr size_t memalignment = 16; auto progress_init(const char * prompt, const uint64_t size) -> void @@ -70,13 +71,32 @@ auto progress_done() -> void } +auto xmalloc(size_t size) -> void * +{ + if (size == 0) { + size = 1; + } + void * t {nullptr}; +#ifdef _WIN32 + t = _aligned_malloc(size, memalignment); +#else + if (posix_memalign(& t, memalignment, size) != 0) { + t = nullptr; + } +#endif + if (t == nullptr) { + fatal(error_prefix, "Unable to allocate enough memory."); + } + return t; +} + + auto xrealloc(void *ptr, size_t size) -> void * { if (size == 0) { size = 1; } #ifdef _WIN32 - constexpr size_t memalignment = 16; void * t = _aligned_realloc(ptr, size, memalignment); #else void * t = realloc(ptr, size); diff --git a/src/util.h b/src/util.h index ffcfcfbb..d0620130 100644 --- a/src/util.h +++ b/src/util.h @@ -25,6 +25,7 @@ #include +auto xmalloc(size_t size) -> void *; auto xrealloc(void * ptr, size_t size) -> void *; auto xfree(void * ptr) -> void; auto xgetline(char ** linep, size_t * linecapp, FILE * stream) -> ssize_t;