| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_RRDENGINELIB_H |
| 4 | #define NETDATA_RRDENGINELIB_H |
| 5 | |
| 6 | #include "libnetdata/libnetdata.h" |
| 7 | |
| 8 | /* Forward declarations */ |
| 9 | struct rrdengine_instance; |
| 10 | |
| 11 | #define ALIGN_BYTES_FLOOR(x) (((x) / RRDENG_BLOCK_SIZE) * RRDENG_BLOCK_SIZE) |
| 12 | #define ALIGN_BYTES_CEILING(x) ((((x) + RRDENG_BLOCK_SIZE - 1) / RRDENG_BLOCK_SIZE) * RRDENG_BLOCK_SIZE) |
| 13 | |
| 14 | typedef uintptr_t rrdeng_stats_t; |
| 15 | |
| 16 | #ifdef __ATOMIC_RELAXED |
| 17 | #define rrd_atomic_fetch_add(p, n) __atomic_fetch_add(p, n, __ATOMIC_RELAXED) |
| 18 | #define rrd_atomic_add_fetch(p, n) __atomic_add_fetch(p, n, __ATOMIC_RELAXED) |
| 19 | #else |
| 20 | #define rrd_atomic_fetch_add(p, n) __sync_fetch_and_add(p, n) |
| 21 | #define rrd_atomic_add_fetch(p, n) __sync_add_and_fetch(p, n) |
| 22 | #endif |
| 23 | |
| 24 | #define rrd_stat_atomic_add(p, n) rrd_atomic_fetch_add(p, n) |
| 25 | |
| 26 | /* returns -1 if it didn't find the first cleared bit, the position otherwise. Starts from LSB. */ |
| 27 | static inline int find_first_zero(unsigned x) |
| 28 | { |
| 29 | return ffs((int)(~x)) - 1; |
| 30 | } |
| 31 | |
| 32 | /* Starts from LSB. */ |
| 33 | static inline uint8_t check_bit(unsigned x, size_t pos) |
| 34 | { |
| 35 | return !!(x & (1 << pos)); |
| 36 | } |
| 37 | |
| 38 | /* Starts from LSB. val is 0 or 1 */ |
| 39 | static inline void modify_bit(unsigned *x, unsigned pos, uint8_t val) |
| 40 | { |
| 41 | switch(val) { |
| 42 | case 0: |
| 43 | *x &= ~(1U << pos); |
| 44 | break; |
| 45 | case 1: |
| 46 | *x |= 1U << pos; |
| 47 | break; |
| 48 | default: |
| 49 | netdata_log_error("modify_bit() called with invalid argument."); |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #define RRDENG_PATH_MAX (FILENAME_MAX + 1) |
| 55 | |
| 56 | /* returns old *ptr value */ |
| 57 | static inline unsigned long ulong_compare_and_swap(volatile unsigned long *ptr, |
| 58 | unsigned long oldval, unsigned long newval) |
| 59 | { |
| 60 | return __sync_val_compare_and_swap(ptr, oldval, newval); |
| 61 | } |
| 62 | |
| 63 | #ifndef O_DIRECT |
| 64 | /* Workaround for OS X */ |
| 65 | #define O_DIRECT (0) |
| 66 | #endif |
| 67 | |
| 68 | static inline int crc32cmp(void *crcp, uLong crc) |
| 69 | { |
| 70 | uint32_t loaded_crc; |
| 71 | memcpy(&loaded_crc, crcp, sizeof(loaded_crc)); |
| 72 | return (loaded_crc != crc); |
| 73 | } |
| 74 | |
| 75 | static inline void crc32set(void *crcp, uLong crc) |
| 76 | { |
| 77 | uint32_t store_crc = (uint32_t) crc; |
| 78 | memcpy(crcp, &store_crc, sizeof(store_crc)); |
| 79 | } |
| 80 | |
| 81 | int check_file_properties(uv_file file, uint64_t *file_size, size_t min_size); |
| 82 | int open_file_for_io(char *path, int flags, uv_file *file, int direct); |
| 83 | static inline int open_file_direct_io(char *path, int flags, uv_file *file) |
| 84 | { |
| 85 | return open_file_for_io(path, flags, file, 1); |
| 86 | } |
| 87 | static inline int open_file_buffered_io(char *path, int flags, uv_file *file) |
| 88 | { |
| 89 | return open_file_for_io(path, flags, file, 0); |
| 90 | } |
| 91 | |
| 92 | #endif /* NETDATA_RRDENGINELIB_H */ |