xmalloc.c revision 76259
11539Srgrimes/* 21539Srgrimes * Author: Tatu Ylonen <ylo@cs.hut.fi> 31539Srgrimes * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 41539Srgrimes * All rights reserved 51539Srgrimes * Versions of malloc and friends that check their results, and never return 61539Srgrimes * failure (they call fatal if they encounter an error). 71539Srgrimes * 81539Srgrimes * As far as I am concerned, the code I have written for this software 91539Srgrimes * can be used freely for any purpose. Any derived versions of this 101539Srgrimes * software must be clearly marked as such, and if the derived work is 111539Srgrimes * incompatible with the protocol description in the RFC file, it must be 121539Srgrimes * called by a name other than "ssh" or "Secure Shell". 131539Srgrimes */ 141539Srgrimes 151539Srgrimes#include "includes.h" 161539SrgrimesRCSID("$OpenBSD: xmalloc.c,v 1.15 2001/04/16 08:05:34 deraadt Exp $"); 171539Srgrimes 181539Srgrimes#include "xmalloc.h" 191539Srgrimes#include "log.h" 201539Srgrimes 211539Srgrimesvoid * 221539Srgrimesxmalloc(size_t size) 231539Srgrimes{ 241539Srgrimes void *ptr; 251539Srgrimes 261539Srgrimes if (size == 0) 271539Srgrimes fatal("xmalloc: zero size"); 281539Srgrimes ptr = malloc(size); 291539Srgrimes if (ptr == NULL) 301539Srgrimes fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size); 311539Srgrimes return ptr; 321539Srgrimes} 3341284Sbde 3450473Spetervoid * 351539Srgrimesxrealloc(void *ptr, size_t new_size) 361539Srgrimes{ 371539Srgrimes void *new_ptr; 381539Srgrimes 391539Srgrimes if (new_size == 0) 401539Srgrimes fatal("xrealloc: zero size"); 411539Srgrimes if (ptr == NULL) 421539Srgrimes new_ptr = malloc(new_size); 431539Srgrimes else 441539Srgrimes new_ptr = realloc(ptr, new_size); 45102227Smike if (new_ptr == NULL) 461539Srgrimes fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size); 471539Srgrimes return new_ptr; 48102227Smike} 491539Srgrimes 501539Srgrimesvoid 5193032Simpxfree(void *ptr) 52102227Smike{ 5393032Simp if (ptr == NULL) 54102227Smike fatal("xfree: NULL pointer given as argument"); 5541284Sbde free(ptr); 5693032Simp} 57102227Smike 5893032Simpchar * 59102227Smikexstrdup(const char *str) 6093032Simp{ 61102227Smike size_t len = strlen(str) + 1; 6293032Simp char *cp; 63102227Smike 6493032Simp if (len == 0) 6593032Simp fatal("xstrdup: zero size"); 661539Srgrimes cp = xmalloc(len); 671539Srgrimes strlcpy(cp, str, len); 681539Srgrimes return cp; 69} 70