Fix alignment of initial chunk in yqueue

Clang's UndefinedBehaviorSanitizer catches this in the tests.
This commit is contained in:
Brian Silverman 2015-12-17 14:28:15 -05:00
parent 768fc7699e
commit 700b91d102

View File

@ -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
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
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