Skip to content

Commit

Permalink
fix: remove null checks before free. Reverses commit eb5395b (#643)
Browse files Browse the repository at this point in the history
* fix: remove null checks before free. Reverses commit eb5395b

* completing the null removal

* updating readme

* adding back some checks.

* lint
  • Loading branch information
lemire authored Jul 30, 2024
1 parent ac8639c commit 8cc603f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 3 additions & 9 deletions src/containers/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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));
}
Expand Down
7 changes: 2 additions & 5 deletions src/containers/bitset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
12 changes: 3 additions & 9 deletions src/containers/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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.
Expand Down

0 comments on commit 8cc603f

Please sign in to comment.