Fix: include stdlib.h in compat/string.h
[lttng-tools.git] / src / common / compat / string.h
1 /*
2 * Copyright (C) 2015 Michael Jeanson <mjeanson@efficios.com>
3 * 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #ifndef _COMPAT_STRING_H
25 #define _COMPAT_STRING_H
26
27 #include <string.h>
28 #include <stdlib.h>
29
30 #ifdef HAVE_STRNLEN
31 static inline
32 size_t lttng_strnlen(const char *str, size_t max)
33 {
34 return strnlen(str, max);
35 }
36 #else
37 static inline
38 size_t lttng_strnlen(const char *str, size_t max)
39 {
40 size_t ret;
41 const char *end;
42
43 end = memchr(str, 0, max);
44
45 if (end) {
46 ret = (size_t) (end - str);
47 } else {
48 ret = max;
49 }
50
51 return ret;
52 }
53 #endif /* HAVE_STRNLEN */
54
55 #ifdef HAVE_STRNDUP
56 static inline
57 char *lttng_strndup(const char *s, size_t n)
58 {
59 return strndup(s, n);
60 }
61 #else
62 static inline
63 char *lttng_strndup(const char *s, size_t n)
64 {
65 char *ret;
66 size_t navail;
67
68 if (!s) {
69 ret = NULL;
70 goto end;
71 }
72
73 /* min() */
74 navail = strlen(s) + 1;
75 if ((n + 1) < navail) {
76 navail = n + 1;
77 }
78
79 ret = malloc(navail);
80 if (!ret) {
81 goto end;
82 }
83
84 memcpy(ret, s, navail);
85 ret[navail - 1] = '\0';
86 end:
87 return ret;
88 }
89 #endif /* HAVE_STRNDUP */
90
91 #ifdef HAVE_FLS
92 static inline int lttng_fls(int val)
93 {
94 return fls(val);
95 }
96 #else
97 static inline int lttng_fls(int val)
98 {
99 int r = 32;
100 unsigned int x = (unsigned int) val;
101
102 if (!x)
103 return 0;
104 if (!(x & 0xFFFF0000U)) {
105 x <<= 16;
106 r -= 16;
107 }
108 if (!(x & 0xFF000000U)) {
109 x <<= 8;
110 r -= 8;
111 }
112 if (!(x & 0xF0000000U)) {
113 x <<= 4;
114 r -= 4;
115 }
116 if (!(x & 0xC0000000U)) {
117 x <<= 2;
118 r -= 2;
119 }
120 if (!(x & 0x80000000U)) {
121 r -= 1;
122 }
123 return r;
124 }
125 #endif /* HAVE_FLS */
126
127 #if HAVE_MEMRCHR
128 static inline
129 void *lttng_memrchr(const void *s, int c, size_t n)
130 {
131 return memrchr(s, c, n);
132 }
133 #else
134 static inline
135 void *lttng_memrchr(const void *s, int c, size_t n)
136 {
137 int i;
138 const char *str = s;
139 for (i = n-1; i >= 0; i--) {
140 if (str[i] == (char)c) {
141 return (void *)(str+i);
142 }
143 }
144 return NULL;
145 }
146 #endif /* HAVE_MEMRCHR */
147
148 #endif /* _COMPAT_STRING_H */
This page took 0.032619 seconds and 4 git commands to generate.