Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#ifndef __MX_UTIL_H__
#define __MX_UTIL_H__ 1
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <sched.h>
#include <unistd.h>
#include <sys/time.h>
#include <dirent.h>
#include "mx_log.h"
#ifdef MX_NDEBUG
# include <assert.h>
# define mx_assert_return_minus_errno(test, eno) \
assert(test)
# define mx_assert_return_NULL(test, eno) \
assert(test)
#else
# define mx_assert_return_minus_errno(test, eno) \
do {\
if (!(test)) {\
errno=(eno);\
mx_log_emerg("%s:%d:%s(): Assertion '" #test "' failed . Returning -(errno=" #eno ") [%d]: %m", __FILE__, __LINE__, __func__, -errno);\
return -errno;\
}\
} while (0)
# define mx_assert_return_NULL(test, eno) \
do {\
if (!(test)) {\
errno=(eno);\
mx_log_emerg("%s:%d:%s(): Assertion '" #test "' failed. Setting errno=" #eno " [%d] and returning NULL: %m", __FILE__, __LINE__, __func__, errno);\
return NULL;\
}\
} while (0)
#endif
#define mx_debug_value(fmt, v) mx_log_debug("mx_debug_value: " #v " = " fmt, v)
#undef mx_free_null
#define mx_free_null(a) do { free(a); (a) = NULL; } while(0)
#undef _mx_cleanup_
#define _mx_cleanup_(x) __attribute__((cleanup(x)))
static inline void __mx_free(void *ptr) {
free(*(void **)ptr);
}
static inline void __mx_fclose(FILE **ptr) {
if (*ptr)
fclose(*ptr);
}
static inline void __mx_closedir(DIR **ptr) {
if (*ptr)
closedir(*ptr);
}
#undef _mx_cleanup_free_
#define _mx_cleanup_free_ _mx_cleanup_(__mx_free)
#undef _mx_cleanup_fclose_
#define _mx_cleanup_fclose_ _mx_cleanup_(__mx_fclose)
#undef _mx_cleanup_closedir_
#define _mx_cleanup_closedir_ _mx_cleanup_(__mx_closedir)
#undef likely
#define likely(x) __builtin_expect((x),1)
#undef unlikely
#define unlikely(x) __builtin_expect((x),0)
#undef mx_streq
#define mx_streq(a, b) (strcmp((a), (b)) == 0)
#undef mx_streq_nocase
#define mx_streq_nocase(a, b) (strcasecmp((a), (b)) == 0)
#define mx_within_rate_limit_or_return(sec, ret) \
do {\
static struct timeval _sleep = {0};\
struct timeval _now;\
struct timeval _delta;\
gettimeofday(&_now, NULL);\
timersub(&_now, &_sleep, &_delta);\
if (_delta.tv_sec < (sec))\
return (ret);\
_sleep = _now;\
} while(0)
int mx_strbeginswith(char *str, const char *start, char **endptr);
int mx_stribeginswith(char *str, const char *start, char **endptr);
int mx_strbeginswithany(char *str, char **starts, char **endptr);
char *mx_strskipwhitespaces(char *str);
int mx_strtobytes(char *str, unsigned long long int *bytes);
int mx_strtoseconds(char *str, unsigned long long int *seconds);
int mx_strtominutes(char *str, unsigned long long int *minutes);
int mx_strtoul(char *str, unsigned long int *to);
int mx_strtoull(char *str, unsigned long long int *to);
int mx_strtoui(char *str, unsigned int *to);
int mx_strtou8(char *str, uint8_t *to);
int mx_strtou16(char *str, uint16_t *to);
int mx_strtou32(char *str, uint32_t *to);
int mx_strtou64(char *str, uint64_t *to);
int mx_strtol(char *str, signed long int *to);
int mx_strtoll(char *str, signed long long int *to);
int mx_strtoi(char *str, signed int *to);
int mx_strtoi8(char *str, int8_t *to);
int mx_strtoi16(char *str, int16_t *to);
int mx_strtoi32(char *str, int32_t *to);
int mx_strtoi64(char *str, int64_t *to);
void *mx_malloc_forever(size_t size);
char *mx_strdup_forever(char *str);
char *mx_vasprintf_forever(const char *fmt, va_list ap);
char *mx_asprintf_forever(const char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
char *mx_hostname(void);
char *mx_dirname(char *path);
char *mx_dirname_forever(char *path);
int mx_dup2_close_new(int oldfd, int newfd);
int mx_dup2_close_both(int oldfd, int newfd);
int mx_setenv_forever(const char *name, const char *value);
int mx_setenvf_forever(const char *name, char *fmt, ...) __attribute__ ((format(printf, 2, 3)));
int mx_open_newfile(char *fname);
int mx_read_first_line_from_file(char *fname, char **line);
int mx_strscan_ull(char **str, unsigned long long int *to);
int mx_strscan_ll(char **str, long long int *to);
char *_mx_strconcat_do(char *first, ...);
#define mx_strconcat(s, ...) _mx_strconcat_do((s), ##__VA_ARGS__, NULL)
int mx_sleep(unsigned int seconds);
int mx_sleep_nofail(unsigned int seconds);
#ifndef MX_CALLOC_FAIL_WAIT_DEFAULT
# define MX_CALLOC_FAIL_WAIT_DEFAULT 1
#endif
#define mx_calloc_forever(n, s) mx_calloc_forever_sec((n), (s), MX_CALLOC_FAIL_WAIT_DEFAULT)
void *mx_calloc_forever_sec(size_t nmemb, size_t size, unsigned int time);
char** mx_strvec_new(void);
size_t mx_strvec_length(char **strvec);
int mx_strvec_push_str(char ***strvecp, char *str);
int mx_strvec_push_strvec(char*** strvecp, char **strvec);
char* mx_strvec_to_str(char **strvec);
char** mx_strvec_from_str(char *str);
void mx_strvec_free(char **strvec);
char* mx_strvec_join(char *sep,char **strvec);
char* mx_cpuset_to_str(cpu_set_t* cpuset_ptr);
int mx_str_to_cpuset(cpu_set_t* cpuset_ptr,char *str);
int mx_mkdir_p(char *path, mode_t mode);
int mx_daemon(int nochdir, int noclose);
void _mx_sort_linked_list(void **list, int (*cmp)(void *o1,void *o2), void ** (*getnextptr)(void *o));
#define mx_sort_linked_list(list,cmp,getnextptr) _mx_sort_linked_list((void **)(list),(int (*)(void *,void *))(cmp),(void ** (*)(void *))(getnextptr))
unsigned long mx_df(const char *path);
int mx_fs_get_sizes(const char *path, unsigned long *free, unsigned long *total);
time_t mx_clock_boottime(void);
int mx_call_external(char *helper, char **argv);
char *mx_pipe_external(char *args, char **argv);
void mx_die(const char *restrict fmt, ...) __attribute__ ((noreturn,format (printf, 1, 2)));
#if __GLIBC__ <2 || __GLIBC__ == 2 && __GLIBC_MINOR__ < 34
#include <sys/syscall.h>
#define close_range(first, last, flags) syscall(SYS_close_range, first, last, flags)
#endif
#endif