pst_allocator refactoring, use mutex to access fields

This commit is contained in:
2020-02-03 18:06:25 +04:00
parent 70e39e14a9
commit 813fe52bf1
2 changed files with 20 additions and 11 deletions
+10
View File
@@ -14,23 +14,29 @@
void heap_free(pst_allocator* alloc, void* buff)
{
pthread_mutex_lock(&alloc->lock);
alloc->size -= malloc_usable_size(buff);
pthread_mutex_unlock(&alloc->lock);
free(buff);
}
void* heap_alloc(pst_allocator* alloc, uint32_t size)
{
void* buff = malloc(size);
pthread_mutex_lock(&alloc->lock);
alloc->size += malloc_usable_size(buff);
pthread_mutex_unlock(&alloc->lock);
return buff;
}
void* heap_realloc(pst_allocator* alloc, void* buff, uint32_t new_size)
{
pthread_mutex_lock(&alloc->lock);
alloc->size -= malloc_usable_size(buff);
void* new_buff = realloc(buff, new_size);
alloc->size += malloc_usable_size(new_buff);
pthread_mutex_unlock(&alloc->lock);
return new_buff;
}
@@ -45,6 +51,8 @@ void pst_alloc_init(pst_allocator* alloc)
alloc->free = heap_free;
alloc->realloc = heap_realloc;
pthread_mutex_init(&alloc->lock, NULL);
return;
}
@@ -62,4 +70,6 @@ void pst_alloc_fini(pst_allocator* alloc)
alloc->base = NULL;
alloc->size = 0;
}
pthread_mutex_destroy(&alloc->lock);
}