Skip to content

Commit a3f399b

Browse files
committed
refactor ba_global_alloc and fix coverity issues
1 parent de4cce7 commit a3f399b

File tree

1 file changed

+4
-15
lines changed

1 file changed

+4
-15
lines changed

src/base_alloc/base_alloc_global.c

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

6767
// returns index of the allocation class for a given size
6868
static int size_to_idx(size_t size) {
69-
assert(size <= BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]);
70-
7169
if (size <= BASE_ALLOC.ac_sizes[0]) {
7270
return 0;
7371
}
@@ -77,15 +75,14 @@ static int size_to_idx(size_t size) {
7775
(int)(log2Utils(size) + !isPowerOf2 - BASE_ALLOC.smallest_ac_size_log2);
7876

7977
assert(index >= 0);
80-
assert(index < NUM_ALLOCATION_CLASSES);
81-
8278
return index;
8379
}
8480

8581
void *umf_ba_global_alloc(size_t size) {
8682
util_init_once(&ba_is_initialized, umf_ba_create_global);
8783

88-
if (size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) {
84+
int ac_index = size_to_idx(size);
85+
if (ac_index >= NUM_ALLOCATION_CLASSES) {
8986
#ifndef NDEBUG
9087
fprintf(stderr,
9188
"base_alloc: allocation size larger than the biggest "
@@ -94,24 +91,16 @@ void *umf_ba_global_alloc(size_t size) {
9491
return ba_os_alloc(size);
9592
}
9693

97-
int ac_index = size_to_idx(size);
98-
if (!BASE_ALLOC.ac[ac_index]) {
99-
// if creating ac failed, fall back to os allocation
100-
fprintf(stderr, "base_alloc: allocation class not created. Falling "
101-
"back to OS memory allocation.\n");
102-
return ba_os_alloc(size);
103-
}
104-
10594
return umf_ba_alloc(BASE_ALLOC.ac[ac_index]);
10695
}
10796

10897
void umf_ba_global_free(void *ptr, size_t size) {
109-
if (size > BASE_ALLOC.ac_sizes[NUM_ALLOCATION_CLASSES - 1]) {
98+
int ac_index = size_to_idx(size);
99+
if (ac_index >= NUM_ALLOCATION_CLASSES) {
110100
ba_os_free(ptr, size);
111101
return;
112102
}
113103

114-
int ac_index = size_to_idx(size);
115104
if (!BASE_ALLOC.ac[ac_index]) {
116105
// if creating ac failed, memory must have been allocated by os
117106
ba_os_free(ptr, size);

0 commit comments

Comments
 (0)