Some changes in memory management

This commit is contained in:
Matthias Schiffer 2013-07-27 14:21:46 +02:00
parent a7ccec3fef
commit 49652b9e28
5 changed files with 29 additions and 32 deletions

View file

@ -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);
}

View file

@ -37,11 +37,9 @@ void dispatch(const std::shared_ptr<packet_t> &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_t> &node, const std::shared_ptr<network_t> &net, const std::string &name, const gmrf_addr_t *address) {
@ -50,8 +48,8 @@ void add_iface(const std::shared_ptr<node_t> &node, const std::shared_ptr<networ
iface->name = 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);

View file

@ -59,7 +59,7 @@ void main(int argc, char *argv[]) {
std::exit(1);
}
const protocol_t *proto = load_protocol(argv[1]);
std::shared_ptr<const protocol_t> proto = load_protocol(argv[1]);
if (!proto)
std::exit(1);

View file

@ -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<const MMSS::protocol_t> &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<MMSS::node_t> create(MMSS::context_t *mmss, const std::string &name, unsigned rand_seed, const MMSS::protocol_t *proto) {
static std::shared_ptr<MMSS::node_t> create(MMSS::context_t *mmss, const std::string &name, unsigned rand_seed, const std::shared_ptr<const MMSS::protocol_t> &proto) {
MMSS::node_t *node = new MMSS::node_t(mmss, name, rand_seed, proto);
std::shared_ptr<MMSS::node_t> ptr(node);
@ -60,7 +60,7 @@ public:
unsigned rand_seed;
const MMSS::protocol_t *proto;
std::shared_ptr<const MMSS::protocol_t> proto;
};
struct gmrf_iface {
@ -84,8 +84,8 @@ public:
std::string name;
gmrf_addr_t address;
std::weak_ptr<MMSS::node_t> node;
std::weak_ptr<MMSS::network_t> 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<const protocol_t> load_protocol(const char *module);
bool read_config(context_t *mmss, config_t *conf, const char *filename);

View file

@ -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<const protocol_t> 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<const protocol_t>();
}
::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<const protocol_t>();
}
std::fprintf(stderr, "loaded protocol `%s' version %s\n", proto->get_name(), proto->get_version());
return proto;
return std::shared_ptr<const protocol_t>(proto, dlcloser(handle));
}
}