diff --git a/README.md b/README.md index c884c438a..f582d3115 100644 --- a/README.md +++ b/README.md @@ -303,6 +303,21 @@ int main(){ } ``` +By default we use: +```C +static roaring_memory_t global_memory_hook = { + .malloc = malloc, + .realloc = realloc, + .calloc = calloc, + .free = free, + .aligned_malloc = roaring_bitmap_aligned_malloc, + .aligned_free = roaring_bitmap_aligned_free, +}; +``` + +We require that the `free`/`aligned_free` functions follow the C +convention where `free(NULL)`/`aligned_free(NULL)` have no effect. + # Example (C) diff --git a/src/containers/array.c b/src/containers/array.c index 0a24482a3..4b5e5bc53 100644 --- a/src/containers/array.c +++ b/src/containers/array.c @@ -145,11 +145,8 @@ int array_container_shrink_to_fit(array_container_t *src) { /* Free memory. */ void array_container_free(array_container_t *arr) { - if (arr->array != - NULL) { // Jon Strabala reports that some tools complain otherwise - roaring_free(arr->array); - arr->array = NULL; // pedantic - } + if (arr == NULL) return; + roaring_free(arr->array); roaring_free(arr); } @@ -177,10 +174,7 @@ void array_container_grow(array_container_t *container, int32_t min, (uint16_t *)roaring_realloc(array, new_capacity * sizeof(uint16_t)); if (container->array == NULL) roaring_free(array); } else { - // Jon Strabala reports that some tools complain otherwise - if (array != NULL) { - roaring_free(array); - } + roaring_free(array); container->array = (uint16_t *)roaring_malloc(new_capacity * sizeof(uint16_t)); } diff --git a/src/containers/bitset.c b/src/containers/bitset.c index f49739594..7b84af82e 100644 --- a/src/containers/bitset.c +++ b/src/containers/bitset.c @@ -130,11 +130,8 @@ void bitset_container_add_from_range(bitset_container_t *bitset, uint32_t min, /* Free memory. */ void bitset_container_free(bitset_container_t *bitset) { - if (bitset->words != - NULL) { // Jon Strabala reports that some tools complain otherwise - roaring_aligned_free(bitset->words); - bitset->words = NULL; // pedantic - } + if (bitset == NULL) return; + roaring_aligned_free(bitset->words); roaring_free(bitset); } diff --git a/src/containers/run.c b/src/containers/run.c index 1da4fc140..986db6d1a 100644 --- a/src/containers/run.c +++ b/src/containers/run.c @@ -189,11 +189,8 @@ void run_container_offset(const run_container_t *c, container_t **loc, /* Free memory. */ void run_container_free(run_container_t *run) { - if (run->runs != - NULL) { // Jon Strabala reports that some tools complain otherwise - roaring_free(run->runs); - run->runs = NULL; // pedantic - } + if (run == NULL) return; + roaring_free(run->runs); roaring_free(run); } @@ -211,10 +208,7 @@ void run_container_grow(run_container_t *run, int32_t min, bool copy) { run->capacity * sizeof(rle16_t)); if (run->runs == NULL) roaring_free(oldruns); } else { - // Jon Strabala reports that some tools complain otherwise - if (run->runs != NULL) { - roaring_free(run->runs); - } + roaring_free(run->runs); run->runs = (rle16_t *)roaring_malloc(run->capacity * sizeof(rle16_t)); } // We may have run->runs == NULL.