Skip to content

Commit

Permalink
Added explicit error message in case of memory exhaustion
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Sustrik <[email protected]>
  • Loading branch information
sustrik committed Feb 22, 2011
1 parent 98ccff1 commit 43e8868
Show file tree
Hide file tree
Showing 24 changed files with 61 additions and 52 deletions.
6 changes: 3 additions & 3 deletions src/connect_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void zmq::connect_session_t::start_connecting (bool wait_)
zmq_connecter_t *connecter = new (std::nothrow) zmq_connecter_t (
io_thread, this, options, protocol.c_str (), address.c_str (),
wait_);
zmq_assert (connecter);
alloc_assert (connecter);
launch_child (connecter);
return;
}
Expand All @@ -77,7 +77,7 @@ void zmq::connect_session_t::start_connecting (bool wait_)
// PGM sender.
pgm_sender_t *pgm_sender = new (std::nothrow) pgm_sender_t (
io_thread, options);
zmq_assert (pgm_sender);
alloc_assert (pgm_sender);

int rc = pgm_sender->init (udp_encapsulation, address.c_str ());
zmq_assert (rc == 0);
Expand All @@ -89,7 +89,7 @@ void zmq::connect_session_t::start_connecting (bool wait_)
// PGM receiver.
pgm_receiver_t *pgm_receiver = new (std::nothrow) pgm_receiver_t (
io_thread, options);
zmq_assert (pgm_receiver);
alloc_assert (pgm_receiver);

int rc = pgm_receiver->init (udp_encapsulation, address.c_str ());
zmq_assert (rc == 0);
Expand Down
6 changes: 3 additions & 3 deletions src/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ zmq::ctx_t::ctx_t (uint32_t io_threads_) :
// internal log socket and the zmq_term thread the reaper thread.
slot_count = max_sockets + io_threads_ + 3;
slots = (mailbox_t**) malloc (sizeof (mailbox_t*) * slot_count);
zmq_assert (slots);
alloc_assert (slots);

// Initialise the infrastructure for zmq_term thread.
slots [term_tid] = &term_mailbox;

// Create the reaper thread.
reaper = new (std::nothrow) reaper_t (this, reaper_tid);
zmq_assert (reaper);
alloc_assert (reaper);
slots [reaper_tid] = reaper->get_mailbox ();
reaper->start ();

// Create I/O thread objects and launch them.
for (uint32_t i = 2; i != io_threads_ + 2; i++) {
io_thread_t *io_thread = new (std::nothrow) io_thread_t (this, i);
zmq_assert (io_thread);
alloc_assert (io_thread);
io_threads.push_back (io_thread);
slots [i] = io_thread->get_mailbox ();
io_thread->start ();
Expand Down
2 changes: 1 addition & 1 deletion src/decoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ namespace zmq
bufsize (bufsize_)
{
buf = (unsigned char*) malloc (bufsize_);
zmq_assert (buf);
alloc_assert (buf);
}

// The destructor doesn't have to be virtual. It is mad virtual
Expand Down
2 changes: 1 addition & 1 deletion src/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace zmq
bufsize (bufsize_)
{
buf = (unsigned char*) malloc (bufsize_);
zmq_assert (buf);
alloc_assert (buf);
}

// The destructor doesn't have to be virtual. It is made virtual
Expand Down
2 changes: 1 addition & 1 deletion src/epoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ zmq::epoll_t::~epoll_t ()
zmq::epoll_t::handle_t zmq::epoll_t::add_fd (fd_t fd_, i_poll_events *events_)
{
poll_entry_t *pe = new (std::nothrow) poll_entry_t;
zmq_assert (pe != NULL);
alloc_assert (pe);

// The memset is not actually needed. It's here to prevent debugging
// tools to complain about using uninitialised memory.
Expand Down
20 changes: 13 additions & 7 deletions src/err.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace zmq
}\
} while (false)

// Provides convenient way to check for POSIX errors.
// Provides convenient way to check for POSIX errors.
#define posix_assert(x) \
do {\
if (unlikely (x)) {\
Expand All @@ -107,7 +107,7 @@ namespace zmq
}\
} while (false)

