hewei.it 5 years ago
parent
commit
27cfd65570
4 changed files with 111 additions and 46 deletions
  1. 91 36
      base/hplatform.h
  2. 4 3
      docs/apis.md
  3. 1 1
      event/hloop.c
  4. 15 6
      hexport.h

+ 91 - 36
base/hplatform.h

@@ -44,24 +44,6 @@
     #define OS_UNIX
 #endif
 
-// CC
-// _MSC_VER
-#ifdef _MSC_VER
-#pragma warning (disable: 4100) // unused param
-#pragma warning (disable: 4251) // STL dll
-#pragma warning (disable: 4819) // Unicode
-#pragma warning (disable: 4996) // _CRT_SECURE_NO_WARNINGS
-#endif
-
-// __MINGW32__
-// __GNUC__
-#ifdef __GNUC__
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE 1
-#endif
-#endif
-// __clang__
-
 // ARCH
 #if defined(__i386) || defined(__i386__) || defined(_M_IX86)
     #define ARCH_X86
@@ -73,23 +55,78 @@
     #define ARCH_ARM
 #elif defined(__aarch64__) || defined(__ARM64__)
     #define ARCH_ARM64
+#else
+    #define ARCH_UNKNOWN
+    #warning "Unknown hardware architecture!"
 #endif
 
-// ENDIAN
-#ifndef BIG_ENDIAN
-    #define BIG_ENDIAN      4321
-#endif
-#ifndef LITTLE_ENDIAN
-    #define LITTLE_ENDIAN   1234
+// COMPILER
+#if defined (_MSC_VER)
+#define COMPILER_MSVC
+
+#if (_MSC_VER < 1200) // Visual C++ 6.0
+#define MSVS_VERSION    1998
+#define MSVC_VERSION    60
+#elif (_MSC_VER >= 1200) && (_MSC_VER < 1300) // Visual Studio 2002, MSVC++ 7.0
+#define MSVS_VERSION    2002
+#define MSVC_VERSION    70
+#elif (_MSC_VER >= 1300) && (_MSC_VER < 1400) // Visual Studio 2003, MSVC++ 7.1
+#define MSVS_VERSION    2003
+#define MSVC_VERSION    71
+#elif (_MSC_VER >= 1400) && (_MSC_VER < 1500) // Visual Studio 2005, MSVC++ 8.0
+#define MSVS_VERSION    2005
+#define MSVC_VERSION    80
+#elif (_MSC_VER >= 1500) && (_MSC_VER < 1600) // Visual Studio 2008, MSVC++ 9.0
+#define MSVS_VERSION    2008
+#define MSVC_VERSION    90
+#elif (_MSC_VER >= 1600) && (_MSC_VER < 1700) // Visual Studio 2010, MSVC++ 10.0
+#define MSVS_VERSION    2010
+#define MSVC_VERSION    100
+#elif (_MSC_VER >= 1700) && (_MSC_VER < 1800) // Visual Studio 2012, MSVC++ 11.0
+#define MSVS_VERSION    2012
+#define MSVC_VERSION    110
+#elif (_MSC_VER >= 1800) && (_MSC_VER < 1900) // Visual Studio 2013, MSVC++ 12.0
+#define MSVS_VERSION    2013
+#define MSVC_VERSION    120
+#elif (_MSC_VER >= 1900) && (_MSC_VER < 1910) // Visual Studio 2015, MSVC++ 14.0
+#define MSVS_VERSION    2015
+#define MSVC_VERSION    140
+#elif (_MSC_VER >= 1910) && (_MSC_VER < 1920) // Visual Studio 2017, MSVC++ 15.0
+#define MSVS_VERSION    2017
+#define MSVC_VERSION    150
+#elif (_MSC_VER >= 1920) && (_MSC_VER < 2000) // Visual Studio 2019, MSVC++ 16.0
+#define MSVS_VERSION    2019
+#define MSVC_VERSION    160
 #endif
-#define NET_ENDIAN     BIG_ENDIAN
 
-#ifndef BYTE_ORDER
-#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
-    #define BYTE_ORDER LITTLE_ENDIAN
-#elif defined(__ARMEB__)
-    #define BYTE_ORDER BIG_ENDIAN
+#undef  HAVE_STDATOMIC_H
+#define HAVE_STDATOMIC_H        0
+#undef  HAVE_SYS_TIME_H
+#define HAVE_SYS_TIME_H         0
+#undef  HAVE_PTHREAD_H
+#define HAVE_PTHREAD_H          0
+
+#pragma warning (disable: 4018) // signed/unsigned comparison
+#pragma warning (disable: 4100) // unused param
+#pragma warning (disable: 4251) // STL dll
+#pragma warning (disable: 4819) // Unicode
+#pragma warning (disable: 4996) // _CRT_SECURE_NO_WARNINGS
+
+#elif defined(__MINGW32__) || defined(__MINGW64__)
+#define COMPILER_MINGW
+
+#elif defined(__GNUC__)
+#define COMPILER_GCC
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE 1
 #endif
