H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright 1996-2018 The NASM Authors - All Rights Reserved |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 17 | * |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | /* |
| 35 | * nasmlib.c library routines for the Netwide Assembler |
| 36 | */ |
| 37 | |
| 38 | #include "compiler.h" |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 39 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 40 | #include "error.h" |
H. Peter Anvin (Intel) | eb5b3ae | 2018-12-12 14:34:34 -0800 | [diff] [blame] | 41 | #include "alloc.h" |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 42 | |
H. Peter Anvin (Intel) | eb5b3ae | 2018-12-12 14:34:34 -0800 | [diff] [blame] | 43 | no_return nasm_alloc_failed(void) |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 44 | { |
H. Peter Anvin (Intel) | 3b91f4c | 2018-12-13 13:55:25 -0800 | [diff] [blame] | 45 | /* If nasm_fatal() gets us back here, then croak hard */ |
| 46 | static bool already_here = false; |
| 47 | FILE *errfile; |
| 48 | |
| 49 | if (likely(!already_here)) { |
| 50 | already_here = true; |
H. Peter Anvin (Intel) | df2195b | 2018-12-13 16:43:43 -0800 | [diff] [blame] | 51 | nasm_fatal("out of memory!"); |
H. Peter Anvin (Intel) | 3b91f4c | 2018-12-13 13:55:25 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | errfile = error_file; |
| 55 | if (!errfile) |
| 56 | error_file = stderr; |
| 57 | |
| 58 | fprintf(error_file, "nasm: out of memory!\n"); |
| 59 | fflush(error_file); |
| 60 | fflush(NULL); |
| 61 | abort(); |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 62 | } |
| 63 | |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 64 | void *nasm_malloc(size_t size) |
| 65 | { |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 66 | return validate_ptr(malloc(size)); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void *nasm_calloc(size_t size, size_t nelem) |
| 70 | { |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 71 | return validate_ptr(calloc(size, nelem)); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void *nasm_zalloc(size_t size) |
| 75 | { |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 76 | return validate_ptr(calloc(1, size)); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void *nasm_realloc(void *q, size_t size) |
| 80 | { |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 81 | return validate_ptr(q ? realloc(q, size) : malloc(size)); |
H. Peter Anvin | 22538e2 | 2016-05-25 05:42:47 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void nasm_free(void *q) |
| 85 | { |
| 86 | if (q) |
| 87 | free(q); |
| 88 | } |
| 89 | |
| 90 | char *nasm_strdup(const char *s) |
| 91 | { |
| 92 | char *p; |
| 93 | size_t size = strlen(s) + 1; |
| 94 | |
| 95 | p = nasm_malloc(size); |
| 96 | return memcpy(p, s, size); |
| 97 | } |
| 98 | |
| 99 | char *nasm_strndup(const char *s, size_t len) |
| 100 | { |
| 101 | char *p; |
| 102 | |
| 103 | len = strnlen(s, len); |
| 104 | p = nasm_malloc(len+1); |
| 105 | p[len] = '\0'; |
| 106 | return memcpy(p, s, len); |
| 107 | } |
| 108 | |
| 109 | char *nasm_strcat(const char *one, const char *two) |
| 110 | { |
| 111 | char *rslt; |
| 112 | size_t l1 = strlen(one); |
| 113 | size_t l2 = strlen(two); |
| 114 | rslt = nasm_malloc(l1 + l2 + 1); |
| 115 | memcpy(rslt, one, l1); |
| 116 | memcpy(rslt + l1, two, l2+1); |
| 117 | return rslt; |
| 118 | } |
H. Peter Anvin | 740ec35 | 2018-05-30 11:40:42 -0700 | [diff] [blame] | 119 | |
| 120 | char *nasm_strcatn(const char *str1, ...) |
| 121 | { |
| 122 | va_list ap; |
| 123 | char *rslt; /* Output buffer */ |
| 124 | size_t s; /* Total buffer size */ |
| 125 | size_t n; /* Number of arguments */ |
| 126 | size_t *ltbl; /* Table of lengths */ |
| 127 | size_t l, *lp; /* Length for current argument */ |
| 128 | const char *p; /* Currently examined argument */ |
| 129 | char *q; /* Output pointer */ |
| 130 | |
| 131 | n = 0; /* No strings encountered yet */ |
| 132 | p = str1; |
| 133 | va_start(ap, str1); |
| 134 | while (p) { |
| 135 | n++; |
| 136 | p = va_arg(ap, const char *); |
| 137 | } |
| 138 | va_end(ap); |
| 139 | |
| 140 | ltbl = nasm_malloc(n * sizeof(size_t)); |
| 141 | |
| 142 | s = 1; /* Space for final NULL */ |
| 143 | p = str1; |
| 144 | lp = ltbl; |
| 145 | va_start(ap, str1); |
| 146 | while (p) { |
| 147 | *lp++ = l = strlen(p); |
| 148 | s += l; |
| 149 | p = va_arg(ap, const char *); |
| 150 | } |
| 151 | va_end(ap); |
| 152 | |
| 153 | q = rslt = nasm_malloc(s); |
| 154 | |
| 155 | p = str1; |
| 156 | lp = ltbl; |
| 157 | va_start(ap, str1); |
| 158 | while (p) { |
| 159 | l = *lp++; |
| 160 | memcpy(q, p, l); |
| 161 | q += l; |
| 162 | p = va_arg(ap, const char *); |
| 163 | } |
| 164 | va_end(ap); |
| 165 | *q = '\0'; |
| 166 | |
| 167 | nasm_free(ltbl); |
| 168 | |
| 169 | return rslt; |
| 170 | } |