Annotation of src/usr.bin/cvs/xmalloc.c, Revision 1.5
1.5 ! xsa 1: /* $OpenBSD: xmalloc.c,v 1.4 2005/12/30 17:51:01 reyk Exp $ */
1.1 joris 2: /*
3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * Versions of xmalloc and friends that check their results, and never return
7: * failure (they call fatal if they encounter an error).
8: *
9: * As far as I am concerned, the code I have written for this software
10: * can be used freely for any purpose. Any derived versions of this
11: * software must be clearly marked as such, and if the derived work is
12: * incompatible with the protocol description in the RFC file, it must be
13: * called by a name other than "ssh" or "Secure Shell".
14: */
15:
1.5 ! xsa 16: #include "includes.h"
1.1 joris 17:
1.3 xsa 18: #include "xmalloc.h"
19: #include "log.h"
1.1 joris 20:
21: void *
22: xmalloc(size_t size)
23: {
24: void *ptr;
25:
26: if (size == 0)
27: fatal("xmalloc: zero size");
28: ptr = malloc(size);
29: if (ptr == NULL)
1.4 reyk 30: fatal("xmalloc: out of memory (allocating %lu bytes)",
31: (u_long) size);
1.1 joris 32: return ptr;
33: }
34:
35: void *
36: xrealloc(void *ptr, size_t new_size)
37: {
38: void *new_ptr;
39:
40: if (new_size == 0)
41: fatal("xrealloc: zero size");
42: if (ptr == NULL)
43: new_ptr = xmalloc(new_size);
44: else
45: new_ptr = realloc(ptr, new_size);
46: if (new_ptr == NULL)
1.4 reyk 47: fatal("xrealloc: out of memory (new_size %lu bytes)",
48: (u_long) new_size);
1.1 joris 49: return new_ptr;
50: }
51:
52: void
53: xfree(void *ptr)
54: {
55: if (ptr == NULL)
56: fatal("xfree: NULL pointer given as argument");
57: free(ptr);
58: }
59:
60: char *
61: xstrdup(const char *str)
62: {
63: size_t len;
64: char *cp;
65:
66: len = strlen(str) + 1;
67: cp = xmalloc(len);
68: strlcpy(cp, str, len);
69: return cp;
70: }