2
0
Эх сурвалжийг харах

Use offsetof to compute ENetProtocolHeader's sentTime offset (#276)

* Use the standard offsetof C library function to
compute the ENetProtocolHeader sentTime offset.

* The build system now checks for the C offsetof macro, and
is expanded into a new enet macro for checking the offset
of a field.
ChrisTrenkamp 1 сар өмнө
parent
commit
8be2368a80
4 өөрчлөгдсөн 24 нэмэгдсэн , 3 устгасан
  1. 10 0
      CMakeLists.txt
  2. 1 0
      configure.ac
  3. 10 0
      include/enet/utility.h
  4. 3 3
      protocol.c

+ 10 - 0
CMakeLists.txt

@@ -14,6 +14,13 @@ check_function_exists("gethostbyname_r" HAS_GETHOSTBYNAME_R)
 check_function_exists("gethostbyaddr_r" HAS_GETHOSTBYADDR_R)
 check_function_exists("inet_pton" HAS_INET_PTON)
 check_function_exists("inet_ntop" HAS_INET_NTOP)
+check_c_source_compiles("
+    #include <stddef.h>
+    struct S { int a; double b; };
+    int main() {
+    return (int)offsetof(struct S, b);
+    }
+" HAS_OFFSETOF)
 check_struct_has_member("struct msghdr" "msg_flags" "sys/types.h;sys/socket.h" HAS_MSGHDR_FLAGS)
 set(CMAKE_EXTRA_INCLUDE_FILES "sys/types.h" "sys/socket.h")
 check_type_size("socklen_t" HAS_SOCKLEN_T BUILTIN_TYPES_ONLY)
@@ -48,6 +55,9 @@ endif()
 if(HAS_INET_NTOP)
     add_definitions(-DHAS_INET_NTOP=1)
 endif()
+if(HAS_OFFSETOF)
+    add_definitions(-DHAS_OFFSETOF=1)
+endif()
 if(HAS_MSGHDR_FLAGS)
     add_definitions(-DHAS_MSGHDR_FLAGS=1)
 endif()

+ 1 - 0
configure.ac

@@ -15,6 +15,7 @@ AC_CHECK_FUNC(poll, [AC_DEFINE(HAS_POLL)])
 AC_CHECK_FUNC(fcntl, [AC_DEFINE(HAS_FCNTL)])
 AC_CHECK_FUNC(inet_pton, [AC_DEFINE(HAS_INET_PTON)])
 AC_CHECK_FUNC(inet_ntop, [AC_DEFINE(HAS_INET_NTOP)])
+AC_CHECK_DECLS(offsetof, [AC_DEFINE(HAS_OFFSETOF)], [], [#include <stddef.h>])
 
 AC_CHECK_MEMBER(struct msghdr.msg_flags, [AC_DEFINE(HAS_MSGHDR_FLAGS)], , [#include <sys/socket.h>])
 

+ 10 - 0
include/enet/utility.h

@@ -5,9 +5,19 @@
 #ifndef __ENET_UTILITY_H__
 #define __ENET_UTILITY_H__
 
+#ifdef HAS_OFFSETOF
+#include <stddef.h>
+#endif
+
 #define ENET_MAX(x, y) ((x) > (y) ? (x) : (y))
 #define ENET_MIN(x, y) ((x) < (y) ? (x) : (y))
 #define ENET_DIFFERENCE(x, y) ((x) < (y) ? (y) - (x) : (x) - (y))
 
+#ifdef HAS_OFFSETOF
+#define ENET_OFFSETOF(str, field) (offsetof(str, field))
+#else
+#define ENET_OFFSETOF(str, field) ((size_t) & ((str *) 0) -> field)
+#endif
+
 #endif /* __ENET_UTILITY_H__ */
 

+ 3 - 3
protocol.c

@@ -1020,7 +1020,7 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
     enet_uint16 peerID, flags;
     enet_uint8 sessionID;
 
-    if (host -> receivedDataLength < (size_t) & ((ENetProtocolHeader *) 0) -> sentTime)
+    if (host -> receivedDataLength < ENET_OFFSETOF(ENetProtocolHeader, sentTime))
       return 0;
 
     header = (ENetProtocolHeader *) host -> receivedData;
@@ -1030,7 +1030,7 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)
     flags = peerID & ENET_PROTOCOL_HEADER_FLAG_MASK;
     peerID &= ~ (ENET_PROTOCOL_HEADER_FLAG_MASK | ENET_PROTOCOL_HEADER_SESSION_MASK);
 
-    headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : (size_t) & ((ENetProtocolHeader *) 0) -> sentTime);
+    headerSize = (flags & ENET_PROTOCOL_HEADER_FLAG_SENT_TIME ? sizeof (ENetProtocolHeader) : ENET_OFFSETOF(ENetProtocolHeader, sentTime));
     if (host -> checksum != NULL)
       headerSize += sizeof (enet_uint32);
 
@@ -1682,7 +1682,7 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch
             host -> buffers -> dataLength = sizeof (ENetProtocolHeader);
         }
         else
-          host -> buffers -> dataLength = (size_t) & ((ENetProtocolHeader *) 0) -> sentTime;
+          host -> buffers -> dataLength = ENET_OFFSETOF(ENetProtocolHeader, sentTime);
 
         shouldCompress = 0;
         if (host -> compressor.context != NULL && host -> compressor.compress != NULL)