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