xref: /netbsd-src/usr.bin/xlint/common/mem.c (revision 9de7a84bd227629b18ef17b21041529854809f96)
1*9de7a84bSrillig /*	$NetBSD: mem.c,v 1.25 2024/01/20 12:02:09 rillig Exp $	*/
241d48940Sthorpej 
341d48940Sthorpej /*
441d48940Sthorpej  * Copyright (c) 1994, 1995 Jochen Pohl
541d48940Sthorpej  * All Rights Reserved.
641d48940Sthorpej  *
741d48940Sthorpej  * Redistribution and use in source and binary forms, with or without
841d48940Sthorpej  * modification, are permitted provided that the following conditions
941d48940Sthorpej  * are met:
1041d48940Sthorpej  * 1. Redistributions of source code must retain the above copyright
1141d48940Sthorpej  *    notice, this list of conditions and the following disclaimer.
1241d48940Sthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1341d48940Sthorpej  *    notice, this list of conditions and the following disclaimer in the
1441d48940Sthorpej  *    documentation and/or other materials provided with the distribution.
1541d48940Sthorpej  * 3. All advertising materials mentioning features or use of this software
1641d48940Sthorpej  *    must display the following acknowledgement:
1741d48940Sthorpej  *	This product includes software developed by Jochen Pohl for
1841d48940Sthorpej  *	The NetBSD Project.
1941d48940Sthorpej  * 4. The name of the author may not be used to endorse or promote products
2041d48940Sthorpej  *    derived from this software without specific prior written permission.
2141d48940Sthorpej  *
2241d48940Sthorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2341d48940Sthorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2441d48940Sthorpej  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2541d48940Sthorpej  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2641d48940Sthorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2741d48940Sthorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2841d48940Sthorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2941d48940Sthorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
3041d48940Sthorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3141d48940Sthorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3241d48940Sthorpej  */
3341d48940Sthorpej 
34b2f78261Sjmc #if HAVE_NBTOOL_CONFIG_H
35b2f78261Sjmc #include "nbtool_config.h"
36b2f78261Sjmc #endif
37b2f78261Sjmc 
3841d48940Sthorpej #include <sys/cdefs.h>
390386644fSrillig #if defined(__RCSID)
40*9de7a84bSrillig __RCSID("$NetBSD: mem.c,v 1.25 2024/01/20 12:02:09 rillig Exp $");
4141d48940Sthorpej #endif
4241d48940Sthorpej 
43b6b0a28bSchristos #include <stdarg.h>
4441d48940Sthorpej #include <stdlib.h>
4541d48940Sthorpej #include <string.h>
4641d48940Sthorpej 
4741d48940Sthorpej #include "lint.h"
4841d48940Sthorpej 
496e889c43Srillig static void *
not_null(void * ptr)506e889c43Srillig not_null(void *ptr)
51cd66f68dSrillig {
52cd66f68dSrillig 
536e889c43Srillig 	if (ptr == NULL)
54*9de7a84bSrillig 		errx(1, "out of memory");
556e889c43Srillig 	return ptr;
56cd66f68dSrillig }
57cd66f68dSrillig 
58*9de7a84bSrillig #if IS_LINT1 || IS_LINT2
5941d48940Sthorpej void *
xmalloc(size_t s)6041d48940Sthorpej xmalloc(size_t s)
6141d48940Sthorpej {
6241d48940Sthorpej 
636e889c43Srillig 	return not_null(malloc(s));
6441d48940Sthorpej }
6541d48940Sthorpej 
6641d48940Sthorpej void *
xcalloc(size_t n,size_t s)6741d48940Sthorpej xcalloc(size_t n, size_t s)
6841d48940Sthorpej {
6941d48940Sthorpej 
706e889c43Srillig 	return not_null(calloc(n, s));
7141d48940Sthorpej }
72e6919d40Srillig #endif
7341d48940Sthorpej 
7441d48940Sthorpej void *
xrealloc(void * p,size_t s)7541d48940Sthorpej xrealloc(void *p, size_t s)
7641d48940Sthorpej {
7741d48940Sthorpej 
786e889c43Srillig 	return not_null(realloc(p, s));
7941d48940Sthorpej }
8041d48940Sthorpej 
8141d48940Sthorpej char *
xstrdup(const char * s)8241d48940Sthorpej xstrdup(const char *s)
8341d48940Sthorpej {
8441d48940Sthorpej 
856e889c43Srillig 	return not_null(strdup(s));
8641d48940Sthorpej }
8741d48940Sthorpej 
88*9de7a84bSrillig #if IS_XLINT
8986a595a3Srillig char *
xasprintf(const char * fmt,...)9086a595a3Srillig xasprintf(const char *fmt, ...)
91b6b0a28bSchristos {
9286a595a3Srillig 	char *str;
93b6b0a28bSchristos 	int e;
94b6b0a28bSchristos 	va_list ap;
95b6b0a28bSchristos 
96b6b0a28bSchristos 	va_start(ap, fmt);
9786a595a3Srillig 	e = vasprintf(&str, fmt, ap);
98b6b0a28bSchristos 	va_end(ap);
99b6b0a28bSchristos 	if (e < 0)
10044802c0bSrillig 		(void)not_null(NULL);
10186a595a3Srillig 	return str;
102b6b0a28bSchristos }
103f7d8392aSrillig #endif
104