xref: /illumos-gate/usr/src/cmd/backup/lib/memutils.c (revision fe4627ef755b7c263f91a0e6f07cdca5d7083501)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <libintl.h>
32 #include <string.h>
33 #include "memutils.h"
34 
35 extern void msg(const char *, ...);
36 extern void dumpabort(void);
37 
38 void *
39 xmalloc(bytes)
40 	size_t bytes;
41 {
42 	void *cp;
43 
44 	cp = malloc(bytes);
45 	if (cp == NULL) {
46 		int saverr = errno;
47 		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
48 		dumpabort();
49 	}
50 	return (cp);
51 }
52 
53 void *
54 xcalloc(nelem, size)
55 	size_t nelem;
56 	size_t size;
57 {
58 	void *cp;
59 
60 	cp = calloc(nelem, size);
61 	if (cp == NULL) {
62 		int saverr = errno;
63 		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
64 		dumpabort();
65 	}
66 	return (cp);
67 }
68 
69 void *
70 xrealloc(allocated, newsize)
71 	void *allocated;
72 	size_t newsize;
73 {
74 	void *cp;
75 
76 	/* LINTED realloc knows what to do with a NULL pointer */
77 	cp = realloc(allocated, newsize);
78 	if (cp == NULL) {
79 		int saverr = errno;
80 		msg(gettext("Cannot allocate memory: %s\n"), strerror(saverr));
81 		dumpabort();
82 	}
83 	return (cp);
84 }
85