Commit bcfe863f authored by Ian Barber's avatar Ian Barber

Merge pull request #630 from ulikoehler/cmsg

Optimized zmq::msg_t for constant messages
parents b3ca7fd4 121a838a
...@@ -75,19 +75,28 @@ int zmq::msg_t::init_size (size_t size_) ...@@ -75,19 +75,28 @@ int zmq::msg_t::init_size (size_t size_)
int zmq::msg_t::init_data (void *data_, size_t size_, msg_free_fn *ffn_, int zmq::msg_t::init_data (void *data_, size_t size_, msg_free_fn *ffn_,
void *hint_) void *hint_)
{ {
u.lmsg.type = type_lmsg; // Initialize constant message if there's no need to deallocate
u.lmsg.flags = 0; if(ffn_ == NULL) {
u.lmsg.content = (content_t*) malloc (sizeof (content_t)); u.cmsg.type = type_cmsg;
if (!u.lmsg.content) { u.cmsg.flags = 0;
errno = ENOMEM; u.cmsg.data = data_;
return -1; u.cmsg.size = size_;
} }
else {
u.lmsg.type = type_lmsg;
u.lmsg.flags = 0;
u.lmsg.content = (content_t*) malloc (sizeof (content_t));
if (!u.lmsg.content) {
errno = ENOMEM;
return -1;
}
u.lmsg.content->data = data_; u.lmsg.content->data = data_;
u.lmsg.content->size = size_; u.lmsg.content->size = size_;
u.lmsg.content->ffn = ffn_; u.lmsg.content->ffn = ffn_;
u.lmsg.content->hint = hint_; u.lmsg.content->hint = hint_;
new (&u.lmsg.content->refcnt) zmq::atomic_counter_t (); new (&u.lmsg.content->refcnt) zmq::atomic_counter_t ();
}
return 0; return 0;
} }
...@@ -193,6 +202,8 @@ void *zmq::msg_t::data () ...@@ -193,6 +202,8 @@ void *zmq::msg_t::data ()
return u.vsm.data; return u.vsm.data;
case type_lmsg: case type_lmsg:
return u.lmsg.content->data; return u.lmsg.content->data;
case type_cmsg:
return u.cmsg.data;
default: default:
zmq_assert (false); zmq_assert (false);
return NULL; return NULL;
...@@ -209,6 +220,8 @@ size_t zmq::msg_t::size () ...@@ -209,6 +220,8 @@ size_t zmq::msg_t::size ()
return u.vsm.size; return u.vsm.size;
case type_lmsg: case type_lmsg:
return u.lmsg.content->size; return u.lmsg.content->size;
case type_cmsg:
return u.cmsg.size;
default: default:
zmq_assert (false); zmq_assert (false);
return 0; return 0;
...@@ -245,6 +258,11 @@ bool zmq::msg_t::is_vsm () ...@@ -245,6 +258,11 @@ bool zmq::msg_t::is_vsm ()
return u.base.type == type_vsm; return u.base.type == type_vsm;
} }
bool zmq::msg_t::is_cmsg ()
{
return u.base.type == type_cmsg;
}
void zmq::msg_t::add_refs (int refs_) void zmq::msg_t::add_refs (int refs_)
{ {
zmq_assert (refs_ >= 0); zmq_assert (refs_ >= 0);
...@@ -253,8 +271,8 @@ void zmq::msg_t::add_refs (int refs_) ...@@ -253,8 +271,8 @@ void zmq::msg_t::add_refs (int refs_)
if (!refs_) if (!refs_)
return; return;
// VSMs and delimiters can be copied straight away. The only message type // VSMs, CMSGS and delimiters can be copied straight away. The only
// that needs special care are long messages. // message type that needs special care are long messages.
if (u.base.type == type_lmsg) { if (u.base.type == type_lmsg) {
if (u.lmsg.flags & msg_t::shared) if (u.lmsg.flags & msg_t::shared)
u.lmsg.content->refcnt.add (refs_); u.lmsg.content->refcnt.add (refs_);
......
...@@ -69,6 +69,7 @@ namespace zmq ...@@ -69,6 +69,7 @@ namespace zmq
bool is_identity () const; bool is_identity () const;
bool is_delimiter (); bool is_delimiter ();
bool is_vsm (); bool is_vsm ();
bool is_cmsg ();
// After calling this function you can copy the message in POD-style // After calling this function you can copy the message in POD-style
// refs_ times. No need to call copy. // refs_ times. No need to call copy.
...@@ -107,7 +108,9 @@ namespace zmq ...@@ -107,7 +108,9 @@ namespace zmq
type_vsm = 101, type_vsm = 101,
type_lmsg = 102, type_lmsg = 102,
type_delimiter = 103, type_delimiter = 103,
type_max = 103 // CMSG messages point to constant data
type_cmsg = 104,
type_max = 104
}; };
// Note that fields shared between different message types are not // Note that fields shared between different message types are not
...@@ -132,6 +135,14 @@ namespace zmq ...@@ -132,6 +135,14 @@ namespace zmq
unsigned char type; unsigned char type;
unsigned char flags; unsigned char flags;
} lmsg; } lmsg;
struct {
void* data;
size_t size;
unsigned char unused
[max_vsm_size + 1 - sizeof (void*) - sizeof (size_t)];
unsigned char type;
unsigned char flags;
} cmsg;
struct { struct {
unsigned char unused [max_vsm_size + 1]; unsigned char unused [max_vsm_size + 1];
unsigned char type; unsigned char type;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment