inlined.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 simple_hash_strcmp(const char *name, const char *b, uint32_t *hash) {
  37. unsigned char *s = (unsigned char *) name;
  38. uint32_t hval = 0x811c9dc5;
  39. int ret = 0;
  40. while (*s) {
  41. if(!ret) ret = *s - *b++;
  42. hval *= 16777619;
  43. hval ^= (uint32_t) *s++;
  44. }
  45. *hash = hval;
  46. return ret;
  47. }
  48. static inline int str2i(const char *s) {
  49. int n = 0;
  50. char c, negative = (*s == '-');
  51. for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
  52. n *= 10;
  53. n += c - '0';
  54. }
  55. if(unlikely(negative))
  56. return -n;
  57. return n;
  58. }
  59. static inline long str2l(const char *s) {
  60. long n = 0;
  61. char c, negative = (*s == '-');
  62. for(c = (negative)?*(++s):*s; c >= '0' && c <= '9' ; c = *(++s)) {
  63. n *= 10;
  64. n += c - '0';
  65. }
  66. if(unlikely(negative))
  67. return -n;
  68. return n;
  69. }
  70. static inline uint32_t str2uint32_t(const char *s) {
  71. uint32_t n = 0;
  72. char c;
  73. for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
  74. n *= 10;
  75. n += c - '0';
  76. }
  77. return n;
  78. }
  79. static inline uint64_t str2uint64_t(const char *s) {
  80. uint64_t n = 0;
  81. char c;
  82. for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
  83. n *= 10;
  84. n += c - '0';
  85. }
  86. return n;
  87. }
  88. static inline unsigned long str2ul(const char *s) {
  89. unsigned long n = 0;
  90. char c;
  91. for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
  92. n *= 10;
  93. n += c - '0';
  94. }
  95. return n;
  96. }
  97. static inline unsigned long long str2ull(const char *s) {
  98. unsigned long long n = 0;
  99. char c;
  100. for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
  101. n *= 10;
  102. n += c - '0';
  103. }
  104. return n;
  105. }
  106. static inline long long str2ll(const char *s, char **endptr) {
  107. int negative = 0;
  108. if(unlikely(*s == '-')) {
  109. s++;
  110. negative = 1;
  111. }
  112. else if(unlikely(*s == '+'))
  113. s++;
  114. long long n = 0;
  115. char c;
  116. for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
  117. n *= 10;
  118. n += c - '0';
  119. }
  120. if(unlikely(endptr))
  121. *endptr = (char *)s;
  122. if(unlikely(negative))
  123. return -n;
  124. else
  125. return n;
  126. }
  127. static inline char *strncpyz(char *dst, const char *src, size_t n) {
  128. char *p = dst;
  129. while (*src && n--)
  130. *dst++ = *src++;
  131. *dst = '\0';
  132. return p;
  133. }
  134. static inline void sanitize_json_string(char *dst, const char *src, size_t dst_size) {
  135. while (*src != '\0' && dst_size > 1) {
  136. if (*src < 0x1F) {
  137. *dst++ = '_';
  138. src++;
  139. dst_size--;
  140. }
  141. else if (*src == '\\' || *src == '\"') {
  142. *dst++ = '\\';
  143. *dst++ = *src++;
  144. dst_size -= 2;
  145. }
  146. else {
  147. *dst++ = *src++;
  148. dst_size--;
  149. }
  150. }
  151. *dst = '\0';
  152. }
  153. static inline int read_file(const char *filename, char *buffer, size_t size) {
  154. if(unlikely(!size)) return 3;
  155. int fd = open(filename, O_RDONLY, 0666);
  156. if(unlikely(fd == -1)) {
  157. buffer[0] = '\0';
  158. return 1;
  159. }
  160. ssize_t r = read(fd, buffer, size);
  161. if(unlikely(r == -1)) {
  162. buffer[0] = '\0';
  163. close(fd);
  164. return 2;
  165. }
  166. buffer[r] = '\0';
  167. close(fd);
  168. return 0;
  169. }
  170. static inline int read_single_number_file(const char *filename, unsigned long long *result) {
  171. char buffer[30 + 1];
  172. int ret = read_file(filename, buffer, 30);
  173. if(unlikely(ret)) {
  174. *result = 0;
  175. return ret;
  176. }
  177. buffer[30] = '\0';
  178. *result = str2ull(buffer);
  179. return 0;
  180. }
  181. static inline int read_single_signed_number_file(const char *filename, long long *result) {
  182. char buffer[30 + 1];
  183. int ret = read_file(filename, buffer, 30);
  184. if(unlikely(ret)) {
  185. *result = 0;
  186. return ret;
  187. }
  188. buffer[30] = '\0';
  189. *result = atoll(buffer);
  190. return 0;
  191. }
  192. #endif //NETDATA_INLINED_H