Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | xmalloc.c |
| 4 | |
| 5 | Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | |
| 7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | All rights reserved |
| 9 | |
| 10 | Created: Mon Mar 20 21:23:10 1995 ylo |
| 11 | |
| 12 | Versions of malloc and friends that check their results, and never return |
| 13 | failure (they call fatal if they encounter an error). |
| 14 | |
| 15 | */ |
| 16 | |
| 17 | #include "includes.h" |
| 18 | RCSID("$Id: xmalloc.c,v 1.1 1999/10/27 03:42:46 damien Exp $"); |
| 19 | |
| 20 | #include "ssh.h" |
| 21 | |
| 22 | void *xmalloc(size_t size) |
| 23 | { |
| 24 | void *ptr = malloc(size); |
| 25 | if (ptr == NULL) |
| 26 | fatal("xmalloc: out of memory (allocating %d bytes)", (int)size); |
| 27 | return ptr; |
| 28 | } |
| 29 | |
| 30 | void *xrealloc(void *ptr, size_t new_size) |
| 31 | { |
| 32 | void *new_ptr; |
| 33 | |
| 34 | if (ptr == NULL) |
| 35 | fatal("xrealloc: NULL pointer given as argument"); |
| 36 | new_ptr = realloc(ptr, new_size); |
| 37 | if (new_ptr == NULL) |
| 38 | fatal("xrealloc: out of memory (new_size %d bytes)", (int)new_size); |
| 39 | return new_ptr; |
| 40 | } |
| 41 | |
| 42 | void xfree(void *ptr) |
| 43 | { |
| 44 | if (ptr == NULL) |
| 45 | fatal("xfree: NULL pointer given as argument"); |
| 46 | free(ptr); |
| 47 | } |
| 48 | |
| 49 | char *xstrdup(const char *str) |
| 50 | { |
| 51 | int len = strlen(str) + 1; |
| 52 | |
| 53 | char *cp = xmalloc(len); |
| 54 | strlcpy(cp, str, len); |
| 55 | return cp; |
| 56 | } |