refactoring, const

This commit is contained in:
Maxim Devaev
2022-07-20 06:42:18 +03:00
parent 5c747a5b5d
commit 2c9334d53f
35 changed files with 246 additions and 258 deletions

View File

@@ -54,7 +54,7 @@ void us_base64_encode(const uint8_t *data, size_t size, char **encoded, size_t *
OCTET(octet_c);
# undef OCTET
unsigned triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
const unsigned triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;
# define ENCODE(_offset) (*encoded)[encoded_index++] = _ENCODING_TABLE[(triple >> _offset * 6) & 0x3F]
ENCODE(3);

View File

@@ -50,7 +50,7 @@ void us_frame_set_data(us_frame_s *frame, const uint8_t *data, size_t size) {
}
void us_frame_append_data(us_frame_s *frame, const uint8_t *data, size_t size) {
size_t new_used = frame->used + size;
const size_t new_used = frame->used + size;
us_frame_realloc_data(frame, new_used);
memcpy(frame->data + frame->used, data, size);
frame->used = new_used;

View File

@@ -40,7 +40,7 @@ us_memsink_s *us_memsink_init(
US_LOG_INFO("Using %s-sink: %s", name, obj);
mode_t mask = umask(0);
const mode_t mask = umask(0);
sink->fd = shm_open(sink->obj, (server ? O_RDWR | O_CREAT : O_RDWR), mode);
umask(mask);
if (sink->fd == -1) {
@@ -107,7 +107,7 @@ bool us_memsink_server_check(us_memsink_s *sink, const us_frame_s *frame) {
return true;
}
bool has_clients = (sink->mem->last_client_ts + sink->client_ttl > us_get_now_monotonic());
const bool has_clients = (sink->mem->last_client_ts + sink->client_ttl > us_get_now_monotonic());
atomic_store(&sink->has_clients, has_clients);
if (flock(sink->fd, LOCK_UN) < 0) {

View File

@@ -73,12 +73,12 @@ extern char **environ;
#ifdef HAS_PDEATHSIG
INLINE int us_process_track_parent_death(void) {
pid_t parent = getppid();
const pid_t parent = getppid();
int signum = SIGTERM;
# if defined(__linux__)
int retval = prctl(PR_SET_PDEATHSIG, signum);
const int retval = prctl(PR_SET_PDEATHSIG, signum);
# elif defined(__FreeBSD__)
int retval = procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum);
const int retval = procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signum);
# else
# error WTF?
# endif
@@ -91,7 +91,6 @@ INLINE int us_process_track_parent_death(void) {
US_LOG_PERROR("The parent process %d is already dead", parent);
return -1;
}
return 0;
}
#endif
@@ -131,16 +130,14 @@ INLINE void us_process_set_name_prefix(int argc, char *argv[], const char *prefi
#endif
INLINE void us_process_notify_parent(void) {
pid_t parent = getppid();
const pid_t parent = getppid();
if (kill(parent, SIGUSR2) < 0) {
US_LOG_PERROR("Can't send SIGUSR2 to the parent process %d", parent);
}
}
INLINE void us_process_suicide(void) {
pid_t pid = getpid();
const pid_t pid = getpid();
if (kill(pid, SIGTERM) < 0) {
US_LOG_PERROR("Can't send SIGTERM to own pid %d", pid);
}

View File

@@ -104,17 +104,17 @@ INLINE void us_thread_get_name(char *name) { // Always required for logging
#endif
#if defined(__linux__)
pid_t tid = syscall(SYS_gettid);
const pid_t tid = syscall(SYS_gettid);
#elif defined(__FreeBSD__)
pid_t tid = syscall(SYS_thr_self);
const pid_t tid = syscall(SYS_thr_self);
#elif defined(__OpenBSD__)
pid_t tid = syscall(SYS_getthrid);
const pid_t tid = syscall(SYS_getthrid);
#elif defined(__NetBSD__)
pid_t tid = syscall(SYS__lwp_self);
const pid_t tid = syscall(SYS__lwp_self);
#elif defined(__DragonFly__)
pid_t tid = syscall(SYS_lwp_gettid);
const pid_t tid = syscall(SYS_lwp_gettid);
#else
pid_t tid = 0; // Makes cppcheck happy
const pid_t tid = 0; // Makes cppcheck happy
# warning gettid() not implemented
#endif
assert(snprintf(name, US_MAX_THREAD_NAME, "tid=%d", tid) > 0);

View File

@@ -53,8 +53,8 @@
#define INLINE inline __attribute__((always_inline))
#define UNUSED __attribute__((unused))
#define US_CALLOC(x_dest, x_nmemb) assert(((x_dest) = calloc((x_nmemb), sizeof(*(x_dest)))))
#define US_REALLOC(x_dest, x_nmemb) assert(((x_dest) = realloc((x_dest), (x_nmemb) * sizeof(*(x_dest)))))
#define US_CALLOC(x_dest, x_nmemb) assert(((x_dest) = calloc((x_nmemb), sizeof(*(x_dest)))) != NULL)
#define US_REALLOC(x_dest, x_nmemb) assert(((x_dest) = realloc((x_dest), (x_nmemb) * sizeof(*(x_dest)))) != NULL)
#define US_DELETE(x_dest, x_free) { if (x_dest) { x_free(x_dest); } }
#define US_MEMSET_ZERO(x_obj) memset(&(x_obj), 0, sizeof(x_obj))
@@ -63,6 +63,12 @@
#define US_ARRAY_LEN(x_array) (sizeof(x_array) / sizeof((x_array)[0]))
INLINE char *us_strdup(const char *str) {
char *const new = strdup(str);
assert(new != NULL);
return new;
}
INLINE const char *us_bool_to_string(bool flag) {
return (flag ? "true" : "false");
}
@@ -131,7 +137,7 @@ INLINE uint64_t us_get_now_monotonic_u64(void) {
#undef _X_CLOCK_MONOTONIC
INLINE uint64_t us_get_now_id(void) {
uint64_t now = us_get_now_monotonic_u64();
const uint64_t now = us_get_now_monotonic_u64();
return (uint64_t)us_triple_u32(now) | ((uint64_t)us_triple_u32(now + 12345) << 32);
}
@@ -162,7 +168,7 @@ INLINE long double us_timespec_to_ld(const struct timespec *ts) {
}
INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) {
long double deadline_ts = us_get_now_monotonic() + timeout;
const long double deadline_ts = us_get_now_monotonic() + timeout;
int retval = -1;
while (true) {
@@ -178,10 +184,10 @@ INLINE int us_flock_timedwait_monotonic(int fd, long double timeout) {
}
INLINE char *us_errno_to_string(int error, char *buf, size_t size) {
assert(buf);
assert(buf != NULL);
assert(size > 0);
locale_t locale = newlocale(LC_MESSAGES_MASK, "C", NULL);
char *str = "!!! newlocale() error !!!";
const char *str = "!!! newlocale() error !!!";
strncpy(buf, (locale ? strerror_l(error, locale) : str), size - 1);
buf[size - 1] = '\0';
if (locale) {