Parcourir la source

fix: improve compiler compatibility and warnings (#647)

* 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 <tayne3@dingtalk.com>
Tayne il y a 1 an
Parent
commit
fd2ad2847c
5 fichiers modifiés avec 11 ajouts et 4 suppressions
  1. 1 1
      base/hatomic.h
  2. 4 0
      base/hplatform.h
  3. 2 2
      base/htime.c
  4. 3 0
      event/hloop.c
  5. 1 1
      ssl/wintls.c

+ 1 - 1
base/hatomic.h

@@ -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) {

+ 4 - 0
base/hplatform.h

@@ -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

+ 2 - 2
base/htime.c

@@ -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
 }
 

+ 3 - 0
event/hloop.c

@@ -187,8 +187,10 @@ process_timers:
     //         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);
@@ -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;

+ 1 - 1
ssl/wintls.c

@@ -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