Annotation of src/usr.bin/tmux/xmalloc.c, Revision 1.4
1.4 ! nicm 1: /* $OpenBSD: xmalloc.c,v 1.3 2009/10/26 21:42:04 deraadt 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:
19: #include <sys/param.h>
20:
21: #include <errno.h>
22: #include <libgen.h>
23: #include <stdint.h>
24: #include <stdlib.h>
25: #include <string.h>
26:
27: #include "tmux.h"
28:
29: char *
30: xstrdup(const char *s)
31: {
1.2 nicm 32: char *ptr;
1.1 nicm 33: size_t len;
34:
35: len = strlen(s) + 1;
36: ptr = xmalloc(len);
37:
1.2 nicm 38: strlcpy(ptr, s, len);
39: return (ptr);
1.1 nicm 40: }
41:
42: void *
43: xcalloc(size_t nmemb, size_t size)
44: {
1.3 deraadt 45: void *ptr;
1.1 nicm 46:
1.3 deraadt 47: if (size == 0 || nmemb == 0)
1.1 nicm 48: fatalx("zero size");
1.3 deraadt 49: if (SIZE_MAX / nmemb < size)
50: fatalx("nmemb * size > SIZE_MAX");
51: if ((ptr = calloc(nmemb, size)) == NULL)
1.1 nicm 52: fatal("xcalloc failed");
53:
1.3 deraadt 54: return (ptr);
1.1 nicm 55: }
56:
57: void *
58: xmalloc(size_t size)
59: {
60: void *ptr;
61:
1.3 deraadt 62: if (size == 0)
63: fatalx("zero size");
64: if ((ptr = malloc(size)) == NULL)
1.1 nicm 65: fatal("xmalloc failed");
66:
1.3 deraadt 67: return (ptr);
1.1 nicm 68: }
69:
70: void *
71: xrealloc(void *oldptr, size_t nmemb, size_t size)
72: {
73: size_t newsize = nmemb * size;
74: void *newptr;
75:
76: if (newsize == 0)
1.3 deraadt 77: fatalx("zero size");
78: if (SIZE_MAX / nmemb < size)
79: fatalx("nmemb * size > SIZE_MAX");
80: if ((newptr = realloc(oldptr, newsize)) == NULL)
1.1 nicm 81: fatal("xrealloc failed");
82:
1.3 deraadt 83: return (newptr);
1.1 nicm 84: }
85:
86: int printflike2
87: xasprintf(char **ret, const char *fmt, ...)
88: {
1.3 deraadt 89: va_list ap;
90: int i;
1.1 nicm 91:
1.3 deraadt 92: va_start(ap, fmt);
93: i = xvasprintf(ret, fmt, ap);
94: va_end(ap);
1.1 nicm 95:
96: return (i);
97: }
98:
99: int
100: xvasprintf(char **ret, const char *fmt, va_list ap)
101: {
102: int i;
103:
104: i = vasprintf(ret, fmt, ap);
1.3 deraadt 105: if (i < 0 || *ret == NULL)
106: fatal("xvasprintf failed");
1.1 nicm 107:
1.3 deraadt 108: return (i);
1.1 nicm 109: }
110:
111: int printflike3
112: xsnprintf(char *buf, size_t len, const char *fmt, ...)
113: {
1.3 deraadt 114: va_list ap;
115: int i;
1.1 nicm 116:
1.3 deraadt 117: va_start(ap, fmt);
118: i = xvsnprintf(buf, len, fmt, ap);
119: va_end(ap);
1.1 nicm 120:
121: return (i);
122: }
123:
124: int
125: xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
126: {
127: int i;
128:
129: if (len > INT_MAX)
130: fatalx("len > INT_MAX");
131:
132: i = vsnprintf(buf, len, fmt, ap);
1.3 deraadt 133: if (i < 0)
134: fatal("vsnprintf failed");
1.1 nicm 135:
1.3 deraadt 136: return (i);
1.1 nicm 137: }