[zeromq-dev] [PATCH] Custom zmq_msg_t allocator

Douglas Creager dcreager at dcreager.net
Thu Jan 20 00:22:41 CET 2011

>> This is a patch against the latest master, which allows you to
>> provide a custom allocation function that ZMQ will use to allocate
>> the internal buffers for zmq_msg_t objects.  I've got a
>> multi-threaded ZMQ app that's passing millions of messages through
>> inproc connections, and when I swap in glib's slice allocator, I see
>> about a 25% increase in speed.  (The biggest win is that the standard
>> Mac OS malloc() and free() need to lock a global mutex, while the
>> slice allocator can work fairly independently in each thread.)
> Why not use the existing zmq_msg_init_data() function to get the desired 
> functionality? It allows you to wrap pre-allocated buffer into a 
> message. It also allows you to specify the deallocation function.
> So using the custom allocator would look something like this:
> void *buff = my_alloc (1000);
> ...
> zmq_msg_t msg;
> zmq_msg_init_data (buff, 1000, my_free);
> ...

That's what I tried at first; it lets you allocate the buffer that contains the message content.  But there's still a malloc call inside of zmq_msg_init_data that allocates space for the zmq::msg_content_t bookkeeping struct.  Without this patch, that struct is always managed with malloc/free.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 203 bytes
Desc: This is a digitally signed message part
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20110119/28456527/attachment.sig>

More information about the zeromq-dev mailing list