aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin (Intel) <hpa@zytor.com>2018-12-12 14:34:34 -0800
committerH. Peter Anvin (Intel) <hpa@zytor.com>2018-12-12 14:34:34 -0800
commiteb5b3ae0d3383cef2651ae642ad297bc1dc6f228 (patch)
treec16780b9a0ef1bf47009c452733dba699cd94c38
parent64471097ca7598e8238f9e5ed25b3afaa9b10f2c (diff)
downloadnasm-eb5b3ae0d3383cef2651ae642ad297bc1dc6f228.tar.gz
nasm-eb5b3ae0d3383cef2651ae642ad297bc1dc6f228.tar.xz
nasm-eb5b3ae0d3383cef2651ae642ad297bc1dc6f228.zip
nasmlib: Add nasm_(v)asprintf()
Add a version of (v)asprintf(), which allocates a string on the heap. Unlike the standard version of (v)asprintf(), we return the pointer; if one wants the length of the string then one can simply use the %n pattern. Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
-rw-r--r--Makefile.in2
-rw-r--r--Mkfiles/msvc.mak6
-rw-r--r--Mkfiles/openwcom.mak6
-rw-r--r--configure.ac2
-rw-r--r--include/compiler.h21
-rw-r--r--include/nasmlib.h18
-rw-r--r--nasmlib/alloc.c (renamed from nasmlib/malloc.c)13
-rw-r--r--nasmlib/alloc.h48
8 files changed, 92 insertions, 24 deletions
diff --git a/Makefile.in b/Makefile.in
index 82939be3..8e55e83e 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -96,7 +96,7 @@ LIBOBJ = stdlib/snprintf.$(O) stdlib/vsnprintf.$(O) stdlib/strlcpy.$(O) \
stdlib/strnlen.$(O) stdlib/strrchrnul.$(O) \
\
nasmlib/ver.$(O) \
- nasmlib/crc64.$(O) nasmlib/malloc.$(O) \
+ nasmlib/crc64.$(O) nasmlib/alloc.$(O) nasmlib/asprintf.$(O) \
nasmlib/md5c.$(O) nasmlib/string.$(O) nasmlib/nctype.$(O) \
nasmlib/file.$(O) nasmlib/mmap.$(O) nasmlib/ilog2.$(O) \
nasmlib/realpath.$(O) nasmlib/path.$(O) \
diff --git a/Mkfiles/msvc.mak b/Mkfiles/msvc.mak
index 7ea3b55a..89e392d9 100644
--- a/Mkfiles/msvc.mak
+++ b/Mkfiles/msvc.mak
@@ -68,11 +68,11 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) \
stdlib\strnlen.$(O) stdlib\strrchrnul.$(O) \
\
nasmlib\ver.$(O) \
- nasmlib\crc64.$(O) nasmlib\malloc.$(O) \
+ nasmlib\crc64.$(O) nasmlib\alloc.$(O) nasmlib\asprintf.$(O) \
nasmlib\md5c.$(O) nasmlib\string.$(O) nasmlib\nctype.$(O) \
nasmlib\file.$(O) nasmlib\mmap.$(O) nasmlib\ilog2.$(O) \
nasmlib\realpath.$(O) nasmlib\path.$(O) \
- nasmlib\filename.$(O) nasmlib\srcfile.$(O) \
+ nasmlib\filename.$(O) \
nasmlib\zerobuf.$(O) nasmlib\readnum.$(O) nasmlib\bsi.$(O) \
nasmlib\rbtree.$(O) nasmlib\hashtbl.$(O) \
nasmlib\raa.$(O) nasmlib\saa.$(O) \
@@ -97,7 +97,7 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) \
asm\segalloc.$(O) \
asm\preproc-nop.$(O) \
asm\rdstrnum.$(O) \
- \
+ asm\srcfile.$(O) \
macros\macros.$(O) \
\
output\outform.$(O) output\outlib.$(O) output\legacy.$(O) \
diff --git a/Mkfiles/openwcom.mak b/Mkfiles/openwcom.mak
index 2b0031af..1c407dea 100644
--- a/Mkfiles/openwcom.mak
+++ b/Mkfiles/openwcom.mak
@@ -57,11 +57,11 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) &
stdlib\strnlen.$(O) stdlib\strrchrnul.$(O) &
&
nasmlib\ver.$(O) &
- nasmlib\crc64.$(O) nasmlib\malloc.$(O) &
+ nasmlib\crc64.$(O) nasmlib\alloc.$(O) nasmlib\asprintf.$(O) &
nasmlib\md5c.$(O) nasmlib\string.$(O) nasmlib\nctype.$(O) &
nasmlib\file.$(O) nasmlib\mmap.$(O) nasmlib\ilog2.$(O) &
nasmlib\realpath.$(O) nasmlib\path.$(O) &
- nasmlib\filename.$(O) nasmlib\srcfile.$(O) &
+ nasmlib\filename.$(O) &
nasmlib\zerobuf.$(O) nasmlib\readnum.$(O) nasmlib\bsi.$(O) &
nasmlib\rbtree.$(O) nasmlib\hashtbl.$(O) &
nasmlib\raa.$(O) nasmlib\saa.$(O) &
@@ -86,7 +86,7 @@ LIBOBJ = stdlib\snprintf.$(O) stdlib\vsnprintf.$(O) stdlib\strlcpy.$(O) &
asm\segalloc.$(O) &
asm\preproc-nop.$(O) &
asm\rdstrnum.$(O) &
- &
+ asm\srcfile.$(O) &
macros\macros.$(O) &
&
output\outform.$(O) output\outlib.$(O) output\legacy.$(O) &
diff --git a/configure.ac b/configure.ac
index 2b2858ec..878b3269 100644
--- a/configure.ac
+++ b/configure.ac
@@ -118,6 +118,8 @@ AC_CHECK_FUNCS(strrchrnul)
AC_CHECK_FUNCS(iscntrl)
AC_CHECK_FUNCS(isascii)
+AC_CHECK_FUNCS(vasprintf)
+
AC_CHECK_FUNCS(getuid)
AC_CHECK_FUNCS(getgid)
diff --git a/include/compiler.h b/include/compiler.h
index 4178c98e..4ce60ccc 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -1,6 +1,6 @@
/* ----------------------------------------------------------------------- *
*
- * Copyright 2007-2017 The NASM Authors - All Rights Reserved
+ * Copyright 2007-2018 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
@@ -81,6 +81,7 @@
#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>
+#include <stdlib.h>
#include <limits.h>
#ifdef HAVE_SYS_TYPES_H
@@ -350,6 +351,24 @@ size_t strnlen(const char *s, size_t maxlen);
# define is_constant(x) false
#endif
+/*
+ * The autoconf documentation states:
+ *
+ * `va_copy'
+ * The C99 standard provides `va_copy' for copying `va_list'
+ * variables. It may be available in older environments too, though
+ * possibly as `__va_copy' (e.g., `gcc' in strict pre-C99 mode).
+ * These can be tested with `#ifdef'. A fallback to `memcpy (&dst,
+ * &src, sizeof (va_list))' gives maximum portability.
+ */
+#ifndef va_copy
+# ifdef __va_copy
+# define va_copy(dst,src) __va_copy(dst,src)
+# else
+# define va_copy(dst,src) memcpy(&(dst),&(src),sizeof(va_list))
+# endif
+#endif
+
/* Watcom doesn't handle switch statements with 64-bit types, hack around it */
#ifdef __WATCOMC__
# define BOGUS_CASE 0x76543210
diff --git a/include/nasmlib.h b/include/nasmlib.h
index db624987..7d07a216 100644
--- a/include/nasmlib.h
+++ b/include/nasmlib.h
@@ -48,11 +48,11 @@
#endif
/*
- * Wrappers around malloc, realloc and free. nasm_malloc will
- * fatal-error and die rather than return NULL; nasm_realloc will
- * do likewise, and will also guarantee to work right on being
- * passed a NULL pointer; nasm_free will do nothing if it is passed
- * a NULL pointer.
+ * Wrappers around malloc, realloc, free and a few more. nasm_malloc
+ * will fatal-error and die rather than return NULL; nasm_realloc will
+ * do likewise, and will also guarantee to work right on being passed
+ * a NULL pointer; nasm_free will do nothing if it is passed a NULL
+ * pointer.
*/
void * safe_malloc(1) nasm_malloc(size_t);
void * safe_malloc(1) nasm_zalloc(size_t);
@@ -64,6 +64,14 @@ char * safe_alloc nasm_strndup(const char *, size_t);
char * safe_alloc nasm_strcat(const char *one, const char *two);
char * safe_alloc end_with_null nasm_strcatn(const char *one, ...);
+/*
+ * Variants on asprintf(). Note that unlike the standard version, we
+ * return the pointer rather than the length; if you want the length
+ * use %n.
+ */
+char * safe_alloc nasm_asprintf(const char *fmt, ...);
+char * safe_alloc nasm_vasprintf(const char *fmt, va_list ap);
+
/* Assert the argument is a pointer without evaluating it */
#define nasm_assert_pointer(p) ((void)sizeof(*(p)))
diff --git a/nasmlib/malloc.c b/nasmlib/alloc.c
index 1e643185..2f3f9519 100644
--- a/nasmlib/malloc.c
+++ b/nasmlib/alloc.c
@@ -36,24 +36,15 @@
*/
#include "compiler.h"
-
-#include <stdlib.h>
-
#include "nasmlib.h"
#include "error.h"
+#include "alloc.h"
-static no_return nasm_alloc_failed(void)
+no_return nasm_alloc_failed(void)
{
nasm_fatal("out of memory");
}
-static inline void *validate_ptr(void *p)
-{
- if (unlikely(!p))
- nasm_alloc_failed();
- return p;
-}
-
void *nasm_malloc(size_t size)
{
return validate_ptr(malloc(size));
diff --git a/nasmlib/alloc.h b/nasmlib/alloc.h
new file mode 100644
index 00000000..c599d213
--- /dev/null
+++ b/nasmlib/alloc.h
@@ -0,0 +1,48 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2018 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+#ifndef NASMLIB_ALLOC_H
+#define NASMLIB_ALLOC_H
+
+#include "compiler.h"
+
+no_return nasm_alloc_failed(void);
+
+static inline void *validate_ptr(void *p)
+{
+ if (unlikely(!p))
+ nasm_alloc_failed();
+ return p;
+}
+
+#endif /* NASMLIB_ALLOC_H */