xref: /illumos-gate/usr/src/cmd/backup/lib/memutils.c (revision 59d65d3175825093531e82f44269d948ed510a00)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1998 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <libintl.h>
30 #include <string.h>
31 #include "memutils.h"
32 
33 extern void msg(const char *, ...);
34 extern void dumpabort(void);
35 
36 void *
37 xmalloc(bytes)
38 	size_t bytes;
39 {
40 	void *cp;
41 
42 	cp = malloc(bytes);
43 	if (cp == NULL) {
44 		int saverr = errno;
45 		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
46 		dumpabort();
47 	}
48 	return (cp);
49 }
50 
51 void *
52 xcalloc(nelem, size)
53 	size_t nelem;
54 	size_t size;
55 {
56 	void *cp;
57 
58 	cp = calloc(nelem, size);
59 	if (cp == NULL) {
60 		int saverr = errno;
61 		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
62 		dumpabort();
63 	}
64 	return (cp);
65 }
66 
67 void *
68 xrealloc(allocated, newsize)
69 	void *allocated;
70 	size_t newsize;
71 {
72 	void *cp;
73 
74 	/* LINTED realloc knows what to do with a NULL pointer */
75 	cp = realloc(allocated, newsize);
76 	if (cp == NULL) {
77 		int saverr = errno;
78 		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
79 		dumpabort();
80 	}
81 	return (cp);
82 }
83