From 49652b9e2882a1c3cd5203ea91f90cdd32a090e9 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sat, 27 Jul 2013 14:21:46 +0200 Subject: Some changes in memory management --- mmss/gmrf.cpp | 18 +++--------------- mmss/iface.cpp | 10 ++++------ mmss/mmss.cpp | 2 +- mmss/mmss.hpp | 12 ++++++------ mmss/protocol.cpp | 19 +++++++++++++++---- 5 files changed, 29 insertions(+), 32 deletions(-) diff --git a/mmss/gmrf.cpp b/mmss/gmrf.cpp index 08db8e6..3a8a224 100644 --- a/mmss/gmrf.cpp +++ b/mmss/gmrf.cpp @@ -41,22 +41,14 @@ const char* gmrf_iface_get_name(gmrf_t *gmrf, gmrf_iface_t *iface) { } size_t gmrf_iface_get_mtu(gmrf_t *gmrf, gmrf_iface_t *iface) { - auto net = iface->net.lock(); - if (!net) - return 0; - - return net->mtu; + return iface->net->mtu; } bool gmrf_iface_send(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, size_t len, const gmrf_addr_t *dest) { auto src_iface = iface->iface.lock(); assert(src_iface); - auto net = src_iface->net.lock(); - if (!net) - return false; - - for (auto dest_iface : net->interfaces) { + for (auto dest_iface : src_iface->net->interfaces) { if (gmrf_addr_equal(&dest_iface->address, dest)) { MMSS::enqueue(gmrf->mmss, src_iface, dest_iface, data, len); break; @@ -70,11 +62,7 @@ bool gmrf_iface_send_bc(gmrf_t *gmrf, gmrf_iface_t *iface, const void *data, siz auto src_iface = iface->iface.lock(); assert(src_iface); - auto net = src_iface->net.lock(); - if (!net) - return false; - - for (auto dest_iface : net->interfaces) { + for (auto dest_iface : src_iface->net->interfaces) { if (dest_iface != src_iface) MMSS::enqueue(gmrf->mmss, src_iface, dest_iface, data, len); } diff --git a/mmss/iface.cpp b/mmss/iface.cpp index f54c1ea..38808f1 100644 --- a/mmss/iface.cpp +++ b/mmss/iface.cpp @@ -37,11 +37,9 @@ void dispatch(const std::shared_ptr &packet) { if (!source || !dest) return; - auto node = dest->node.lock(); - if (!node) - return; + node_t *node = dest->node; - node->proto->handle_packet(node.get(), node->ctx, dest.get(), &source->address, packet->data.get(), packet->len); + node->proto->handle_packet(node, node->ctx, dest.get(), &source->address, packet->data.get(), packet->len); } void add_iface(const std::shared_ptr &node, const std::shared_ptr &net, const std::string &name, const gmrf_addr_t *address) { @@ -50,8 +48,8 @@ void add_iface(const std::shared_ptr &node, const std::shared_ptrname = name; iface->address = *address; - iface->node = node; - iface->net = net; + iface->node = node.get(); + iface->net = net.get(); node->interfaces.push_back(iface); net->interfaces.push_back(iface); diff --git a/mmss/mmss.cpp b/mmss/mmss.cpp index 0e3b230..bbdb0ca 100644 --- a/mmss/mmss.cpp +++ b/mmss/mmss.cpp @@ -59,7 +59,7 @@ void main(int argc, char *argv[]) { std::exit(1); } - const protocol_t *proto = load_protocol(argv[1]); + std::shared_ptr proto = load_protocol(argv[1]); if (!proto) std::exit(1); diff --git a/mmss/mmss.hpp b/mmss/mmss.hpp index d7202e7..723e0d6 100644 --- a/mmss/mmss.hpp +++ b/mmss/mmss.hpp @@ -34,13 +34,13 @@ struct gmrf { private: - gmrf(MMSS::context_t *mmss0, const std::string &name0, unsigned rand_seed0, const MMSS::protocol_t *proto0) : + gmrf(MMSS::context_t *mmss0, const std::string &name0, unsigned rand_seed0, const std::shared_ptr &proto0) : mmss(mmss0), name(name0), rand_seed(rand_seed0), proto(proto0) {} gmrf(gmrf const&) = delete; gmrf& operator=(gmrf const&) = delete; public: - static std::shared_ptr create(MMSS::context_t *mmss, const std::string &name, unsigned rand_seed, const MMSS::protocol_t *proto) { + static std::shared_ptr create(MMSS::context_t *mmss, const std::string &name, unsigned rand_seed, const std::shared_ptr &proto) { MMSS::node_t *node = new MMSS::node_t(mmss, name, rand_seed, proto); std::shared_ptr ptr(node); @@ -60,7 +60,7 @@ public: unsigned rand_seed; - const MMSS::protocol_t *proto; + std::shared_ptr proto; }; struct gmrf_iface { @@ -84,8 +84,8 @@ public: std::string name; gmrf_addr_t address; - std::weak_ptr node; - std::weak_ptr net; + MMSS::node_t *node; + MMSS::network_t *net; }; @@ -132,7 +132,7 @@ public: }; -const protocol_t* load_protocol(const char *module); +std::shared_ptr load_protocol(const char *module); bool read_config(context_t *mmss, config_t *conf, const char *filename); diff --git a/mmss/protocol.cpp b/mmss/protocol.cpp index 3bbfeb1..246b136 100644 --- a/mmss/protocol.cpp +++ b/mmss/protocol.cpp @@ -32,11 +32,22 @@ namespace MMSS { -const protocol_t* load_protocol(const char *module) { +class dlcloser { +private: + void *handle; + +public: + dlcloser(void *handle0) : handle(handle0) {} + void operator()(const protocol_t *ptr) const { + ::dlclose(handle); + } +}; + +std::shared_ptr load_protocol(const char *module) { void *handle = ::dlopen(module, RTLD_NOW); if (!handle) { std::fprintf(stderr, "unable to load protocol from `%s': %s\n", module, dlerror()); - return NULL; + return std::shared_ptr(); } ::dlerror(); @@ -44,12 +55,12 @@ const protocol_t* load_protocol(const char *module) { if (!proto) { std::fprintf(stderr, "unable to load protocol from `%s': %s\n", module, dlerror()); ::dlclose(handle); - return NULL; + return std::shared_ptr(); } std::fprintf(stderr, "loaded protocol `%s' version %s\n", proto->get_name(), proto->get_version()); - return proto; + return std::shared_ptr(proto, dlcloser(handle)); } } -- cgit v1.2.3