1272343Sngie/* $NetBSD: mem.c,v 1.25 2024/01/20 12:02:09 rillig Exp $ */ 2272343Sngie 3272343Sngie/* 4272343Sngie * Copyright (c) 1994, 1995 Jochen Pohl 5272343Sngie * All Rights Reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 3. All advertising materials mentioning features or use of this software 16272343Sngie * must display the following acknowledgement: 17272343Sngie * This product includes software developed by Jochen Pohl for 18272343Sngie * The NetBSD Project. 19272343Sngie * 4. The name of the author may not be used to endorse or promote products 20272343Sngie * derived from this software without specific prior written permission. 21272343Sngie * 22272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23272343Sngie * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24272343Sngie * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25272343Sngie * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27272343Sngie * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28272343Sngie * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29272343Sngie * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30272343Sngie * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31272343Sngie * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32272343Sngie */ 33272343Sngie 34272343Sngie#if HAVE_NBTOOL_CONFIG_H 35272343Sngie#include "nbtool_config.h" 36272343Sngie#endif 37272343Sngie 38272343Sngie#include <sys/cdefs.h> 39272343Sngie#if defined(__RCSID) 40272343Sngie__RCSID("$NetBSD: mem.c,v 1.25 2024/01/20 12:02:09 rillig Exp $"); 41272343Sngie#endif 42272343Sngie 43272343Sngie#include <stdarg.h> 44272343Sngie#include <stdlib.h> 45272343Sngie#include <string.h> 46272343Sngie 47272343Sngie#include "lint.h" 48276478Sngie 49276478Sngiestatic void * 50276478Sngienot_null(void *ptr) 51272343Sngie{ 52272343Sngie 53272343Sngie if (ptr == NULL) 54272343Sngie errx(1, "out of memory"); 55272343Sngie return ptr; 56272343Sngie} 57272343Sngie 58272343Sngie#if IS_LINT1 || IS_LINT2 59272343Sngievoid * 60272343Sngiexmalloc(size_t s) 61272343Sngie{ 62272343Sngie 63272343Sngie return not_null(malloc(s)); 64272343Sngie} 65272343Sngie 66272343Sngievoid * 67272343Sngiexcalloc(size_t n, size_t s) 68272343Sngie{ 69272343Sngie 70272343Sngie return not_null(calloc(n, s)); 71272343Sngie} 72272343Sngie#endif 73272343Sngie 74272343Sngievoid * 75272343Sngiexrealloc(void *p, size_t s) 76272343Sngie{ 77272343Sngie 78272343Sngie return not_null(realloc(p, s)); 79272343Sngie} 80272343Sngie 81272343Sngiechar * 82272343Sngiexstrdup(const char *s) 83272343Sngie{ 84272343Sngie 85272343Sngie return not_null(strdup(s)); 86272343Sngie} 87272343Sngie 88272343Sngie#if IS_XLINT 89272343Sngiechar * 90272343Sngiexasprintf(const char *fmt, ...) 91272343Sngie{ 92272343Sngie char *str; 93272343Sngie int e; 94272343Sngie va_list ap; 95272343Sngie 96272343Sngie va_start(ap, fmt); 97272343Sngie e = vasprintf(&str, fmt, ap); 98272343Sngie va_end(ap); 99272343Sngie if (e < 0) 100272343Sngie (void)not_null(NULL); 101272343Sngie return str; 102272343Sngie} 103272343Sngie#endif 104272343Sngie