[zeromq-dev] Clarifying msg logic

Brian Granger ellisonbg at gmail.com
Wed May 19 00:34:40 CEST 2010


On Mon, May 17, 2010 at 11:41 PM, Martin Sustrik <sustrik at 250bpm.com> wrote:
> Hi Brian,
>
>> We are working on non-copying logic for the Python bindings and have
>> some questions about the lifecycle of the zmq_msg_t objects.  Here is
>> our current understanding:
>>
>>
>> The following create a zmq_msg_t with ref-count = 1
>>
>> int zmq_msg_init (zmq_msg_t *msg)
>> int zmq_msg_init_size (zmq_msg_t *msg, size_t size)
>> int zmq_msg_init_data (zmq_msg_t *msg, void *data, size_t size,
>> zmq_free_fn *ffn, void *hint)
>>
>> Next, these increase or decrease the ref-count
>>
>> int zmq_msg_close (zmq_msg_t *msg)  -> decrease ref-count of msg by 1,
>> call zmq_free_fn if ref-count = 0
>> int zmq_msg_copy (zmq_msg_t *dest, zmq_msg_t *src) -> increase
>> ref-count of src by 1
>> zmq_socket_send -> decrease ref-count of msg by 1 after sending, if
>> reaches 0 call zmq_free_fn
>>
>> Is this thinking correct?
>
> Yes. That's correct.
>
>> Next, we are wondering how zmq_free_fn behaves when you start calling
>> zmq_msg_copy.
>>
>> If you do:
>>
>> zmq_msg_init_data(msg, ...., // pass in a zmq_free_fn)
>> zmq_send(msg)  // ref count - 1 and zmq_free_fn is called
>> zmq_msg_copy(dest, msg)  // is this even valid at this point or do you
>> have to do this before the send?
>
> Send empties the message:
>
> assert (msg.size () == 100);
> s.send (msg);
> assert (msg.size () == 0);
>
> Thus, in your example you just copy the empty message.
>
>> If you do the copy before the send, is the zmq_free_fn copied as well?
>>  Is it called a 2nd time.  It seems like it is not copied as
>> everything is done by reference.  Correct?
>
> Generally speaking, yes. zmq_msg_copy copies a pointer to data rather than
> data itself. zmq_free_fn is called when all references are closed.
>
> Now, there's an optimisation for messages below 30 bytes. These are really
> copied as copying 30 bytes is more efficient than messaging with dynamically
> allocated memory blocks and reference counts.

How is the zmq_free_fn handled when the msg content is copied.  Does
0MQ take responsibility for deallocating the copy, so zmq_free_fn
doesn't come into play?

Cheers,

Brian

> Btw, if you feel that 0MQ man pages are lacking relevant information,
> please, do suggest how to improve it.
>
> HTH
> Martin
>



-- 
Brian E. Granger, Ph.D.
Assistant Professor of Physics
Cal Poly State University, San Luis Obispo
bgranger at calpoly.edu
ellisonbg at gmail.com



More information about the zeromq-dev mailing list