| 1 | /* -*- c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | Copyright(C) 2009-2016 Brazil |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License version 2.1 as published by the Free Software Foundation. |
| 8 | |
| 9 | This library is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | Lesser General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU Lesser General Public |
| 15 | License along with this library; if not, write to the Free Software |
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "grn.h" |
| 22 | |
| 23 | #ifdef __cplusplus |
| 24 | extern "C" { |
| 25 | #endif |
| 26 | |
| 27 | void grn_alloc_init_from_env(void); |
| 28 | |
| 29 | void grn_alloc_init_ctx_impl(grn_ctx *ctx); |
| 30 | void grn_alloc_fin_ctx_impl(grn_ctx *ctx); |
| 31 | |
| 32 | void grn_alloc_info_init(void); |
| 33 | void grn_alloc_info_fin(void); |
| 34 | |
| 35 | void grn_alloc_info_dump(grn_ctx *ctx); |
| 36 | void grn_alloc_info_free(grn_ctx *ctx); |
| 37 | |
| 38 | #define GRN_MALLOC(s) grn_malloc(ctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 39 | #define GRN_CALLOC(s) grn_calloc(ctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 40 | #define GRN_REALLOC(p,s) grn_realloc(ctx,p,s,__FILE__,__LINE__,__FUNCTION__) |
| 41 | #define GRN_STRDUP(s) grn_strdup(ctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 42 | #define GRN_GMALLOC(s) grn_malloc(&grn_gctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 43 | #define GRN_GCALLOC(s) grn_calloc(&grn_gctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 44 | #define GRN_GREALLOC(p,s) grn_realloc(&grn_gctx,p,s,__FILE__,__LINE__,__FUNCTION__) |
| 45 | #define GRN_GSTRDUP(s) grn_strdup(&grn_gctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 46 | #define GRN_FREE(p) grn_free(ctx,p,__FILE__,__LINE__,__FUNCTION__) |
| 47 | #define GRN_MALLOCN(t,n) ((t *)(GRN_MALLOC(sizeof(t) * (n)))) |
| 48 | #define GRN_GFREE(p) grn_free(&grn_gctx,p,__FILE__,__LINE__,__FUNCTION__) |
| 49 | #define GRN_GMALLOCN(t,n) ((t *)(GRN_GMALLOC(sizeof(t) * (n)))) |
| 50 | |
| 51 | #define GRN_CTX_ALLOC(ctx,s) grn_ctx_calloc(ctx,s,__FILE__,__LINE__,__FUNCTION__) |
| 52 | #define GRN_CTX_FREE(ctx,p) grn_ctx_free(ctx,p,__FILE__,__LINE__,__FUNCTION__) |
| 53 | #define GRN_CTX_ALLOC_L(ctx,s) grn_ctx_alloc_lifo(ctx,s,f,__FILE__,__LINE__,__FUNCTION__) |
| 54 | #define GRN_CTX_FREE_L(ctx,p) grn_ctx_free_lifo(ctx,p,__FILE__,__LINE__,__FUNCTION__) |
| 55 | |
| 56 | void *grn_ctx_malloc(grn_ctx *ctx, |
| 57 | size_t size, |
| 58 | const char *file, |
| 59 | int line, |
| 60 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 61 | void *grn_ctx_calloc(grn_ctx *ctx, |
| 62 | size_t size, |
| 63 | const char *file, |
| 64 | int line, |
| 65 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 66 | void *grn_ctx_realloc(grn_ctx *ctx, |
| 67 | void *ptr, |
| 68 | size_t size, |
| 69 | const char *file, |
| 70 | int line, |
| 71 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(3); |
| 72 | char *grn_ctx_strdup(grn_ctx *ctx, const char *s, |
| 73 | const char* file, int line, const char *func); |
| 74 | void grn_ctx_free(grn_ctx *ctx, void *ptr, |
| 75 | const char* file, int line, const char *func); |
| 76 | void *grn_ctx_alloc_lifo(grn_ctx *ctx, |
| 77 | size_t size, |
| 78 | const char *file, |
| 79 | int line, |
| 80 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 81 | void grn_ctx_free_lifo(grn_ctx *ctx, void *ptr, |
| 82 | const char* file, int line, const char *func); |
| 83 | |
| 84 | #ifdef USE_DYNAMIC_MALLOC_CHANGE |
| 85 | typedef void *(*grn_malloc_func) (grn_ctx *ctx, size_t size, |
| 86 | const char *file, int line, const char *func); |
| 87 | typedef void *(*grn_calloc_func) (grn_ctx *ctx, size_t size, |
| 88 | const char *file, int line, const char *func); |
| 89 | typedef void *(*grn_realloc_func) (grn_ctx *ctx, void *ptr, size_t size, |
| 90 | const char *file, int line, const char *func); |
| 91 | typedef char *(*grn_strdup_func) (grn_ctx *ctx, const char *string, |
| 92 | const char *file, int line, const char *func); |
| 93 | typedef void (*grn_free_func) (grn_ctx *ctx, void *ptr, |
| 94 | const char *file, int line, const char *func); |
| 95 | grn_malloc_func grn_ctx_get_malloc(grn_ctx *ctx); |
| 96 | void grn_ctx_set_malloc(grn_ctx *ctx, grn_malloc_func malloc_func); |
| 97 | grn_calloc_func grn_ctx_get_calloc(grn_ctx *ctx); |
| 98 | void grn_ctx_set_calloc(grn_ctx *ctx, grn_calloc_func calloc_func); |
| 99 | grn_realloc_func grn_ctx_get_realloc(grn_ctx *ctx); |
| 100 | void grn_ctx_set_realloc(grn_ctx *ctx, grn_realloc_func realloc_func); |
| 101 | grn_strdup_func grn_ctx_get_strdup(grn_ctx *ctx); |
| 102 | void grn_ctx_set_strdup(grn_ctx *ctx, grn_strdup_func strdup_func); |
| 103 | grn_free_func grn_ctx_get_free(grn_ctx *ctx); |
| 104 | void grn_ctx_set_free(grn_ctx *ctx, grn_free_func free_func); |
| 105 | |
| 106 | void *grn_malloc(grn_ctx *ctx, |
| 107 | size_t size, |
| 108 | const char *file, |
| 109 | int line, |
| 110 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 111 | void *grn_calloc(grn_ctx *ctx, |
| 112 | size_t size, |
| 113 | const char *file, |
| 114 | int line, |
| 115 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 116 | void *grn_realloc(grn_ctx *ctx, |
| 117 | void *ptr, |
| 118 | size_t size, |
| 119 | const char *file, |
| 120 | int line, |
| 121 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(3); |
| 122 | char *grn_strdup(grn_ctx *ctx, const char *s, const char* file, int line, const char *func); |
| 123 | void grn_free(grn_ctx *ctx, void *ptr, const char *file, int line, const char *func); |
| 124 | #else |
| 125 | # define grn_malloc grn_malloc_default |
| 126 | # define grn_calloc grn_calloc_default |
| 127 | # define grn_realloc grn_realloc_default |
| 128 | # define grn_strdup grn_strdup_default |
| 129 | # define grn_free grn_free_default |
| 130 | #endif |
| 131 | |
| 132 | GRN_API void *grn_malloc_default(grn_ctx *ctx, |
| 133 | size_t size, |
| 134 | const char *file, |
| 135 | int line, |
| 136 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 137 | void *grn_calloc_default(grn_ctx *ctx, |
| 138 | size_t size, |
| 139 | const char *file, |
| 140 | int line, |
| 141 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(2); |
| 142 | void *grn_realloc_default(grn_ctx *ctx, |
| 143 | void *ptr, |
| 144 | size_t size, |
| 145 | const char *file, |
| 146 | int line, |
| 147 | const char *func) GRN_ATTRIBUTE_ALLOC_SIZE(3); |
| 148 | GRN_API char *grn_strdup_default(grn_ctx *ctx, const char *s, const char* file, int line, const char *func); |
| 149 | GRN_API void grn_free_default(grn_ctx *ctx, void *ptr, const char* file, int line, const char *func); |
| 150 | |
| 151 | #ifdef USE_FAIL_MALLOC |
| 152 | int grn_fail_malloc_check(size_t size, const char *file, int line, const char *func); |
| 153 | void *grn_malloc_fail(grn_ctx *ctx, size_t size, const char* file, int line, const char *func); |
| 154 | void *grn_calloc_fail(grn_ctx *ctx, size_t size, const char* file, int line, const char *func); |
| 155 | void *grn_realloc_fail(grn_ctx *ctx, void *ptr, size_t size, const char* file, int line, const char *func); |
| 156 | char *grn_strdup_fail(grn_ctx *ctx, const char *s, const char* file, int line, const char *func); |
| 157 | #endif |
| 158 | |
| 159 | int grn_alloc_count(void); |
| 160 | |
| 161 | #ifdef __cplusplus |
| 162 | } |
| 163 | #endif |
| 164 | |