Skip to content

Commit db92ea0

Browse files
committed
skip ba_global_free when BA is already destroyed
1 parent e53e52f commit db92ea0

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/base_alloc/base_alloc_global.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323

2424
// global base allocator used by all providers and pools
2525
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT;
26+
static bool ba_is_destroyed = false;
2627

2728
#define ALLOC_METADATA_SIZE (sizeof(size_t))
2829

2930
// allocation classes need to be consecutive powers of 2
30-
#define ALLOCATION_CLASSES \
31-
{ 16, 32, 64, 128, 256 }
31+
#define ALLOCATION_CLASSES {16, 32, 64, 128, 256}
3232
#define NUM_ALLOCATION_CLASSES 5
3333

3434
struct base_alloc_t {
@@ -40,6 +40,8 @@ struct base_alloc_t {
4040
static struct base_alloc_t BASE_ALLOC = {.ac_sizes = ALLOCATION_CLASSES};
4141

4242
void umf_ba_destroy_global(void) {
43+
ba_is_destroyed = true;
44+
4345
for (int i = 0; i < NUM_ALLOCATION_CLASSES; i++) {
4446
if (BASE_ALLOC.ac[i]) {
4547
umf_ba_destroy(BASE_ALLOC.ac[i]);
@@ -48,10 +50,12 @@ void umf_ba_destroy_global(void) {
4850
}
4951

5052
// portable version of "ba_is_initialized = UTIL_ONCE_FLAG_INIT;"
51-
static UTIL_ONCE_FLAG is_initialized = UTIL_ONCE_FLAG_INIT;
52-
memcpy(&ba_is_initialized, &is_initialized, sizeof(ba_is_initialized));
53+
static UTIL_ONCE_FLAG set_once = UTIL_ONCE_FLAG_INIT;
54+
memcpy(&ba_is_initialized, &set_once, sizeof(ba_is_initialized));
5355
}
5456

57+
bool umf_ba_global_is_destroyed(void) { return ba_is_destroyed; }
58+
5559
static void umf_ba_create_global(void) {
5660
for (int i = 0; i < NUM_ALLOCATION_CLASSES; i++) {
5761
// allocation classes need to be powers of 2
@@ -202,6 +206,11 @@ void umf_ba_global_free(void *ptr) {
202206
return;
203207
}
204208

209+
if (ba_is_destroyed) {
210+
LOG_WARN("base_alloc: calling free after the base alloc is destroyed");
211+
return;
212+
}
213+
205214
size_t total_size;
206215
ptr = get_original_alloc(ptr, &total_size, NULL);
207216

src/base_alloc/base_alloc_global.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#ifndef UMF_BASE_ALLOC_GLOBAL_H
99
#define UMF_BASE_ALLOC_GLOBAL_H 1
1010

11+
#include <stdbool.h>
12+
1113
#include "base_alloc.h"
1214

1315
#ifdef __cplusplus
@@ -17,6 +19,7 @@ extern "C" {
1719
void *umf_ba_global_alloc(size_t size);
1820
void umf_ba_global_free(void *ptr);
1921
void umf_ba_destroy_global(void);
22+
bool umf_ba_global_is_destroyed();
2023
size_t umf_ba_global_malloc_usable_size(void *ptr);
2124
void *umf_ba_global_aligned_alloc(size_t size, size_t alignment);
2225

0 commit comments

Comments
 (0)