diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2023-07-26 10:49:23 +0100 |
commit | 537a7180a0d5132c11636c4fd8b1450cd99d352c (patch) | |
tree | fb9f8c84955a411bdffbd6371ea32f2716fb3687 /src/libutil/util.h | |
parent | 5fd7a90fdaa33f52c59bdb0ca84451e5c1e22365 (diff) | |
download | rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.tar.gz rspamd-537a7180a0d5132c11636c4fd8b1450cd99d352c.zip |
[Rework] Use clang-format to unify formatting in all sources
No meaningful changes.
Diffstat (limited to 'src/libutil/util.h')
-rw-r--r-- | src/libutil/util.h | 229 |
1 files changed, 119 insertions, 110 deletions
diff --git a/src/libutil/util.h b/src/libutil/util.h index 0231cb440..662f4e819 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -15,7 +15,7 @@ #include "contrib/libev/ev.h" #include <time.h> -#ifdef __cplusplus +#ifdef __cplusplus extern "C" { #endif @@ -44,26 +44,26 @@ struct rspamd_process_exception { * @param async set non-blocking on a socket * @return socket FD or -1 in case of error */ -gint rspamd_socket_create (gint af, gint type, gint protocol, gboolean async); +gint rspamd_socket_create(gint af, gint type, gint protocol, gboolean async); /* * Create socket and bind or connect it to specified address and port */ -gint rspamd_socket_tcp (struct addrinfo *, gboolean is_server, gboolean async); +gint rspamd_socket_tcp(struct addrinfo *, gboolean is_server, gboolean async); /* * Create socket and bind or connect it to specified address and port */ -gint rspamd_socket_udp (struct addrinfo *, gboolean is_server, gboolean async); +gint rspamd_socket_udp(struct addrinfo *, gboolean is_server, gboolean async); /* * Create and bind or connect unix socket */ -gint rspamd_socket_unix (const gchar *, - struct sockaddr_un *, - gint type, - gboolean is_server, - gboolean async); +gint rspamd_socket_unix(const gchar *, + struct sockaddr_un *, + gint type, + gboolean is_server, + gboolean async); /** * Make a universal socket @@ -74,41 +74,41 @@ gint rspamd_socket_unix (const gchar *, * @param is_server make this socket as server socket * @param try_resolve try name resolution for a socket (BLOCKING) */ -gint rspamd_socket (const gchar *credits, guint16 port, gint type, - gboolean async, gboolean is_server, gboolean try_resolve); +gint rspamd_socket(const gchar *credits, guint16 port, gint type, + gboolean async, gboolean is_server, gboolean try_resolve); /* * Create socketpair */ -gboolean rspamd_socketpair (gint pair[2], gint af); +gboolean rspamd_socketpair(gint pair[2], gint af); /* * Make specified socket non-blocking */ -gint rspamd_socket_nonblocking (gint); +gint rspamd_socket_nonblocking(gint); /* * Make specified socket blocking */ -gint rspamd_socket_blocking (gint); +gint rspamd_socket_blocking(gint); /* * Poll a sync socket for specified events */ -gint rspamd_socket_poll (gint fd, gint timeout, short events); +gint rspamd_socket_poll(gint fd, gint timeout, short events); /* * Init signals */ #ifdef HAVE_SA_SIGINFO -void rspamd_signals_init (struct sigaction *sa, void (*sig_handler) (gint, - siginfo_t *, - void *)); +void rspamd_signals_init(struct sigaction *sa, void (*sig_handler)(gint, + siginfo_t *, + void *)); #else -void rspamd_signals_init (struct sigaction *sa, void (*sig_handler)(gint)); +void rspamd_signals_init(struct sigaction *sa, void (*sig_handler)(gint)); #endif #ifndef HAVE_SETPROCTITLE @@ -116,9 +116,9 @@ void rspamd_signals_init (struct sigaction *sa, void (*sig_handler)(gint)); /* * Process title utility functions */ -gint init_title (rspamd_mempool_t *pool, gint argc, gchar *argv[], gchar *envp[]); +gint init_title(rspamd_mempool_t *pool, gint argc, gchar *argv[], gchar *envp[]); -gint setproctitle (const gchar *fmt, ...); +gint setproctitle(const gchar *fmt, ...); #endif @@ -139,15 +139,15 @@ typedef struct rspamd_pidfh_s { ino_t pf_ino; } rspamd_pidfh_t; -rspamd_pidfh_t *rspamd_pidfile_open (const gchar *path, - mode_t mode, - pid_t *pidptr); +rspamd_pidfh_t *rspamd_pidfile_open(const gchar *path, + mode_t mode, + pid_t *pidptr); -gint rspamd_pidfile_write (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_write(rspamd_pidfh_t *pfh); -gint rspamd_pidfile_close (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_close(rspamd_pidfh_t *pfh); -gint rspamd_pidfile_remove (rspamd_pidfh_t *pfh); +gint rspamd_pidfile_remove(rspamd_pidfh_t *pfh); #else typedef struct pidfh rspamd_pidfh_t; @@ -160,63 +160,72 @@ typedef struct pidfh rspamd_pidfh_t; /* * Replace %r with rcpt value and %f with from value, new string is allocated in pool */ -gchar *resolve_stat_filename (rspamd_mempool_t *pool, - gchar *pattern, - gchar *rcpt, - gchar *from); +gchar *resolve_stat_filename(rspamd_mempool_t *pool, + gchar *pattern, + gchar *rcpt, + gchar *from); const gchar * -rspamd_log_check_time (gdouble start, gdouble end, gint resolution); +rspamd_log_check_time(gdouble start, gdouble end, gint resolution); /* * File locking functions */ -gboolean rspamd_file_lock (gint fd, gboolean async); +gboolean rspamd_file_lock(gint fd, gboolean async); -gboolean rspamd_file_unlock (gint fd, gboolean async); +gboolean rspamd_file_unlock(gint fd, gboolean async); /* * Workarounds for older versions of glib */ #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 22)) -void g_ptr_array_unref (GPtrArray *array); -gboolean g_int64_equal (gconstpointer v1, gconstpointer v2); -guint g_int64_hash (gconstpointer v); +void g_ptr_array_unref(GPtrArray *array); +gboolean g_int64_equal(gconstpointer v1, gconstpointer v2); +guint g_int64_hash(gconstpointer v); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 14)) -void g_queue_clear (GQueue *queue); +void g_queue_clear(GQueue *queue); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32)) -void g_queue_free_full (GQueue *queue, GDestroyNotify free_func); +void g_queue_free_full(GQueue *queue, GDestroyNotify free_func); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 40)) -void g_ptr_array_insert (GPtrArray *array, gint index_, gpointer data); +void g_ptr_array_insert(GPtrArray *array, gint index_, gpointer data); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 30)) -GPtrArray* g_ptr_array_new_full (guint reserved_size, - GDestroyNotify element_free_func); +GPtrArray *g_ptr_array_new_full(guint reserved_size, + GDestroyNotify element_free_func); #endif #if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 32)) -const gchar *g_environ_getenv (gchar **envp, const gchar *variable); +const gchar *g_environ_getenv(gchar **envp, const gchar *variable); #endif /* * Convert milliseconds to timeval fields */ -#define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = \ - ((msec) - (tv)->tv_sec * 1000) * 1000; \ -} while (0) -#define double_to_tv(dbl, tv) do { (tv)->tv_sec = (int)(dbl); (tv)->tv_usec = \ - ((dbl) - (int)(dbl)) * 1000 * 1000; \ -} while (0) -#define double_to_ts(dbl, ts) do { (ts)->tv_sec = (int)(dbl); (ts)->tv_nsec = \ - ((dbl) - (int)(dbl)) * 1e9; \ -} while (0) +#define msec_to_tv(msec, tv) \ + do { \ + (tv)->tv_sec = (msec) / 1000; \ + (tv)->tv_usec = \ + ((msec) - (tv)->tv_sec * 1000) * 1000; \ + } while (0) +#define double_to_tv(dbl, tv) \ + do { \ + (tv)->tv_sec = (int) (dbl); \ + (tv)->tv_usec = \ + ((dbl) - (int) (dbl)) * 1000 * 1000; \ + } while (0) +#define double_to_ts(dbl, ts) \ + do { \ + (ts)->tv_sec = (int) (dbl); \ + (ts)->tv_nsec = \ + ((dbl) - (int) (dbl)) * 1e9; \ + } while (0) #define tv_to_msec(tv) ((tv)->tv_sec * 1000LLU + (tv)->tv_usec / 1000LLU) -#define tv_to_double(tv) ((double)(tv)->tv_sec + (tv)->tv_usec / 1.0e6) -#define ts_to_usec(ts) ((ts)->tv_sec * 1000000LLU + \ - (ts)->tv_nsec / 1000LLU) -#define ts_to_double(tv) ((double)(tv)->tv_sec + (tv)->tv_nsec / 1.0e9) +#define tv_to_double(tv) ((double) (tv)->tv_sec + (tv)->tv_usec / 1.0e6) +#define ts_to_usec(ts) ((ts)->tv_sec * 1000000LLU + \ + (ts)->tv_nsec / 1000LLU) +#define ts_to_double(tv) ((double) (tv)->tv_sec + (tv)->tv_nsec / 1.0e9) /** * Try to allocate a file on filesystem (using fallocate or posix_fallocate) @@ -225,7 +234,7 @@ const gchar *g_environ_getenv (gchar **envp, const gchar *variable); * @param len length to allocate * @return -1 in case of failure */ -gint rspamd_fallocate (gint fd, off_t offset, off_t len); +gint rspamd_fallocate(gint fd, off_t offset, off_t len); /** * Utils for working with threads to be compatible with all glib versions @@ -243,25 +252,25 @@ typedef struct rspamd_mutex_s { * Create new mutex * @return mutex or NULL */ -rspamd_mutex_t *rspamd_mutex_new (void); +rspamd_mutex_t *rspamd_mutex_new(void); /** * Lock mutex * @param mtx */ -void rspamd_mutex_lock (rspamd_mutex_t *mtx); +void rspamd_mutex_lock(rspamd_mutex_t *mtx); /** * Unlock mutex * @param mtx */ -void rspamd_mutex_unlock (rspamd_mutex_t *mtx); +void rspamd_mutex_unlock(rspamd_mutex_t *mtx); /** * Clear rspamd mutex * @param mtx */ -void rspamd_mutex_free (rspamd_mutex_t *mtx); +void rspamd_mutex_free(rspamd_mutex_t *mtx); /** * Deep copy of one hash table to another @@ -271,10 +280,10 @@ void rspamd_mutex_free (rspamd_mutex_t *mtx); * @param value_copy_func function called to copy or modify values (or NULL) * @param ud user data for copy functions */ -void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, - gpointer (*key_copy_func) (gconstpointer data, gpointer ud), - gpointer (*value_copy_func) (gconstpointer data, gpointer ud), - gpointer ud); +void rspamd_hash_table_copy(GHashTable *src, GHashTable *dst, + gpointer (*key_copy_func)(gconstpointer data, gpointer ud), + gpointer (*value_copy_func)(gconstpointer data, gpointer ud), + gpointer ud); /** @@ -296,105 +305,105 @@ void rspamd_hash_table_copy (GHashTable *src, GHashTable *dst, * @param key unused key * @return */ -gint rspamd_read_passphrase_with_prompt (const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key); +gint rspamd_read_passphrase_with_prompt(const gchar *prompt, gchar *buf, gint size, bool echo, gpointer key); /** * Portably return the current clock ticks as seconds * @return */ -gdouble rspamd_get_ticks (gboolean rdtsc_ok); +gdouble rspamd_get_ticks(gboolean rdtsc_ok); /** * Portably return the current virtual clock ticks as seconds * @return */ -gdouble rspamd_get_virtual_ticks (void); +gdouble rspamd_get_virtual_ticks(void); /** * Return the real timestamp as unixtime */ -gdouble rspamd_get_calendar_ticks (void); +gdouble rspamd_get_calendar_ticks(void); /** * Special utility to help array freeing in rspamd_mempool * @param p */ -void rspamd_ptr_array_free_hard (gpointer p); +void rspamd_ptr_array_free_hard(gpointer p); /** * Special utility to help array freeing in rspamd_mempool * @param p */ -void rspamd_array_free_hard (gpointer p); +void rspamd_array_free_hard(gpointer p); /** * Special utility to help GString freeing in rspamd_mempool * @param p */ -void rspamd_gstring_free_hard (gpointer p); +void rspamd_gstring_free_hard(gpointer p); /** * Special utility to help GError freeing in rspamd_mempool * @param p */ -void rspamd_gerror_free_maybe (gpointer p); +void rspamd_gerror_free_maybe(gpointer p); /** * Special utility to help GString freeing (without freeing the memory segment) in rspamd_mempool * @param p */ -void rspamd_gstring_free_soft (gpointer p); +void rspamd_gstring_free_soft(gpointer p); /** * Returns some statically initialized random hash seed * @return hash seed */ -guint64 rspamd_hash_seed (void); +guint64 rspamd_hash_seed(void); /** * Returns random hex string of the specified length * @param buf * @param len */ -void rspamd_random_hex (guchar *buf, guint64 len); +void rspamd_random_hex(guchar *buf, guint64 len); /** * Returns * @param pattern pattern to create (should end with some number of X symbols), modified by this function * @return */ -gint rspamd_shmem_mkstemp (gchar *pattern); +gint rspamd_shmem_mkstemp(gchar *pattern); /** * Return jittered time value */ -gdouble rspamd_time_jitter (gdouble in, gdouble jitter); +gdouble rspamd_time_jitter(gdouble in, gdouble jitter); /** * Return random double in range [0..1) * @return */ -gdouble rspamd_random_double (void); +gdouble rspamd_random_double(void); /** * Return random double in range [0..1) using xoroshiro128+ algorithm (not crypto secure) * @return */ -gdouble rspamd_random_double_fast (void); -gdouble rspamd_random_double_fast_seed (guint64 *seed); -guint64 rspamd_random_uint64_fast (void); +gdouble rspamd_random_double_fast(void); +gdouble rspamd_random_double_fast_seed(guint64 *seed); +guint64 rspamd_random_uint64_fast(void); /** * Seed fast rng */ -void rspamd_random_seed_fast (void); +void rspamd_random_seed_fast(void); /** * Constant time version of memcmp */ -gboolean rspamd_constant_memcmp (const void *a, const void *b, gsize len); +gboolean rspamd_constant_memcmp(const void *a, const void *b, gsize len); /** * Open file without following symlinks or special stuff @@ -403,8 +412,8 @@ gboolean rspamd_constant_memcmp (const void *a, const void *b, gsize len); * @param mode mode to open * @return fd or -1 in case of error */ -int rspamd_file_xopen (const char *fname, int oflags, guint mode, - gboolean allow_symlink); +int rspamd_file_xopen(const char *fname, int oflags, guint mode, + gboolean allow_symlink); /** * Map file without following symlinks or special stuff @@ -413,8 +422,8 @@ int rspamd_file_xopen (const char *fname, int oflags, guint mode, * @param size target size (must NOT be NULL) * @return pointer to memory (should be freed using munmap) or NULL in case of error */ -gpointer rspamd_file_xmap (const char *fname, guint mode, gsize *size, - gboolean allow_symlink); +gpointer rspamd_file_xmap(const char *fname, guint mode, gsize *size, + gboolean allow_symlink); /** * Map named shared memory segment @@ -423,15 +432,15 @@ gpointer rspamd_file_xmap (const char *fname, guint mode, gsize *size, * @param size target size (must NOT be NULL) * @return pointer to memory (should be freed using munmap) or NULL in case of error */ -gpointer rspamd_shmem_xmap (const char *fname, guint mode, - gsize *size); +gpointer rspamd_shmem_xmap(const char *fname, guint mode, + gsize *size); /** * Normalize probabilities using polynomial function * @param x probability (bias .. 1) * @return */ -gdouble rspamd_normalize_probability (gdouble x, gdouble bias); +gdouble rspamd_normalize_probability(gdouble x, gdouble bias); /** * Converts struct tm to time_t @@ -439,23 +448,23 @@ gdouble rspamd_normalize_probability (gdouble x, gdouble bias); * @param tz timezone in format (hours * 100) + minutes * @return */ -guint64 rspamd_tm_to_time (const struct tm *tm, glong tz); +guint64 rspamd_tm_to_time(const struct tm *tm, glong tz); /** * Splits unix timestamp into struct tm using GMT timezone * @param ts * @param dest */ -void rspamd_gmtime (gint64 ts, struct tm *dest); +void rspamd_gmtime(gint64 ts, struct tm *dest); /** * Split unix timestamp into struct tm using local timezone * @param ts * @param dest */ -void rspamd_localtime (gint64 ts, struct tm *dest); +void rspamd_localtime(gint64 ts, struct tm *dest); -#define PTR_ARRAY_FOREACH(ar, i, cur) for ((i) = 0; (ar) != NULL && (i) < (ar)->len && (((cur) = (__typeof__(cur))g_ptr_array_index((ar), (i))) || 1); ++(i)) +#define PTR_ARRAY_FOREACH(ar, i, cur) for ((i) = 0; (ar) != NULL && (i) < (ar)->len && (((cur) = (__typeof__(cur)) g_ptr_array_index((ar), (i))) || 1); ++(i)) /** * Compresses the input string using gzip+zlib. Old string is replaced and freed @@ -463,7 +472,7 @@ void rspamd_localtime (gint64 ts, struct tm *dest); * @param in * @return TRUE if a string has been compressed */ -gboolean rspamd_fstring_gzip (rspamd_fstring_t **in); +gboolean rspamd_fstring_gzip(rspamd_fstring_t **in); /** * Compresses the input string using gzip+zlib. Old string is replaced and freed @@ -471,7 +480,7 @@ gboolean rspamd_fstring_gzip (rspamd_fstring_t **in); * @param in * @return TRUE if a string has been compressed */ -gboolean rspamd_fstring_gunzip (rspamd_fstring_t **in); +gboolean rspamd_fstring_gunzip(rspamd_fstring_t **in); /** * Perform globbing searching for the specified path. Allow recursion, @@ -481,10 +490,10 @@ gboolean rspamd_fstring_gunzip (rspamd_fstring_t **in); * @param err * @return GPtrArray of gchar *, elements are freed when array is freed */ -GPtrArray *rspamd_glob_path (const gchar *dir, - const gchar *pattern, - gboolean recursive, - GError **err); +GPtrArray *rspamd_glob_path(const gchar *dir, + const gchar *pattern, + gboolean recursive, + GError **err); struct rspamd_counter_data { float mean; @@ -499,9 +508,9 @@ struct rspamd_counter_data { * @param alpha decay coefficient (0..1) * @return new counter value */ -float rspamd_set_counter_ema (struct rspamd_counter_data *cd, - float value, - float alpha); +float rspamd_set_counter_ema(struct rspamd_counter_data *cd, + float value, + float alpha); /** * Sets counter's data using flat moving average @@ -509,14 +518,14 @@ float rspamd_set_counter_ema (struct rspamd_counter_data *cd, * @param value new counter value * @return new counter value */ -double rspamd_set_counter (struct rspamd_counter_data *cd, - gdouble value); +double rspamd_set_counter(struct rspamd_counter_data *cd, + gdouble value); /** * Shuffle elements in an array inplace * @param ar */ -void rspamd_ptr_array_shuffle (GPtrArray *ar); +void rspamd_ptr_array_shuffle(GPtrArray *ar); enum rspamd_pbkdf_version_id { RSPAMD_PBKDF_ID_V1 = 1, @@ -543,7 +552,7 @@ extern const struct rspamd_controller_pbkdf pbkdf_list[]; * @param nelts * @return */ -float rspamd_sum_floats (float *ar, gsize *nelts); +float rspamd_sum_floats(float *ar, gsize *nelts); /** * Normalize file path removing dot sequences and repeating '/' symbols as @@ -552,9 +561,9 @@ float rspamd_sum_floats (float *ar, gsize *nelts); * @param len * @param nlen */ -void rspamd_normalize_path_inplace (gchar *path, guint len, gsize *nlen); +void rspamd_normalize_path_inplace(gchar *path, guint len, gsize *nlen); -#ifdef __cplusplus +#ifdef __cplusplus } #endif |