From 700b91d102a88272c00a63b2297600ac5cb21fd6 Mon Sep 17 00:00:00 2001 From: Brian Silverman Date: Thu, 17 Dec 2015 14:28:15 -0500 Subject: [PATCH] Fix alignment of initial chunk in yqueue Clang's UndefinedBehaviorSanitizer catches this in the tests. --- src/yqueue.hpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/yqueue.hpp b/src/yqueue.hpp index 66ea8bdc..d02ccc12 100644 --- a/src/yqueue.hpp +++ b/src/yqueue.hpp @@ -67,7 +67,7 @@ namespace zmq // Create the queue. inline yqueue_t () { - begin_chunk = (chunk_t*) malloc (sizeof (chunk_t)); + begin_chunk = allocate_chunk(); alloc_assert (begin_chunk); begin_pos = 0; back_chunk = NULL; @@ -121,13 +121,7 @@ namespace zmq end_chunk->next = sc; sc->prev = end_chunk; } else { -#ifdef HAVE_POSIX_MEMALIGN - void *pv; - if (posix_memalign(&pv, ALIGN, sizeof (chunk_t)) == 0) - end_chunk->next = (chunk_t*) pv; -#else - end_chunk->next = (chunk_t*) malloc (sizeof (chunk_t)); -#endif + end_chunk->next = allocate_chunk(); alloc_assert (end_chunk->next); end_chunk->next->prev = end_chunk; } @@ -193,6 +187,18 @@ namespace zmq chunk_t *next; }; + inline chunk_t *allocate_chunk () + { +#ifdef HAVE_POSIX_MEMALIGN + void *pv; + if (posix_memalign(&pv, ALIGN, sizeof (chunk_t)) == 0) + return (chunk_t*) pv; + return NULL; +#else + return (chunk_t*) malloc (sizeof (chunk_t)); +#endif + } + // Back position may point to invalid memory if the queue is empty, // while begin & end positions are always valid. Begin position is // accessed exclusively be queue reader (front/pop), while back and