xref: /illumos-gate/usr/src/tools/ctf/common/memory.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
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 2001-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Routines for memory management
29  */
30 
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <strings.h>
36 
37 static void
38 memory_bailout(void)
39 {
40 	(void) fprintf(stderr, "Out of memory\n");
41 	exit(1);
42 }
43 
44 void *
45 xmalloc(size_t size)
46 {
47 	void *mem;
48 
49 	if ((mem = malloc(size)) == NULL)
50 		memory_bailout();
51 
52 	return (mem);
53 }
54 
55 void *
56 xcalloc(size_t size)
57 {
58 	void *mem;
59 
60 	mem = xmalloc(size);
61 	bzero(mem, size);
62 
63 	return (mem);
64 }
65 
66 char *
67 xstrdup(const char *str)
68 {
69 	char *newstr;
70 
71 	if ((newstr = strdup(str)) == NULL)
72 		memory_bailout();
73 
74 	return (newstr);
75 }
76 
77 char *
78 xstrndup(char *str, size_t len)
79 {
80 	char *newstr;
81 
82 	if ((newstr = malloc(len + 1)) == NULL)
83 		memory_bailout();
84 
85 	(void) strncpy(newstr, str, len);
86 	newstr[len] = '\0';
87 
88 	return (newstr);
89 }
90 
91 void *
92 xrealloc(void *ptr, size_t size)
93 {
94 	void *mem;
95 
96 	if ((mem = realloc(ptr, size)) == NULL)
97 		memory_bailout();
98 
99 	return (mem);
100 }
101