| 1 | /* |
| 2 | * Copyright (c) 2013-2022 Joris Vink <[email protected]> |
| 3 | * |
| 4 | * Permission to use, copy, modify, and distribute this software for any |
| 5 | * purpose with or without fee is hereby granted, provided that the above |
| 6 | * copyright notice and this permission notice appear in all copies. |
| 7 | * |
| 8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/mman.h> |
| 19 | #include <sys/queue.h> |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 24 | |
| 25 | #define POOL_MIN_ELEMENTScxx . . $ `src/pool.c:25:9`! 16 |
| 26 | |
| 27 | #define POOL_ELEMENT_BUSYcxx . . $ `src/pool.c:27:9`! 0 |
| 28 | #define POOL_ELEMENT_FREEcxx . . $ `src/pool.c:28:9`! 1 |
| 29 | |
| 30 | #if defined(KORE_USE_TASKS) |
| 31 | static void pool_lock(struct kore_pool *); |
| 32 | static void pool_unlock(struct kore_pool *); |
| 33 | #endif |
| 34 | |
| 35 | static void pool_region_createcxx . . $ pool_region_create(75e36fdc7bbfd803).(struct kore_poolcxx . . $ kore_pool# *, size_t); |
| 36 | static void pool_region_destroycxx . . $ pool_region_destroy(68cee620b8bd445e).(struct kore_poolcxx . . $ kore_pool# *); |
| 37 | |
| 38 | void |
| 39 | kore_pool_initcxx . . $ kore_pool_init(19b74537ff42c712).(struct kore_poolcxx . . $ kore_pool# *pool, const char *name, |
| 40 | size_t len, size_t elm) |
| 41 | { |
| 42 | if (elm < POOL_MIN_ELEMENTScxx . . $ `src/pool.c:25:9`!) |
| 43 | elm = POOL_MIN_ELEMENTScxx . . $ `src/pool.c:25:9`!; |
| 44 | |
| 45 | if ((pool->namecxx . . $ kore_pool#name. = strdupcxx . . $ strdup(a8b8e32347182212).(name)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 46 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_pool_init: strdup %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 47 | |
| 48 | len = (len + (8 - 1)) & ~(8 - 1); |
| 49 | |
| 50 | pool->lockcxx . . $ kore_pool#lock. = 0; |
| 51 | pool->elmscxx . . $ kore_pool#elms. = 0; |
| 52 | pool->inusecxx . . $ kore_pool#inuse. = 0; |
| 53 | pool->elencxx . . $ kore_pool#elen. = len; |
| 54 | pool->growthcxx . . $ kore_pool#growth. = elm * 0.25f; |
| 55 | pool->slencxx . . $ kore_pool#slen. = pool->elencxx . . $ kore_pool#elen. + sizeof(struct kore_pool_entrycxx . . $ kore_pool_entry#); |
| 56 | |
| 57 | LIST_INITcxx . . $ `<external>/2546723208511035544/queue.h:101:9`!(&(pool->regions)); |
| 58 | LIST_INITcxx . . $ `<external>/2546723208511035544/queue.h:101:9`!(&(pool->freelist)); |
| 59 | |
| 60 | pool_region_createcxx . . $ pool_region_create(75e36fdc7bbfd803).(pool, elm); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | kore_pool_cleanupcxx . . $ kore_pool_cleanup(68cee620b8bd445e).(struct kore_poolcxx . . $ kore_pool# *pool) |
| 65 | { |
| 66 | pool->lockcxx . . $ kore_pool#lock. = 0; |
| 67 | pool->elmscxx . . $ kore_pool#elms. = 0; |
| 68 | pool->inusecxx . . $ kore_pool#inuse. = 0; |
| 69 | pool->elencxx . . $ kore_pool#elen. = 0; |
| 70 | pool->slencxx . . $ kore_pool#slen. = 0; |
| 71 | |
| 72 | freecxx . . $ free(5d22bdacc48458e8).(pool->namecxx . . $ kore_pool#name.); |
| 73 | pool->namecxx . . $ kore_pool#name. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; |
| 74 | |
| 75 | pool_region_destroycxx . . $ pool_region_destroy(68cee620b8bd445e).(pool); |
| 76 | } |
| 77 | |
| 78 | void * |
| 79 | kore_pool_getcxx . . $ kore_pool_get(f5c578167d321b17).(struct kore_poolcxx . . $ kore_pool# *pool) |
| 80 | { |
| 81 | u_int8_tcxx . . $ u_int8_t# *ptr; |
| 82 | struct kore_pool_entrycxx . . $ kore_pool_entry# *entry; |
| 83 | |
| 84 | #if defined(KORE_USE_TASKS) |
| 85 | pool_lock(pool); |
| 86 | #endif |
| 87 | |
| 88 | if (LIST_EMPTYcxx . . $ `<external>/2546723208511035544/queue.h:142:9`!(&(pool->freelist))) |
| 89 | pool_region_createcxx . . $ pool_region_create(75e36fdc7bbfd803).(pool, pool->growthcxx . . $ kore_pool#growth.); |
| 90 | |
| 91 | entry = LIST_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:143:9`!(&(pool->freelist)); |
| 92 | if (entry->statecxx . . $ kore_pool_entry#state. != POOL_ELEMENT_FREEcxx . . $ `src/pool.c:28:9`!) |
| 93 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: element %p was not free", pool->namecxx . . $ kore_pool#name., (void *)entry); |
| 94 | LIST_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:127:9`!(entry, list); |
| 95 | |
| 96 | entry->statecxx . . $ kore_pool_entry#state. = POOL_ELEMENT_BUSYcxx . . $ `src/pool.c:27:9`!; |
| 97 | ptr = (u_int8_tcxx . . $ u_int8_t# *)entry + sizeof(struct kore_pool_entrycxx . . $ kore_pool_entry#); |
| 98 | |
| 99 | pool->inusecxx . . $ kore_pool#inuse.++; |
| 100 | |
| 101 | #if defined(KORE_USE_TASKS) |
| 102 | pool_unlock(pool); |
| 103 | #endif |
| 104 | |
| 105 | return (ptr); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | kore_pool_putcxx . . $ kore_pool_put(d9a47b307a51c9d5).(struct kore_poolcxx . . $ kore_pool# *pool, void *ptr) |
| 110 | { |
| 111 | struct kore_pool_entrycxx . . $ kore_pool_entry# *entry; |
| 112 | |
| 113 | #if defined(KORE_USE_TASKS) |
| 114 | pool_lock(pool); |
| 115 | #endif |
| 116 | |
| 117 | entry = (struct kore_pool_entrycxx . . $ kore_pool_entry# *) |
| 118 | ((u_int8_tcxx . . $ u_int8_t# *)ptr - sizeof(struct kore_pool_entrycxx . . $ kore_pool_entry#)); |
| 119 | |
| 120 | if (entry->statecxx . . $ kore_pool_entry#state. != POOL_ELEMENT_BUSYcxx . . $ `src/pool.c:27:9`!) |
| 121 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: element %p was not busy", pool->namecxx . . $ kore_pool#name., ptr); |
| 122 | |
| 123 | entry->statecxx . . $ kore_pool_entry#state. = POOL_ELEMENT_FREEcxx . . $ `src/pool.c:28:9`!; |
| 124 | LIST_INSERT_HEADcxx . . $ `<external>/2546723208511035544/queue.h:120:9`!(&(pool->freelist), entry, list); |
| 125 | |
| 126 | pool->inusecxx . . $ kore_pool#inuse.--; |
| 127 | |
| 128 | #if defined(KORE_USE_TASKS) |
| 129 | pool_unlock(pool); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | static void |
| 134 | pool_region_createcxx . . $ pool_region_create(75e36fdc7bbfd803).(struct kore_poolcxx . . $ kore_pool# *pool, size_t elms) |
| 135 | { |
| 136 | size_t i; |
| 137 | u_int8_tcxx . . $ u_int8_t# *p; |
| 138 | struct kore_pool_regioncxx . . $ kore_pool_region# *reg; |
| 139 | struct kore_pool_entrycxx . . $ kore_pool_entry# *entry; |
| 140 | |
| 141 | if ((reg = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, sizeof(struct kore_pool_regioncxx . . $ kore_pool_region#))) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 142 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("pool_region_create: calloc: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 143 | |
| 144 | LIST_INSERT_HEADcxx . . $ `<external>/2546723208511035544/queue.h:120:9`!(&(pool->regions), reg, list); |
| 145 | |
| 146 | if (SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`! / elms < pool->slencxx . . $ kore_pool#slen.) |
| 147 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("pool_region_create: overflow"); |
| 148 | |
| 149 | reg->lengthcxx . . $ kore_pool_region#length. = elms * pool->slencxx . . $ kore_pool#slen.; |
| 150 | reg->startcxx . . $ kore_pool_region#start. = mmapcxx . . $ mmap(ff06c1c904cdf0e2).(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, reg->lengthcxx . . $ kore_pool_region#length., PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!, |
| 151 | MAP_ANONYMOUScxx . . $ `<external>/17226767460805440860/mman-linux.h:55:10`! | MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`!, -1, 0); |
| 152 | if (reg->startcxx . . $ kore_pool_region#start. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) |
| 153 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("mmap: %s", errno_scxx . . $ `include/kore/kore.h:100:9`!); |
| 154 | |
| 155 | p = (u_int8_tcxx . . $ u_int8_t# *)reg->startcxx . . $ kore_pool_region#start.; |
| 156 | |
| 157 | for (i = 0; i < elms; i++) { |
| 158 | entry = (struct kore_pool_entrycxx . . $ kore_pool_entry# *)p; |
| 159 | entry->regioncxx . . $ kore_pool_entry#region. = reg; |
| 160 | entry->statecxx . . $ kore_pool_entry#state. = POOL_ELEMENT_FREEcxx . . $ `src/pool.c:28:9`!; |
| 161 | LIST_INSERT_HEADcxx . . $ `<external>/2546723208511035544/queue.h:120:9`!(&(pool->freelist), entry, list); |
| 162 | |
| 163 | p = p + pool->slencxx . . $ kore_pool#slen.; |
| 164 | } |
| 165 | |
| 166 | pool->elmscxx . . $ kore_pool#elms. += elms; |
| 167 | } |
| 168 | |
| 169 | static void |
| 170 | pool_region_destroycxx . . $ pool_region_destroy(68cee620b8bd445e).(struct kore_poolcxx . . $ kore_pool# *pool) |
| 171 | { |
| 172 | struct kore_pool_regioncxx . . $ kore_pool_region# *reg; |
| 173 | |
| 174 | /* Take care iterating when modifying list contents */ |
| 175 | while (!LIST_EMPTYcxx . . $ `<external>/2546723208511035544/queue.h:142:9`!(&pool->regions)) { |
| 176 | reg = LIST_FIRSTcxx . . $ `<external>/2546723208511035544/queue.h:143:9`!(&pool->regions); |
| 177 | LIST_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:127:9`!(reg, list); |
| 178 | (void)munmapcxx . . $ munmap(c0a5f96701d3f89).(reg->startcxx . . $ kore_pool_region#start., reg->lengthcxx . . $ kore_pool_region#length.); |
| 179 | freecxx . . $ free(5d22bdacc48458e8).(reg); |
| 180 | } |
| 181 | |
| 182 | /* Freelist references into the regions memory allocations */ |
| 183 | LIST_INITcxx . . $ `<external>/2546723208511035544/queue.h:101:9`!(&pool->freelist); |
| 184 | pool->elmscxx . . $ kore_pool#elms. = 0; |
| 185 | } |
| 186 | |
| 187 | #if defined(KORE_USE_TASKS) |
| 188 | static void |
| 189 | pool_lock(struct kore_pool *pool) |
| 190 | { |
| 191 | for (;;) { |
| 192 | if (__sync_bool_compare_and_swap(&pool->lock, 0, 1)) |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | pool_unlock(struct kore_pool *pool) |
| 199 | { |
| 200 | if (!__sync_bool_compare_and_swap(&pool->lock, 1, 0)) |
| 201 | fatal("pool_unlock: failed to release %s", pool->name); |
| 202 | } |
| 203 | #endif |