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