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 #if HAVE_NBTOOL_CONFIG_H 34 # include "nbtool_config.h" 35 #endif 36 37 #include <sys/types.h> 38 #include <stdio.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <strings.h> 42 #include "memory.h" 43 44 static void __dead 45 memory_bailout(void) 46 { 47 (void) fprintf(stderr, "Out of memory\n"); 48 exit(1); 49 } 50 51 void * 52 xmalloc(size_t size) 53 { 54 void *mem; 55 56 if ((mem = malloc(size)) == NULL) 57 memory_bailout(); 58 59 return (mem); 60 } 61 62 void * 63 xcalloc(size_t size) 64 { 65 void *mem; 66 67 mem = xmalloc(size); 68 bzero(mem, size); 69 70 return (mem); 71 } 72 73 char * 74 xstrdup(const char *str) 75 { 76 char *newstr; 77 78 if ((newstr = strdup(str)) == NULL) 79 memory_bailout(); 80 81 return (newstr); 82 } 83 84 char * 85 xstrndup(char *str, size_t len) 86 { 87 char *newstr; 88 89 if ((newstr = malloc(len + 1)) == NULL) 90 memory_bailout(); 91 92 (void) strncpy(newstr, str, len); 93 newstr[len] = '\0'; 94 95 return (newstr); 96 } 97 98 void * 99 xrealloc(void *ptr, size_t size) 100 { 101 void *mem; 102 103 if ((mem = realloc(ptr, size)) == NULL) 104 memory_bailout(); 105 106 return (mem); 107 } 108