From 408505ae310b418258ebbc3bd3fe4bb95d33717c Mon Sep 17 00:00:00 2001 From: Alex Helfet Date: Thu, 23 Mar 2017 05:11:45 +0000 Subject: [PATCH] 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;