From 6f685ae7ac93a04963532c21152fa0491931988b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 29 Jan 2016 20:59:43 +0100 Subject: Remove pointer from audiofiles map. --- src/audiocachefile.cc | 25 +++++++++---------------- src/audiocachefile.h | 2 +- test/audiocachefiletest.cc | 6 ++++-- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc index 91fcecf..e9ed322 100644 --- a/src/audiocachefile.cc +++ b/src/audiocachefile.cc @@ -130,25 +130,19 @@ AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename) { std::lock_guard lock(mutex); - AudioCacheFile* cache_audio_file = nullptr; - auto it = audiofiles.find(filename); if(it == audiofiles.end()) { - cache_audio_file = new AudioCacheFile(filename, read_buffer); - audiofiles.insert(std::make_pair(filename, cache_audio_file)); - } - else - { - cache_audio_file = it->second; + it = audiofiles.emplace(std::piecewise_construct, std::make_tuple(filename), + std::make_tuple(filename, std::ref(read_buffer))).first; } - assert(cache_audio_file); + auto& cache_audio_file = it->second; // Increase ref count. - ++cache_audio_file->ref; + ++cache_audio_file.ref; - return *cache_audio_file; + return cache_audio_file; } void AudioCacheFiles::releaseFile(const std::string& filename) @@ -162,14 +156,13 @@ void AudioCacheFiles::releaseFile(const std::string& filename) return; // not open } - auto audiofile = it->second; + auto& audiofile = it->second; - assert(audiofile->ref); // If ref is not > 0 it shouldn't be in the map. + assert(audiofile.ref); // If ref is not > 0 it shouldn't be in the map. - --audiofile->ref; - if(audiofile->ref == 0) + --audiofile.ref; + if(audiofile.ref == 0) { - delete audiofile; audiofiles.erase(it); } } diff --git a/src/audiocachefile.h b/src/audiocachefile.h index 8e40a2e..343133e 100644 --- a/src/audiocachefile.h +++ b/src/audiocachefile.h @@ -95,7 +95,7 @@ public: void releaseFile(const std::string& filename); protected: - std::map audiofiles; + std::map audiofiles; std::mutex mutex; std::vector read_buffer; }; diff --git a/test/audiocachefiletest.cc b/test/audiocachefiletest.cc index b7839b7..6f84ba2 100644 --- a/test/audiocachefiletest.cc +++ b/test/audiocachefiletest.cc @@ -39,12 +39,14 @@ public: //void release(const std::string& filename); int getRef(const std::string& filename) { - if(audiofiles.find(filename) == audiofiles.end()) + auto it = audiofiles.find(filename); + + if(it == audiofiles.end()) { return -1; } - return audiofiles[filename]->ref; + return (it->second).ref; } }; -- cgit v1.2.3 From 9f9d9f1da1536f7b6103e3eb260d3f5dc573294c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 29 Jan 2016 21:09:37 +0100 Subject: Add a comment to clarify. --- src/audiocachefile.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/audiocachefile.cc b/src/audiocachefile.cc index e9ed322..7b05bfc 100644 --- a/src/audiocachefile.cc +++ b/src/audiocachefile.cc @@ -133,6 +133,7 @@ AudioCacheFile& AudioCacheFiles::getFile(const std::string& filename) auto it = audiofiles.find(filename); if(it == audiofiles.end()) { + // Construct a new AudioCacheFile in place. The in place construction is relevant. it = audiofiles.emplace(std::piecewise_construct, std::make_tuple(filename), std::make_tuple(filename, std::ref(read_buffer))).first; } -- cgit v1.2.3 From 07e8fba977bdc8162991857a19ca838393ce79bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 29 Jan 2016 22:29:07 +0100 Subject: audiocacheeventhandler review --- src/audiocacheeventhandler.cc | 29 ++++++++++++++--------------- src/audiocacheeventhandler.h | 3 +-- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/audiocacheeventhandler.cc b/src/audiocacheeventhandler.cc index 6ec68b5..788cac6 100644 --- a/src/audiocacheeventhandler.cc +++ b/src/audiocacheeventhandler.cc @@ -41,7 +41,7 @@ enum class EventType { class CacheEvent { public: - EventType eventType; + EventType event_type; // For close event: cacheid_t id; @@ -105,8 +105,7 @@ void AudioCacheEventHandler::setThreaded(bool threaded) { start(); } - - if(!threaded && running) + else if(!threaded && running) { stop(); } @@ -135,7 +134,7 @@ void AudioCacheEventHandler::pushLoadNextEvent(AudioCacheFile* afile, volatile bool* ready) { CacheEvent cache_event; - cache_event.eventType = EventType::LoadNext; + cache_event.event_type = EventType::LoadNext; cache_event.pos = pos; cache_event.afile = afile; @@ -154,7 +153,7 @@ void AudioCacheEventHandler::pushLoadNextEvent(AudioCacheFile* afile, void AudioCacheEventHandler::pushCloseEvent(cacheid_t id) { CacheEvent cache_event; - cache_event.eventType = EventType::Close; + cache_event.event_type = EventType::Close; cache_event.id = id; pushEvent(cache_event); @@ -172,17 +171,17 @@ void AudioCacheEventHandler::setChunkSize(size_t chunksize) return; } - DEBUG(cache, "1)\n"); + DEBUG(cache, "setChunkSize 1\n"); // Remove all events from event queue. clearEvents(); - DEBUG(cache, "2)\n"); + DEBUG(cache, "setChunkSize 2\n"); // Skip all active cacheids and make their buffers point at nodata. id_manager.disableActive(); - DEBUG(cache, "3)\n"); + DEBUG(cache, "setChunkSize 3\n"); this->chunksize = chunksize; } @@ -203,7 +202,7 @@ void AudioCacheEventHandler::clearEvents() // Iterate all events ignoring load events and handling close events. for(auto& event : eventqueue) { - if(event.eventType == EventType::Close) + if(event.event_type == EventType::Close) { handleCloseCache(event.id); // This method does not lock. } @@ -224,21 +223,21 @@ void AudioCacheEventHandler::handleCloseEvent(CacheEvent& cache_event) handleCloseCache(cache_event.id); } -void AudioCacheEventHandler::handleCloseCache(cacheid_t cacheid) +void AudioCacheEventHandler::handleCloseCache(cacheid_t id) { - auto& cache = id_manager.getCache(cacheid); + auto& cache = id_manager.getCache(id); files.releaseFile(cache.afile->getFilename()); delete[] cache.front; delete[] cache.back; - id_manager.releaseID(cacheid); + id_manager.releaseID(id); } void AudioCacheEventHandler::handleEvent(CacheEvent& cache_event) { - switch(cache_event.eventType) + switch(cache_event.event_type) { case EventType::LoadNext: handleLoadNextEvent(cache_event); @@ -291,11 +290,11 @@ void AudioCacheEventHandler::pushEvent(CacheEvent& cache_event) bool found = false; - if(cache_event.eventType == EventType::LoadNext) + if(cache_event.event_type == EventType::LoadNext) { for(auto& queued_event : eventqueue) { - if((queued_event.eventType == EventType::LoadNext) && + if((queued_event.event_type == EventType::LoadNext) && (cache_event.afile->getFilename() == queued_event.afile->getFilename()) && (cache_event.pos == queued_event.pos)) diff --git a/src/audiocacheeventhandler.h b/src/audiocacheeventhandler.h index 473a010..f00a080 100644 --- a/src/audiocacheeventhandler.h +++ b/src/audiocacheeventhandler.h @@ -27,7 +27,6 @@ #pragma once #include -#include #include #include "thread.h" @@ -88,7 +87,7 @@ protected: void handleCloseEvent(CacheEvent& cache_event); //! Close decrease the file ref and release the cache id. - void handleCloseCache(cacheid_t cacheid); + void handleCloseCache(cacheid_t id); void handleEvent(CacheEvent& cache_event); -- cgit v1.2.3 From 22715c84cc861ba6ce7e9caa5d214bb3f006679d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Nusser?= Date: Fri, 29 Jan 2016 22:31:14 +0100 Subject: Added test binaries to .gitignore --- .gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.gitignore b/.gitignore index c819996..a2767e8 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,11 @@ test/engine test/gui test/lv2 test/resource +test/audiocache +test/audiocacheeventhandler +test/audiocachefile +test/audiocacheidmanager +test/configfile drumgizmo-*.tar.gz tst vst/Makefile.mingw32 -- cgit v1.2.3