Commit 700b91d1 authored by Brian Silverman's avatar Brian Silverman

Fix alignment of initial chunk in yqueue

Clang's UndefinedBehaviorSanitizer catches this in the tests.
parent 768fc769
...@@ -67,7 +67,7 @@ namespace zmq ...@@ -67,7 +67,7 @@ namespace zmq
// Create the queue. // Create the queue.
inline yqueue_t () inline yqueue_t ()
{ {
begin_chunk = (chunk_t*) malloc (sizeof (chunk_t)); begin_chunk = allocate_chunk();
alloc_assert (begin_chunk); alloc_assert (begin_chunk);
begin_pos = 0; begin_pos = 0;
back_chunk = NULL; back_chunk = NULL;
...@@ -121,13 +121,7 @@ namespace zmq ...@@ -121,13 +121,7 @@ namespace zmq
end_chunk->next = sc; end_chunk->next = sc;
sc->prev = end_chunk; sc->prev = end_chunk;
} else { } else {
#ifdef HAVE_POSIX_MEMALIGN end_chunk->next = allocate_chunk();
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
alloc_assert (end_chunk->next); alloc_assert (end_chunk->next);
end_chunk->next->prev = end_chunk; end_chunk->next->prev = end_chunk;
} }
...@@ -193,6 +187,18 @@ namespace zmq ...@@ -193,6 +187,18 @@ namespace zmq
chunk_t *next; 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, // Back position may point to invalid memory if the queue is empty,
// while begin & end positions are always valid. Begin position is // while begin & end positions are always valid. Begin position is
// accessed exclusively be queue reader (front/pop), while back and // accessed exclusively be queue reader (front/pop), while back and
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment