Skip to content

Commit

Permalink
race condition brutal fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Antonin Reitz committed Oct 17, 2023
1 parent 60b818b commit 1a14aa6
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/lib-alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,51 @@ uint8_t* StarMalloc_memset_u8(uint8_t* dest, uint8_t v, size_t n) {
}

void* malloc(size_t size) {
pthread_mutex_lock(&m);
if (! init_status) {
init_status=1UL;
pthread_mutex_lock(&m);
krmlinit_globals();
pthread_mutex_unlock(&m);
}
pthread_mutex_unlock(&m);
if (thread_arena == N_ARENA) {
thread_arena = thread_arena_counter++ % N_ARENA;
}
return StarMalloc_malloc(thread_arena, size);
}

void* aligned_alloc(size_t alignment, size_t size) {
pthread_mutex_lock(&m);
if (! init_status) {
pthread_mutex_lock(&m);
krmlinit_globals();
init_status=1UL;
pthread_mutex_unlock(&m);
}
pthread_mutex_unlock(&m);
if (thread_arena == N_ARENA) {
thread_arena = thread_arena_counter++ % N_ARENA;
}
return StarMalloc_aligned_alloc(thread_arena, alignment, size);
}

void free(void *ptr) {
pthread_mutex_lock(&m);
if (! init_status) {
pthread_mutex_lock(&m);
krmlinit_globals();
init_status=1UL;
pthread_mutex_unlock(&m);
}
pthread_mutex_unlock(&m);
//printf("free ptr: %p\n", ptr);
bool b = StarMalloc_free(ptr);
//printf(" result: %b\n");
return;
}

void* realloc(void* ptr, size_t new_size) {
pthread_mutex_lock(&m);
if (! init_status) {
pthread_mutex_lock(&m);
krmlinit_globals();
init_status=1UL;
pthread_mutex_unlock(&m);
}
pthread_mutex_unlock(&m);
if (thread_arena == N_ARENA) {
thread_arena = thread_arena_counter++ % N_ARENA;
}
Expand All @@ -79,12 +79,12 @@ void* realloc(void* ptr, size_t new_size) {
}

void* calloc(size_t nb_elem, size_t size_elem) {
pthread_mutex_lock(&m);
if (! init_status) {
pthread_mutex_lock(&m);
krmlinit_globals();
init_status=1UL;
pthread_mutex_unlock(&m);
}
pthread_mutex_unlock(&m);
if (thread_arena == N_ARENA) {
thread_arena = thread_arena_counter++ % N_ARENA;
}
Expand All @@ -93,11 +93,11 @@ void* calloc(size_t nb_elem, size_t size_elem) {
}

size_t malloc_usable_size(void* ptr) {
pthread_mutex_lock(&m);
if (! init_status) {
pthread_mutex_lock(&m);
krmlinit_globals();
init_status=1UL;
pthread_mutex_unlock(&m);
}
pthread_mutex_unlock(&m);
return StarMalloc_getsize(ptr);
}

0 comments on commit 1a14aa6

Please sign in to comment.