| 1 | /* |
| 2 | * Copyright (C) 2015 Michael Jeanson <mjeanson@efficios.com> |
| 3 | * Copyright (C) 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 4 | * |
| 5 | * SPDX-License-Identifier: MIT |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #ifndef _COMPAT_STRING_H |
| 10 | #define _COMPAT_STRING_H |
| 11 | |
| 12 | #include <string.h> |
| 13 | |
| 14 | #ifdef HAVE_STRNLEN |
| 15 | static inline |
| 16 | size_t lttng_strnlen(const char *str, size_t max) |
| 17 | { |
| 18 | return strnlen(str, max); |
| 19 | } |
| 20 | #else |
| 21 | static inline |
| 22 | size_t lttng_strnlen(const char *str, size_t max) |
| 23 | { |
| 24 | size_t ret; |
| 25 | const char *end; |
| 26 | |
| 27 | end = memchr(str, 0, max); |
| 28 | |
| 29 | if (end) { |
| 30 | ret = (size_t) (end - str); |
| 31 | } else { |
| 32 | ret = max; |
| 33 | } |
| 34 | |
| 35 | return ret; |
| 36 | } |
| 37 | #endif /* HAVE_STRNLEN */ |
| 38 | |
| 39 | #ifdef HAVE_STRNDUP |
| 40 | static inline |
| 41 | char *lttng_strndup(const char *s, size_t n) |
| 42 | { |
| 43 | return strndup(s, n); |
| 44 | } |
| 45 | #else |
| 46 | static inline |
| 47 | char *lttng_strndup(const char *s, size_t n) |
| 48 | { |
| 49 | char *ret; |
| 50 | size_t navail; |
| 51 | |
| 52 | if (!s) { |
| 53 | ret = NULL; |
| 54 | goto end; |
| 55 | } |
| 56 | |
| 57 | /* min() */ |
| 58 | navail = strlen(s) + 1; |
| 59 | if ((n + 1) < navail) { |
| 60 | navail = n + 1; |
| 61 | } |
| 62 | |
| 63 | ret = malloc(navail); |
| 64 | if (!ret) { |
| 65 | goto end; |
| 66 | } |
| 67 | |
| 68 | memcpy(ret, s, navail); |
| 69 | ret[navail - 1] = '\0'; |
| 70 | end: |
| 71 | return ret; |
| 72 | } |
| 73 | #endif /* HAVE_STRNDUP */ |
| 74 | |
| 75 | #ifdef HAVE_FLS |
| 76 | static inline int lttng_fls(int val) |
| 77 | { |
| 78 | return fls(val); |
| 79 | } |
| 80 | #else |
| 81 | static inline int lttng_fls(int val) |
| 82 | { |
| 83 | int r = 32; |
| 84 | unsigned int x = (unsigned int) val; |
| 85 | |
| 86 | if (!x) |
| 87 | return 0; |
| 88 | if (!(x & 0xFFFF0000U)) { |
| 89 | x <<= 16; |
| 90 | r -= 16; |
| 91 | } |
| 92 | if (!(x & 0xFF000000U)) { |
| 93 | x <<= 8; |
| 94 | r -= 8; |
| 95 | } |
| 96 | if (!(x & 0xF0000000U)) { |
| 97 | x <<= 4; |
| 98 | r -= 4; |
| 99 | } |
| 100 | if (!(x & 0xC0000000U)) { |
| 101 | x <<= 2; |
| 102 | r -= 2; |
| 103 | } |
| 104 | if (!(x & 0x80000000U)) { |
| 105 | r -= 1; |
| 106 | } |
| 107 | return r; |
| 108 | } |
| 109 | #endif /* HAVE_FLS */ |
| 110 | |
| 111 | #if HAVE_MEMRCHR |
| 112 | static inline |
| 113 | void *lttng_memrchr(const void *s, int c, size_t n) |
| 114 | { |
| 115 | return memrchr(s, c, n); |
| 116 | } |
| 117 | #else |
| 118 | static inline |
| 119 | void *lttng_memrchr(const void *s, int c, size_t n) |
| 120 | { |
| 121 | int i; |
| 122 | const char *str = s; |
| 123 | for (i = n-1; i >= 0; i--) { |
| 124 | if (str[i] == (char)c) { |
| 125 | return (void *)(str+i); |
| 126 | } |
| 127 | } |
| 128 | return NULL; |
| 129 | } |
| 130 | #endif /* HAVE_MEMRCHR */ |
| 131 | |
| 132 | #endif /* _COMPAT_STRING_H */ |