Annotation of src/usr.bin/tmux/xmalloc.c, Revision 1.8
1.8 ! tobias 1: /* $OpenBSD: xmalloc.c,v 1.7 2014/10/20 23:57:14 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2004 Nicholas Marriott <nicm@users.sourceforge.net>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
1.6 nicm 19: #include <sys/types.h>
1.1 nicm 20:
21: #include <stdint.h>
22: #include <stdlib.h>
23: #include <string.h>
24:
25: #include "tmux.h"
26:
27: char *
1.8 ! tobias 28: xstrdup(const char *str)
1.1 nicm 29: {
1.8 ! tobias 30: char *cp;
1.1 nicm 31:
1.8 ! tobias 32: if ((cp = strdup(str)) == NULL)
! 33: fatal("xstrdup");
! 34: return (cp);
1.1 nicm 35: }
36:
37: void *
38: xcalloc(size_t nmemb, size_t size)
39: {
1.3 deraadt 40: void *ptr;
1.1 nicm 41:
1.3 deraadt 42: if (size == 0 || nmemb == 0)
1.8 ! tobias 43: fatalx("xcalloc: zero size");
1.3 deraadt 44: if ((ptr = calloc(nmemb, size)) == NULL)
1.8 ! tobias 45: log_fatal("xcalloc: allocating %zu bytes", size);
1.1 nicm 46:
1.3 deraadt 47: return (ptr);
1.1 nicm 48: }
49:
50: void *
51: xmalloc(size_t size)
52: {
53: void *ptr;
54:
1.3 deraadt 55: if (size == 0)
1.8 ! tobias 56: fatalx("xmalloc: zero size");
1.3 deraadt 57: if ((ptr = malloc(size)) == NULL)
1.8 ! tobias 58: log_fatal("xmalloc: allocating %zu bytes", size);
1.1 nicm 59:
1.3 deraadt 60: return (ptr);
1.1 nicm 61: }
62:
63: void *
1.5 nicm 64: xrealloc(void *oldptr, size_t newsize)
65: {
66: void *newptr;
67:
68: if (newsize == 0)
1.8 ! tobias 69: fatalx("xrealloc: zero size");
1.5 nicm 70: if ((newptr = realloc(oldptr, newsize)) == NULL)
1.8 ! tobias 71: log_fatal("xrealloc: allocating %zu bytes", newsize);
1.5 nicm 72:
73: return (newptr);
74: }
75:
76: void *
77: xreallocarray(void *oldptr, size_t nmemb, size_t size)
1.1 nicm 78: {
79: void *newptr;
80:
1.8 ! tobias 81: if (nmemb == 0 || size == 0)
! 82: fatalx("xreallocarray: zero size");
! 83: if ((newptr = reallocarray(oldptr, nmemb, size)) == NULL)
! 84: log_fatal("xreallocarray: allocating %zu * %zu bytes",
! 85: nmemb, size);
1.1 nicm 86:
1.3 deraadt 87: return (newptr);
1.1 nicm 88: }
89:
1.7 nicm 90: int
1.1 nicm 91: xasprintf(char **ret, const char *fmt, ...)
92: {
1.3 deraadt 93: va_list ap;
94: int i;
1.1 nicm 95:
1.3 deraadt 96: va_start(ap, fmt);
97: i = xvasprintf(ret, fmt, ap);
98: va_end(ap);
1.1 nicm 99:
100: return (i);
101: }
102:
103: int
104: xvasprintf(char **ret, const char *fmt, va_list ap)
105: {
106: int i;
107:
108: i = vasprintf(ret, fmt, ap);
1.3 deraadt 109: if (i < 0 || *ret == NULL)
1.8 ! tobias 110: fatal("xvasprintf");
1.1 nicm 111:
1.3 deraadt 112: return (i);
1.1 nicm 113: }
114:
1.7 nicm 115: int
1.1 nicm 116: xsnprintf(char *buf, size_t len, const char *fmt, ...)
117: {
1.3 deraadt 118: va_list ap;
119: int i;
1.1 nicm 120:
1.3 deraadt 121: va_start(ap, fmt);
122: i = xvsnprintf(buf, len, fmt, ap);
123: va_end(ap);
1.1 nicm 124:
125: return (i);
126: }
127:
128: int
129: xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
130: {
131: int i;
132:
133: if (len > INT_MAX)
1.8 ! tobias 134: fatalx("xvsnprintf: len > INT_MAX");
1.1 nicm 135:
136: i = vsnprintf(buf, len, fmt, ap);
1.8 ! tobias 137: if (i < 0 || i >= (int)len)
! 138: fatalx("xvsnprintf: overflow");
1.1 nicm 139:
1.3 deraadt 140: return (i);
1.1 nicm 141: }