xref: /freebsd-src/cddl/contrib/opensolaris/tools/ctf/common/memory.c (revision 1165fc9a526630487a1feb63daef65c5aee1a583)
11673e404SJohn Birrell /*
21673e404SJohn Birrell  * CDDL HEADER START
31673e404SJohn Birrell  *
41673e404SJohn Birrell  * The contents of this file are subject to the terms of the
51673e404SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
61673e404SJohn Birrell  * (the "License").  You may not use this file except in compliance
71673e404SJohn Birrell  * with the License.
81673e404SJohn Birrell  *
91673e404SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
101673e404SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
111673e404SJohn Birrell  * See the License for the specific language governing permissions
121673e404SJohn Birrell  * and limitations under the License.
131673e404SJohn Birrell  *
141673e404SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
151673e404SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
161673e404SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
171673e404SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
181673e404SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
191673e404SJohn Birrell  *
201673e404SJohn Birrell  * CDDL HEADER END
211673e404SJohn Birrell  */
221673e404SJohn Birrell /*
231673e404SJohn Birrell  * Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
241673e404SJohn Birrell  * Use is subject to license terms.
251673e404SJohn Birrell  */
261673e404SJohn Birrell 
271673e404SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
281673e404SJohn Birrell 
291673e404SJohn Birrell /*
301673e404SJohn Birrell  * Routines for memory management
311673e404SJohn Birrell  */
321673e404SJohn Birrell 
331673e404SJohn Birrell #include <sys/types.h>
341673e404SJohn Birrell #include <stdio.h>
351673e404SJohn Birrell #include <stdlib.h>
361673e404SJohn Birrell #include <string.h>
371673e404SJohn Birrell #include <strings.h>
384cc75139SJohn Birrell #include "memory.h"
391673e404SJohn Birrell 
401673e404SJohn Birrell static void
memory_bailout(void)411673e404SJohn Birrell memory_bailout(void)
421673e404SJohn Birrell {
431673e404SJohn Birrell 	(void) fprintf(stderr, "Out of memory\n");
441673e404SJohn Birrell 	exit(1);
451673e404SJohn Birrell }
461673e404SJohn Birrell 
47*1165fc9aSMark Johnston int
xasprintf(char ** s,const char * fmt,...)48*1165fc9aSMark Johnston xasprintf(char **s, const char *fmt, ...)
49*1165fc9aSMark Johnston {
50*1165fc9aSMark Johnston 	va_list ap;
51*1165fc9aSMark Johnston 	int ret;
52*1165fc9aSMark Johnston 
53*1165fc9aSMark Johnston 	va_start(ap, fmt);
54*1165fc9aSMark Johnston 	ret = vasprintf(s, fmt, ap);
55*1165fc9aSMark Johnston 	va_end(ap);
56*1165fc9aSMark Johnston 	if (ret == -1)
57*1165fc9aSMark Johnston 		memory_bailout();
58*1165fc9aSMark Johnston 	return (ret);
59*1165fc9aSMark Johnston }
60*1165fc9aSMark Johnston 
611673e404SJohn Birrell void *
xmalloc(size_t size)621673e404SJohn Birrell xmalloc(size_t size)
631673e404SJohn Birrell {
641673e404SJohn Birrell 	void *mem;
651673e404SJohn Birrell 
661673e404SJohn Birrell 	if ((mem = malloc(size)) == NULL)
671673e404SJohn Birrell 		memory_bailout();
681673e404SJohn Birrell 
691673e404SJohn Birrell 	return (mem);
701673e404SJohn Birrell }
711673e404SJohn Birrell 
721673e404SJohn Birrell void *
xcalloc(size_t size)731673e404SJohn Birrell xcalloc(size_t size)
741673e404SJohn Birrell {
751673e404SJohn Birrell 	void *mem;
761673e404SJohn Birrell 
771673e404SJohn Birrell 	mem = xmalloc(size);
781673e404SJohn Birrell 	bzero(mem, size);
791673e404SJohn Birrell 
801673e404SJohn Birrell 	return (mem);
811673e404SJohn Birrell }
821673e404SJohn Birrell 
831673e404SJohn Birrell char *
xstrdup(const char * str)841673e404SJohn Birrell xstrdup(const char *str)
851673e404SJohn Birrell {
861673e404SJohn Birrell 	char *newstr;
871673e404SJohn Birrell 
881673e404SJohn Birrell 	if ((newstr = strdup(str)) == NULL)
891673e404SJohn Birrell 		memory_bailout();
901673e404SJohn Birrell 
911673e404SJohn Birrell 	return (newstr);
921673e404SJohn Birrell }
931673e404SJohn Birrell 
941673e404SJohn Birrell char *
xstrndup(char * str,size_t len)951673e404SJohn Birrell xstrndup(char *str, size_t len)
961673e404SJohn Birrell {
971673e404SJohn Birrell 	char *newstr;
981673e404SJohn Birrell 
991673e404SJohn Birrell 	if ((newstr = malloc(len + 1)) == NULL)
1001673e404SJohn Birrell 		memory_bailout();
1011673e404SJohn Birrell 
1021673e404SJohn Birrell 	(void) strncpy(newstr, str, len);
1031673e404SJohn Birrell 	newstr[len] = '\0';
1041673e404SJohn Birrell 
1051673e404SJohn Birrell 	return (newstr);
1061673e404SJohn Birrell }
1071673e404SJohn Birrell 
1081673e404SJohn Birrell void *
xrealloc(void * ptr,size_t size)1091673e404SJohn Birrell xrealloc(void *ptr, size_t size)
1101673e404SJohn Birrell {
1111673e404SJohn Birrell 	void *mem;
1121673e404SJohn Birrell 
1131673e404SJohn Birrell 	if ((mem = realloc(ptr, size)) == NULL)
1141673e404SJohn Birrell 		memory_bailout();
1151673e404SJohn Birrell 
1161673e404SJohn Birrell 	return (mem);
1171673e404SJohn Birrell }
118