Skip to content

Commit

Permalink
Merge pull request #2494 from bjovke/master
Browse files Browse the repository at this point in the history
Unnecessary dynamic memory allocation in zmq_poller_poll()
  • Loading branch information
bluca authored Apr 4, 2017
2 parents 5d5263e + 319eb27 commit 26513b7
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -757,10 +757,9 @@ inline int zmq_poller_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
// implement zmq_poll on top of zmq_poller
int rc;
zmq_poller_event_t *events;
zmq::socket_poller_t poller;
events = new (std::nothrow) zmq_poller_event_t[nitems_];
alloc_assert(events);
void *poller = zmq_poller_new ();
alloc_assert(poller);

bool repeat_items = false;
// Register sockets with poller
Expand All @@ -780,12 +779,11 @@ inline int zmq_poller_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
}
}
if (modify) {
rc = zmq_poller_modify (poller, items_[i].socket, e);
rc = zmq_poller_modify (&poller, items_[i].socket, e);
} else {
rc = zmq_poller_add (poller, items_[i].socket, NULL, e);
rc = zmq_poller_add (&poller, items_[i].socket, NULL, e);
}
if (rc < 0) {
zmq_poller_destroy (&poller);
delete [] events;
return rc;
}
Expand All @@ -800,22 +798,20 @@ inline int zmq_poller_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
}
}
if (modify) {
rc = zmq_poller_modify_fd (poller, items_[i].fd, e);
rc = zmq_poller_modify_fd (&poller, items_[i].fd, e);
} else {
rc = zmq_poller_add_fd (poller, items_[i].fd, NULL, e);
rc = zmq_poller_add_fd (&poller, items_[i].fd, NULL, e);
}
if (rc < 0) {
zmq_poller_destroy (&poller);
delete [] events;
return rc;
}
}
}

// Wait for events
rc = zmq_poller_wait_all (poller, events, nitems_, timeout_);
rc = zmq_poller_wait_all (&poller, events, nitems_, timeout_);
if (rc < 0) {
zmq_poller_destroy (&poller);
delete [] events;
if (zmq_errno() == ETIMEDOUT) {
return 0;
Expand Down Expand Up @@ -851,7 +847,6 @@ inline int zmq_poller_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
}

// Cleanup
zmq_poller_destroy (&poller);
delete [] events;
return rc;
}
Expand Down

0 comments on commit 26513b7

Please sign in to comment.