| 1 | /* |
| 2 | * enhanced_alloc.h |
| 3 | * |
| 4 | * Copyright (C) 2013-2017 Aerospike, Inc. |
| 5 | * |
| 6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
| 7 | * license agreements. |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU Affero General Public License as published by the Free |
| 11 | * Software Foundation, either version 3 of the License, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 17 | * details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU Affero General Public License |
| 20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
| 21 | */ |
| 22 | |
| 23 | #pragma once |
| 24 | |
| 25 | //========================================================== |
| 26 | // Includes. |
| 27 | // |
| 28 | |
| 29 | #include <malloc.h> |
| 30 | #include <stdbool.h> |
| 31 | #include <stddef.h> |
| 32 | #include <stdint.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include "citrusleaf/cf_atomic.h" |
| 36 | |
| 37 | |
| 38 | //========================================================== |
| 39 | // Typedefs & constants. |
| 40 | // |
| 41 | |
| 42 | typedef struct { |
| 43 | cf_atomic32 ; |
| 44 | uint32_t ; |
| 45 | } ; |
| 46 | |
| 47 | typedef enum { |
| 48 | CF_ALLOC_DEBUG_NONE, |
| 49 | CF_ALLOC_DEBUG_TRANSIENT, |
| 50 | CF_ALLOC_DEBUG_PERSISTENT, |
| 51 | CF_ALLOC_DEBUG_ALL |
| 52 | } cf_alloc_debug; |
| 53 | |
| 54 | |
| 55 | //========================================================== |
| 56 | // Public API - arena management and stats. |
| 57 | // |
| 58 | |
| 59 | extern __thread int32_t g_ns_arena; |
| 60 | |
| 61 | void cf_alloc_init(void); |
| 62 | void cf_alloc_set_debug(cf_alloc_debug debug_allocations, bool indent_allocations); |
| 63 | int32_t cf_alloc_create_arena(void); |
| 64 | |
| 65 | #define CF_ALLOC_SET_NS_ARENA(_ns) \ |
| 66 | (g_ns_arena = _ns->storage_data_in_memory ? _ns->jem_arena : -1) |
| 67 | |
| 68 | static inline int32_t |
| 69 | cf_alloc_clear_ns_arena(void) |
| 70 | { |
| 71 | int32_t old_arena = g_ns_arena; |
| 72 | g_ns_arena = -1; |
| 73 | return old_arena; |
| 74 | } |
| 75 | |
| 76 | static inline void |
| 77 | cf_alloc_restore_ns_arena(int32_t old_arena) |
| 78 | { |
| 79 | g_ns_arena = old_arena; |
| 80 | } |
| 81 | |
| 82 | void cf_alloc_heap_stats(size_t *allocated_kbytes, size_t *active_kbytes, size_t *mapped_kbytes, double *efficiency_pct, uint32_t *site_count); |
| 83 | void cf_alloc_log_stats(const char *file, const char *opts); |
| 84 | void cf_alloc_log_site_infos(const char *file); |
| 85 | |
| 86 | |
| 87 | //========================================================== |
| 88 | // Public API - ordinary allocation. |
| 89 | // |
| 90 | |
| 91 | // Don't call these directly - use wrappers below. |
| 92 | void *cf_alloc_try_malloc(size_t sz); |
| 93 | void *cf_alloc_malloc_arena(size_t sz, int32_t arena); |
| 94 | void *cf_alloc_calloc_arena(size_t n, size_t sz, int32_t arena); |
| 95 | void *cf_alloc_realloc_arena(void *p, size_t sz, int32_t arena); |
| 96 | |
| 97 | #define cf_try_malloc(_sz) cf_alloc_try_malloc(_sz) |
| 98 | |
| 99 | #define cf_malloc(_sz) malloc(_sz) |
| 100 | #define cf_malloc_ns(_sz) cf_alloc_malloc_arena(_sz, g_ns_arena) |
| 101 | |
| 102 | #define cf_calloc(_n, _sz) calloc(_n, _sz) |
| 103 | #define cf_calloc_ns(_n, _sz) cf_alloc_calloc_arena(_n, _sz, g_ns_arena) |
| 104 | |
| 105 | #define cf_realloc(_p, _sz) realloc(_p, _sz) |
| 106 | #define cf_realloc_ns(_p, _sz) cf_alloc_realloc_arena(_p, _sz, g_ns_arena) |
| 107 | |
| 108 | #define cf_valloc(_sz) valloc(_sz) |
| 109 | |
| 110 | #define cf_strdup(_s) strdup(_s) |
| 111 | #define cf_strndup(_s, _n) strndup(_s, _n) |
| 112 | |
| 113 | #define cf_asprintf(_s, _f, ...) ({ \ |
| 114 | int32_t _n = asprintf(_s, _f, __VA_ARGS__); \ |
| 115 | _n; \ |
| 116 | }) |
| 117 | |
| 118 | #define cf_free(_p) free(_p) |
| 119 | |
| 120 | extern bool g_alloc_started; |
| 121 | |
| 122 | |
| 123 | //========================================================== |
| 124 | // Public API - reference-counted allocation. |
| 125 | // |
| 126 | |
| 127 | void *cf_rc_alloc(size_t sz); |
| 128 | void cf_rc_free(void *p); |
| 129 | |
| 130 | int32_t cf_rc_count(const void *p); |
| 131 | int32_t cf_rc_reserve(void *p); |
| 132 | int32_t cf_rc_release(void *p); |
| 133 | int32_t cf_rc_releaseandfree(void *p); |
| 134 | |