]> andersk Git - openssh.git/blame - xmalloc.c
- djm@cvs.openbsd.org 2006/03/22 21:27:15
[openssh.git] / xmalloc.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * Versions of malloc and friends that check their results, and never return
6 * failure (they call fatal if they encounter an error).
2b87da3b 7 *
bcbf86ec 8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
8efc0c15 16
42f11eb2 17#include "xmalloc.h"
18#include "log.h"
8efc0c15 19
5260325f 20void *
21xmalloc(size_t size)
8efc0c15 22{
a2e6d17d 23 void *ptr;
24
25 if (size == 0)
26 fatal("xmalloc: zero size");
27 ptr = malloc(size);
5260325f 28 if (ptr == NULL)
a2e6d17d 29 fatal("xmalloc: out of memory (allocating %lu bytes)", (u_long) size);
5260325f 30 return ptr;
8efc0c15 31}
32
5260325f 33void *
34xrealloc(void *ptr, size_t new_size)
8efc0c15 35{
5260325f 36 void *new_ptr;
37
a2e6d17d 38 if (new_size == 0)
39 fatal("xrealloc: zero size");
5260325f 40 if (ptr == NULL)
764291b3 41 new_ptr = malloc(new_size);
42 else
43 new_ptr = realloc(ptr, new_size);
5260325f 44 if (new_ptr == NULL)
a2e6d17d 45 fatal("xrealloc: out of memory (new_size %lu bytes)", (u_long) new_size);
5260325f 46 return new_ptr;
8efc0c15 47}
48
6ae2364d 49void
5260325f 50xfree(void *ptr)
8efc0c15 51{
5260325f 52 if (ptr == NULL)
53 fatal("xfree: NULL pointer given as argument");
54 free(ptr);
8efc0c15 55}
56
5260325f 57char *
58xstrdup(const char *str)
8efc0c15 59{
bac2ef55 60 size_t len;
a2e6d17d 61 char *cp;
8efc0c15 62
bac2ef55 63 len = strlen(str) + 1;
a2e6d17d 64 cp = xmalloc(len);
5260325f 65 strlcpy(cp, str, len);
66 return cp;
8efc0c15 67}
This page took 0.518106 seconds and 5 git commands to generate.