1 /* $NetBSD: ealloc.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1989 by Berkeley Softworks 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to Berkeley by 10 * Adam de Boor. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 3. All advertising materials mentioning features or use of this software 21 * must display the following acknowledgement: 22 * This product includes software developed by the University of 23 * California, Berkeley and its contributors. 24 * 4. Neither the name of the University nor the names of its contributors 25 * may be used to endorse or promote products derived from this software 26 * without specific prior written permission. 27 * 28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 38 * SUCH DAMAGE. 39 */ 40 41 #include <sys/cdefs.h> 42 #ifndef lint 43 __RCSID("$NetBSD: ealloc.c,v 1.1.1.1 1999/11/19 04:30:56 mrg Exp $"); 44 #endif /* not lint */ 45 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <err.h> 50 51 #include "ealloc.h" 52 53 static void enomem __P((void)); 54 55 /* 56 * enomem -- 57 * die when out of memory. 58 */ 59 static void 60 enomem() 61 { 62 errx(2, "Cannot allocate memory."); 63 } 64 65 /* 66 * emalloc -- 67 * malloc, but die on error. 68 */ 69 void * 70 emalloc(len) 71 size_t len; 72 { 73 void *p; 74 75 if ((p = malloc(len)) == NULL) 76 enomem(); 77 return(p); 78 } 79 80 /* 81 * estrdup -- 82 * strdup, but die on error. 83 */ 84 char * 85 estrdup(str) 86 const char *str; 87 { 88 char *p; 89 90 if ((p = strdup(str)) == NULL) 91 enomem(); 92 return(p); 93 } 94 95 /* 96 * erealloc -- 97 * realloc, but die on error. 98 */ 99 void * 100 erealloc(ptr, size) 101 void *ptr; 102 size_t size; 103 { 104 if ((ptr = realloc(ptr, size)) == NULL) 105 enomem(); 106 return(ptr); 107 } 108 109 /* 110 * ecalloc -- 111 * calloc, but die on error. 112 */ 113 void * 114 ecalloc(nmemb, size) 115 size_t nmemb; 116 size_t size; 117 { 118 void *ptr; 119 120 if ((ptr = calloc(nmemb, size)) == NULL) 121 enomem(); 122 return(ptr); 123 } 124