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