Commit 57b8986638fc1b7a9a5824269250cd04b27da96a

Authored by Wiebe Cazemier
1 parent f44f1e79

Use std::make_shared and std::make_unique

This saves some allocations.

This also meant having to set the C++ standard to 2014.

The getCopy() methods of sessions and mqttpackets can't be changed,
because of access errors (private).
CMakeLists.txt
... ... @@ -7,7 +7,7 @@ project(FlashMQ VERSION 0.9.9 LANGUAGES CXX)
7 7 add_definitions(-DOPENSSL_API_COMPAT=0x10100000L)
8 8 add_definitions(-DFLASHMQ_VERSION=\"${PROJECT_VERSION}\")
9 9  
10   -set(CMAKE_CXX_STANDARD 11)
  10 +set(CMAKE_CXX_STANDARD 14)
11 11 set(CMAKE_CXX_STANDARD_REQUIRED ON)
12 12  
13 13 SET(CMAKE_CXX_FLAGS "-msse4.2")
... ...
acltree.cpp
... ... @@ -14,7 +14,7 @@ AclNode *AclNode::getChildren(const std::string &subtopic, bool registerPattern)
14 14  
15 15 if (!node)
16 16 {
17   - node.reset(new AclNode());
  17 + node = std::make_unique<AclNode>();
18 18  
19 19 if (registerPattern)
20 20 {
... ... @@ -44,7 +44,7 @@ const AclNode *AclNode::getChildren(const std::string &amp;subtopic) const
44 44 AclNode *AclNode::getChildrenPlus()
45 45 {
46 46 if (!childrenPlus)
47   - childrenPlus.reset(new AclNode());
  47 + childrenPlus = std::make_unique<AclNode>();
48 48  
49 49 return childrenPlus.get();
50 50 }
... ...
authplugin.cpp
... ... @@ -417,7 +417,8 @@ void Authentication::loadMosquittoPasswordFile()
417 417 try
418 418 {
419 419 std::ifstream infile(this->mosquittoPasswordFile, std::ios::in);
420   - std::unique_ptr<std::unordered_map<std::string, MosquittoPasswordFileEntry>> passwordEntries_tmp(new std::unordered_map<std::string, MosquittoPasswordFileEntry>());
  420 + std::unique_ptr<std::unordered_map<std::string, MosquittoPasswordFileEntry>> passwordEntries_tmp =
  421 + std::make_unique<std::unordered_map<std::string, MosquittoPasswordFileEntry>>();
421 422  
422 423 for(std::string line; getline(infile, line ); )
423 424 {
... ...
configfileparser.cpp
... ... @@ -121,7 +121,7 @@ ConfigFileParser::ConfigFileParser(const std::string &amp;path) :
121 121 validListenKeys.insert("inet4_bind_address");
122 122 validListenKeys.insert("inet6_bind_address");
123 123  
124   - settings.reset(new Settings());
  124 + settings = std::make_unique<Settings>();
125 125 }
126 126  
127 127 void ConfigFileParser::loadFile(bool test)
... ... @@ -202,7 +202,7 @@ void ConfigFileParser::loadFile(bool test)
202 202  
203 203 ConfigParseLevel curParseLevel = ConfigParseLevel::Root;
204 204 std::shared_ptr<Listener> curListener;
205   - std::unique_ptr<Settings> tmpSettings(new Settings);
  205 + std::unique_ptr<Settings> tmpSettings = std::make_unique<Settings>();
206 206  
207 207 // Then once we know the config file is valid, process it.
208 208 for (std::string &line : lines)
... ... @@ -215,7 +215,7 @@ void ConfigFileParser::loadFile(bool test)
215 215 if (matches[1].str() == "listen")
216 216 {
217 217 curParseLevel = ConfigParseLevel::Listen;
218   - curListener.reset(new Listener);
  218 + curListener = std::make_shared<Listener>();
219 219 }
220 220 else
221 221 {
... ... @@ -481,7 +481,7 @@ void ConfigFileParser::loadFile(bool test)
481 481 std::unique_ptr<Settings> ConfigFileParser::moveSettings()
482 482 {
483 483 std::unique_ptr<Settings> tmp = std::move(settings);
484   - settings.reset(new Settings);
  484 + settings = std::make_unique<Settings>();
485 485 return tmp;
486 486 }
487 487  
... ...
listener.cpp
... ... @@ -83,7 +83,7 @@ void Listener::loadCertAndKeyFromConfig()
83 83  
84 84 if (!sslctx)
85 85 {
86   - sslctx.reset(new SslCtxManager());
  86 + sslctx = std::make_unique<SslCtxManager>();
87 87 SSL_CTX_set_options(sslctx->get(), SSL_OP_NO_SSLv3); // TODO: config option
88 88 SSL_CTX_set_options(sslctx->get(), SSL_OP_NO_TLSv1); // TODO: config option
89 89 }
... ...
mainapp.cpp
... ... @@ -23,6 +23,7 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
23 23 #include <stdio.h>
24 24 #include <sys/sysinfo.h>
25 25 #include <arpa/inet.h>
  26 +#include <memory>
26 27  
27 28 #include <openssl/ssl.h>
28 29 #include <openssl/err.h>
... ... @@ -36,12 +37,12 @@ License along with FlashMQ. If not, see &lt;https://www.gnu.org/licenses/&gt;.
36 37 MainApp *MainApp::instance = nullptr;
37 38  
38 39 MainApp::MainApp(const std::string &configFilePath) :
39   - subscriptionStore(new SubscriptionStore())
  40 + subscriptionStore(std::make_shared<SubscriptionStore>())
40 41 {
41 42 epollFdAccept = check<std::runtime_error>(epoll_create(999));
42 43 taskEventFd = eventfd(0, EFD_NONBLOCK);
43 44  
44   - confFileParser.reset(new ConfigFileParser(configFilePath));
  45 + confFileParser = std::make_unique<ConfigFileParser>(configFilePath);
45 46 loadConfig();
46 47  
47 48 this->num_threads = get_nprocs();
... ... @@ -437,14 +438,14 @@ void MainApp::start()
437 438 Authentication auth(settingsLocalCopy);
438 439 ThreadGlobals::assign(&auth);
439 440  
440   - std::shared_ptr<ThreadData> threaddata(new ThreadData(0, subscriptionStore, settings));
  441 + std::shared_ptr<ThreadData> threaddata = std::make_shared<ThreadData>(0, subscriptionStore, settings);
441 442  
442   - std::shared_ptr<Client> client(new Client(fd, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true));
443   - std::shared_ptr<Client> subscriber(new Client(fdnull, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true));
  443 + std::shared_ptr<Client> client = std::make_shared<Client>(fd, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true);
  444 + std::shared_ptr<Client> subscriber = std::make_shared<Client>(fdnull, threaddata, nullptr, fuzzWebsockets, nullptr, settings, true);
444 445 subscriber->setClientProperties(ProtocolVersion::Mqtt311, "subscriber", "subuser", true, 60, true);
445 446 subscriber->setAuthenticated(true);
446 447  
447   - std::shared_ptr<Client> websocketsubscriber(new Client(fdnull2, threaddata, nullptr, true, nullptr, settings, true));
  448 + std::shared_ptr<Client> websocketsubscriber = std::make_shared<Client>(fdnull2, threaddata, nullptr, true, nullptr, settings, true);
448 449 websocketsubscriber->setClientProperties(ProtocolVersion::Mqtt311, "websocketsubscriber", "websocksubuser", true, 60, true);
449 450 websocketsubscriber->setAuthenticated(true);
450 451 websocketsubscriber->setFakeUpgraded();
... ... @@ -483,7 +484,7 @@ void MainApp::start()
483 484  
484 485 for (int i = 0; i < num_threads; i++)
485 486 {
486   - std::shared_ptr<ThreadData> t(new ThreadData(i, subscriptionStore, settings));
  487 + std::shared_ptr<ThreadData> t = std::make_shared<ThreadData>(i, subscriptionStore, settings);
487 488 t->start(&do_thread_work);
488 489 threads.push_back(t);
489 490 }
... ... @@ -542,7 +543,7 @@ void MainApp::start()
542 543 SSL_set_fd(clientSSL, fd);
543 544 }
544 545  
545   - std::shared_ptr<Client> client(new Client(fd, thread_data, clientSSL, listener->websocket, addr, settings));
  546 + std::shared_ptr<Client> client = std::make_shared<Client>(fd, thread_data, clientSSL, listener->websocket, addr, settings);
546 547 thread_data->giveClient(client);
547 548 }
548 549 else
... ... @@ -655,7 +656,7 @@ void MainApp::loadConfig()
655 656  
656 657 if (settings->listeners.empty())
657 658 {
658   - std::shared_ptr<Listener> defaultListener(new Listener());
  659 + std::shared_ptr<Listener> defaultListener = std::make_shared<Listener>();
659 660 defaultListener->isValid();
660 661 settings->listeners.push_back(defaultListener);
661 662 }
... ...
sessionsandsubscriptionsdb.cpp
... ... @@ -88,7 +88,7 @@ SessionsAndSubscriptionsResult SessionsAndSubscriptionsDB::readDataV1()
88 88 readCheck(buf.data(), 1, clientIdLength, f);
89 89 std::string clientId(buf.data(), clientIdLength);
90 90  
91   - std::shared_ptr<Session> ses(new Session());
  91 + std::shared_ptr<Session> ses = std::make_shared<Session>();
92 92 result.sessions.push_back(ses);
93 93 ses->username = username;
94 94 ses->client_id = clientId;
... ...
subscriptionstore.cpp
... ... @@ -122,7 +122,7 @@ SubscriptionNode *SubscriptionStore::getDeepestNode(const std::string &amp;topic, co
122 122  
123 123 if (!node)
124 124 {
125   - node.reset(new SubscriptionNode(subtopic));
  125 + node = std::make_unique<SubscriptionNode>(subtopic);
126 126 }
127 127 deepestNode = node.get();
128 128 }
... ... @@ -240,7 +240,7 @@ void SubscriptionStore::registerClientAndKickExistingOne(std::shared_ptr&lt;Client&gt;
240 240  
241 241 if (!session || client->getCleanSession() || originalClientDemandsSessionDestruction)
242 242 {
243   - session.reset(new Session());
  243 + session = std::make_shared<Session>();
244 244  
245 245 sessionsById[client->getClientId()] = session;
246 246 }
... ... @@ -448,7 +448,7 @@ void SubscriptionStore::setRetainedMessage(const std::string &amp;topic, const std::
448 448  
449 449 if (!selectedChildren)
450 450 {
451   - selectedChildren.reset(new RetainedMessageNode());
  451 + selectedChildren = std::make_unique<RetainedMessageNode>();
452 452 }
453 453 deepestNode = selectedChildren.get();
454 454 }
... ...