Skip to content

skip ba_global_free when BA is already destroyed #939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

// global base allocator used by all providers and pools
static UTIL_ONCE_FLAG ba_is_initialized = UTIL_ONCE_FLAG_INIT;
static bool ba_is_destroyed = false;

#define ALLOC_METADATA_SIZE (sizeof(size_t))

Expand All @@ -40,6 +41,8 @@ struct base_alloc_t {
static struct base_alloc_t BASE_ALLOC = {.ac_sizes = ALLOCATION_CLASSES};

void umf_ba_destroy_global(void) {
ba_is_destroyed = true;

for (int i = 0; i < NUM_ALLOCATION_CLASSES; i++) {
if (BASE_ALLOC.ac[i]) {
umf_ba_destroy(BASE_ALLOC.ac[i]);
Expand All @@ -48,10 +51,12 @@ void umf_ba_destroy_global(void) {
}

// portable version of "ba_is_initialized = UTIL_ONCE_FLAG_INIT;"
static UTIL_ONCE_FLAG is_initialized = UTIL_ONCE_FLAG_INIT;
memcpy(&ba_is_initialized, &is_initialized, sizeof(ba_is_initialized));
static UTIL_ONCE_FLAG set_once = UTIL_ONCE_FLAG_INIT;
memcpy(&ba_is_initialized, &set_once, sizeof(ba_is_initialized));
}

bool umf_ba_global_is_destroyed(void) { return ba_is_destroyed; }

static void umf_ba_create_global(void) {
for (int i = 0; i < NUM_ALLOCATION_CLASSES; i++) {
// allocation classes need to be powers of 2
Expand Down Expand Up @@ -202,6 +207,11 @@ void umf_ba_global_free(void *ptr) {
return;
}

if (ba_is_destroyed) {
LOG_WARN("base_alloc: calling free after the base alloc is destroyed");
return;
}

size_t total_size;
ptr = get_original_alloc(ptr, &total_size, NULL);

Expand Down
3 changes: 3 additions & 0 deletions src/base_alloc/base_alloc_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#ifndef UMF_BASE_ALLOC_GLOBAL_H
#define UMF_BASE_ALLOC_GLOBAL_H 1

#include <stdbool.h>

#include "base_alloc.h"

#ifdef __cplusplus
Expand All @@ -17,6 +19,7 @@ extern "C" {
void *umf_ba_global_alloc(size_t size);
void umf_ba_global_free(void *ptr);
void umf_ba_destroy_global(void);
bool umf_ba_global_is_destroyed(void);
size_t umf_ba_global_malloc_usable_size(void *ptr);
void *umf_ba_global_aligned_alloc(size_t size, size_t alignment);

Expand Down
Loading