make_malloc.c revision 256281
178189Sbrian/* $NetBSD: make_malloc.c,v 1.10 2012/06/20 17:46:28 sjg Exp $ */ 278189Sbrian 378189Sbrian/*- 478189Sbrian * Copyright (c) 2009 The NetBSD Foundation, Inc. 578189Sbrian * All rights reserved. 66059Samurai * 778189Sbrian * Redistribution and use in source and binary forms, with or without 878189Sbrian * modification, are permitted provided that the following conditions 978189Sbrian * are met: 1078189Sbrian * 1. Redistributions of source code must retain the above copyright 1178189Sbrian * notice, this list of conditions and the following disclaimer. 1278189Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1378189Sbrian * notice, this list of conditions and the following disclaimer in the 1478189Sbrian * documentation and/or other materials provided with the distribution. 156059Samurai * 1678189Sbrian * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1778189Sbrian * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1878189Sbrian * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1978189Sbrian * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2078189Sbrian * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2178189Sbrian * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2278189Sbrian * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2378189Sbrian * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2478189Sbrian * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2578189Sbrian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2678189Sbrian * POSSIBILITY OF SUCH DAMAGE. 276059Samurai */ 2850479Speter 296059Samurai#ifdef MAKE_NATIVE 306059Samurai#include <sys/cdefs.h> 3149140Sbrian__RCSID("$NetBSD: make_malloc.c,v 1.10 2012/06/20 17:46:28 sjg Exp $"); 326059Samurai#endif 336059Samurai 346059Samurai#include <stdio.h> 3549140Sbrian#include <stdlib.h> 366059Samurai#include <string.h> 3747648Sbrian#include <errno.h> 3847648Sbrian 3947648Sbrian#include "make.h" 4081634Sbrian 4181634Sbrian#ifndef USE_EMALLOC 4281634Sbrianstatic void enomem(void) MAKE_ATTR_DEAD; 4381634Sbrian 4481634Sbrian/* 4547648Sbrian * enomem -- 4649140Sbrian * die when out of memory. 4749140Sbrian */ 4849140Sbrianstatic void 4949140Sbrianenomem(void) 5049140Sbrian{ 5149140Sbrian (void)fprintf(stderr, "%s: %s.\n", progname, strerror(ENOMEM)); 5249140Sbrian exit(2); 5349140Sbrian} 5449140Sbrian 556059Samurai/* 5681634Sbrian * bmake_malloc -- 5781634Sbrian * malloc, but die on error. 5849140Sbrian */ 5949140Sbrianvoid * 6058073Sbrianbmake_malloc(size_t len) 6158073Sbrian{ 6249140Sbrian void *p; 6349140Sbrian 6449140Sbrian if ((p = malloc(len)) == NULL) 6549140Sbrian enomem(); 6681634Sbrian return(p); 6781634Sbrian} 6849140Sbrian 6949140Sbrian/* 7062977Sbrian * bmake_strdup -- 716059Samurai * strdup, but die on error. 726059Samurai */ 7349140Sbrianchar * 746059Samuraibmake_strdup(const char *str) 7549140Sbrian{ 7649140Sbrian size_t len; 7749140Sbrian char *p; 7849140Sbrian 7949140Sbrian len = strlen(str) + 1; 8036285Sbrian if ((p = malloc(len)) == NULL) 8136285Sbrian enomem(); 8236285Sbrian return memcpy(p, str, len); 8336285Sbrian} 8436285Sbrian 8536285Sbrian/* 8636285Sbrian * bmake_strndup -- 8749140Sbrian * strndup, but die on error. 887001Samurai */ 897001Samuraichar * 907001Samuraibmake_strndup(const char *str, size_t max_len) 917001Samurai{ 926059Samurai size_t len; 9336285Sbrian char *p; 9436285Sbrian 9530715Sbrian if (str == NULL) 9636285Sbrian return NULL; 9736285Sbrian 98134789Sbrian len = strlen(str); 99134789Sbrian if (len > max_len) 10081634Sbrian len = max_len; 10181634Sbrian p = bmake_malloc(len + 1); 102 memcpy(p, str, len); 103 p[len] = '\0'; 104 105 return(p); 106} 107 108/* 109 * bmake_realloc -- 110 * realloc, but die on error. 111 */ 112void * 113bmake_realloc(void *ptr, size_t size) 114{ 115 if ((ptr = realloc(ptr, size)) == NULL) 116 enomem(); 117 return(ptr); 118} 119#endif 120