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