[zeromq-dev] Memory alignment

Pieter Hintjens ph at imatix.com
Fri Jul 8 09:04:37 CEST 2011


On Fri, Jul 8, 2011 at 8:48 AM, Martin Sustrik <sustrik at 250bpm.com> wrote:

> This can't be done in a generic way. When sending messages initialised
> using zmq_msg_init_data() via inproc, you get the same buffer that was
> sent by the sender. I.e. if the sender aligned the buffer in a special
> way, that's what you'll get.

There are three cases: inproc, small messages, and large messages.
Inproc will give you what you send, so the problem is solvable in the
application. Large messages align on 16 bytes, or whatever the heap
manager alignment is. The only issue is with vsm data.

C structure alignment is not random. The current layout of zmq_msg_t is:

    void *content;
    unsigned char flags;
    unsigned char vsm_size;
    unsigned char vsm_data [ZMQ_MAX_VSM_SIZE];

If you change this to:

    unsigned char flags;
    unsigned char vsm_size;
    void *content;
    unsigned char vsm_data [ZMQ_MAX_VSM_SIZE];

Then the content pointer _will_ be aligned on a 4-byte or 8-byte
boundary (32 or 64-bit) and the vsm_data on an 8 or 16-byte boundary.
This will work consistently across all architectures afaics. See e.g.
http://en.wikipedia.org/wiki/Data_structure_alignment

The cost is 2 or 6 extra bytes.

-Pieter



More information about the zeromq-dev mailing list