Skip to content

Commit

Permalink
Merge pull request #155 from bluca/alignment
Browse files Browse the repository at this point in the history
Problem: pointer union for zmq_msg_t is a hack
  • Loading branch information
somdoron authored Nov 1, 2016
2 parents 21e4847 + fb1ee2b commit 3ae04f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

* Fixed #2161 - messages dropped due to HWM race

* Fixed #1325 - alignment issue with zmq_msg_t causes SIGBUS on SPARC and ARM


0MQ version 4.1.5 stable, released on 2016/06/17
================================================
Expand Down
19 changes: 16 additions & 3 deletions include/zmq.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,23 @@ ZMQ_EXPORT int zmq_ctx_destroy (void *context);
/* 0MQ message definition. */
/******************************************************************************/

/* union here ensures correct alignment on architectures that require it, e.g.
* SPARC and ARM
/* Some architectures, like sparc64 and some variants of aarch64, enforce pointer
* alignment and raise sigbus on violations. Make sure applications allocate
* zmq_msg_t on addresses aligned on a pointer-size boundary to avoid this issue.
*/
typedef union zmq_msg_t { unsigned char _ [64]; void *p; } zmq_msg_t;
typedef struct zmq_msg_t {
#if defined (__GNUC__) || defined ( __INTEL_COMPILER) || \
(defined (__SUNPRO_C) && __SUNPRO_C >= 0x590) || \
(defined (__SUNPRO_CC) && __SUNPRO_CC >= 0x590)
unsigned char _ [64] __attribute__ ((aligned (sizeof (void *))));
#elif defined (_MSC_VER) && (defined (_M_X64) || defined (_M_ARM64))
__declspec (align (8)) unsigned char _ [64];
#elif defined (_MSC_VER) && (defined (_M_IX86) || defined (_M_ARM_ARMV7VE))
__declspec (align (4)) unsigned char _ [64];
#else
unsigned char _ [64];
#endif
} zmq_msg_t;

typedef void (zmq_free_fn) (void *data, void *hint);

Expand Down

0 comments on commit 3ae04f1

Please sign in to comment.