From 8f8cb6a52f9cc03bae3a8449dd81538f567cf995 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Wed, 20 Aug 2014 11:24:29 +0200 Subject: [PATCH 1/3] keep server instance in a unique_ptr --- Server/Server.h | 17 +++++++- .../ServerFactory.h => Util/make_unique.hpp | 43 +++++++++++-------- routed.cpp | 8 ++-- 3 files changed, 44 insertions(+), 24 deletions(-) rename Server/ServerFactory.h => Util/make_unique.hpp (63%) diff --git a/Server/Server.h b/Server/Server.h index d5dab1ff3f7..b4f6bbd0764 100644 --- a/Server/Server.h +++ b/Server/Server.h @@ -28,14 +28,18 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef SERVER_H #define SERVER_H -#include "../Util/StringUtil.h" - #include "Connection.h" #include "RequestHandler.h" +#include "../Util/make_unique.hpp" +#include "../Util/SimpleLogger.h" +#include "../Util/StringUtil.h" + #include #include +#include + #include #include #include @@ -44,6 +48,15 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. class Server { public: + + static std::unique_ptr CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) + { + SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion(); + const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency()); + const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads); + return osrm::make_unique(ip_address, ip_port, real_num_threads); + } + explicit Server(const std::string &address, const int port, const unsigned thread_pool_size) : thread_pool_size(thread_pool_size), acceptor(io_service), new_connection(new http::Connection(io_service, request_handler)), request_handler() diff --git a/Server/ServerFactory.h b/Util/make_unique.hpp similarity index 63% rename from Server/ServerFactory.h rename to Util/make_unique.hpp index 5e037cf0587..50809d04356 100644 --- a/Server/ServerFactory.h +++ b/Util/make_unique.hpp @@ -25,25 +25,32 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef SERVER_FACTORY_H -#define SERVER_FACTORY_H +#ifndef MAKE_UNIQUE_H__ +#define MAKE_UNIQUE_H__ -#include "Server.h" -#include "../Util/SimpleLogger.h" +#include +#include +#include -#include +namespace osrm +{ +// Taken from http://msdn.microsoft.com/en-us/library/dn439780.asp +// Note, that the snippet is broken there and needed minor massaging + +// make_unique +template std::unique_ptr make_unique(Types &&... Args) +{ + return (std::unique_ptr(new T(std::forward(Args)...))); +} -struct ServerFactory +// make_unique +template std::unique_ptr make_unique(std::size_t Size) { - ServerFactory() = delete; - ServerFactory(const ServerFactory &) = delete; - static Server *CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) - { - SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion(); - const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency()); - const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads); - return new Server(ip_address, ip_port, real_num_threads); - } -}; - -#endif // SERVER_FACTORY_H + return (std::unique_ptr(new T[Size]())); +} + +// make_unique disallowed +template +typename std::enable_if::value != 0, void>::type make_unique(Types &&...) = delete; +} +#endif diff --git a/routed.cpp b/routed.cpp index f0891ce3ecf..e317797c588 100644 --- a/routed.cpp +++ b/routed.cpp @@ -26,7 +26,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "Library/OSRM.h" -#include "Server/ServerFactory.h" +#include "Server/Server.h" #include "Util/GitDescription.h" #include "Util/ProgramOptions.h" #include "Util/SimpleLogger.h" @@ -131,8 +131,8 @@ int main(int argc, const char *argv[]) #endif OSRM osrm_lib(server_paths, use_shared_memory); - Server *routing_server = - ServerFactory::CreateServer(ip_address, ip_port, requested_thread_num); + auto routing_server = + Server::CreateServer(ip_address, ip_port, requested_thread_num); routing_server->GetRequestHandlerPtr().RegisterRoutingMachine(&osrm_lib); @@ -181,7 +181,7 @@ int main(int argc, const char *argv[]) } SimpleLogger().Write() << "freeing objects"; - delete routing_server; + routing_server.reset(); SimpleLogger().Write() << "shutdown completed"; } catch (const std::exception &e) From 96318bbe11394b27728de2a6e4687beeafe7db36 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Wed, 20 Aug 2014 11:57:31 +0200 Subject: [PATCH 2/3] switch from unique to shared ptr --- Server/Server.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Server/Server.h b/Server/Server.h index b4f6bbd0764..a01b8068ae4 100644 --- a/Server/Server.h +++ b/Server/Server.h @@ -49,12 +49,13 @@ class Server { public: - static std::unique_ptr CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) + // Note: returns a shared instead of a unique ptr as it is captured in a lambda somewhere else + static std::shared_ptr CreateServer(std::string &ip_address, int ip_port, unsigned requested_num_threads) { SimpleLogger().Write() << "http 1.1 compression handled by zlib version " << zlibVersion(); const unsigned hardware_threads = std::max(1u, std::thread::hardware_concurrency()); const unsigned real_num_threads = std::min(hardware_threads, requested_num_threads); - return osrm::make_unique(ip_address, ip_port, real_num_threads); + return std::make_shared(ip_address, ip_port, real_num_threads); } explicit Server(const std::string &address, const int port, const unsigned thread_pool_size) From 6a8c5c886962a3fd4db861b187ef194d56b48a63 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Wed, 20 Aug 2014 11:57:46 +0200 Subject: [PATCH 3/3] fix typo --- Util/make_unique.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Util/make_unique.hpp b/Util/make_unique.hpp index 50809d04356..44020a09532 100644 --- a/Util/make_unique.hpp +++ b/Util/make_unique.hpp @@ -35,7 +35,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace osrm { // Taken from http://msdn.microsoft.com/en-us/library/dn439780.asp -// Note, that the snippet is broken there and needed minor massaging +// Note, that the snippet was broken there and needed minor massaging // make_unique template std::unique_ptr make_unique(Types &&... Args)