[zeromq-dev] [PATCH] zmq::msg_t::const_data for const correctness?

Steven McCoy steven.mccoy at miru.hk
Fri May 27 18:27:06 CEST 2011


On 27 May 2011 19:14, Martin Sustrik <sustrik at 250bpm.com> wrote:

> On 05/27/2011 10:26 AM, Ilja Golshtein wrote:
> > The idea of the patch was to have something easily applicable.
> >
> > It does not break current interface, it is harmless, though not very
> useful.
> > I hope it can slightly reduce chances of copy() misuse.
>
> Well, I would say, let's fix the thing systematically. Now that 3.0 API
> is being devised we can even do it in core.
>
> Here's how it can possibly look like:
>
> void *zmq_msg_data (zmq_msg_t *msg);
> void *zmq_msg_const_data (const zmq_msg_t *msg);
> size_t zmq_msg_size (const zmq_msg_t *msg);
>
>
These are the main two that are required, but with zmq_msg_const_data should
be returning a const pointer.  You could go crazy by making the pointer
itself also const:

const void * const zmq_msg_const_data (const zmq_msg_t * const msg);
size_t zmq_msg_size (const zmq_msg_t * const msg);

Standard form tends to be less strict:

const void *zmq_msg_const_data (const zmq_msg_t *msg);
size_t zmq_msg_size (const zmq_msg_t *msg);




> int zmq_msg_copy (const zmq_msg_t *src, const zmq_msg_t **dest1, const
> zmq_msg_t **dest2);
>
> int zmq_msg_move (zmq_msg_t *src, zmq_msg_t *dest);
> int zmq_msg_const_move (const zmq_msg_t *src, const zmq_msg_t *dest);
>
>
I'll just like to point out how non-intuitive these are.

So the idea is looking for a snapshot of a message, which may or may not be
constant?

zmq_msg_t original;
zmq_msg_init_size (&original, strlen ("original") + 1);
memcpy (zmq_msg_data (&original), "original", strlen ("original") + 1);
const zmq_msg_t *const_snapshot = zmq_msg_const_snapshot (&original);
zmq_msg_t *snapshot = zmq_msg_snapshot (&original);
memcpy (zmq_msg_data (&original), "update", strlen ("update"));
memcpy (zmq_msg_data (&snapshot), "snap", strlen ("snap"));
printf ("%s\n", zmq_msg_data (&original));
printf ("%s\n", zmq_msg_data (&const_snapshot));
printf ("%s\n", zmq_msg_data (&snapshot));

Displays the following output:

updateal
original
snapinal

With presumably a copy construct for constant message, which is when C
becomes messy if you want to permit CoW copies instead of full copies.
 Maybe a zmq_msg_const_copy/dup might be then necessary that only takes a
constant message source.

const zmq_msg_t *const_copy = zmq_msg_const_dup (&const_snapshot);

-- 
Steve-o
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110528/9abc2f4f/attachment.htm>


More information about the zeromq-dev mailing list