From 0be620602ad7dd14ddb9f4eb26749759131cb7db Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Wed, 22 Mar 2017 17:40:05 +0000 Subject: [PATCH 1/6] Fix compiler warnings and errors in GCC. --- lib/capn-malloc.c | 4 ++++ lib/capn.c | 2 ++ lib/capnp_c.h | 25 +++++++++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/capn-malloc.c b/lib/capn-malloc.c index a3c2de0..708294e 100644 --- a/lib/capn-malloc.c +++ b/lib/capn-malloc.c @@ -25,6 +25,10 @@ struct check_segment_alignment { }; static struct capn_segment *create(void *u, uint32_t id, int sz) { + // Silence warnings about unused parameters. + UNUSED(u); + UNUSED(id); + struct capn_segment *s; sz += sizeof(*s); if (sz < 4096) { diff --git a/lib/capn.c b/lib/capn.c index 60e139c..0677452 100644 --- a/lib/capn.c +++ b/lib/capn.c @@ -7,6 +7,8 @@ * of the MIT license. See the LICENSE file for details. */ +#pragma GCC diagnostic ignored "-Wmissing-field-initializers" + #include "capnp_c.h" #include diff --git a/lib/capnp_c.h b/lib/capnp_c.h index a1133df..86d5608 100644 --- a/lib/capnp_c.h +++ b/lib/capnp_c.h @@ -13,6 +13,12 @@ #include #include + +// ssize_t is defined in unistd.h in GCC. +#ifdef __GNUC__ +#include +#endif + #if defined(unix) && !defined(__APPLE__) #include #endif @@ -22,6 +28,19 @@ typedef intmax_t ssize_t; #endif +// Macro UNUSED silences compiler warnings about unused parameters. +#ifndef UNUSED +#define UNUSED(x) (void)(x) +#endif + +// Cross-platform macro ALIGNED_(x) aligns a struct by x bits. +#ifdef _MSC_VER +#define ALIGNED_(x) __declspec(align(x)) +#endif +#ifdef __GNUC__ +#define ALIGNED_(x) __attribute__ ((aligned(x))) +#endif + #ifdef __cplusplus extern "C" { #endif @@ -103,10 +122,8 @@ struct capn_tree *capn_tree_insert(struct capn_tree *root, struct capn_tree *n); * data, len, cap, and user should all set by the user. Other values * should be zero initialized. */ -#ifdef _MSC_VER -__declspec(align(64)) -#endif -struct capn_segment { + +struct ALIGNED_(64) capn_segment { struct capn_tree hdr; struct capn_segment *next; struct capn *capn; From 1b25eae747ccf73472ed3984d283ab5a62389dff Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Wed, 22 Mar 2017 17:44:17 +0000 Subject: [PATCH 2/6] Fix runtime library compiler warnings and errors in GCC. --- lib/capnp_c.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/capnp_c.h b/lib/capnp_c.h index 86d5608..58867a3 100644 --- a/lib/capnp_c.h +++ b/lib/capnp_c.h @@ -28,12 +28,12 @@ typedef intmax_t ssize_t; #endif -// Macro UNUSED silences compiler warnings about unused parameters. +// Macro UNUSED(x) silences compiler warnings about unused parameter or variable `x`. #ifndef UNUSED #define UNUSED(x) (void)(x) #endif -// Cross-platform macro ALIGNED_(x) aligns a struct by x bits. +// Cross-platform macro ALIGNED_(x) aligns a struct by `x` bits. #ifdef _MSC_VER #define ALIGNED_(x) __declspec(align(x)) #endif From ef88fd8e9d2b26317d71d2ed9de7a191840205b1 Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Thu, 23 Mar 2017 04:33:03 +0000 Subject: [PATCH 3/6] Fixed some issues raised by eqvinox in pull request. PR URL: https://github.com/opensourcerouting/c-capnproto/pull/15/files/1b25eae747ccf73472ed3984d283ab5a62389dff --- lib/capn-malloc.c | 8 ++++---- lib/capn.c | 2 ++ lib/capnp_c.h | 6 +----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/capn-malloc.c b/lib/capn-malloc.c index 708294e..65fcb9f 100644 --- a/lib/capn-malloc.c +++ b/lib/capn-malloc.c @@ -8,6 +8,10 @@ * of the MIT license. See the LICENSE file for details. */ +#ifdef __GNUC__ +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + #include "capnp_c.h" #include "capnp_priv.h" #include @@ -25,10 +29,6 @@ struct check_segment_alignment { }; static struct capn_segment *create(void *u, uint32_t id, int sz) { - // Silence warnings about unused parameters. - UNUSED(u); - UNUSED(id); - struct capn_segment *s; sz += sizeof(*s); if (sz < 4096) { diff --git a/lib/capn.c b/lib/capn.c index 0677452..f9d22d8 100644 --- a/lib/capn.c +++ b/lib/capn.c @@ -7,7 +7,9 @@ * of the MIT license. See the LICENSE file for details. */ +#ifdef __GNUC__ #pragma GCC diagnostic ignored "-Wmissing-field-initializers" +#endif #include "capnp_c.h" diff --git a/lib/capnp_c.h b/lib/capnp_c.h index 58867a3..edbc43e 100644 --- a/lib/capnp_c.h +++ b/lib/capnp_c.h @@ -13,11 +13,7 @@ #include #include - -// ssize_t is defined in unistd.h in GCC. -#ifdef __GNUC__ #include -#endif #if defined(unix) && !defined(__APPLE__) #include @@ -33,7 +29,7 @@ typedef intmax_t ssize_t; #define UNUSED(x) (void)(x) #endif -// Cross-platform macro ALIGNED_(x) aligns a struct by `x` bits. +// Cross-platform macro ALIGNED_(x) aligns a struct by `x` bytes. #ifdef _MSC_VER #define ALIGNED_(x) __declspec(align(x)) #endif From a93ae127052a196a08ce13838226d7295fc95742 Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Thu, 23 Mar 2017 04:33:27 +0000 Subject: [PATCH 4/6] .gitignore .dirstamp files created during the build. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 04ffc2b..76d9560 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ Makefile Makefile.in .deps .libs +.dirstamp test-driver *.log From 57d0ffea333d8602e3a1c7ac5d723251b3b49af0 Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Thu, 23 Mar 2017 04:37:51 +0000 Subject: [PATCH 5/6] Remove UNUSED(x) macro. No longer needed. --- lib/capnp_c.h | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/capnp_c.h b/lib/capnp_c.h index edbc43e..df6604c 100644 --- a/lib/capnp_c.h +++ b/lib/capnp_c.h @@ -24,11 +24,6 @@ typedef intmax_t ssize_t; #endif -// Macro UNUSED(x) silences compiler warnings about unused parameter or variable `x`. -#ifndef UNUSED -#define UNUSED(x) (void)(x) -#endif - // Cross-platform macro ALIGNED_(x) aligns a struct by `x` bytes. #ifdef _MSC_VER #define ALIGNED_(x) __declspec(align(x)) From 408505ae310b418258ebbc3bd3fe4bb95d33717c Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Thu, 23 Mar 2017 05:11:45 +0000 Subject: [PATCH 6/6] 8 byte alignment on capn_segment, updated comments about this. As discussed in PR: https://github.com/opensourcerouting/c-capnproto/pull/15#discussion_r107588334 --- lib/capn-malloc.c | 13 +++++++++---- lib/capnp_c.h | 9 +++++---- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/capn-malloc.c b/lib/capn-malloc.c index 65fcb9f..7308061 100644 --- a/lib/capn-malloc.c +++ b/lib/capn-malloc.c @@ -19,10 +19,15 @@ #include #include -/* Visual Studio notes: - * Unless capn_segment is defined with __declspec(align(64)), check_segment_alignment - * Fails to compile in x86 mode, as (sizeof(struct capn_segment)&7) -> (44 & 7) evaluates to 4 - * Compiles in x64 mode, as (sizeof(struct capn_segment)&7) -> (80 & 7) evaluates to 0 +/* + * 8 byte alignment is required for struct capn_segment. + * This struct check_segment_alignment verifies this at compile time. + * + * Unless capn_segment is defined with 8 byte alignment, check_segment_alignment + * fails to compile in x86 mode (or on another CPU with 32-bit pointers), + * as (sizeof(struct capn_segment)&7) -> (44 & 7) evaluates to 4. + * It compiles in x64 mode (or on another CPU with 64-bit pointers), + * as (sizeof(struct capn_segment)&7) -> (80 & 7) evaluates to 0. */ struct check_segment_alignment { unsigned int foo : (sizeof(struct capn_segment)&7) ? -1 : 1; diff --git a/lib/capnp_c.h b/lib/capnp_c.h index df6604c..deb550c 100644 --- a/lib/capnp_c.h +++ b/lib/capnp_c.h @@ -103,18 +103,19 @@ struct capn_tree *capn_tree_insert(struct capn_tree *root, struct capn_tree *n); * * cap specifies the segment capacity. * - * When creating new structures len will be incremented until it reaces cap, + * When creating new structures len will be incremented until it reaches cap, * at which point a new segment will be requested via capn->create. The * create callback can either create a new segment or expand an existing * one by incrementing cap and returning the expanded segment. * - * data, len, and cap must all by 8 byte aligned + * data, len, and cap must all be 8 byte aligned, hence the ALIGNED_(8) macro + * on the struct definition. * - * data, len, cap, and user should all set by the user. Other values + * data, len, cap, and user should all be set by the user. Other values * should be zero initialized. */ -struct ALIGNED_(64) capn_segment { +struct ALIGNED_(8) capn_segment { struct capn_tree hdr; struct capn_segment *next; struct capn *capn;