+
+#elif defined(__clang__)
+#define COMPILER_CLANG
+
+#else
+#warning "Untested compiler!"
 #endif
 
 // headers
@@ -149,6 +186,26 @@
     #define strnicmp    strncasecmp
 #endif
 
+// ENDIAN
+#ifndef BIG_ENDIAN
+#define BIG_ENDIAN      4321
+#endif
+#ifndef LITTLE_ENDIAN
+#define LITTLE_ENDIAN   1234
+#endif
+#ifndef NET_ENDIAN
+#define NET_ENDIAN      BIG_ENDIAN
+#endif
+
+// BYTE_ORDER
+#ifndef BYTE_ORDER
+#if defined(ARCH_X86) || defined(ARCH_X86_64) || defined(__ARMEL__)
+#define BYTE_ORDER      LITTLE_ENDIAN
+#elif defined(__ARMEB__)
+#define BYTE_ORDER      BIG_ENDIAN
+#endif
+#endif
+
 // ANSI C
 #include <assert.h>
 #include <stddef.h>
@@ -220,18 +277,16 @@ typedef void (*procedure_t)(void* userdata);
 #include <sys/stat.h>
 #endif
 
-#if HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-
-#ifndef _MSC_VER
 #if HAVE_SYS_TIME_H
 #include <sys/time.h>   // for gettimeofday
 #endif
 
+#if HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
 #if HAVE_PTHREAD_H
 #include <pthread.h>
 #endif
-#endif
 
 #endif // HV_PLATFORM_H_

+ 4 - 3
docs/apis.md

@@ -5,8 +5,8 @@
 ### hplatform.h
 - OS: OS_WIN, OS_UNIX (OS_LINUX, OS_ANDROID, OS_DARWIN ...)
 - ARCH: ARCH_X86, ARCH_X64, ARCH_ARM, ARCH_ARM64
+- COMPILER: COMPILER_MSVC, COMPILER_MINGW, COMPILER_GCC, COMPILER_CLANG
 - BYTE_ORDER: BIG_ENDIAN, LITTLE_ENDIAN
-- HV_EXPORT
 - stdbool.h: bool, true, false
 - stdint.h: int8_t, int16_t, int32_t, int64_t
 - var
@@ -16,10 +16,11 @@
 
 ### hexport.h
 - HV_EXPORT
-- HV_SOURCE, HV_STATICLIB, HV_EXPORTS
+- HV_SOURCE, HV_STATICLIB, HV_DYNAMICLIB
+- HV_DEPRECATED
+- HV_UNUSED
 - EXTERN_C, BEGIN_EXTERN_C, END_EXTERN_C
 - BEGIN_NAMESPACE, END_NAMESPACE, USING_NAMESPACE
-- DEPRECATED
 - DEFAULT
 - ENUM, STRUCT
 - IN, OUT, INOUT

+ 1 - 1
event/hloop.c

@@ -123,8 +123,8 @@ static int hloop_process_events(hloop_t* loop) {
 
     // calc blocktime
     int32_t blocktime = MAX_BLOCK_TIME;
-    hloop_update_time(loop);
     if (loop->timers.root) {
+        hloop_update_time(loop);
         uint64_t next_min_timeout = TIMER_ENTRY(loop->timers.root)->next_timeout;
         int64_t blocktime_us = next_min_timeout - hloop_now_hrtime(loop);
         if (blocktime_us <= 0) goto process_timers;

+ 15 - 6
hexport.h

@@ -16,13 +16,22 @@
     #define HV_EXPORT
 #endif
 
-// DEPRECATED
-#if defined(__GNUC__) || defined(__clang__)
-    #define DEPRECATED  __attribute__((visibility("deprecated")))
-    #define UNUSED      __attribute__((visibility("unused")))
+// HV_DEPRECATED
+#if defined(HV_NO_DEPRECATED)
+#define HV_DEPRECATED
+#elif defined(_GNUC_) || defined(__clang__)
+#define HV_DEPRECATED   __attribute__((deprecated))
+#elif defined(_MSC_VER)
+#define HV_DEPRECATED   __declspec(deprecated)
+#else
+#define HV_DEPRECATED
+#endif
+
+// HV_UNUSED
+#if defined(__GNUC__)
+    #define HV_UNUSED   __attribute__((visibility("unused")))
 #else
-    #define DEPRECATED
-    #define UNUSED(v)   ((void)(v))
+    #define HV_UNUSED
 #endif
 
 // @param[IN | OUT | INOUT]