Path: blob/main/cddl/contrib/opensolaris/tools/ctf/common/memory.c
39563 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License, Version 1.0 only5* (the "License"). You may not use this file except in compliance6* with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or http://www.opensolaris.org/os/licensing.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*22* Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526#pragma ident "%Z%%M% %I% %E% SMI"2728/*29* Routines for memory management30*/3132#include <sys/types.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>36#include <strings.h>37#include "memory.h"3839static void40memory_bailout(void)41{42(void) fprintf(stderr, "Out of memory\n");43exit(1);44}4546int47xasprintf(char **s, const char *fmt, ...)48{49va_list ap;50int ret;5152va_start(ap, fmt);53ret = vasprintf(s, fmt, ap);54va_end(ap);55if (ret == -1)56memory_bailout();57return (ret);58}5960void *61xmalloc(size_t size)62{63void *mem;6465if ((mem = malloc(size)) == NULL)66memory_bailout();6768return (mem);69}7071void *72xcalloc(size_t size)73{74void *mem;7576mem = xmalloc(size);77bzero(mem, size);7879return (mem);80}8182char *83xstrdup(const char *str)84{85char *newstr;8687if ((newstr = strdup(str)) == NULL)88memory_bailout();8990return (newstr);91}9293char *94xstrndup(char *str, size_t len)95{96char *newstr;9798if ((newstr = malloc(len + 1)) == NULL)99memory_bailout();100101(void) strncpy(newstr, str, len);102newstr[len] = '\0';103104return (newstr);105}106107void *108xrealloc(void *ptr, size_t size)109{110void *mem;111112if ((mem = realloc(ptr, size)) == NULL)113memory_bailout();114115return (mem);116}117118119