From 16bb62e6f7d6419528077b2f5d765ba6e0a414e7 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Mon, 28 May 2018 09:23:43 +0200 Subject: [PATCH] Problem: ctx_t::_slots is a plain array Solution: use a std::vector instead --- src/ctx.cpp | 38 ++++++++++++++++---------------------- src/ctx.hpp | 3 +-- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/ctx.cpp b/src/ctx.cpp index c601be17..a2802372 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -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 (malloc (sizeof (i_mailbox *) * _slot_count)); - if (!_slots) { - errno = ENOMEM; - goto fail; + 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; + 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 (_slot_count) - 1; - i >= static_cast (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 (_slot_count) - 1; - i >= static_cast (ios) + 2; i--) { + for (int32_t i = static_cast (_slots.size ()) - 1; + i >= static_cast (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; } diff --git a/src/ctx.hpp b/src/ctx.hpp index a43dc5df..5146ce76 100644 --- a/src/ctx.hpp +++ b/src/ctx.hpp @@ -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 _slots; // Mailbox for zmq_ctx_term thread. mailbox_t _term_mailbox;