ealloc.c revision 78344
178344Sobrien/* $NetBSD: ealloc.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $ */ 278344Sobrien 378344Sobrien/* 478344Sobrien * Copyright (c) 1988, 1989, 1990, 1993 578344Sobrien * The Regents of the University of California. All rights reserved. 678344Sobrien * Copyright (c) 1989 by Berkeley Softworks 778344Sobrien * All rights reserved. 878344Sobrien * 978344Sobrien * This code is derived from software contributed to Berkeley by 1078344Sobrien * Adam de Boor. 1178344Sobrien * 1278344Sobrien * Redistribution and use in source and binary forms, with or without 1378344Sobrien * modification, are permitted provided that the following conditions 1478344Sobrien * are met: 1578344Sobrien * 1. Redistributions of source code must retain the above copyright 1678344Sobrien * notice, this list of conditions and the following disclaimer. 1778344Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1878344Sobrien * notice, this list of conditions and the following disclaimer in the 1978344Sobrien * documentation and/or other materials provided with the distribution. 2078344Sobrien * 3. All advertising materials mentioning features or use of this software 2178344Sobrien * must display the following acknowledgement: 2278344Sobrien * This product includes software developed by the University of 2378344Sobrien * California, Berkeley and its contributors. 2478344Sobrien * 4. Neither the name of the University nor the names of its contributors 2578344Sobrien * may be used to endorse or promote products derived from this software 2678344Sobrien * without specific prior written permission. 2778344Sobrien * 2878344Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2978344Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3078344Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3178344Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3278344Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3378344Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3478344Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3578344Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3678344Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3778344Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3878344Sobrien * SUCH DAMAGE. 3978344Sobrien */ 4078344Sobrien 4178344Sobrien#include <sys/cdefs.h> 4278344Sobrien#ifndef lint 4378344Sobrien__RCSID("$NetBSD: ealloc.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $"); 4478344Sobrien#endif /* not lint */ 4578344Sobrien 4678344Sobrien#include <stdio.h> 4778344Sobrien#include <stdlib.h> 4878344Sobrien#include <string.h> 4978344Sobrien#include <err.h> 5078344Sobrien 5178344Sobrien#include "ealloc.h" 5278344Sobrien 5378344Sobrienstatic void enomem __P((void)); 5478344Sobrien 5578344Sobrien/* 5678344Sobrien * enomem -- 5778344Sobrien * die when out of memory. 5878344Sobrien */ 5978344Sobrienstatic void 6078344Sobrienenomem() 6178344Sobrien{ 6278344Sobrien errx(2, "Cannot allocate memory."); 6378344Sobrien} 6478344Sobrien 6578344Sobrien/* 6678344Sobrien * emalloc -- 6778344Sobrien * malloc, but die on error. 6878344Sobrien */ 6978344Sobrienvoid * 7078344Sobrienemalloc(len) 7178344Sobrien size_t len; 7278344Sobrien{ 7378344Sobrien void *p; 7478344Sobrien 7578344Sobrien if ((p = malloc(len)) == NULL) 7678344Sobrien enomem(); 7778344Sobrien return(p); 7878344Sobrien} 7978344Sobrien 8078344Sobrien/* 8178344Sobrien * estrdup -- 8278344Sobrien * strdup, but die on error. 8378344Sobrien */ 8478344Sobrienchar * 8578344Sobrienestrdup(str) 8678344Sobrien const char *str; 8778344Sobrien{ 8878344Sobrien char *p; 8978344Sobrien 9078344Sobrien if ((p = strdup(str)) == NULL) 9178344Sobrien enomem(); 9278344Sobrien return(p); 9378344Sobrien} 9478344Sobrien 9578344Sobrien/* 9678344Sobrien * erealloc -- 9778344Sobrien * realloc, but die on error. 9878344Sobrien */ 9978344Sobrienvoid * 10078344Sobrienerealloc(ptr, size) 10178344Sobrien void *ptr; 10278344Sobrien size_t size; 10378344Sobrien{ 10478344Sobrien if ((ptr = realloc(ptr, size)) == NULL) 10578344Sobrien enomem(); 10678344Sobrien return(ptr); 10778344Sobrien} 10878344Sobrien 10978344Sobrien/* 11078344Sobrien * ecalloc -- 11178344Sobrien * calloc, but die on error. 11278344Sobrien */ 11378344Sobrienvoid * 11478344Sobrienecalloc(nmemb, size) 11578344Sobrien size_t nmemb; 11678344Sobrien size_t size; 11778344Sobrien{ 11878344Sobrien void *ptr; 11978344Sobrien 12078344Sobrien if ((ptr = calloc(nmemb, size)) == NULL) 12178344Sobrien enomem(); 12278344Sobrien return(ptr); 12378344Sobrien} 124