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
17 size_t lttng_strnlen(const char *str, size_t max)
19 return strnlen(str, max);
23 size_t lttng_strnlen(const char *str, size_t max)
28 end = (const char *) memchr(str, 0, max);
31 ret = (size_t) (end - str);
38 #endif /* HAVE_STRNLEN */
42 char *lttng_strndup(const char *s, size_t n)
48 char *lttng_strndup(const char *s, size_t n)
59 navail = strlen(s) + 1;
60 if ((n + 1) < navail) {
64 ret = malloc<char>(navail);
69 memcpy(ret, s, navail);
70 ret[navail - 1] = '\0';
74 #endif /* HAVE_STRNDUP */
77 static inline int lttng_fls(int val)
82 static inline int lttng_fls(int val)
85 unsigned int x = (unsigned int) val;
89 if (!(x & 0xFFFF0000U)) {
93 if (!(x & 0xFF000000U)) {
97 if (!(x & 0xF0000000U)) {
101 if (!(x & 0xC0000000U)) {
105 if (!(x & 0x80000000U)) {
110 #endif /* HAVE_FLS */
114 void *lttng_memrchr(const void *s, int c, size_t n)
116 return (void *) memrchr(s, c, n);
120 void *lttng_memrchr(const void *s, int c, size_t n)
123 const char *str = (const char *) s;
124 for (i = n-1; i >= 0; i--) {
125 if (str[i] == (char)c) {
126 return (void *)(str+i);
131 #endif /* HAVE_MEMRCHR */
133 #endif /* _COMPAT_STRING_H */