inlined.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_INLINED_H
  3. #define NETDATA_INLINED_H 1
  4. #include "libnetdata.h"
  5. #ifdef KERNEL_32BIT
  6. typedef uint32_t kernel_uint_t;
  7. #define str2kernel_uint_t(string) str2uint32_t(string)
  8. #define KERNEL_UINT_FORMAT "%u"
  9. #else
  10. typedef uint64_t kernel_uint_t;
  11. #define str2kernel_uint_t(string) str2uint64_t(string)
  12. #define KERNEL_UINT_FORMAT "%" PRIu64
  13. #endif
  14. #define str2pid_t(string) str2uint32_t(string)
  15. // for faster execution, allow the compiler to inline
  16. // these functions that are called thousands of times per second
  17. static inline uint32_t simple_hash(const char *name) {
  18. unsigned char *s = (unsigned char *) name;
  19. uint32_t hval = 0x811c9dc5;
  20. while (*s) {
  21. hval *= 16777619;
  22. hval ^= (uint32_t) *s++;
  23. }
  24. return hval;
  25. }
  26. static inline uint32_t simple_uhash(const char *name) {
  27. unsigned char *s = (unsigned char *) name;
  28. uint32_t hval = 0x811c9dc5, c;
  29. while ((c = *s++)) {
  30. if (unlikely(c >= 'A' && c <= 'Z')) c += 'a' - 'A';
  31. hval *= 16777619;
  32. hval ^= c;
  33. }
  34. return hval;
  35. }
  36. static inline int str2i(const char *s) {
  37. int n = 0;
  38. char c, negative = (char)(*s == '-');
  39. const char *e = s + 30; // max number of character to iterate
  40. for(c = (char)((negative)?*(++s):*s); c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  41. n *= 10;
  42. n += c - '0';
  43. }
  44. if(unlikely(negative))
  45. return -n;
  46. return n;
  47. }
  48. static inline long str2l(const char *s) {
  49. long n = 0;
  50. char c, negative = (*s == '-');
  51. const char *e = &s[30]; // max number of character to iterate
  52. for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  53. n *= 10;
  54. n += c - '0';
  55. }
  56. if(unlikely(negative))
  57. return -n;
  58. return n;
  59. }
  60. static inline uint32_t str2uint32_t(const char *s) {
  61. uint32_t n = 0;
  62. char c;
  63. const char *e = &s[30]; // max number of character to iterate
  64. for(c = *s; c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  65. n *= 10;
  66. n += c - '0';
  67. }
  68. return n;
  69. }
  70. static inline uint64_t str2uint64_t(const char *s) {
  71. uint64_t n = 0;
  72. char c;
  73. const char *e = &s[30]; // max number of character to iterate
  74. for(c = *s; c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  75. n *= 10;
  76. n += c - '0';
  77. }
  78. return n;
  79. }
  80. static inline unsigned long str2ul(const char *s) {
  81. unsigned long n = 0;
  82. char c;
  83. const char *e = &s[30]; // max number of character to iterate
  84. for(c = *s; c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  85. n *= 10;
  86. n += c - '0';
  87. }
  88. return n;
  89. }
  90. static inline unsigned long long str2ull(const char *s) {
  91. unsigned long long n = 0;
  92. char c;
  93. const char *e = &s[30]; // max number of character to iterate
  94. for(c = *s; c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  95. n *= 10;
  96. n += c - '0';
  97. }
  98. return n;
  99. }
  100. static inline long long str2ll(const char *s, char **endptr) {
  101. int negative = 0;
  102. if(unlikely(*s == '-')) {
  103. s++;
  104. negative = 1;
  105. }
  106. else if(unlikely(*s == '+'))
  107. s++;
  108. long long n = 0;
  109. char c;
  110. const char *e = &s[30]; // max number of character to iterate
  111. for(c = *s; c >= '0' && c <= '9' && s < e ; c = *(++s)) {
  112. n *= 10;
  113. n += c - '0';
  114. }
  115. if(unlikely(endptr))
  116. *endptr = (char *)s;
  117. if(unlikely(negative))
  118. return -n;
  119. else
  120. return n;
  121. }
  122. static inline char *strncpyz(char *dst, const char *src, size_t n) {
  123. char *p = dst;
  124. while (*src && n--)
  125. *dst++ = *src++;
  126. *dst = '\0';
  127. return p;
  128. }
  129. static inline void sanitize_json_string(char *dst, const char *src, size_t dst_size) {
  130. while (*src != '\0' && dst_size > 1) {
  131. if (*src < 0x1F) {
  132. *dst++ = '_';
  133. src++;
  134. dst_size--;
  135. }
  136. else if (*src == '\\' || *src == '\"') {
  137. *dst++ = '\\';
  138. *dst++ = *src++;
  139. dst_size -= 2;
  140. }
  141. else {
  142. *dst++ = *src++;
  143. dst_size--;
  144. }
  145. }
  146. *dst = '\0';
  147. }
  148. static inline bool sanitize_command_argument_string(char *dst, const char *src, size_t dst_size) {
  149. // skip leading dashes
  150. while (src[0] == '-')
  151. src++;
  152. // escape single quotes
  153. while (src[0] != '\0') {
  154. if (src[0] == '\'') {
  155. if (dst_size < 4)
  156. return false;
  157. dst[0] = '\''; dst[1] = '\\'; dst[2] = '\''; dst[3] = '\'';
  158. dst += 4;
  159. dst_size -= 4;
  160. } else {
  161. if (dst_size < 1)
  162. return false;
  163. dst[0] = src[0];
  164. dst += 1;
  165. dst_size -= 1;
  166. }
  167. src++;
  168. }
  169. // make sure we have space to terminate the string
  170. if (dst_size == 0)
  171. return false;
  172. *dst = '\0';
  173. return true;
  174. }
  175. static inline int read_file(const char *filename, char *buffer, size_t size) {
  176. if(unlikely(!size)) return 3;
  177. int fd = open(filename, O_RDONLY, 0666);
  178. if(unlikely(fd == -1)) {
  179. buffer[0] = '\0';
  180. return 1;
  181. }
  182. ssize_t r = read(fd, buffer, size);
  183. if(unlikely(r == -1)) {
  184. buffer[0] = '\0';
  185. close(fd);
  186. return 2;
  187. }
  188. buffer[r] = '\0';
  189. close(fd);
  190. return 0;
  191. }
  192. static inline int read_single_number_file(const char *filename, unsigned long long *result) {
  193. char buffer[30 + 1];
  194. int ret = read_file(filename, buffer, 30);
  195. if(unlikely(ret)) {
  196. *result = 0;
  197. return ret;
  198. }
  199. buffer[30] = '\0';
  200. *result = str2ull(buffer);
  201. return 0;
  202. }
  203. static inline int read_single_signed_number_file(const char *filename, long long *result) {
  204. char buffer[30 + 1];
  205. int ret = read_file(filename, buffer, 30);
  206. if(unlikely(ret)) {
  207. *result = 0;
  208. return ret;
  209. }
  210. buffer[30] = '\0';
  211. *result = atoll(buffer);
  212. return 0;
  213. }
  214. #endif //NETDATA_INLINED_H