pst_allocator refactoring, use mutex to access fields
This commit is contained in:
@@ -14,23 +14,29 @@
|
|||||||
|
|
||||||
void heap_free(pst_allocator* alloc, void* buff)
|
void heap_free(pst_allocator* alloc, void* buff)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&alloc->lock);
|
||||||
alloc->size -= malloc_usable_size(buff);
|
alloc->size -= malloc_usable_size(buff);
|
||||||
|
pthread_mutex_unlock(&alloc->lock);
|
||||||
free(buff);
|
free(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* heap_alloc(pst_allocator* alloc, uint32_t size)
|
void* heap_alloc(pst_allocator* alloc, uint32_t size)
|
||||||
{
|
{
|
||||||
void* buff = malloc(size);
|
void* buff = malloc(size);
|
||||||
|
pthread_mutex_lock(&alloc->lock);
|
||||||
alloc->size += malloc_usable_size(buff);
|
alloc->size += malloc_usable_size(buff);
|
||||||
|
pthread_mutex_unlock(&alloc->lock);
|
||||||
|
|
||||||
return buff;
|
return buff;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* heap_realloc(pst_allocator* alloc, void* buff, uint32_t new_size)
|
void* heap_realloc(pst_allocator* alloc, void* buff, uint32_t new_size)
|
||||||
{
|
{
|
||||||
|
pthread_mutex_lock(&alloc->lock);
|
||||||
alloc->size -= malloc_usable_size(buff);
|
alloc->size -= malloc_usable_size(buff);
|
||||||
void* new_buff = realloc(buff, new_size);
|
void* new_buff = realloc(buff, new_size);
|
||||||
alloc->size += malloc_usable_size(new_buff);
|
alloc->size += malloc_usable_size(new_buff);
|
||||||
|
pthread_mutex_unlock(&alloc->lock);
|
||||||
|
|
||||||
return new_buff;
|
return new_buff;
|
||||||
}
|
}
|
||||||
@@ -45,6 +51,8 @@ void pst_alloc_init(pst_allocator* alloc)
|
|||||||
alloc->free = heap_free;
|
alloc->free = heap_free;
|
||||||
alloc->realloc = heap_realloc;
|
alloc->realloc = heap_realloc;
|
||||||
|
|
||||||
|
pthread_mutex_init(&alloc->lock, NULL);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,4 +70,6 @@ void pst_alloc_fini(pst_allocator* alloc)
|
|||||||
alloc->base = NULL;
|
alloc->base = NULL;
|
||||||
alloc->size = 0;
|
alloc->size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_mutex_destroy(&alloc->lock);
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-11
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
// concatenation
|
// concatenation
|
||||||
#define CAT(a, ...) CAT2(a, __VA_ARGS__)
|
#define CAT(a, ...) CAT2(a, __VA_ARGS__)
|
||||||
@@ -27,26 +28,24 @@
|
|||||||
#define pst_fini(TYPE, NAME) \
|
#define pst_fini(TYPE, NAME) \
|
||||||
CAT2(TYPE, _fini) (NAME);
|
CAT2(TYPE, _fini) (NAME);
|
||||||
|
|
||||||
typedef struct __pst_allocator pst_allocator;
|
|
||||||
|
|
||||||
typedef void* (*pst_alloc_def)(pst_allocator* alloc, uint32_t size);
|
|
||||||
typedef void (*pst_free_def)(pst_allocator* alloc, void* buff);
|
|
||||||
typedef void* (*pst_realloc_def)(pst_allocator* alloc, void* buff, uint32_t new_size);
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
ALLOC_NONE = 0, // not initialized
|
ALLOC_NONE = 0, // not initialized
|
||||||
ALLOC_HEAP = 1, // use libc memory allocator
|
ALLOC_HEAP = 1, // use libc memory allocator
|
||||||
ALLOC_CUSTOM = 2 // use custom allocator in predefined range of memory
|
ALLOC_CUSTOM = 2 // use custom allocator in predefined range of memory
|
||||||
} pst_alloc_type;
|
} pst_alloc_type;
|
||||||
|
|
||||||
typedef struct __pst_allocator {
|
typedef struct pst_allocator {
|
||||||
|
// methods
|
||||||
|
void* (*alloc)(pst_allocator* alloc, uint32_t size);
|
||||||
|
void (*free)(pst_allocator* alloc, void* buff);
|
||||||
|
void* (*realloc)(pst_allocator* alloc, void* buff, uint32_t new_size);
|
||||||
|
|
||||||
|
// fields
|
||||||
int type;
|
int type;
|
||||||
void* base;
|
void* base;
|
||||||
uint32_t size;
|
uint32_t size;
|
||||||
// routines
|
pthread_mutex_t lock;
|
||||||
pst_alloc_def alloc;
|
|
||||||
pst_free_def free;
|
|
||||||
pst_realloc_def realloc;
|
|
||||||
} pst_allocator;
|
} pst_allocator;
|
||||||
|
|
||||||
void pst_alloc_init(pst_allocator* alloc);
|
void pst_alloc_init(pst_allocator* alloc);
|
||||||
|
|||||||
Reference in New Issue
Block a user