1178481Sjb/* 2178481Sjb * CDDL HEADER START 3178481Sjb * 4178481Sjb * The contents of this file are subject to the terms of the 5178481Sjb * Common Development and Distribution License, Version 1.0 only 6178481Sjb * (the "License"). You may not use this file except in compliance 7178481Sjb * with the License. 8178481Sjb * 9178481Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10178481Sjb * or http://www.opensolaris.org/os/licensing. 11178481Sjb * See the License for the specific language governing permissions 12178481Sjb * and limitations under the License. 13178481Sjb * 14178481Sjb * When distributing Covered Code, include this CDDL HEADER in each 15178481Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16178481Sjb * If applicable, add the following below this CDDL HEADER, with the 17178481Sjb * fields enclosed by brackets "[]" replaced with your own identifying 18178481Sjb * information: Portions Copyright [yyyy] [name of copyright owner] 19178481Sjb * 20178481Sjb * CDDL HEADER END 21178481Sjb */ 22178481Sjb/* 23178481Sjb * Copyright 2001-2002 Sun Microsystems, Inc. All rights reserved. 24178481Sjb * Use is subject to license terms. 25178481Sjb */ 26178481Sjb 27178481Sjb#pragma ident "%Z%%M% %I% %E% SMI" 28178481Sjb 29178481Sjb/* 30178481Sjb * Routines for memory management 31178481Sjb */ 32178481Sjb 33178481Sjb#include <sys/types.h> 34178481Sjb#include <stdio.h> 35178481Sjb#include <stdlib.h> 36178481Sjb#include <string.h> 37178481Sjb#include <strings.h> 38178546Sjb#include "memory.h" 39178481Sjb 40178481Sjbstatic void 41178481Sjbmemory_bailout(void) 42178481Sjb{ 43178481Sjb (void) fprintf(stderr, "Out of memory\n"); 44178481Sjb exit(1); 45178481Sjb} 46178481Sjb 47178481Sjbvoid * 48178481Sjbxmalloc(size_t size) 49178481Sjb{ 50178481Sjb void *mem; 51178481Sjb 52178481Sjb if ((mem = malloc(size)) == NULL) 53178481Sjb memory_bailout(); 54178481Sjb 55178481Sjb return (mem); 56178481Sjb} 57178481Sjb 58178481Sjbvoid * 59178481Sjbxcalloc(size_t size) 60178481Sjb{ 61178481Sjb void *mem; 62178481Sjb 63178481Sjb mem = xmalloc(size); 64178481Sjb bzero(mem, size); 65178481Sjb 66178481Sjb return (mem); 67178481Sjb} 68178481Sjb 69178481Sjbchar * 70178481Sjbxstrdup(const char *str) 71178481Sjb{ 72178481Sjb char *newstr; 73178481Sjb 74178481Sjb if ((newstr = strdup(str)) == NULL) 75178481Sjb memory_bailout(); 76178481Sjb 77178481Sjb return (newstr); 78178481Sjb} 79178481Sjb 80178481Sjbchar * 81178481Sjbxstrndup(char *str, size_t len) 82178481Sjb{ 83178481Sjb char *newstr; 84178481Sjb 85178481Sjb if ((newstr = malloc(len + 1)) == NULL) 86178481Sjb memory_bailout(); 87178481Sjb 88178481Sjb (void) strncpy(newstr, str, len); 89178481Sjb newstr[len] = '\0'; 90178481Sjb 91178481Sjb return (newstr); 92178481Sjb} 93178481Sjb 94178481Sjbvoid * 95178481Sjbxrealloc(void *ptr, size_t size) 96178481Sjb{ 97178481Sjb void *mem; 98178481Sjb 99178481Sjb if ((mem = realloc(ptr, size)) == NULL) 100178481Sjb memory_bailout(); 101178481Sjb 102178481Sjb return (mem); 103178481Sjb} 104