1237578Sobrien/* $NetBSD: make_malloc.c,v 1.10 2012/06/20 17:46:28 sjg Exp $ */ 2236769Sobrien 3236769Sobrien/*- 4236769Sobrien * Copyright (c) 2009 The NetBSD Foundation, Inc. 5236769Sobrien * All rights reserved. 6236769Sobrien * 7236769Sobrien * Redistribution and use in source and binary forms, with or without 8236769Sobrien * modification, are permitted provided that the following conditions 9236769Sobrien * are met: 10236769Sobrien * 1. Redistributions of source code must retain the above copyright 11236769Sobrien * notice, this list of conditions and the following disclaimer. 12236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 13236769Sobrien * notice, this list of conditions and the following disclaimer in the 14236769Sobrien * documentation and/or other materials provided with the distribution. 15236769Sobrien * 16236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17236769Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18236769Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19236769Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20236769Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21236769Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22236769Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23236769Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24236769Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25236769Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26236769Sobrien * POSSIBILITY OF SUCH DAMAGE. 27236769Sobrien */ 28236769Sobrien 29236769Sobrien#ifdef MAKE_NATIVE 30236769Sobrien#include <sys/cdefs.h> 31237578Sobrien__RCSID("$NetBSD: make_malloc.c,v 1.10 2012/06/20 17:46:28 sjg Exp $"); 32236769Sobrien#endif 33236769Sobrien 34236769Sobrien#include <stdio.h> 35236769Sobrien#include <stdlib.h> 36236769Sobrien#include <string.h> 37236769Sobrien#include <errno.h> 38236769Sobrien 39237578Sobrien#include "make.h" 40236769Sobrien 41236769Sobrien#ifndef USE_EMALLOC 42237578Sobrienstatic void enomem(void) MAKE_ATTR_DEAD; 43237578Sobrien 44236769Sobrien/* 45236769Sobrien * enomem -- 46236769Sobrien * die when out of memory. 47236769Sobrien */ 48236769Sobrienstatic void 49236769Sobrienenomem(void) 50236769Sobrien{ 51236769Sobrien (void)fprintf(stderr, "%s: %s.\n", progname, strerror(ENOMEM)); 52236769Sobrien exit(2); 53236769Sobrien} 54236769Sobrien 55236769Sobrien/* 56236769Sobrien * bmake_malloc -- 57236769Sobrien * malloc, but die on error. 58236769Sobrien */ 59236769Sobrienvoid * 60236769Sobrienbmake_malloc(size_t len) 61236769Sobrien{ 62236769Sobrien void *p; 63236769Sobrien 64236769Sobrien if ((p = malloc(len)) == NULL) 65236769Sobrien enomem(); 66236769Sobrien return(p); 67236769Sobrien} 68236769Sobrien 69236769Sobrien/* 70236769Sobrien * bmake_strdup -- 71236769Sobrien * strdup, but die on error. 72236769Sobrien */ 73236769Sobrienchar * 74236769Sobrienbmake_strdup(const char *str) 75236769Sobrien{ 76236769Sobrien size_t len; 77236769Sobrien char *p; 78236769Sobrien 79236769Sobrien len = strlen(str) + 1; 80236769Sobrien if ((p = malloc(len)) == NULL) 81236769Sobrien enomem(); 82236769Sobrien return memcpy(p, str, len); 83236769Sobrien} 84236769Sobrien 85236769Sobrien/* 86236769Sobrien * bmake_strndup -- 87236769Sobrien * strndup, but die on error. 88236769Sobrien */ 89236769Sobrienchar * 90236769Sobrienbmake_strndup(const char *str, size_t max_len) 91236769Sobrien{ 92236769Sobrien size_t len; 93236769Sobrien char *p; 94236769Sobrien 95236769Sobrien if (str == NULL) 96236769Sobrien return NULL; 97236769Sobrien 98236769Sobrien len = strlen(str); 99236769Sobrien if (len > max_len) 100236769Sobrien len = max_len; 101236769Sobrien p = bmake_malloc(len + 1); 102236769Sobrien memcpy(p, str, len); 103236769Sobrien p[len] = '\0'; 104236769Sobrien 105236769Sobrien return(p); 106236769Sobrien} 107236769Sobrien 108236769Sobrien/* 109236769Sobrien * bmake_realloc -- 110236769Sobrien * realloc, but die on error. 111236769Sobrien */ 112236769Sobrienvoid * 113236769Sobrienbmake_realloc(void *ptr, size_t size) 114236769Sobrien{ 115236769Sobrien if ((ptr = realloc(ptr, size)) == NULL) 116236769Sobrien enomem(); 117236769Sobrien return(ptr); 118236769Sobrien} 119236769Sobrien#endif 120