From 26fe3ab755034ea3be8321ec0af548670f8c3bd8 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 5 Apr 2018 09:47:27 +0200 Subject: [PATCH] Use unique_ptr a few places instead of explicit delete. --- main.cpp | 12 ++++++------ server.cpp | 18 +++++++----------- server.h | 3 ++- serverpool.cpp | 11 +---------- serverpool.h | 6 +++--- 5 files changed, 19 insertions(+), 31 deletions(-) diff --git a/main.cpp b/main.cpp index 7022655..c208b2d 100644 --- a/main.cpp +++ b/main.cpp @@ -528,20 +528,20 @@ start: } // Start writing statistics. - StatsThread *stats_thread = NULL; + unique_ptr stats_thread; if (!config.stats_file.empty()) { - stats_thread = new StatsThread(config.stats_file, config.stats_interval); + stats_thread.reset(new StatsThread(config.stats_file, config.stats_interval)); stats_thread->run(); } - InputStatsThread *input_stats_thread = NULL; + unique_ptr input_stats_thread; if (!config.input_stats_file.empty()) { vector inputs_no_refcount; for (const auto &key_and_input_with_refcount : inputs) { inputs_no_refcount.push_back(key_and_input_with_refcount.second.input); } - input_stats_thread = new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount); + input_stats_thread.reset(new InputStatsThread(config.input_stats_file, config.input_stats_interval, inputs_no_refcount)); input_stats_thread->run(); } @@ -575,11 +575,11 @@ start: if (input_stats_thread != NULL) { input_stats_thread->stop(); - delete input_stats_thread; + input_stats_thread.reset(); } if (stats_thread != NULL) { stats_thread->stop(); - delete stats_thread; + stats_thread.reset(); } for (Acceptor *acceptor : acceptors) { acceptor->stop(); diff --git a/server.cpp b/server.cpp index 5026deb..0b4d076 100644 --- a/server.cpp +++ b/server.cpp @@ -69,10 +69,6 @@ Server::Server() Server::~Server() { - for (Stream *stream : streams) { - delete stream; - } - safe_close(epoll_fd); } @@ -122,7 +118,7 @@ void Server::do_work() // Process each client where its stream has new data, // even if there was no socket activity. - for (Stream *stream : streams) { + for (unique_ptr &stream : streams) { vector to_process; swap(stream->to_process, to_process); for (Client *client : to_process) { @@ -195,7 +191,7 @@ CubemapStateProto Server::serialize() for (const auto &fd_and_client : clients) { serialized.add_clients()->MergeFrom(fd_and_client.second.serialize()); } - for (Stream *stream : streams) { + for (unique_ptr &stream : streams) { serialized.add_streams()->MergeFrom(stream->serialize()); } return serialized; @@ -265,7 +261,7 @@ void Server::add_client_from_serialized(const ClientProto &client) assert(client.state() != Client::SENDING_DATA); stream = NULL; } else { - stream = streams[stream_index]; + stream = streams[stream_index].get(); } auto inserted = clients.insert(make_pair(client.sock(), Client(client, stream))); assert(inserted.second == true); // Should not already exist. @@ -319,7 +315,7 @@ int Server::add_stream(const string &url, size_t backlog_size, size_t prebufferi { MutexLock lock(&mutex); stream_url_map.insert(make_pair(url, streams.size())); - streams.push_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding)); + streams.emplace_back(new Stream(url, backlog_size, prebuffering_bytes, encoding, src_encoding)); return streams.size() - 1; } @@ -327,7 +323,7 @@ int Server::add_stream_from_serialized(const StreamProto &stream, int data_fd) { MutexLock lock(&mutex); stream_url_map.insert(make_pair(stream.url(), streams.size())); - streams.push_back(new Stream(stream, data_fd)); + streams.emplace_back(new Stream(stream, data_fd)); return streams.size() - 1; } @@ -852,7 +848,7 @@ int Server::parse_request(Client *client) } } - Stream *stream = streams[stream_url_map_it->second]; + Stream *stream = streams[stream_url_map_it->second].get(); if (stream->http_header.empty()) { return 503; // Service unavailable. } @@ -990,7 +986,7 @@ void Server::process_queued_data() queued_add_clients.clear(); } - for (Stream *stream : streams) { + for (unique_ptr &stream : streams) { stream->process_queued_data(); } } diff --git a/server.h b/server.h index 40e9f75..dfcac0b 100644 --- a/server.h +++ b/server.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -90,7 +91,7 @@ private: mutable pthread_mutex_t mutex; // All streams. - std::vector streams; + std::vector> streams; // Map from URL to index into . std::map stream_url_map; diff --git a/serverpool.cpp b/serverpool.cpp index 8556127..1414d21 100644 --- a/serverpool.cpp +++ b/serverpool.cpp @@ -22,15 +22,6 @@ ServerPool::ServerPool(int size) { } -ServerPool::~ServerPool() -{ - delete[] servers; - - for (UDPStream *udp_stream : udp_streams) { - delete udp_stream; - } -} - CubemapStateProto ServerPool::serialize() { CubemapStateProto state; @@ -123,7 +114,7 @@ int ServerPool::add_stream_from_serialized(const StreamProto &stream, const vect int ServerPool::add_udpstream(const sockaddr_in6 &dst, int pacing_rate, int ttl, int multicast_iface_index) { - udp_streams.push_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index)); + udp_streams.emplace_back(new UDPStream(dst, pacing_rate, ttl, multicast_iface_index)); return num_http_streams + udp_streams.size() - 1; } diff --git a/serverpool.h b/serverpool.h index d257a86..87f3f8e 100644 --- a/serverpool.h +++ b/serverpool.h @@ -2,6 +2,7 @@ #define _SERVERPOOL_H 1 #include +#include #include #include @@ -20,7 +21,6 @@ struct sockaddr_in6; class ServerPool { public: ServerPool(int num_servers); - ~ServerPool(); // Fills streams() and clients(). CubemapStateProto serialize(); @@ -75,7 +75,7 @@ public: std::vector get_client_stats() const; private: - Server *servers; + std::unique_ptr servers; int num_servers, clients_added; // Our indexing is currently rather primitive; every stream_index in @@ -83,7 +83,7 @@ private: // has exactly one copy), and after that, it's mapping directly into // . int num_http_streams; - std::vector udp_streams; + std::vector> udp_streams; ServerPool(const ServerPool &); }; -- 2.39.2