| 1 |
#ifndef CARYLL_SUPPORT_OTFCC_ALLOC_H |
| 2 |
#define CARYLL_SUPPORT_OTFCC_ALLOC_H |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#ifndef INLINE |
| 8 |
#ifdef _MSC_VER |
| 9 |
#define INLINE __forceinline /* use __forceinline (VC++ specific) */ |
| 10 |
#else |
| 11 |
#define INLINE inline /* use standard inline */ |
| 12 |
#endif |
| 13 |
#endif |
| 14 |
|
| 15 |
// Allocators |
| 16 |
// Change this if you prefer other allocators |
| 17 |
#define __caryll_malloc malloc |
| 18 |
#define __caryll_calloc calloc |
| 19 |
#define __caryll_realloc realloc |
| 20 |
#define __caryll_free free |
| 21 |
|
| 22 |
static INLINE void *__caryll_allocate_dirty(size_t n, unsigned long line) { |
| 23 |
if (!n) return NULL; |
| 24 |
void *p = __caryll_malloc(n); |
| 25 |
if (!p) { |
| 26 |
fprintf(stderr, "[%ld]Out of memory(%ld bytes)\n", line, (unsigned long)n); |
| 27 |
exit(EXIT_FAILURE); |
| 28 |
} |
| 29 |
return p; |
| 30 |
} |
| 31 |
static INLINE void *__caryll_allocate_clean(size_t n, unsigned long line) { |
| 32 |
if (!n) return NULL; |
| 33 |
void *p = __caryll_calloc(n, 1); |
| 34 |
if (!p) { |
| 35 |
fprintf(stderr, "[%ld]Out of memory(%ld bytes)\n", line, (unsigned long)n); |
| 36 |
exit(EXIT_FAILURE); |
| 37 |
} |
| 38 |
return p; |
| 39 |
} |
| 40 |
static INLINE void *__caryll_reallocate(void *ptr, size_t n, unsigned long line) { |
| 41 |
if (!n) { |
| 42 |
__caryll_free(ptr); |
| 43 |
return NULL; |
| 44 |
} |
| 45 |
if (!ptr) { |
| 46 |
return __caryll_allocate_clean(n, line); |
| 47 |
} else { |
| 48 |
void *p = __caryll_realloc(ptr, n); |
| 49 |
if (!p) { |
| 50 |
fprintf(stderr, "[%ld]Out of memory(%ld bytes)\n", line, (unsigned long)n); |
| 51 |
exit(EXIT_FAILURE); |
| 52 |
} |
| 53 |
return p; |
| 54 |
} |
| 55 |
} |
| 56 |
#ifdef __cplusplus |
| 57 |
#define NEW_CLEAN_S(ptr, size) ptr = __caryll_allocate_clean((size), __LINE__) |
| 58 |
#define NEW_CLEAN_1(ptr) \ |
| 59 |
ptr = (decltype(ptr))__caryll_allocate_clean(sizeof(decltype(*ptr)), __LINE__) |
| 60 |
#define NEW_CLEAN_N(ptr, n) \ |
| 61 |
ptr = (decltype(ptr))__caryll_allocate_clean(sizeof(decltype(*ptr)) * (n), __LINE__) |
| 62 |
#define NEW_DIRTY(ptr) \ |
| 63 |
ptr = (decltype(ptr))__caryll_allocate_dirty(sizeof(decltype(*ptr)), __LINE__) |
| 64 |
#define NEW_DIRTY_N(ptr, n) \ |
| 65 |
ptr = (decltype(ptr))__caryll_allocate_dirty(sizeof(decltype(*ptr)) * (n), __LINE__) |
| 66 |
#define FREE(ptr) (__caryll_free(ptr), ptr = nullptr) |
| 67 |
#define DELETE(fn, ptr) (fn(ptr), ptr = nullptr) |
| 68 |
#define RESIZE(ptr, n) ptr = (decltype(ptr))__caryll_reallocate(ptr, sizeof(*ptr) * (n), __LINE__) |
| 69 |
#else |
| 70 |
#define NEW_CLEAN_S(ptr, size) ptr = __caryll_allocate_clean((size), __LINE__) |
| 71 |
#define NEW_CLEAN_1(ptr) ptr = __caryll_allocate_clean(sizeof(*ptr), __LINE__) |
| 72 |
#define NEW_CLEAN_N(ptr, n) ptr = __caryll_allocate_clean(sizeof(*ptr) * (n), __LINE__) |
| 73 |
#define NEW_DIRTY(ptr) ptr = __caryll_allocate_dirty(sizeof(*ptr), __LINE__) |
| 74 |
#define NEW_DIRTY_N(ptr, n) ptr = __caryll_allocate_dirty(sizeof(*ptr) * (n), __LINE__) |
| 75 |
#define FREE(ptr) (__caryll_free(ptr), ptr = NULL) |
| 76 |
#define DELETE(fn, ptr) (fn(ptr), ptr = NULL) |
| 77 |
#define RESIZE(ptr, n) ptr = __caryll_reallocate(ptr, sizeof(*ptr) * (n), __LINE__) |
| 78 |
#endif |
| 79 |
|
| 80 |
#define __GET_MACRO_OTFCC_ALLOC_2(_1, _2, NAME, ...) NAME |
| 81 |
#define NEW(...) __GET_MACRO_OTFCC_ALLOC_2(__VA_ARGS__, NEW_CLEAN_N, NEW_CLEAN_1)(__VA_ARGS__) |
| 82 |
|
| 83 |
#endif |