From f42ae722cc18573378db3318c74f5d82f28b24ca Mon Sep 17 00:00:00 2001 From: falkTX Date: Thu, 10 Apr 2014 15:30:40 +0000 Subject: [PATCH] Simplify LinkedList a bit, other misc changes --- source/tests/RtLinkedList.cpp | 4 +- source/utils/CarlaDssiUtils.cpp | 41 +++++----- source/utils/CarlaDssiUtils.hpp | 2 +- source/utils/CarlaLadspaUtils.hpp | 12 +-- source/utils/CarlaOscUtils.hpp | 2 +- source/utils/LinkedList.hpp | 122 +++++++++++++----------------- source/utils/RtLinkedList.hpp | 83 ++++++++++---------- 7 files changed, 128 insertions(+), 138 deletions(-) diff --git a/source/tests/RtLinkedList.cpp b/source/tests/RtLinkedList.cpp index 085672505..b20b1f6ac 100644 --- a/source/tests/RtLinkedList.cpp +++ b/source/tests/RtLinkedList.cpp @@ -43,8 +43,8 @@ struct PostRtEvents { PostRtEvents() : dataPool(MIN_RT_EVENTS, MAX_RT_EVENTS), - data(dataPool), - dataPendingRT(dataPool) {} + data(dataPool, true), + dataPendingRT(dataPool, true) {} ~PostRtEvents() { diff --git a/source/utils/CarlaDssiUtils.cpp b/source/utils/CarlaDssiUtils.cpp index 61520ba59..1697e07b2 100644 --- a/source/utils/CarlaDssiUtils.cpp +++ b/source/utils/CarlaDssiUtils.cpp @@ -23,38 +23,41 @@ // ----------------------------------------------------------------------- -const char* find_dssi_ui(const char* const filename, const char* const label) +const char* find_dssi_ui(const char* const filename, const char* const label) noexcept { CARLA_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', nullptr); CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', nullptr); carla_debug("find_dssi_ui(\"%s\", \"%s\")", filename, label); - QString guiFilename; - QString pluginDir(filename); - pluginDir.resize(pluginDir.lastIndexOf(".")); + try { + QString guiFilename; + QString pluginDir(filename); + pluginDir.resize(pluginDir.lastIndexOf(".")); - QString checkLabel(label); - QString checkSName(QFileInfo(pluginDir).baseName()); + QString checkLabel(label); + QString checkSName(QFileInfo(pluginDir).baseName()); - if (! checkLabel.endsWith("_")) checkLabel += "_"; - if (! checkSName.endsWith("_")) checkSName += "_"; + if (! checkLabel.endsWith("_")) checkLabel += "_"; + if (! checkSName.endsWith("_")) checkSName += "_"; - QStringList guiFiles(QDir(pluginDir).entryList()); + QStringList guiFiles(QDir(pluginDir).entryList()); - foreach (const QString& gui, guiFiles) - { - if (gui.startsWith(checkLabel) || gui.startsWith(checkSName)) + foreach (const QString& gui, guiFiles) { - QFileInfo finalname(pluginDir + QDir::separator() + gui); - guiFilename = finalname.absoluteFilePath(); - break; + if (gui.startsWith(checkLabel) || gui.startsWith(checkSName)) + { + QFileInfo finalname(pluginDir + QDir::separator() + gui); + guiFilename = finalname.absoluteFilePath(); + break; + } } - } - if (guiFilename.isEmpty()) - return nullptr; + if (guiFilename.isEmpty()) + return nullptr; - return carla_strdup(guiFilename.toUtf8().constData()); + return carla_strdup(guiFilename.toUtf8().constData()); + + } CARLA_SAFE_EXCEPTION_RETURN("find_dssi_ui", nullptr); } // ----------------------------------------------------------------------- diff --git a/source/utils/CarlaDssiUtils.hpp b/source/utils/CarlaDssiUtils.hpp index 6b1feddbe..f952508a5 100644 --- a/source/utils/CarlaDssiUtils.hpp +++ b/source/utils/CarlaDssiUtils.hpp @@ -24,7 +24,7 @@ // ----------------------------------------------------------------------- // Find UI binary for a plugin (returned value must be deleted) -const char* find_dssi_ui(const char* const filename, const char* const label); +const char* find_dssi_ui(const char* const filename, const char* const label) noexcept; // ----------------------------------------------------------------------- diff --git a/source/utils/CarlaLadspaUtils.hpp b/source/utils/CarlaLadspaUtils.hpp index 3815c6adf..dfbf0baf2 100644 --- a/source/utils/CarlaLadspaUtils.hpp +++ b/source/utils/CarlaLadspaUtils.hpp @@ -49,7 +49,7 @@ const LADSPA_RDF_Descriptor* ladspa_rdf_dup(const LADSPA_RDF_Descriptor* const o { newDescriptor->Ports = new LADSPA_RDF_Port[newDescriptor->PortCount]; - for (unsigned long i=0; i < newDescriptor->PortCount; ++i) + for (ulong i=0; i < newDescriptor->PortCount; ++i) { LADSPA_RDF_Port* const oldPort(&oldDescriptor->Ports[i]); LADSPA_RDF_Port* const newPort(&newDescriptor->Ports[i]); @@ -67,7 +67,7 @@ const LADSPA_RDF_Descriptor* ladspa_rdf_dup(const LADSPA_RDF_Descriptor* const o { newPort->ScalePoints = new LADSPA_RDF_ScalePoint[oldPort->ScalePointCount]; - for (unsigned long j=0; j < oldPort->ScalePointCount; ++j) + for (ulong j=0; j < oldPort->ScalePointCount; ++j) { LADSPA_RDF_ScalePoint* const oldScalePoint(&oldPort->ScalePoints[j]); LADSPA_RDF_ScalePoint* const newScalePoint(&newPort->ScalePoints[j]); @@ -124,7 +124,7 @@ bool is_ladspa_rdf_descriptor_valid(const LADSPA_RDF_Descriptor* const rdfDescri return false; } - for (unsigned long i=0; i < rdfDescriptor->PortCount; ++i) + for (ulong i=0; i < rdfDescriptor->PortCount; ++i) { if (! is_ladspa_port_good(rdfDescriptor->Ports[i].Type, descriptor->PortDescriptors[i])) { @@ -169,7 +169,7 @@ LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor h { try { return std::exp((std::log(min)*0.75f) + (std::log(max)*0.25f)); - } catch(...) {} + } CARLA_SAFE_EXCEPTION("exp(log)"); } return (min*0.75f) + (max*0.25f); @@ -178,7 +178,7 @@ LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor h { try { return std::sqrt(min*max); - } catch(...) {} + } CARLA_SAFE_EXCEPTION("sqrt"); } return (min+max)/2; @@ -187,7 +187,7 @@ LADSPA_Data get_default_ladspa_port_value(const LADSPA_PortRangeHintDescriptor h { try { return std::exp((std::log(min)*0.25f) + (std::log(max)*0.75f)); - } catch(...) {} + } CARLA_SAFE_EXCEPTION("exp(log)"); } return (min*0.25f) + (max*0.75f); } diff --git a/source/utils/CarlaOscUtils.hpp b/source/utils/CarlaOscUtils.hpp index bdb785274..8c5867ed3 100644 --- a/source/utils/CarlaOscUtils.hpp +++ b/source/utils/CarlaOscUtils.hpp @@ -25,7 +25,7 @@ #define try_lo_send(...) \ try { \ lo_send(__VA_ARGS__); \ - } catch(...) {} + } CARLA_SAFE_EXCEPTION("lo_send"); // ----------------------------------------------------------------------- diff --git a/source/utils/LinkedList.hpp b/source/utils/LinkedList.hpp index 0f8b2e084..fbda852a4 100644 --- a/source/utils/LinkedList.hpp +++ b/source/utils/LinkedList.hpp @@ -57,9 +57,10 @@ protected: k_list_head siblings; }; - AbstractLinkedList() noexcept + AbstractLinkedList(const bool needsCopyCtr) noexcept : fDataSize(sizeof(Data)), - fCount(0) + fCount(0), + fNeedsCopyCtr(needsCopyCtr) { _init(); } @@ -146,86 +147,22 @@ public: bool append(const T& value) noexcept { - if (Data* const data = _allocate()) - { - try { - new(data)Data(); - } - catch(...) { - _deallocate(data); - return false; - } - - data->value = value; - list_add_tail(&data->siblings, &fQueue); - ++fCount; - return true; - } - - return false; + return _add(value, true, &fQueue); } bool appendAt(const T& value, const Itenerator& it) noexcept { - if (Data* const data = _allocate()) - { - try { - new(data)Data(); - } - catch(...) { - _deallocate(data); - return false; - } - - data->value = value; - list_add_tail(&data->siblings, it.fEntry->next); - ++fCount; - return true; - } - - return false; + return _add(value, true, it.fEntry->next); } bool insert(const T& value) noexcept { - if (Data* const data = _allocate()) - { - try { - new(data)Data(); - } - catch(...) { - _deallocate(data); - return false; - } - - data->value = value; - list_add(&data->siblings, &fQueue); - ++fCount; - return true; - } - - return false; + return _add(value, false, &fQueue); } bool insertAt(const T& value, const Itenerator& it) noexcept { - if (Data* const data = _allocate()) - { - try { - new(data)Data(); - } - catch(...) { - _deallocate(data); - return false; - } - - data->value = value; - list_add(&data->siblings, it.fEntry->prev); - ++fCount; - return true; - } - - return false; + return _add(value, false, it.fEntry->prev); } T& getAt(const size_t index) const noexcept @@ -399,6 +336,8 @@ protected: size_t fCount; k_list_head fQueue; + const bool fNeedsCopyCtr; + virtual Data* _allocate() noexcept = 0; virtual void _deallocate(Data* const dataPtr) noexcept = 0; @@ -411,6 +350,46 @@ private: INIT_LIST_HEAD(&fQueue); } + bool _add(const T& value, const bool inTail, k_list_head* const queue) noexcept + { + if (Data* const data = _allocate()) + { + if (fNeedsCopyCtr) + { + try { + new(data)Data(); + } + catch(...) { + _deallocate(data); + return false; + } + + try { + data->value = value; + } + catch(...) { + data->~Data(); + _deallocate(data); + return false; + } + } + else + { + std::memcpy(&data->value, &value, fDataSize); + } + + if (inTail) + list_add_tail(&data->siblings, queue); + else + list_add(&data->siblings, queue); + + ++fCount; + return true; + } + + return false; + } + T& _getFirstOrLast(const bool first, const bool removeObj) noexcept { if (fCount == 0) @@ -447,7 +426,8 @@ template class LinkedList : public AbstractLinkedList { public: - LinkedList() noexcept {} + LinkedList(const bool needsCopyCtr = false) noexcept + : AbstractLinkedList(needsCopyCtr) {} private: typename AbstractLinkedList::Data* _allocate() noexcept override diff --git a/source/utils/RtLinkedList.hpp b/source/utils/RtLinkedList.hpp index c417c00be..3dd9bfdf3 100644 --- a/source/utils/RtLinkedList.hpp +++ b/source/utils/RtLinkedList.hpp @@ -98,51 +98,18 @@ public: // ------------------------------------------------------------------- // Now the actual rt-linkedlist code - RtLinkedList(Pool& memPool) noexcept - : fMemPool(memPool) {} + RtLinkedList(Pool& memPool, const bool needsCopyCtr = false) noexcept + : AbstractLinkedList(needsCopyCtr), + fMemPool(memPool) {} bool append_sleepy(const T& value) noexcept { - if (typename AbstractLinkedList::Data* const data = _allocate_sleepy()) - { - try { - new(data)typename AbstractLinkedList::Data(); - } - catch(...) { - _deallocate(data); - return false; - } - - data->value = value; - list_add_tail(&data->siblings, &this->fQueue); - ++(this->fCount); - - return true; - } - - return false; + return _add_sleepy(value, true); } bool insert_sleepy(const T& value) noexcept { - if (typename AbstractLinkedList::Data* const data = _allocate_sleepy()) - { - try { - new(data)typename AbstractLinkedList::Data(); - } - catch(...) { - _deallocate(data); - return false; - } - - data->value = value; - list_add(&data->siblings, &this->fQueue); - ++(this->fCount); - - return true; - } - - return false; + return _add_sleepy(value, false); } void resize(const size_t minPreallocated, const size_t maxPreallocated) noexcept @@ -186,6 +153,46 @@ private: fMemPool.deallocate(dataPtr); } + bool _add_sleepy(const T& value, const bool inTail) noexcept + { + if (typename AbstractLinkedList::Data* const data = _allocate_sleepy()) + { + if (this->fNeedsCopyCtr) + { + try { + new(data)typename AbstractLinkedList::Data(); + } + catch(...) { + _deallocate(data); + return false; + } + + try { + data->value = value; + } + catch(...) { + data->~Data(); + _deallocate(data); + return false; + } + } + else + { + std::memcpy(&data->value, &value, this->fDataSize); + } + + if (inTail) + list_add_tail(&data->siblings, &this->fQueue); + else + list_add(&data->siblings, &this->fQueue); + + ++(this->fCount); + return true; + } + + return false; + } + LINKED_LIST_DECLARATIONS(RtLinkedList) };