// Provides convenient way to check for errors from getaddrinfo.
// Provides convenient way to check for errors from getaddrinfo.
#define gai_assert(x) \
do {\
if (unlikely (x)) {\
Expand All @@ -117,10 +117,16 @@ namespace zmq
}\
} while (false)

#endif

#define zmq_not_implemented() \
// Provides convenient way to check whether memory allocation have succeeded.
#define alloc_assert(x) \
do {\
fprintf (stderr, "Hic sunt leones (%s:%d)\n", __FILE__, __LINE__);\
abort ();\
if (unlikely (!x)) {\
fprintf (stderr, "FATAL ERROR: OUT OF MEMORY (%s:%d)\n",\
__FILE__, __LINE__);\
abort ();\
}\
} while (false)

#endif


2 changes: 1 addition & 1 deletion src/io_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ zmq::io_thread_t::io_thread_t (ctx_t *ctx_, uint32_t tid_) :
object_t (ctx_, tid_)
{
poller = new (std::nothrow) poller_t;
zmq_assert (poller);
alloc_assert (poller);

mailbox_handle = poller->add_fd (mailbox.get_fd (), this);
poller->set_pollin (mailbox_handle);
Expand Down
2 changes: 1 addition & 1 deletion src/ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static int resolve_nic_name (in_addr* addr_, char const *interface_)
// Allocate memory to get interface names.
size_t ifr_size = sizeof (struct lifreq) * ifn.lifn_count;
char *ifr = (char*) malloc (ifr_size);
errno_assert (ifr);
alloc_assert (ifr);

// Retrieve interface names.
lifconf ifc;
Expand Down
2 changes: 1 addition & 1 deletion src/kqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ zmq::kqueue_t::handle_t zmq::kqueue_t::add_fd (fd_t fd_,
i_poll_events *reactor_)
{
poll_entry_t *pe = new (std::nothrow) poll_entry_t;
zmq_assert (pe != NULL);
alloc_assert (pe);

pe->fd = fd_;
pe->flag_pollin = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ void zmq::object_t::send_attach (session_t *destination_, i_engine *engine_,
(unsigned char) peer_identity_.size ();
cmd.args.attach.peer_identity =
(unsigned char*) malloc (peer_identity_.size ());
zmq_assert (cmd.args.attach.peer_identity_size);
alloc_assert (cmd.args.attach.peer_identity_size);
memcpy (cmd.args.attach.peer_identity, peer_identity_.data (),
peer_identity_.size ());
}
Expand Down Expand Up @@ -259,7 +259,7 @@ void zmq::object_t::send_bind (own_t *destination_, reader_t *in_pipe_,
(unsigned char) peer_identity_.size ();
cmd.args.bind.peer_identity =
(unsigned char*) malloc (peer_identity_.size ());
zmq_assert (cmd.args.bind.peer_identity_size);
alloc_assert (cmd.args.bind.peer_identity_size);
memcpy (cmd.args.bind.peer_identity, peer_identity_.data (),
peer_identity_.size ());
}
Expand Down
1 change: 1 addition & 0 deletions src/pgm_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ void zmq::pgm_receiver_t::in_event ()

// Create and connect decoder for the peer.
it->second.decoder = new (std::nothrow) decoder_t (0);
alloc_assert (it->second.decoder);
it->second.decoder->set_inout (inout);
}

Expand Down
2 changes: 1 addition & 1 deletion src/pgm_sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ int zmq::pgm_sender_t::init (bool udp_encapsulation_, const char *network_)

out_buffer_size = pgm_socket.get_max_tsdu_size ();
out_buffer = (unsigned char*) malloc (out_buffer_size);
zmq_assert (out_buffer);
alloc_assert (out_buffer);

return rc;
}
Expand Down
3 changes: 2 additions & 1 deletion src/pgm_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ int zmq::pgm_socket_t::init (bool udp_encapsulation_, const char *network_)
zmq_assert (pgm_msgv_len);

pgm_msgv = (pgm_msgv_t*) malloc (sizeof (pgm_msgv_t) * pgm_msgv_len);
alloc_assert (pgm_msgv);
}

