| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Common code for memory handling (in a do-or-die kind of way). | ||
| 3 | * Copyright © 2024-2026, albinoloverats ~ Software Development | ||
| 4 | * email: webmaster@albinoloverats.net | ||
| 5 | * | ||
| 6 | * This program is free software: you can redistribute it and/or modify | ||
| 7 | * it under the terms of the GNU General Public License as published by | ||
| 8 | * the Free Software Foundation, either version 3 of the License, or | ||
| 9 | * (at your option) any later version. | ||
| 10 | * | ||
| 11 | * This program is distributed in the hope that it will be useful, | ||
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 14 | * GNU General Public License for more details. | ||
| 15 | * | ||
| 16 | * You should have received a copy of the GNU General Public License | ||
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 18 | * | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include <errno.h> | ||
| 22 | |||
| 23 | #include <stdio.h> | ||
| 24 | #include <stdlib.h> | ||
| 25 | #include <stdarg.h> | ||
| 26 | |||
| 27 | #include <string.h> | ||
| 28 | |||
| 29 | #include "mem.h" | ||
| 30 | #include "error.h" | ||
| 31 | #include "non-gnu.h" | ||
| 32 | |||
| 33 | #define VSN_SIZE (1024*1024) | ||
| 34 | |||
| 35 | 427 | extern void *mem_mod(const char *file, int line, const char *func, size_t size) | |
| 36 | { | ||
| 37 | 427 | void *m = malloc(size); | |
| 38 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 426 times.
|
427 | if (!m) |
| 39 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, size); | |
| 40 | 426 | return m; | |
| 41 | } | ||
| 42 | |||
| 43 | 1245 | extern void *mem_cod(const char *file, int line, const char *func, size_t count, size_t size) | |
| 44 | { | ||
| 45 | 1245 | void *m = calloc(count, size); | |
| 46 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1244 times.
|
1245 | if (!m) |
| 47 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, size); | |
| 48 | 1244 | return m; | |
| 49 | } | ||
| 50 | |||
| 51 | 213 | extern void *mem_rod(const char *file, int line, const char *func, void *ptr, size_t newsize) | |
| 52 | { | ||
| 53 | 213 | void *m = realloc(ptr, newsize); | |
| 54 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 212 times.
|
213 | if (!m) |
| 55 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, newsize); | |
| 56 | 212 | return m; | |
| 57 | } | ||
| 58 | |||
| 59 | 442 | extern char *mem_sod(const char *file, int line, const char *func, const char *str) | |
| 60 | { | ||
| 61 | 442 | size_t len = strlen(str); | |
| 62 | 442 | char *new = strdup(str); | |
| 63 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 441 times.
|
442 | if (!new) |
| 64 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, len); | |
| 65 | 441 | return new; | |
| 66 | } | ||
| 67 | |||
| 68 | 541 | extern char *mem_nod(const char *file, int line, const char *func, const char *str, size_t len) | |
| 69 | { | ||
| 70 | 541 | size_t olen = strlen(str); | |
| 71 | 541 | char *new = strndup(str, len); | |
| 72 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 540 times.
|
541 | if (!new) |
| 73 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, olen < len ? len : olen); | |
| 74 | 540 | return new; | |
| 75 | } | ||
| 76 | |||
| 77 | 244 | extern int mem_aod(const char *file, int line, const char *func, char **ptr, const char *template, ...) | |
| 78 | { | ||
| 79 | 2 | int r = 0; | |
| 80 | 244 | size_t l = strlen(template); | |
| 81 | va_list ap; | ||
| 82 | 244 | va_start(ap, template); | |
| 83 | #ifndef _WIN32 | ||
| 84 | 244 | r = vasprintf(ptr, template, ap); | |
| 85 | #else | ||
| 86 | if (!(*ptr)) | ||
| 87 | *ptr = mem_cod(file, line, func, l, VSN_SIZE); | ||
| 88 | else | ||
| 89 | *ptr = mem_rod(file, line, func, *ptr, l * VSN_SIZE); | ||
| 90 | r = vsnprintf(*ptr, (l * VSN_SIZE) - 1, template, ap); | ||
| 91 | #endif | ||
| 92 | 244 | va_end(ap); | |
| 93 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 243 times.
|
244 | if (r < 0) |
| 94 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, l); | |
| 95 | 243 | return r; | |
| 96 | } | ||
| 97 | |||
| 98 | 277 | extern char *mem_fod(const char *file, int line, const char *func, const char *template, ...) | |
| 99 | { | ||
| 100 | 110 | int r = 0; | |
| 101 | 277 | size_t l = strlen(template); | |
| 102 | va_list ap; | ||
| 103 | 277 | va_start(ap, template); | |
| 104 | |||
| 105 | 277 | char *ptr = NULL; | |
| 106 | #ifndef _WIN32 | ||
| 107 | 277 | r = vasprintf(&ptr, template, ap); | |
| 108 | #else | ||
| 109 | ptr = mem_cod(file, line, func, l, VSN_SIZE); | ||
| 110 | l *= VSN_SIZE; | ||
| 111 | r = vsnprintf(ptr, l - 1, template, ap); | ||
| 112 | #endif | ||
| 113 | 277 | va_end(ap); | |
| 114 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 276 times.
|
277 | if (r < 0) |
| 115 | 1 | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, l); | |
| 116 | 276 | return ptr; | |
| 117 | } | ||
| 118 | |||
| 119 | #ifdef USE_GCRYPT | ||
| 120 | |||
| 121 | #include <gcrypt.h> | ||
| 122 | |||
| 123 | 1515 | extern void *mem_sec_mod(const char *file, int line, const char *func, size_t size) | |
| 124 | { | ||
| 125 | 1515 | void *m = gcry_malloc_secure(size); | |
| 126 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1515 times.
|
1515 | if (!m) |
| 127 | ✗ | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, size); | |
| 128 | 1515 | return m; | |
| 129 | } | ||
| 130 | |||
| 131 | 1271 | extern void *mem_sec_cod(const char *file, int line, const char *func, size_t count, size_t size) | |
| 132 | { | ||
| 133 | 1271 | void *m = gcry_calloc_secure(count, size); | |
| 134 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1271 times.
|
1271 | if (!m) |
| 135 | ✗ | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, size); | |
| 136 | 1271 | return m; | |
| 137 | } | ||
| 138 | |||
| 139 | ✗ | extern void *mem_sec_rod(const char *file, int line, const char *func, void *ptr, size_t newsize) | |
| 140 | { | ||
| 141 | ✗ | void *m = gcry_realloc(ptr, newsize); | |
| 142 | ✗ | if (!m) | |
| 143 | ✗ | die(_("Out of memory @ %s:%d:%s [%zu]"), file, line, func, newsize); | |
| 144 | ✗ | return m; | |
| 145 | } | ||
| 146 | |||
| 147 | #endif /* USE_GCRYPT */ | ||
| 148 |