| 1 | #ifndef XC_ALLOCATOR_H_155E773CA8AFC18F3CCCDCF0831EE41D |
|---|
| 2 | #define XC_ALLOCATOR_H_155E773CA8AFC18F3CCCDCF0831EE41D |
|---|
| 3 | |
|---|
| 4 | #if _MSC_VER > 1000 |
|---|
| 5 | #pragma once |
|---|
| 6 | #endif /* _MSC_VER > 1000 */ |
|---|
| 7 | |
|---|
| 8 | #include "xc_shm.h" |
|---|
| 9 | |
|---|
| 10 | typedef struct _xc_allocator_vtable_t xc_allocator_vtable_t; |
|---|
| 11 | |
|---|
| 12 | #ifndef _xc_allocator_t |
|---|
| 13 | struct _xc_allocator_t { |
|---|
| 14 | const xc_allocator_vtable_t *vtable; |
|---|
| 15 | xc_shm_t *shm; |
|---|
| 16 | }; |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | typedef struct _xc_allocator_t xc_allocator_t; |
|---|
| 20 | typedef struct _xc_allocator_block_t xc_allocator_block_t; |
|---|
| 21 | typedef xc_shmsize_t xc_memsize_t; |
|---|
| 22 | |
|---|
| 23 | /* allocator */ |
|---|
| 24 | #define XC_ALLOCATOR_MALLOC(func) void *func(xc_allocator_t *allocator, xc_memsize_t size) |
|---|
| 25 | #define XC_ALLOCATOR_FREE(func) xc_memsize_t func(xc_allocator_t *allocator, const void *p) |
|---|
| 26 | #define XC_ALLOCATOR_CALLOC(func) void *func(xc_allocator_t *allocator, xc_memsize_t memb, xc_memsize_t size) |
|---|
| 27 | #define XC_ALLOCATOR_REALLOC(func) void *func(xc_allocator_t *allocator, const void *p, xc_memsize_t size) |
|---|
| 28 | #define XC_ALLOCATOR_AVAIL(func) xc_memsize_t func(const xc_allocator_t *allocator) |
|---|
| 29 | #define XC_ALLOCATOR_SIZE(func) xc_memsize_t func(const xc_allocator_t *allocator) |
|---|
| 30 | #define XC_ALLOCATOR_FREEBLOCK_FIRST(func) const xc_allocator_block_t *func(const xc_allocator_t *allocator) |
|---|
| 31 | #define XC_ALLOCATOR_FREEBLOCK_NEXT(func) const xc_allocator_block_t *func(const xc_allocator_block_t *block) |
|---|
| 32 | #define XC_ALLOCATOR_BLOCK_SIZE(func) xc_memsize_t func(const xc_allocator_block_t *block) |
|---|
| 33 | #define XC_ALLOCATOR_BLOCK_OFFSET(func) xc_memsize_t func(const xc_allocator_t *allocator, const xc_allocator_block_t *block) |
|---|
| 34 | |
|---|
| 35 | #define XC_ALLOCATOR_INIT(func) xc_allocator_t *func(xc_shm_t *shm, xc_allocator_t *allocator, xc_memsize_t size) |
|---|
| 36 | #define XC_ALLOCATOR_DESTROY(func) void func(xc_allocator_t *allocator) |
|---|
| 37 | |
|---|
| 38 | #define XC_ALLOCATOR_VTABLE(name) { \ |
|---|
| 39 | xc_##name##_malloc \ |
|---|
| 40 | , xc_##name##_free \ |
|---|
| 41 | , xc_##name##_calloc \ |
|---|
| 42 | , xc_##name##_realloc \ |
|---|
| 43 | , xc_##name##_avail \ |
|---|
| 44 | , xc_##name##_size \ |
|---|
| 45 | , xc_##name##_freeblock_first \ |
|---|
| 46 | , xc_##name##_freeblock_next \ |
|---|
| 47 | , xc_##name##_block_size \ |
|---|
| 48 | , xc_##name##_block_offset \ |
|---|
| 49 | \ |
|---|
| 50 | , xc_##name##_init \ |
|---|
| 51 | , xc_##name##_destroy \ |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | struct _xc_allocator_vtable_t { |
|---|
| 55 | XC_ALLOCATOR_MALLOC((*malloc)); |
|---|
| 56 | XC_ALLOCATOR_FREE((*free)); |
|---|
| 57 | XC_ALLOCATOR_CALLOC((*calloc)); |
|---|
| 58 | XC_ALLOCATOR_REALLOC((*realloc)); |
|---|
| 59 | XC_ALLOCATOR_AVAIL((*avail)); |
|---|
| 60 | XC_ALLOCATOR_SIZE((*size)); |
|---|
| 61 | XC_ALLOCATOR_FREEBLOCK_FIRST((*freeblock_first)); |
|---|
| 62 | XC_ALLOCATOR_FREEBLOCK_NEXT((*freeblock_next)); |
|---|
| 63 | XC_ALLOCATOR_BLOCK_SIZE((*block_size)); |
|---|
| 64 | XC_ALLOCATOR_BLOCK_OFFSET((*block_offset)); |
|---|
| 65 | |
|---|
| 66 | XC_ALLOCATOR_INIT((*init)); |
|---|
| 67 | XC_ALLOCATOR_DESTROY((*destroy)); |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | int xc_allocator_register(const char *name, const xc_allocator_vtable_t *allocator_vtable); |
|---|
| 71 | const xc_allocator_vtable_t *xc_allocator_find(const char *name); |
|---|
| 72 | |
|---|
| 73 | #endif /* XC_ALLOCATOR_H_155E773CA8AFC18F3CCCDCF0831EE41D */ |
|---|