8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / tools / ctf / common / memory.c
blob390ff128e4332257024d61c797637572812544c8
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * Routines for memory management
33 #include <sys/types.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <strings.h>
39 static void
40 memory_bailout(void)
42 (void) fprintf(stderr, "Out of memory\n");
43 exit(1);
46 void *
47 xmalloc(size_t size)
49 void *mem;
51 if ((mem = malloc(size)) == NULL)
52 memory_bailout();
54 return (mem);
57 void *
58 xcalloc(size_t size)
60 void *mem;
62 mem = xmalloc(size);
63 bzero(mem, size);
65 return (mem);
68 char *
69 xstrdup(const char *str)
71 char *newstr;
73 if ((newstr = strdup(str)) == NULL)
74 memory_bailout();
76 return (newstr);
79 char *
80 xstrndup(char *str, size_t len)
82 char *newstr;
84 if ((newstr = malloc(len + 1)) == NULL)
85 memory_bailout();
87 (void) strncpy(newstr, str, len);
88 newstr[len] = '\0';
90 return (newstr);
93 void *
94 xrealloc(void *ptr, size_t size)
96 void *mem;
98 if ((mem = realloc(ptr, size)) == NULL)
99 memory_bailout();
101 return (mem);