blob: 4e0ff9fe0161bc473beb2a43a0fac81c3be3a2ba [file] [log] [blame]
H. Peter Anvin22538e22016-05-25 05:42:47 -07001/* ----------------------------------------------------------------------- *
H. Peter Anvin740ec352018-05-30 11:40:42 -07002 *
3 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
H. Peter Anvin22538e22016-05-25 05:42:47 -07004 * 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 Anvin740ec352018-05-30 11:40:42 -070017 *
H. Peter Anvin22538e22016-05-25 05:42:47 -070018 * 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 Anvin22538e22016-05-25 05:42:47 -070039#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080040#include "error.h"
H. Peter Anvin (Intel)eb5b3ae2018-12-12 14:34:34 -080041#include "alloc.h"
H. Peter Anvin22538e22016-05-25 05:42:47 -070042
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070043size_t _nasm_last_string_size;
44
H. Peter Anvin (Intel)eb5b3ae2018-12-12 14:34:34 -080045no_return nasm_alloc_failed(void)
H. Peter Anvin740ec352018-05-30 11:40:42 -070046{
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -080047 /* If nasm_fatal() gets us back here, then croak hard */
48 static bool already_here = false;
49 FILE *errfile;
50
51 if (likely(!already_here)) {
52 already_here = true;
H. Peter Anvin (Intel)df2195b2018-12-13 16:43:43 -080053 nasm_fatal("out of memory!");
H. Peter Anvin (Intel)3b91f4c2018-12-13 13:55:25 -080054 }
55
56 errfile = error_file;
57 if (!errfile)
58 error_file = stderr;
59
60 fprintf(error_file, "nasm: out of memory!\n");
61 fflush(error_file);
62 fflush(NULL);
63 abort();
H. Peter Anvin740ec352018-05-30 11:40:42 -070064}
65
H. Peter Anvin22538e22016-05-25 05:42:47 -070066void *nasm_malloc(size_t size)
67{
H. Peter Anvin740ec352018-05-30 11:40:42 -070068 return validate_ptr(malloc(size));
H. Peter Anvin22538e22016-05-25 05:42:47 -070069}
70
71void *nasm_calloc(size_t size, size_t nelem)
72{
H. Peter Anvin740ec352018-05-30 11:40:42 -070073 return validate_ptr(calloc(size, nelem));
H. Peter Anvin22538e22016-05-25 05:42:47 -070074}
75
76void *nasm_zalloc(size_t size)
77{
H. Peter Anvin740ec352018-05-30 11:40:42 -070078 return validate_ptr(calloc(1, size));
H. Peter Anvin22538e22016-05-25 05:42:47 -070079}
80
81void *nasm_realloc(void *q, size_t size)
82{
H. Peter Anvin740ec352018-05-30 11:40:42 -070083 return validate_ptr(q ? realloc(q, size) : malloc(size));
H. Peter Anvin22538e22016-05-25 05:42:47 -070084}
85
86void nasm_free(void *q)
87{
88 if (q)
89 free(q);
90}
91
92char *nasm_strdup(const char *s)
93{
94 char *p;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070095 const size_t size = strlen(s) + 1;
H. Peter Anvin22538e22016-05-25 05:42:47 -070096
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -070097 _nasm_last_string_size = size;
H. Peter Anvin22538e22016-05-25 05:42:47 -070098 p = nasm_malloc(size);
99 return memcpy(p, s, size);
100}
101
102char *nasm_strndup(const char *s, size_t len)
103{
104 char *p;
105
106 len = strnlen(s, len);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700107 _nasm_last_string_size = len + 1;
H. Peter Anvin22538e22016-05-25 05:42:47 -0700108 p = nasm_malloc(len+1);
109 p[len] = '\0';
110 return memcpy(p, s, len);
111}
112
113char *nasm_strcat(const char *one, const char *two)
114{
115 char *rslt;
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700116 const size_t l1 = strlen(one);
117 const size_t s2 = strlen(two) + 1;
118
119 _nasm_last_string_size = l1 + s2;
120 rslt = nasm_malloc(l1 + s2);
H. Peter Anvin22538e22016-05-25 05:42:47 -0700121 memcpy(rslt, one, l1);
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700122 memcpy(rslt + l1, two, s2);
H. Peter Anvin22538e22016-05-25 05:42:47 -0700123 return rslt;
124}
H. Peter Anvin740ec352018-05-30 11:40:42 -0700125
126char *nasm_strcatn(const char *str1, ...)
127{
128 va_list ap;
129 char *rslt; /* Output buffer */
130 size_t s; /* Total buffer size */
131 size_t n; /* Number of arguments */
132 size_t *ltbl; /* Table of lengths */
133 size_t l, *lp; /* Length for current argument */
134 const char *p; /* Currently examined argument */
135 char *q; /* Output pointer */
136
137 n = 0; /* No strings encountered yet */
138 p = str1;
139 va_start(ap, str1);
140 while (p) {
141 n++;
142 p = va_arg(ap, const char *);
143 }
144 va_end(ap);
145
146 ltbl = nasm_malloc(n * sizeof(size_t));
147
148 s = 1; /* Space for final NULL */
149 p = str1;
150 lp = ltbl;
151 va_start(ap, str1);
152 while (p) {
153 *lp++ = l = strlen(p);
154 s += l;
155 p = va_arg(ap, const char *);
156 }
157 va_end(ap);
158
H. Peter Anvin (Intel)1c21a532019-08-09 02:34:21 -0700159 _nasm_last_string_size = s;
160
H. Peter Anvin740ec352018-05-30 11:40:42 -0700161 q = rslt = nasm_malloc(s);
162
163 p = str1;
164 lp = ltbl;
165 va_start(ap, str1);
166 while (p) {
167 l = *lp++;
168 memcpy(q, p, l);
169 q += l;
170 p = va_arg(ap, const char *);
171 }
172 va_end(ap);
173 *q = '\0';
174
175 nasm_free(ltbl);
176
177 return rslt;
178}