return 0;
Expand Down Expand Up @@ -602,7 +603,7 @@ ssize_t zmq::pgm_socket_t::receive (void **raw_data_, const pgm_tsi_t **tsi_)
// Data loss.
if (status == PGM_IO_STATUS_RESET) {

struct pgm_sk_buff_t* skb = pgm_msgv[0].msgv_skb[0];
struct pgm_sk_buff_t* skb = pgm_msgv [0].msgv_skb [0];

// Save lost data TSI.
*tsi_ = &skb->tsi;
Expand Down
8 changes: 4 additions & 4 deletions src/pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ zmq::writer_t::writer_t (object_t *parent_, pipe_t *pipe_, reader_t *reader_,
// Open the swap file, if required.
if (swap_size_ > 0) {
swap = new (std::nothrow) swap_t (swap_size_);
zmq_assert (swap);
alloc_assert (swap);
int rc = swap->init ();
zmq_assert (rc == 0);
}
Expand Down Expand Up @@ -399,10 +399,10 @@ void zmq::create_pipe (object_t *reader_parent_, object_t *writer_parent_,
// writer. The pipe will be handled by reader and writer, its never passed
// to the user. Reader and writer are returned to the user.
pipe_t *pipe = new (std::nothrow) pipe_t ();
zmq_assert (pipe);
alloc_assert (pipe);
*reader_ = new (std::nothrow) reader_t (reader_parent_, pipe, lwm);
zmq_assert (*reader_);
alloc_assert (*reader_);
*writer_ = new (std::nothrow) writer_t (writer_parent_, pipe, *reader_,
hwm_, swap_size_);
zmq_assert (*writer_);
alloc_assert (*writer_);
}
2 changes: 1 addition & 1 deletion src/reaper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ zmq::reaper_t::reaper_t (class ctx_t *ctx_, uint32_t tid_) :
terminating (false)
{
poller = new (std::nothrow) poller_t;
zmq_assert (poller);
alloc_assert (poller);

mailbox_handle = poller->add_fd (mailbox.get_fd (), this);
poller->set_pollin (mailbox_handle);
Expand Down
2 changes: 0 additions & 2 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <new>

#include "session.hpp"
#include "socket_base.hpp"
#include "i_engine.hpp"
Expand Down
6 changes: 3 additions & 3 deletions src/socket_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ zmq::socket_base_t *zmq::socket_base_t::create (int type_, class ctx_t *parent_,
errno = EINVAL;
return NULL;
}
zmq_assert (s);
alloc_assert (s);
return s;
}

Expand Down Expand Up @@ -318,7 +318,7 @@ int zmq::socket_base_t::bind (const char *addr_)
// Create and run the listener.
zmq_listener_t *listener = new (std::nothrow) zmq_listener_t (
io_thread, this, options);
zmq_assert (listener);
alloc_assert (listener);
int rc = listener->set_address (protocol.c_str(), address.c_str ());
if (rc != 0) {
delete listener;
Expand Down Expand Up @@ -420,7 +420,7 @@ int zmq::socket_base_t::connect (const char *addr_)
// Create session.
connect_session_t *session = new (std::nothrow) connect_session_t (
io_thread, this, options, protocol.c_str (), address.c_str ());
zmq_assert (session);
alloc_assert (session);

// If 'immediate connect' feature is required, we'll create the pipes
// to the session straight away. Otherwise, they'll be created by the
Expand Down
13 changes: 8 additions & 5 deletions src/swap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ zmq::swap_t::swap_t (int64_t filesize_) :
zmq_assert (block_size > 0);

buf1 = new (std::nothrow) char [block_size];
zmq_assert (buf1);
alloc_assert (buf1);

buf2 = new (std::nothrow) char [block_size];
zmq_assert (buf2);
alloc_assert (buf2);

read_buf = write_buf = buf1;
}
Expand Down Expand Up @@ -278,7 +278,8 @@ void zmq::swap_t::fill_buf (char *buf, int64_t pos)
#ifdef ZMQ_HAVE_WINDOWS
int rc = _read (fd, &buf [octets_stored], octets_total - octets_stored);
#else
ssize_t rc = read (fd, &buf [octets_stored], octets_total - octets_stored);
ssize_t rc = read (fd, &buf [octets_stored],
octets_total - octets_stored);
#endif
errno_assert (rc > 0);
octets_stored += rc;
Expand All @@ -302,9 +303,11 @@ void zmq::swap_t::save_write_buf ()

while (octets_stored < octets_total) {
#ifdef ZMQ_HAVE_WINDOWS
int rc = _write (fd, &write_buf [octets_stored], octets_total - octets_stored);
int rc = _write (fd, &write_buf [octets_stored],
octets_total - octets_stored);
#else
ssize_t rc = write (fd, &write_buf [octets_stored], octets_total - octets_stored);
ssize_t rc = write (fd, &write_buf [octets_stored],
octets_total - octets_stored);
#endif
errno_assert (rc > 0);
octets_stored += rc;
Expand Down
6 changes: 3 additions & 3 deletions src/trie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void zmq::trie_t::add (unsigned char *prefix_, size_t size_)
count = (min < c ? c - min : min - c) + 1;
next.table = (trie_t**)
malloc (sizeof (trie_t*) * count);
zmq_assert (next.table);
alloc_assert (next.table);
for (unsigned short i = 0; i != count; ++i)
next.table [i] = 0;
min = std::min (min, c);
Expand Down Expand Up @@ -110,14 +110,14 @@ void zmq::trie_t::add (unsigned char *prefix_, size_t size_)
if (count == 1) {
if (!next.node) {
next.node = new (std::nothrow) trie_t;
zmq_assert (next.node);
alloc_assert (next.node);
}
next.node->add (prefix_ + 1, size_ - 1);
}
else {
if (!next.table [c - min]) {
next.table [c - min] = new (std::nothrow) trie_t;
zmq_assert (next.table [c - min]);
alloc_assert (next.table [c - min]);
}
next.table [c - min]->add (prefix_ + 1, size_ - 1);
}
Expand Down
4 changes: 2 additions & 2 deletions src/yqueue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace zmq
inline yqueue_t ()
{
begin_chunk = (chunk_t*) malloc (sizeof (chunk_t));
zmq_assert (begin_chunk);
alloc_assert (begin_chunk);
begin_pos = 0;
back_chunk = NULL;
back_pos = 0;
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace zmq
sc->prev = end_chunk;
} else {
end_chunk->next = (chunk_t*) malloc (sizeof (chunk_t));
zmq_assert (end_chunk->next);
alloc_assert (end_chunk->next);
end_chunk->next->prev = end_chunk;
}
end_chunk = end_chunk->next;
Expand Down
8 changes: 4 additions & 4 deletions src/zmq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ int zmq_msg_init_data (zmq_msg_t *msg_, void *data_, size_t size_,
zmq_free_fn *ffn_, void *hint_)
{
msg_->content = (zmq::msg_content_t*) malloc (sizeof (zmq::msg_content_t));
zmq_assert (msg_->content);
alloc_assert (msg_->content);
msg_->flags = 0;
zmq::msg_content_t *content = (zmq::msg_content_t*) msg_->content;
content->data = data_;
Expand Down Expand Up @@ -255,7 +255,7 @@ void *zmq_init (int io_threads_)

// Create 0MQ context.
zmq::ctx_t *ctx = new (std::nothrow) zmq::ctx_t ((uint32_t) io_threads_);
zmq_assert (ctx);
alloc_assert (ctx);
return (void*) ctx;
}

Expand Down Expand Up @@ -403,7 +403,7 @@ int zmq_poll (zmq_pollitem_t *items_, int nitems_, long timeout_)
uint64_t end = 0;

pollfd *pollfds = (pollfd*) malloc (nitems_ * sizeof (pollfd));
zmq_assert (pollfds);
alloc_assert (pollfds);

// Build pollset for poll () system call.
for (int i = 0; i != nitems_; i++) {
Expand Down Expand Up @@ -761,7 +761,7 @@ void zmq_sleep (int seconds_)
void *zmq_stopwatch_start ()
{
uint64_t *watch = (uint64_t*) malloc (sizeof (uint64_t));
assert (watch);
alloc_assert (watch);
*watch = zmq::clock_t::now_us ();
return (void*) watch;
}
Expand Down
2 changes: 1 addition & 1 deletion src/zmq_connecter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void zmq::zmq_connecter_t::out_event ()
// Create an init object.
zmq_init_t *init = new (std::nothrow) zmq_init_t (io_thread, NULL,
session, fd, options);
zmq_assert (init);
alloc_assert (init);
launch_sibling (init);

// Shut the connecter down.
Expand Down
Loading

0 comments on commit 43e8868

Please sign in to comment.