2 * Copyright (C) 2015 Michael Jeanson <mjeanson@efficios.com>
3 * Copyright (C) 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: MIT
9 #ifndef _COMPAT_STRING_H
10 #define _COMPAT_STRING_H
16 static inline size_t lttng_strnlen(const char *str, size_t max)
18 return strnlen(str, max);
21 static inline size_t lttng_strnlen(const char *str, size_t max)
26 end = (const char *) memchr(str, 0, max);
29 ret = (size_t) (end - str);
36 #endif /* HAVE_STRNLEN */
39 static inline char *lttng_strndup(const char *s, size_t n)
44 static inline char *lttng_strndup(const char *s, size_t n)
55 navail = strlen(s) + 1;
56 if ((n + 1) < navail) {
60 ret = malloc<char>(navail);
65 memcpy(ret, s, navail);
66 ret[navail - 1] = '\0';
70 #endif /* HAVE_STRNDUP */
73 static inline int lttng_fls(int val)
78 static inline int lttng_fls(int val)
81 unsigned int x = (unsigned int) val;
85 if (!(x & 0xFFFF0000U)) {
89 if (!(x & 0xFF000000U)) {
93 if (!(x & 0xF0000000U)) {
97 if (!(x & 0xC0000000U)) {
101 if (!(x & 0x80000000U)) {
106 #endif /* HAVE_FLS */
109 static inline void *lttng_memrchr(const void *s, int c, size_t n)
111 return (void *) memrchr(s, c, n);
114 static inline void *lttng_memrchr(const void *s, int c, size_t n)
117 const char *str = (const char *) s;
118 for (i = n - 1; i >= 0; i--) {
119 if (str[i] == (char) c) {
120 return (void *) (str + i);
125 #endif /* HAVE_MEMRCHR */
127 #endif /* _COMPAT_STRING_H */