diff options
author | H. Peter Anvin (Intel) <hpa@zytor.com> | 2018-12-12 14:34:34 -0800 |
---|---|---|
committer | H. Peter Anvin (Intel) <hpa@zytor.com> | 2018-12-12 14:34:34 -0800 |
commit | eb5b3ae0d3383cef2651ae642ad297bc1dc6f228 (patch) | |
tree | c16780b9a0ef1bf47009c452733dba699cd94c38 /nasmlib | |
parent | 64471097ca7598e8238f9e5ed25b3afaa9b10f2c (diff) | |
download | nasm-eb5b3ae0d3383cef2651ae642ad297bc1dc6f228.tar.gz |
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>
Diffstat (limited to 'nasmlib')
-rw-r--r-- | nasmlib/alloc.c (renamed from nasmlib/malloc.c) | 13 | ||||
-rw-r--r-- | nasmlib/alloc.h | 48 |
2 files changed, 50 insertions, 11 deletions
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 */ |