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> 38*4cc75139SJohn Birrell #include "memory.h" 391673e404SJohn Birrell 401673e404SJohn Birrell static 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 471673e404SJohn Birrell void * 481673e404SJohn Birrell xmalloc(size_t size) 491673e404SJohn Birrell { 501673e404SJohn Birrell void *mem; 511673e404SJohn Birrell 521673e404SJohn Birrell if ((mem = malloc(size)) == NULL) 531673e404SJohn Birrell memory_bailout(); 541673e404SJohn Birrell 551673e404SJohn Birrell return (mem); 561673e404SJohn Birrell } 571673e404SJohn Birrell 581673e404SJohn Birrell void * 591673e404SJohn Birrell xcalloc(size_t size) 601673e404SJohn Birrell { 611673e404SJohn Birrell void *mem; 621673e404SJohn Birrell 631673e404SJohn Birrell mem = xmalloc(size); 641673e404SJohn Birrell bzero(mem, size); 651673e404SJohn Birrell 661673e404SJohn Birrell return (mem); 671673e404SJohn Birrell } 681673e404SJohn Birrell 691673e404SJohn Birrell char * 701673e404SJohn Birrell xstrdup(const char *str) 711673e404SJohn Birrell { 721673e404SJohn Birrell char *newstr; 731673e404SJohn Birrell 741673e404SJohn Birrell if ((newstr = strdup(str)) == NULL) 751673e404SJohn Birrell memory_bailout(); 761673e404SJohn Birrell 771673e404SJohn Birrell return (newstr); 781673e404SJohn Birrell } 791673e404SJohn Birrell 801673e404SJohn Birrell char * 811673e404SJohn Birrell xstrndup(char *str, size_t len) 821673e404SJohn Birrell { 831673e404SJohn Birrell char *newstr; 841673e404SJohn Birrell 851673e404SJohn Birrell if ((newstr = malloc(len + 1)) == NULL) 861673e404SJohn Birrell memory_bailout(); 871673e404SJohn Birrell 881673e404SJohn Birrell (void) strncpy(newstr, str, len); 891673e404SJohn Birrell newstr[len] = '\0'; 901673e404SJohn Birrell 911673e404SJohn Birrell return (newstr); 921673e404SJohn Birrell } 931673e404SJohn Birrell 941673e404SJohn Birrell void * 951673e404SJohn Birrell xrealloc(void *ptr, size_t size) 961673e404SJohn Birrell { 971673e404SJohn Birrell void *mem; 981673e404SJohn Birrell 991673e404SJohn Birrell if ((mem = realloc(ptr, size)) == NULL) 1001673e404SJohn Birrell memory_bailout(); 1011673e404SJohn Birrell 1021673e404SJohn Birrell return (mem); 1031673e404SJohn Birrell } 104