Commit 16bb62e6 authored by Simon Giesecke's avatar Simon Giesecke

Problem: ctx_t::_slots is a plain array

Solution: use a std::vector instead
parent a0a60e80
......@@ -70,8 +70,6 @@ zmq::ctx_t::ctx_t () :
_starting (true),
_terminating (false),
_reaper (NULL),
_slot_count (0),
_slots (NULL),
_max_sockets (clipped_maxsocket (ZMQ_MAX_SOCKETS_DFLT)),
_max_msgsz (INT_MAX),
_io_thread_count (ZMQ_IO_THREADS_DFLT),
......@@ -115,10 +113,8 @@ zmq::ctx_t::~ctx_t ()
// Deallocate the reaper thread object.
LIBZMQ_DELETE (_reaper);
// Deallocate the array of mailboxes. No special work is
// needed as mailboxes themselves were deallocated with their
// The mailboxes in _slots themselves were deallocated with their
// corresponding io_thread/socket objects.
free (_slots);
// De-initialise crypto library, if needed.
zmq::random_close ();
......@@ -290,13 +286,16 @@ bool zmq::ctx_t::start ()
const int mazmq = _max_sockets;
const int ios = _io_thread_count;
_opt_sync.unlock ();
_slot_count = mazmq + ios + term_and_reaper_threads_count;
_slots =
static_cast<i_mailbox **> (malloc (sizeof (i_mailbox *) * _slot_count));
if (!_slots) {
int slot_count = mazmq + ios + term_and_reaper_threads_count;
try {
_slots.reserve (slot_count);
_empty_slots.reserve (slot_count - term_and_reaper_threads_count);
}
catch (const std::bad_alloc &) {
errno = ENOMEM;
goto fail;
return false;
}
_slots.resize (term_and_reaper_threads_count);
// Initialise the infrastructure for zmq_ctx_term thread.
_slots[term_tid] = &_term_mailbox;
......@@ -313,12 +312,10 @@ bool zmq::ctx_t::start ()
_reaper->start ();
// Create I/O thread objects and launch them.
for (int32_t i = static_cast<int32_t> (_slot_count) - 1;
i >= static_cast<int32_t> (term_and_reaper_threads_count); i--) {
_slots[i] = NULL;
}
_slots.resize (slot_count, NULL);
for (int i = 2; i != ios + 2; i++) {
for (int i = term_and_reaper_threads_count;
i != ios + term_and_reaper_threads_count; i++) {
io_thread_t *io_thread = new (std::nothrow) io_thread_t (this, i);
if (!io_thread) {
errno = ENOMEM;
......@@ -334,8 +331,8 @@ bool zmq::ctx_t::start ()
}
// In the unused part of the slot array, create a list of empty slots.
for (int32_t i = static_cast<int32_t> (_slot_count) - 1;
i >= static_cast<int32_t> (ios) + 2; i--) {
for (int32_t i = static_cast<int32_t> (_slots.size ()) - 1;
i >= static_cast<int32_t> (ios) + term_and_reaper_threads_count; i--) {
_empty_slots.push_back (i);
}
......@@ -348,10 +345,7 @@ fail_cleanup_reaper:
_reaper = NULL;
fail_cleanup_slots:
free (_slots);
_slots = NULL;
fail:
_slots.clear ();
return false;
}
......
......@@ -198,8 +198,7 @@ class ctx_t : public thread_ctx_t
io_threads_t _io_threads;
// Array of pointers to mailboxes for both application and I/O threads.
uint32_t _slot_count;
i_mailbox **_slots;
std::vector<i_mailbox *> _slots;
// Mailbox for zmq_ctx_term thread.
mailbox_t _term_mailbox;
......
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