From ab2163f6c9e18419739d0fc03f0b08e0f36e2029 Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Tue, 17 Mar 2015 18:59:05 +0100 Subject: [PATCH 1/2] changed integer types to their correct enum types In a few locations integer typed variables where used where their corresponding enum would be a better choice. --- ape_http_parser.c | 12 ++++++------ ape_socket.c | 2 +- ape_socket.h | 19 +++++++++---------- ape_timers.h | 2 +- ape_timers_next.h | 6 +++--- 5 files changed, 20 insertions(+), 21 deletions(-) diff --git a/ape_http_parser.c b/ape_http_parser.c index 607a4e2..d07561a 100644 --- a/ape_http_parser.c +++ b/ape_http_parser.c @@ -72,7 +72,7 @@ typedef enum classes { } parser_class; -static int ascii_class[128] = { +static parser_class ascii_class[128] = { /* This array maps the 128 ASCII characters into character classes. The remaining Unicode characters should be mapped to C_ETC. @@ -126,7 +126,7 @@ typedef enum actions { } parser_actions; -static int state_transition_table[NR_STATES][NR_CLASSES] = { +static int8_t /*parser_state*/ state_transition_table[NR_STATES][NR_CLASSES] = { /* nul white etc ABDF | space | \r\n : , " \ / + - . ? % 09 | * | E G T P H O S C N L _ */ @@ -211,7 +211,7 @@ int parse_http_char(struct _http_parser *parser, const unsigned char c) #define HTTP_BODY_AS_ENDED() (HTTP_ISBODYCONTENT() && --parser->cl == 0 && (parser->rx |= HTTP_FLG_READY, 1)) parser_class c_classe; - int8_t state; + int8_t /*parser_state*/ state; unsigned char ch; if (c >= 128) { @@ -376,7 +376,7 @@ int parse_http_char(struct _http_parser *parser, const unsigned char c) parser->state = R9; break; case RC: /* HTTP response code */ - if ((parser->rcode = (parser->rcode*10) + (c - '0')) > MAX_RCODE) { + if ((parser->rcode = (uint16_t) ((parser->rcode*10) + (c - '0')) > MAX_RCODE)) { return 0; } parser->state = RN; @@ -400,7 +400,7 @@ static int parse_callback(void **ctx, callback_type type, int value, uint32_t st { switch(type) { case HTTP_METHOD: - switch(value) { + switch( (http_method_t) value) { case HTTP_GET: printf("GET method detected\n"); break; @@ -447,7 +447,7 @@ static int parse_callback(void **ctx, callback_type type, int value, uint32_t st int main() { int length = 0, i; - struct _http_parser p; + http_parser p; /* Process BYTE_GET/POST opti check before running the parser */ diff --git a/ape_socket.c b/ape_socket.c index d5c17f1..a1e2417 100644 --- a/ape_socket.c +++ b/ape_socket.c @@ -103,7 +103,7 @@ __inline static void ape_socket_release_data(unsigned char *data, ape_socket_dat } } -ape_socket *APE_socket_new(uint8_t pt, int from, ape_global *ape) +ape_socket *APE_socket_new(enum ape_socket_proto pt, int from, ape_global *ape) { int sock = from, proto = SOCK_STREAM; diff --git a/ape_socket.h b/ape_socket.h index e34d72a..a2c3436 100644 --- a/ape_socket.h +++ b/ape_socket.h @@ -91,19 +91,19 @@ struct iovec enum ape_socket_flags { APE_SOCKET_WOULD_BLOCK = (1 << 0), APE_SOCKET_CORK = (1 << 1) -}; +} __attribute__ ((__packed__)); enum ape_socket_proto { APE_SOCKET_PT_TCP, APE_SOCKET_PT_UDP, APE_SOCKET_PT_SSL -}; +} __attribute__ ((__packed__)); enum ape_socket_type { APE_SOCKET_TP_UNKNOWN, APE_SOCKET_TP_SERVER, APE_SOCKET_TP_CLIENT -}; +} __attribute__ ((__packed__)); enum ape_socket_state { APE_SOCKET_ST_ONLINE, @@ -111,8 +111,7 @@ enum ape_socket_state { APE_SOCKET_ST_PENDING, APE_SOCKET_ST_OFFLINE, APE_SOCKET_ST_SHUTDOWN -}; - +} __attribute__ ((__packed__)); typedef enum _ape_socket_data_autorelease { APE_DATA_STATIC, @@ -174,10 +173,10 @@ struct _ape_socket { struct _ape_dns_cb_argv *dns_state; struct { - uint8_t flags; - uint8_t proto; - uint8_t type; - uint8_t state; + enum ape_socket_flags flags; + enum ape_socket_proto proto; + enum ape_socket_type type; + enum ape_socket_state state; } states; #ifdef _HAVE_SSL_SUPPORT @@ -206,7 +205,7 @@ struct _ape_socket_packet { extern "C" { #endif -ape_socket *APE_socket_new(uint8_t pt, int from, ape_global *ape); +ape_socket *APE_socket_new(enum ape_socket_proto pt, int from, ape_global *ape); int APE_socket_listen(ape_socket *socket, uint16_t port, const char *local_ip, int defer_accept, int reuse_port); diff --git a/ape_timers.h b/ape_timers.h index 99d4d3c..d56de7b 100644 --- a/ape_timers.h +++ b/ape_timers.h @@ -35,7 +35,7 @@ struct _ticks_callback int delta; int times; unsigned int identifier; - int flag; + ape_timer_flags flag; void *func; void *params; diff --git a/ape_timers_next.h b/ape_timers_next.h index 7e527b4..ded70b0 100644 --- a/ape_timers_next.h +++ b/ape_timers_next.h @@ -24,15 +24,15 @@ typedef int (*timer_callback)(void *arg); -enum { +typedef enum { APE_TIMER_IS_PROTECTED = 1 << 0, APE_TIMER_IS_CLEARED = 1 << 1 -}; +} ape_timerng_flags; typedef struct _ape_timer { int identifier; - int flags; + ape_timerng_flags flags; uint64_t ticks_needs; uint64_t schedule; int nexec; From d089e72ca3ccdbce10b3bc71edbe918b0c501fb8 Mon Sep 17 00:00:00 2001 From: Peter Reijnders Date: Wed, 8 Apr 2015 21:33:01 +0200 Subject: [PATCH 2/2] Changed __attribute__ into pragma for Visual studio. The ape_socket.states struct had origially 4 uint8_t fields. That fits nice in memory. By changing this into enums, We get a better typechecking, but enums are int's which use more memory then uint8_t's. By 'packing' that struct, we can have the cake and eat it as well. Apperently GCC/Clang does not handle #pragma packed and Visual studio does not handle __attribute__. These extra ifdef's should do the tricks. (only tested with gcc and clang on linux) pahole with gcc gives: struct { enum ape_socket_flags flags; /* 108 1 */ enum ape_socket_proto proto; /* 109 1 */ enum ape_socket_type type; /* 110 1 */ enum ape_socket_state state; /* 111 1 */ } states; /* 108 4 */ pahole with clang gives: struct { public: enum ape_socket_flags flags; /* 108 1 */ enum ape_socket_proto proto; /* 109 1 */ enum ape_socket_type type; /* 110 1 */ enum ape_socket_state state; /* 111 1 */ } states; /* 108 4 */ --- ape_socket.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/ape_socket.h b/ape_socket.h index a2c3436..7a845a4 100644 --- a/ape_socket.h +++ b/ape_socket.h @@ -91,19 +91,31 @@ struct iovec enum ape_socket_flags { APE_SOCKET_WOULD_BLOCK = (1 << 0), APE_SOCKET_CORK = (1 << 1) +#ifdef _MSC_VER +}; +#else } __attribute__ ((__packed__)); +#endif enum ape_socket_proto { APE_SOCKET_PT_TCP, APE_SOCKET_PT_UDP, APE_SOCKET_PT_SSL +#ifdef _MSC_VER +}; +#else } __attribute__ ((__packed__)); +#endif enum ape_socket_type { APE_SOCKET_TP_UNKNOWN, APE_SOCKET_TP_SERVER, APE_SOCKET_TP_CLIENT +#ifdef _MSC_VER +}; +#else } __attribute__ ((__packed__)); +#endif enum ape_socket_state { APE_SOCKET_ST_ONLINE, @@ -111,7 +123,11 @@ enum ape_socket_state { APE_SOCKET_ST_PENDING, APE_SOCKET_ST_OFFLINE, APE_SOCKET_ST_SHUTDOWN +#ifdef _MSC_VER +}; +#else } __attribute__ ((__packed__)); +#endif typedef enum _ape_socket_data_autorelease { APE_DATA_STATIC, @@ -172,12 +188,20 @@ struct _ape_socket { struct _ape_dns_cb_argv *dns_state; +#ifdef _MSC_VER +#pragma pack(push, 1) struct { +#else + struct __attribute__ ((__packed__)) { +#endif enum ape_socket_flags flags; enum ape_socket_proto proto; enum ape_socket_type type; enum ape_socket_state state; } states; +#ifdef _MSC_VER +#pragma pack(pop) +#endif #ifdef _HAVE_SSL_SUPPORT struct {