Skip to content

Commit 3be6f54

Browse files
authored
Merge pull request #280 from bratpiorka/rrudnick_ba_refactor
refactor ba_global_alloc and fix coverity issues
2 parents 548ce5c + 966eeba commit 3be6f54

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ static void umf_ba_create_global(void) {
6969

7070
// returns index of the allocation class for a given size
7171
static int size_to_idx(size_t size) {
72-
assert(size <= BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]);
73-
7472
if (size <= BASE_ALLOC.ac_sizes[0]) {
7573
return 0;
7674
}
@@ -80,8 +78,6 @@ static int size_to_idx(size_t size) {
8078
(int)(log2Utils(size) + !isPowerOf2 - BASE_ALLOC.smallest_ac_size_log2);
8179

8280
assert(index >= 0);
83-
assert(index < NUM_ALLOCATION_CLASSES);
84-
8581
return index;
8682
}
8783

@@ -147,7 +143,8 @@ void *umf_ba_global_aligned_alloc(size_t size, size_t alignment) {
147143
size += alignment;
148144
}
149145

150-
if (size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) {
146+
int ac_index = size_to_idx(size);
147+
if (ac_index >= NUM_ALLOCATION_CLASSES) {
151148
#ifndef NDEBUG
152149
fprintf(stderr,
153150
"base_alloc: allocation size larger than the biggest "
@@ -156,7 +153,6 @@ void *umf_ba_global_aligned_alloc(size_t size, size_t alignment) {
156153
return add_metadata_and_align(ba_os_alloc(size), size, alignment);
157154
}
158155

159-
int ac_index = size_to_idx(size);
160156
if (!BASE_ALLOC.ac[ac_index]) {
161157
// if creating ac failed, fall back to os allocation
162158
fprintf(stderr, "base_alloc: allocation class not created. Falling "
@@ -180,12 +176,12 @@ void umf_ba_global_free(void *ptr) {
180176
size_t total_size;
181177
ptr = get_original_alloc(ptr, &total_size, NULL);
182178

183-
if (total_size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) {
179+
int ac_index = size_to_idx(total_size);
180+
if (ac_index >= NUM_ALLOCATION_CLASSES) {
184181
ba_os_free(ptr, total_size);
185182
return;
186183
}
187184

188-
int ac_index = size_to_idx(total_size);
189185
if (!BASE_ALLOC.ac[ac_index]) {
190186
// if creating ac failed, memory must have been allocated by os
191187
ba_os_free(ptr, total_size);

0 commit comments

Comments
 (0)