From 2bba7d602744ccd6dab26940661f0fdfbf231af8 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 25 Dec 2016 21:34:03 +0100 Subject: Refactor flowgraph and puncturing rules --- src/Flowgraph.cpp | 129 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 86 insertions(+), 43 deletions(-) (limited to 'src/Flowgraph.cpp') diff --git a/src/Flowgraph.cpp b/src/Flowgraph.cpp index c199945..0eb1c60 100644 --- a/src/Flowgraph.cpp +++ b/src/Flowgraph.cpp @@ -2,7 +2,7 @@ Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) - Copyright (C) 2015 + Copyright (C) 2016 Matthias P. Braendli, matthias.braendli@mpb.li http://opendigitalradio.org @@ -27,6 +27,7 @@ #include "Flowgraph.h" #include "PcDebug.h" #include +#include #if HAVE_DECL__MM_MALLOC # include @@ -36,17 +37,12 @@ #include #include #include -#if defined(_WIN32) and !defined(__MINGW32__) -//#include -//#include -#else #include -#endif using namespace std; -typedef std::vector >::iterator NodeIterator; -typedef std::vector >::iterator EdgeIterator; +using NodeIterator = std::vector >::iterator; +using EdgeIterator = std::vector >::iterator; Node::Node(shared_ptr plugin) : @@ -55,10 +51,8 @@ Node::Node(shared_ptr plugin) : { PDEBUG("Node::Node(plugin(%s): %p) @ %p\n", plugin->name(), plugin.get(), this); - } - Node::~Node() { PDEBUG("Node::~Node() @ %p\n", this); @@ -67,6 +61,55 @@ Node::~Node() assert(myOutputBuffers.size() == 0); } +void Node::addOutputBuffer(Buffer::sptr& buffer) +{ + myOutputBuffers.push_back(buffer); +#if DEBUG + std::string fname = string(myPlugin->name()) + + "-" + to_string(myDebugFiles.size()) + + "-" + to_string((size_t)(void*)myPlugin.get()) + + ".dat"; + FILE* fd = fopen(fname.c_str(), "wb"); + assert(fd != nullptr); + myDebugFiles.push_back(fd); +#endif +} + +void Node::removeOutputBuffer(Buffer::sptr& buffer) +{ + auto it = std::find( + myOutputBuffers.begin(), + myOutputBuffers.end(), + buffer); + if (it != myOutputBuffers.end()) { +#if DEBUG + size_t pos = std::distance(myOutputBuffers.begin(), + it); + + auto fd_it = std::next(myDebugFiles.begin(), pos); + + fclose(*fd_it); + myDebugFiles.erase(fd_it); +#endif + myOutputBuffers.erase(it); + } +} + +void Node::addInputBuffer(Buffer::sptr& buffer) +{ + myInputBuffers.push_back(buffer); +} + +void Node::removeInputBuffer(Buffer::sptr& buffer) +{ + auto it = std::find( + myInputBuffers.begin(), + myInputBuffers.end(), + buffer); + if (it != myInputBuffers.end()) { + myInputBuffers.erase(it); + } +} Edge::Edge(shared_ptr& srcNode, shared_ptr& dstNode) : mySrcNode(srcNode), @@ -78,8 +121,8 @@ Edge::Edge(shared_ptr& srcNode, shared_ptr& dstNode) : this); myBuffer = make_shared(); - srcNode->myOutputBuffers.push_back(myBuffer); - dstNode->myInputBuffers.push_back(myBuffer); + srcNode->addOutputBuffer(myBuffer); + dstNode->addInputBuffer(myBuffer); } @@ -87,52 +130,52 @@ Edge::~Edge() { PDEBUG("Edge::~Edge() @ %p\n", this); - std::vector >::iterator buffer; - if (myBuffer != NULL) { - for (buffer = mySrcNode->myOutputBuffers.begin(); - buffer != mySrcNode->myOutputBuffers.end(); - ++buffer) { - if (*buffer == myBuffer) { - mySrcNode->myOutputBuffers.erase(buffer); - break; - } - } - - for (buffer = myDstNode->myInputBuffers.begin(); - buffer != myDstNode->myInputBuffers.end(); - ++buffer) { - if (*buffer == myBuffer) { - myDstNode->myInputBuffers.erase(buffer); - break; - } - } + if (myBuffer) { + mySrcNode->removeOutputBuffer(myBuffer); + myDstNode->removeInputBuffer(myBuffer); } } int Node::process() { - PDEBUG("Edge::process()\n"); + PDEBUG("Node::process()\n"); PDEBUG(" Plugin name: %s (%p)\n", myPlugin->name(), myPlugin.get()); - // the plugin process() still wants vector + // the plugin process() wants vector // arguments. std::vector inBuffers; - std::vector >::iterator buffer; - for (buffer = myInputBuffers.begin(); - buffer != myInputBuffers.end(); - ++buffer) { - inBuffers.push_back(buffer->get()); + for (auto& buffer : myInputBuffers) { + assert(buffer.get() != nullptr); + inBuffers.push_back(buffer.get()); } std::vector outBuffers; - for (buffer = myOutputBuffers.begin(); - buffer != myOutputBuffers.end(); - ++buffer) { - outBuffers.push_back(buffer->get()); + for (auto& buffer : myOutputBuffers) { + assert(buffer.get() != nullptr); + outBuffers.push_back(buffer.get()); } - return myPlugin->process(inBuffers, outBuffers); + int ret = myPlugin->process(inBuffers, outBuffers); +#if DEBUG + assert(myDebugFiles.size() == myOutputBuffers.size()); + + auto buf = myOutputBuffers.begin(); + auto fd_it = myDebugFiles.begin(); + + for (size_t i = 0; i < myDebugFiles.size(); i++) { + if (*buf) { + Buffer& b = *buf->get(); + FILE* fd = *fd_it; + + fwrite(b.getData(), b.getLength(), 1, fd); + } + + ++buf; + ++fd_it; + } +#endif + return ret; } -- cgit v1.2.3