| 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/param.h> |
| 18 | #include <sys/types.h> |
| 19 | |
| 20 | #include <stdlib.h> |
| 21 | #include <stdint.h> |
| 22 | |
| 23 | #include "kore.h"cxx . . $ `<file>/include/kore/kore.h`/ |
| 24 | |
| 25 | #define KORE_MEM_BLOCKScxx . . $ `src/mem.c:25:9`! 11 |
| 26 | #define KORE_MEM_BLOCK_SIZE_MAXcxx . . $ `src/mem.c:26:9`! 8192 |
| 27 | #define KORE_MEM_BLOCK_PREALLOCcxx . . $ `src/mem.c:27:9`! 128 |
| 28 | |
| 29 | #define KORE_MEM_ALIGNcxx . . $ `src/mem.c:29:9`! 16 |
| 30 | #define KORE_MEM_MAGICcxx . . $ `src/mem.c:30:9`! 0xd0d0 |
| 31 | |
| 32 | #define KORE_MEM_TAGGEDcxx . . $ `src/mem.c:32:9`! 0x0001 |
| 33 | |
| 34 | struct memsizecxx . . $ memsize# { |
| 35 | size_t lencxx . . $ memsize#len.; |
| 36 | size_t magiccxx . . $ memsize#magic.; |
| 37 | } __attribute__((packed)); |
| 38 | |
| 39 | struct meminfocxx . . $ meminfo# { |
| 40 | u_int16_tcxx . . $ u_int16_t# flagscxx . . $ meminfo#flags.; |
| 41 | u_int16_tcxx . . $ u_int16_t# magiccxx . . $ meminfo#magic.; |
| 42 | } __attribute__((packed)); |
| 43 | |
| 44 | struct memblockcxx . . $ memblock# { |
| 45 | struct kore_poolcxx . . $ kore_pool# poolcxx . . $ memblock#pool.; |
| 46 | }; |
| 47 | |
| 48 | struct tagcxx . . $ tag# { |
| 49 | void *ptrcxx . . $ tag#ptr.; |
| 50 | u_int32_tcxx . . $ u_int32_t# idcxx . . $ tag#id.; |
| 51 | TAILQ_ENTRYcxx . . $ `<external>/2546723208511035544/queue.h:382:9`!(tag) listcxx . . $ tag#list.; |
| 52 | }; |
| 53 | |
| 54 | static inline struct memsizecxx . . $ memsize# *memsizecxx . . $ memsize(48235604510b72a3).(void *); |
| 55 | static inline struct meminfocxx . . $ meminfo# *meminfocxx . . $ meminfo(30523716a785bca6).(void *); |
| 56 | static size_t memblock_indexcxx . . $ memblock_index(ecdaa1632f49f591).(size_t); |
| 57 | |
| 58 | static TAILQ_HEADcxx . . $ `<external>/2546723208511035544/queue.h:372:9`!(, tag) tagscxx . . $ tags.; |
| 59 | static struct kore_poolcxx . . $ kore_pool# tag_poolcxx . . $ tag_pool.; |
| 60 | static struct memblockcxx . . $ memblock# blockscxx . . $ blocks.[KORE_MEM_BLOCKScxx . . $ `src/mem.c:25:9`!]; |
| 61 | |
| 62 | void |
| 63 | kore_mem_initcxx . . $ kore_mem_init(49f6e7a06ebc5aa8).(void) |
| 64 | { |
| 65 | int i, len; |
| 66 | char name[32]; |
| 67 | u_int32_tcxx . . $ u_int32_t# size, elm, mlen; |
| 68 | |
| 69 | size = 8; |
| 70 | TAILQ_INITcxx . . $ `<external>/2546723208511035544/queue.h:387:9`!(&tags); |
| 71 | kore_pool_initcxx . . $ kore_pool_init(19b74537ff42c712).(&tag_poolcxx . . $ tag_pool., "tag_pool", sizeof(struct tagcxx . . $ tag#), 100); |
| 72 | |
| 73 | for (i = 0; i < KORE_MEM_BLOCKScxx . . $ `src/mem.c:25:9`!; i++) { |
| 74 | len = snprintfcxx . . $ snprintf(ea502d73e74c754c).(name, sizeof(name), "block-%u", size); |
| 75 | if (len == -1 || (size_t)len >= sizeof(name)) |
| 76 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_mem_init: snprintf"); |
| 77 | |
| 78 | elm = (KORE_MEM_BLOCK_PREALLOCcxx . . $ `src/mem.c:27:9`! * 1024) / size; |
| 79 | mlen = sizeof(struct memsizecxx . . $ memsize#) + size + |
| 80 | sizeof(struct meminfocxx . . $ meminfo#) + KORE_MEM_ALIGNcxx . . $ `src/mem.c:29:9`!; |
| 81 | mlen = mlen & ~(KORE_MEM_ALIGNcxx . . $ `src/mem.c:29:9`! - 1); |
| 82 | |
| 83 | kore_pool_initcxx . . $ kore_pool_init(19b74537ff42c712).(&blockscxx . . $ blocks.[i].poolcxx . . $ memblock#pool., name, mlen, elm); |
| 84 | |
| 85 | size = size << 1; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | kore_mem_cleanupcxx . . $ kore_mem_cleanup(49f6e7a06ebc5aa8).(void) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < KORE_MEM_BLOCKScxx . . $ `src/mem.c:25:9`!; i++) { |
| 95 | kore_pool_cleanupcxx . . $ kore_pool_cleanup(68cee620b8bd445e).(&blockscxx . . $ blocks.[i].poolcxx . . $ memblock#pool.); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void * |
| 100 | kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(size_t len) |
| 101 | { |
| 102 | void *ptr; |
| 103 | struct meminfocxx . . $ meminfo# *mem; |
| 104 | struct memsizecxx . . $ memsize# *size; |
| 105 | u_int8_tcxx . . $ u_int8_t# *addr; |
| 106 | size_t mlen, idx; |
| 107 | |
| 108 | if (len == 0) |
| 109 | len = 8; |
| 110 | |
| 111 | if (len <= KORE_MEM_BLOCK_SIZE_MAXcxx . . $ `src/mem.c:26:9`!) { |
| 112 | idx = memblock_indexcxx . . $ memblock_index(ecdaa1632f49f591).(len); |
| 113 | ptr = kore_pool_getcxx . . $ kore_pool_get(f5c578167d321b17).(&blockscxx . . $ blocks.[idx].poolcxx . . $ memblock#pool.); |
| 114 | } else { |
| 115 | mlen = sizeof(struct memsizecxx . . $ memsize#) + len + sizeof(struct meminfocxx . . $ meminfo#); |
| 116 | if ((ptr = calloccxx . . $ calloc(63a7bf2941bfcd7a).(1, mlen)) == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 117 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_malloc(%zu): %d", len, errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!); |
| 118 | } |
| 119 | |
| 120 | size = (struct memsizecxx . . $ memsize# *)ptr; |
| 121 | size->lencxx . . $ memsize#len. = len; |
| 122 | size->magiccxx . . $ memsize#magic. = KORE_MEM_MAGICcxx . . $ `src/mem.c:30:9`!; |
| 123 | |
| 124 | addr = (u_int8_tcxx . . $ u_int8_t# *)ptr + sizeof(struct memsizecxx . . $ memsize#); |
| 125 | |
| 126 | mem = (struct meminfocxx . . $ meminfo# *)(addr + size->lencxx . . $ memsize#len.); |
| 127 | mem->flagscxx . . $ meminfo#flags. = 0; |
| 128 | mem->magiccxx . . $ meminfo#magic. = KORE_MEM_MAGICcxx . . $ `src/mem.c:30:9`!; |
| 129 | |
| 130 | return (addr); |
| 131 | } |
| 132 | |
| 133 | void * |
| 134 | kore_realloccxx . . $ kore_realloc(417ab160f6baadc6).(void *ptr, size_t len) |
| 135 | { |
| 136 | struct memsizecxx . . $ memsize# *size; |
| 137 | void *nptr; |
| 138 | |
| 139 | if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 140 | nptr = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(len); |
| 141 | } else { |
| 142 | size = memsizecxx . . $ memsize(48235604510b72a3).(ptr); |
| 143 | if (len == size->lencxx . . $ memsize#len.) |
| 144 | return (ptr); |
| 145 | nptr = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(len); |
| 146 | memcpycxx . . $ memcpy(cc817cb175f282d).(nptr, ptr, MINcxx . . $ `<external>/1231830743887005719/param.h:102:9`!(len, size->len)); |
| 147 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(ptr); |
| 148 | } |
| 149 | |
| 150 | return (nptr); |
| 151 | } |
| 152 | |
| 153 | void * |
| 154 | kore_calloccxx . . $ kore_calloc(63a7bf2941bfcd7a).(size_t memb, size_t len) |
| 155 | { |
| 156 | void *ptr; |
| 157 | size_t total; |
| 158 | |
| 159 | if (SIZE_MAXcxx . . $ `<external>/3415975807581488833/stdint.h:216:11`! / memb < len) |
| 160 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_calloc(): memb * len > SIZE_MAX"); |
| 161 | |
| 162 | total = memb * len; |
| 163 | ptr = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(total); |
| 164 | memsetcxx . . $ memset(d1de1e4fb215f1c6).(ptr, 0, total); |
| 165 | |
| 166 | return (ptr); |
| 167 | } |
| 168 | |
| 169 | void |
| 170 | kore_freecxx . . $ kore_free(5d22bdacc48458e8).(void *ptr) |
| 171 | { |
| 172 | size_t idx; |
| 173 | struct meminfocxx . . $ meminfo# *mem; |
| 174 | struct memsizecxx . . $ memsize# *size; |
| 175 | u_int8_tcxx . . $ u_int8_t# *addr; |
| 176 | |
| 177 | if (ptr == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 178 | return; |
| 179 | |
| 180 | mem = meminfocxx . . $ meminfo(30523716a785bca6).(ptr); |
| 181 | if (mem->flagscxx . . $ meminfo#flags. & KORE_MEM_TAGGEDcxx . . $ `src/mem.c:32:9`!) { |
| 182 | kore_mem_untagcxx . . $ kore_mem_untag(5d22bdacc48458e8).(ptr); |
| 183 | mem->flagscxx . . $ meminfo#flags. &= ~KORE_MEM_TAGGEDcxx . . $ `src/mem.c:32:9`!; |
| 184 | } |
| 185 | |
| 186 | size = memsizecxx . . $ memsize(48235604510b72a3).(ptr); |
| 187 | addr = (u_int8_tcxx . . $ u_int8_t# *)ptr - sizeof(struct memsizecxx . . $ memsize#); |
| 188 | |
| 189 | if (size->lencxx . . $ memsize#len. <= KORE_MEM_BLOCK_SIZE_MAXcxx . . $ `src/mem.c:26:9`!) { |
| 190 | idx = memblock_indexcxx . . $ memblock_index(ecdaa1632f49f591).(size->lencxx . . $ memsize#len.); |
| 191 | kore_pool_putcxx . . $ kore_pool_put(d9a47b307a51c9d5).(&blockscxx . . $ blocks.[idx].poolcxx . . $ memblock#pool., addr); |
| 192 | } else { |
| 193 | freecxx . . $ free(5d22bdacc48458e8).(addr); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | char * |
| 198 | kore_strdupcxx . . $ kore_strdup(a8b8e32347182212).(const char *str) |
| 199 | { |
| 200 | size_t len; |
| 201 | char *nstr; |
| 202 | |
| 203 | len = strlencxx . . $ strlen(751346f8406fd082).(str) + 1; |
| 204 | nstr = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(len); |
| 205 | (void)kore_strlcpycxx . . $ kore_strlcpy(55abe9014c6d0e39).(nstr, str, len); |
| 206 | |
| 207 | return (nstr); |
| 208 | } |
| 209 | |
| 210 | void * |
| 211 | kore_malloc_taggedcxx . . $ kore_malloc_tagged(51708a638a6634bc).(size_t len, u_int32_tcxx . . $ u_int32_t# tag) |
| 212 | { |
| 213 | void *ptr; |
| 214 | |
| 215 | ptr = kore_malloccxx . . $ kore_malloc(bfcfa4d6b7f7ef64).(len); |
| 216 | kore_mem_tagcxx . . $ kore_mem_tag(67abb4dd43ac34c5).(ptr, tag); |
| 217 | |
| 218 | return (ptr); |
| 219 | } |
| 220 | |
| 221 | void |
| 222 | kore_mem_tagcxx . . $ kore_mem_tag(67abb4dd43ac34c5).(void *ptr, u_int32_tcxx . . $ u_int32_t# id) |
| 223 | { |
| 224 | struct tagcxx . . $ tag# *tag; |
| 225 | struct meminfocxx . . $ meminfo# *mem; |
| 226 | |
| 227 | if (kore_mem_lookupcxx . . $ kore_mem_lookup(52408c21a1e21ff7).(id) != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 228 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_mem_tag: tag %u taken", id); |
| 229 | |
| 230 | mem = meminfocxx . . $ meminfo(30523716a785bca6).(ptr); |
| 231 | mem->flagscxx . . $ meminfo#flags. |= KORE_MEM_TAGGEDcxx . . $ `src/mem.c:32:9`!; |
| 232 | |
| 233 | tag = kore_pool_getcxx . . $ kore_pool_get(f5c578167d321b17).(&tag_poolcxx . . $ tag_pool.); |
| 234 | tag->idcxx . . $ tag#id. = id; |
| 235 | tag->ptrcxx . . $ tag#ptr. = ptr; |
| 236 | |
| 237 | TAILQ_INSERT_TAILcxx . . $ `<external>/2546723208511035544/queue.h:402:9`!(&tags, tag, list); |
| 238 | } |
| 239 | |
| 240 | void |
| 241 | kore_mem_untagcxx . . $ kore_mem_untag(5d22bdacc48458e8).(void *ptr) |
| 242 | { |
| 243 | struct tagcxx . . $ tag# *tag; |
| 244 | |
| 245 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(tag, &tags, list) { |
| 246 | if (tag->ptrcxx . . $ tag#ptr. == ptr) { |
| 247 | TAILQ_REMOVEcxx . . $ `<external>/2546723208511035544/queue.h:426:9`!(&tags, tag, list); |
| 248 | kore_pool_putcxx . . $ kore_pool_put(d9a47b307a51c9d5).(&tag_poolcxx . . $ tag_pool., tag); |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void * |
| 255 | kore_mem_lookupcxx . . $ kore_mem_lookup(52408c21a1e21ff7).(u_int32_tcxx . . $ u_int32_t# id) |
| 256 | { |
| 257 | struct tagcxx . . $ tag# *tag; |
| 258 | |
| 259 | TAILQ_FOREACHcxx . . $ `<external>/2546723208511035544/queue.h:435:9`!(tag, &tags, list) { |
| 260 | if (tag->idcxx . . $ tag#id. == id) |
| 261 | return (tag->ptrcxx . . $ tag#ptr.); |
| 262 | } |
| 263 | |
| 264 | return (NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 265 | } |
| 266 | |
| 267 | /* Best effort to try and let the compiler not optimize this call away. */ |
| 268 | void |
| 269 | kore_mem_zerocxx . . $ kore_mem_zero(5500c4293b1bbef0).(void *ptr, size_t len) |
| 270 | { |
| 271 | volatile char *p; |
| 272 | |
| 273 | p = (volatile char *)ptr; |
| 274 | |
| 275 | if (p != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) { |
| 276 | while (len-- > 0) |
| 277 | *(p)++ = 0x00; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | static size_t |
| 282 | memblock_indexcxx . . $ memblock_index(ecdaa1632f49f591).(size_t len) |
| 283 | { |
| 284 | size_t mlen, idx; |
| 285 | |
| 286 | idx = 0; |
| 287 | mlen = 8; |
| 288 | while (mlen < len) { |
| 289 | idx++; |
| 290 | mlen = mlen << 1; |
| 291 | } |
| 292 | |
| 293 | if (idx > (KORE_MEM_BLOCKScxx . . $ `src/mem.c:25:9`! - 1)) |
| 294 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("kore_malloc: idx too high"); |
| 295 | |
| 296 | return (idx); |
| 297 | } |
| 298 | |
| 299 | static inline struct memsizecxx . . $ memsize# * |
| 300 | memsizecxx . . $ memsize(48235604510b72a3).(void *ptr) |
| 301 | { |
| 302 | struct memsizecxx . . $ memsize# *ms; |
| 303 | |
| 304 | ms = (struct memsizecxx . . $ memsize# *)((u_int8_tcxx . . $ u_int8_t# *)ptr - sizeof(*ms)); |
| 305 | |
| 306 | if (ms->magiccxx . . $ memsize#magic. != KORE_MEM_MAGICcxx . . $ `src/mem.c:30:9`!) |
| 307 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: bad memsize magic (0x%zx)", __func__, ms->magiccxx . . $ memsize#magic.); |
| 308 | |
| 309 | return (ms); |
| 310 | } |
| 311 | |
| 312 | static inline struct meminfocxx . . $ meminfo# * |
| 313 | meminfocxx . . $ meminfo(30523716a785bca6).(void *ptr) |
| 314 | { |
| 315 | struct memsizecxx . . $ memsize# *ms; |
| 316 | struct meminfocxx . . $ meminfo# *info; |
| 317 | |
| 318 | ms = memsizecxx . . $ memsize(48235604510b72a3).(ptr); |
| 319 | info = (struct meminfocxx . . $ meminfo# *)((u_int8_tcxx . . $ u_int8_t# *)ptr + ms->lencxx . . $ memsize#len.); |
| 320 | |
| 321 | if (info->magiccxx . . $ meminfo#magic. != KORE_MEM_MAGICcxx . . $ `src/mem.c:30:9`!) |
| 322 | fatalcxx . . $ fatal(f6bc48f0426f26ee).("%s: bad meminfo magic (0x%x)", __func__, info->magiccxx . . $ meminfo#magic.); |
| 323 | |
| 324 | return (info); |
| 325 | } |