Skip to content

Commit

Permalink
fix: improve compiler compatibility and warnings (#647)
Browse files Browse the repository at this point in the history
* fix: improve compatibility checks for atomic operations

- Replace GNUC macro check with __GNUC_PREREQ(4, 1) for more precise version control
- Add fallback definition for __GNUC_PREREQ macro when not available

* fix: add explicit type casting in gettick_ms()

- Add explicit (unsigned int) casting to tv_sec and tv_nsec/tv_usec calculations to prevent potential integer overflow

* fix: suppress compiler warnings for unused variable and function

- Add (void) cast and inline qualifier to eliminate unused warnings

---------

Co-authored-by: tayne3 <[email protected]>
  • Loading branch information
tayne3 and tayne3 authored Dec 20, 2024
1 parent 15df63d commit fd2ad28
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/hatomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static inline bool atomic_flag_test_and_set(atomic_flag* p) {
#define ATOMIC_INC InterlockedIncrement
#define ATOMIC_DEC InterlockedDecrement

#elif defined(__GNUC__)
#elif __GNUC_PREREQ(4, 1)

#define ATOMIC_FLAG_TEST_AND_SET atomic_flag_test_and_set
static inline bool atomic_flag_test_and_set(atomic_flag* p) {
Expand Down
4 changes: 4 additions & 0 deletions base/hplatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
#warning "Untested compiler!"
#endif

#ifndef __GNUC_PREREQ
#define __GNUC_PREREQ(a, b) 0
#endif

// headers
#ifdef OS_WIN
#ifndef _WIN32_WINNT
Expand Down
4 changes: 2 additions & 2 deletions base/htime.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ unsigned int gettick_ms() {
#elif HAVE_CLOCK_GETTIME
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
return (unsigned int)ts.tv_sec * 1000 + (unsigned int)ts.tv_nsec / 1000000;
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
return (unsigned int)tv.tv_sec * 1000 + (unsigned int)tv.tv_usec / 1000;
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions event/hloop.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ int hloop_process_events(hloop_t* loop, int timeout_ms) {
// blocktime, nios, loop->nios, ntimers, loop->ntimers, nidles, loop->nidles,
// loop->nactives, npendings, ncbs);
return ncbs;
(void)nios;
}

#ifdef DEBUG
static void hloop_stat_timer_cb(htimer_t* timer) {
hloop_t* loop = timer->loop;
// hlog_set_level(LOG_LEVEL_DEBUG);
Expand All @@ -198,6 +200,7 @@ static void hloop_stat_timer_cb(htimer_t* timer) {
(unsigned long long)loop->loop_cnt,
loop->nactives, loop->nios, loop->ntimers, loop->nidles);
}
#endif

static void eventfd_read_cb(hio_t* io, void* buf, int readbytes) {
hloop_t* loop = io->loop;
Expand Down
2 changes: 1 addition & 1 deletion ssl/wintls.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const char* hssl_backend()
return "schannel";
}

static PCCERT_CONTEXT getservercert(const char* path)
static inline PCCERT_CONTEXT getservercert(const char* path)
{
/*
According to the information I searched from the internet, it is not possible to specify an x509 private key and certificate using the
Expand Down

0 comments on commit fd2ad28

Please sign in to comment.