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 -11
View File
@@ -10,6 +10,7 @@
#include <stdint.h>
#include <assert.h>
#include <pthread.h>
// concatenation
#define CAT(a, ...) CAT2(a, __VA_ARGS__)
@@ -27,26 +28,24 @@
#define pst_fini(TYPE, 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 {
ALLOC_NONE = 0, // not initialized
ALLOC_HEAP = 1, // use libc memory allocator
ALLOC_CUSTOM = 2 // use custom allocator in predefined range of memory
} 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;
void* base;
uint32_t size;
// routines
pst_alloc_def alloc;
pst_free_def free;
pst_realloc_def realloc;
pthread_mutex_t lock;
} pst_allocator;
void pst_alloc_init(pst_allocator* alloc);