]> git.sesse.net Git - cubemap/commitdiff
Use unique_ptr a few places instead of explicit delete.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 5 Apr 2018 07:47:27 +0000 (09:47 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Thu, 5 Apr 2018 07:47:27 +0000 (09:47 +0200)
main.cpp
server.cpp
server.h
serverpool.cpp
serverpool.h

index 7022655b3c34d396a4f38405091d5b42dfdad994..c208b2d19f381146df11e3322f86a4c34302862a 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -528,20 +528,20 @@ start:
        }
 
        // Start writing statistics.
-       StatsThread *stats_thread = NULL;
+       unique_ptr<StatsThread> 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<InputStatsThread> input_stats_thread;
        if (!config.input_stats_file.empty()) {
                vector<Input*> 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();
index 5026deb7ecb6fd911c16888e070fa33e31482a4b..0b4d076cd9f3fd36ac71fea0508e2e58d6d478cb 100644 (file)
@@ -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> &stream : streams) {
                        vector<Client *> 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> &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> &stream : streams) {
                stream->process_queued_data();
        }
 }
index 40e9f7575b708e8d3dc5f9f83268ff7793f6cc04..dfcac0b0414d4addc9dacbabce40234c037f7cec 100644 (file)
--- a/server.h
+++ b/server.h
@@ -8,6 +8,7 @@
 #include <sys/types.h>
 #include <time.h>
 #include <map>
+#include <memory>
 #include <queue>
 #include <string>
 #include <vector>
@@ -90,7 +91,7 @@ private:
        mutable pthread_mutex_t mutex;
 
        // All streams.
-       std::vector<Stream *> streams;
+       std::vector<std::unique_ptr<Stream>> streams;
 
        // Map from URL to index into <streams>.
        std::map<std::string, int> stream_url_map;
index 85561278e6222995f7f863ed48e6a03424597686..1414d219909407c1ca2574ca581cc46179a5483d 100644 (file)
@@ -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;
 }
 
index d257a86c48f7dc1186c12f3e285cce3371f5af28..87f3f8e4d1837054a036dc0b96e66767f8014312 100644 (file)
@@ -2,6 +2,7 @@
 #define _SERVERPOOL_H 1
 
 #include <stddef.h>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -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<ClientStats> get_client_stats() const;
 
 private:
-       Server *servers;
+       std::unique_ptr<Server[]> 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
        // <udp_streams>.
        int num_http_streams;
-       std::vector<UDPStream *> udp_streams;
+       std::vector<std::unique_ptr<UDPStream>> udp_streams;
 
        ServerPool(const ServerPool &);
 };