From 3e1a96693d4863b4660dda8f3765744b649d0ff2 Mon Sep 17 00:00:00 2001 From: Lukas W Date: Tue, 17 Apr 2018 16:52:32 +0200 Subject: [PATCH] Replace NotePlayHandleManager implementation with MemoryPool --- include/NotePlayHandle.h | 30 +----------- src/core/InstrumentFunctions.cpp | 5 +- src/core/Mixer.cpp | 10 ++-- src/core/NotePlayHandle.cpp | 69 +--------------------------- src/core/PresetPreviewPlayHandle.cpp | 2 +- src/core/main.cpp | 3 -- src/tracks/InstrumentTrack.cpp | 6 +-- 7 files changed, 15 insertions(+), 110 deletions(-) diff --git a/include/NotePlayHandle.h b/include/NotePlayHandle.h index 99add8185..717dc6b8a 100644 --- a/include/NotePlayHandle.h +++ b/include/NotePlayHandle.h @@ -32,7 +32,7 @@ #include "Note.h" #include "PlayHandle.h" #include "Track.h" -#include "Memory.h" +#include "MemoryPool.h" class QReadWriteLock; class InstrumentTrack; @@ -44,7 +44,6 @@ typedef QList ConstNotePlayHandleList; class LMMS_EXPORT NotePlayHandle : public PlayHandle, public Note { - MM_OPERATORS public: void * m_pluginData; std::unique_ptr> m_filter; @@ -328,31 +327,6 @@ private: bool m_frequencyNeedsUpdate; // used to update pitch } ; - -const int INITIAL_NPH_CACHE = 256; -const int NPH_CACHE_INCREMENT = 16; - -class NotePlayHandleManager -{ - MM_OPERATORS -public: - static void init(); - static NotePlayHandle * acquire( InstrumentTrack* instrumentTrack, - const f_cnt_t offset, - const f_cnt_t frames, - const Note& noteToPlay, - NotePlayHandle* parent = NULL, - int midiEventChannel = -1, - NotePlayHandle::Origin origin = NotePlayHandle::OriginPattern ); - static void release( NotePlayHandle * nph ); - static void extend( int i ); - -private: - static NotePlayHandle ** s_available; - static QReadWriteLock s_mutex; - static std::atomic_int s_availableIndex; - static int s_size; -}; - +extern MemoryPool NotePlayHandlePool; #endif diff --git a/src/core/InstrumentFunctions.cpp b/src/core/InstrumentFunctions.cpp index 70e2c5e80..a710262b0 100644 --- a/src/core/InstrumentFunctions.cpp +++ b/src/core/InstrumentFunctions.cpp @@ -32,7 +32,6 @@ #include "PresetPreviewPlayHandle.h" #include "stdshims.h" - InstrumentFunctionNoteStacking::ChordTable::Init InstrumentFunctionNoteStacking::ChordTable::s_initTable[] = { { QT_TRANSLATE_NOOP( "InstrumentFunctionNoteStacking", "octave" ), { 0, -1 } }, @@ -263,7 +262,7 @@ void InstrumentFunctionNoteStacking::processNote( NotePlayHandle * _n ) // create sub-note-play-handle, only note is // different Engine::mixer()->addPlayHandle( - NotePlayHandleManager::acquire( _n->instrumentTrack(), _n->offset(), _n->frames(), note_copy, + NotePlayHandlePool.construct( _n->instrumentTrack(), _n->offset(), _n->frames(), note_copy, _n, -1, NotePlayHandle::OriginNoteStacking ) ); } @@ -502,7 +501,7 @@ void InstrumentFunctionArpeggio::processNote( NotePlayHandle * _n ) // create sub-note-play-handle, only ptr to note is different // and is_arp_note=true Engine::mixer()->addPlayHandle( - NotePlayHandleManager::acquire( _n->instrumentTrack(), + NotePlayHandlePool.construct( _n->instrumentTrack(), frames_processed, gated_frames, Note( MidiTime( 0 ), MidiTime( 0 ), sub_note_key, _n->getVolume(), diff --git a/src/core/Mixer.cpp b/src/core/Mixer.cpp index 29301fa63..8ad36c73b 100644 --- a/src/core/Mixer.cpp +++ b/src/core/Mixer.cpp @@ -400,7 +400,7 @@ const surroundSampleFrame * Mixer::renderNextBuffer() ( *it )->audioPort()->removePlayHandle( ( *it ) ); if( ( *it )->type() == PlayHandle::TypeNotePlayHandle ) { - NotePlayHandleManager::release( (NotePlayHandle*) *it ); + NotePlayHandlePool.destroy( (NotePlayHandle*) *it ); } else delete *it; m_playHandles.erase( it ); @@ -454,7 +454,7 @@ const surroundSampleFrame * Mixer::renderNextBuffer() ( *it )->audioPort()->removePlayHandle( ( *it ) ); if( ( *it )->type() == PlayHandle::TypeNotePlayHandle ) { - NotePlayHandleManager::release( (NotePlayHandle*) *it ); + NotePlayHandlePool.destroy( (NotePlayHandle*) *it ); } else delete *it; it = m_playHandles.erase( it ); @@ -689,7 +689,7 @@ bool Mixer::addPlayHandle( PlayHandle* handle ) if( handle->type() == PlayHandle::TypeNotePlayHandle ) { - NotePlayHandleManager::release( (NotePlayHandle*)handle ); + NotePlayHandlePool.destroy( (NotePlayHandle*)handle ); } else delete handle; @@ -742,7 +742,7 @@ void Mixer::removePlayHandle( PlayHandle * _ph ) { if( _ph->type() == PlayHandle::TypeNotePlayHandle ) { - NotePlayHandleManager::release( (NotePlayHandle*) _ph ); + NotePlayHandlePool.destroy( (NotePlayHandle*) _ph ); } else delete _ph; } @@ -768,7 +768,7 @@ void Mixer::removePlayHandlesOfTypes( Track * _track, const quint8 types ) ( *it )->audioPort()->removePlayHandle( ( *it ) ); if( ( *it )->type() == PlayHandle::TypeNotePlayHandle ) { - NotePlayHandleManager::release( (NotePlayHandle*) *it ); + NotePlayHandlePool.destroy( (NotePlayHandle*) *it ); } else delete *it; it = m_playHandles.erase( it ); diff --git a/src/core/NotePlayHandle.cpp b/src/core/NotePlayHandle.cpp index 0039f935a..c71020034 100644 --- a/src/core/NotePlayHandle.cpp +++ b/src/core/NotePlayHandle.cpp @@ -32,7 +32,6 @@ #include "Mixer.h" #include "Song.h" - NotePlayHandle::BaseDetuning::BaseDetuning( DetuningHelper *detuning ) : m_value( detuning ? detuning->automationPattern()->valueAt( 0 ) : 0 ) { @@ -543,69 +542,5 @@ void NotePlayHandle::resize( const bpm_t _new_tempo ) } } - -NotePlayHandle ** NotePlayHandleManager::s_available; -QReadWriteLock NotePlayHandleManager::s_mutex; -std::atomic_int NotePlayHandleManager::s_availableIndex; -int NotePlayHandleManager::s_size; - - -void NotePlayHandleManager::init() -{ - s_available = MM_ALLOC( NotePlayHandle*, INITIAL_NPH_CACHE ); - - NotePlayHandle * n = MM_ALLOC( NotePlayHandle, INITIAL_NPH_CACHE ); - - for( int i=0; i < INITIAL_NPH_CACHE; ++i ) - { - s_available[ i ] = n; - ++n; - } - s_availableIndex = INITIAL_NPH_CACHE - 1; - s_size = INITIAL_NPH_CACHE; -} - - -NotePlayHandle * NotePlayHandleManager::acquire( InstrumentTrack* instrumentTrack, - const f_cnt_t offset, - const f_cnt_t frames, - const Note& noteToPlay, - NotePlayHandle* parent, - int midiEventChannel, - NotePlayHandle::Origin origin ) -{ - // TODO: use some lockless data structures - s_mutex.lockForWrite(); - if (s_availableIndex < 0) { extend(NPH_CACHE_INCREMENT); } - NotePlayHandle * nph = s_available[s_availableIndex--]; - s_mutex.unlock(); - - new( (void*)nph ) NotePlayHandle( instrumentTrack, offset, frames, noteToPlay, parent, midiEventChannel, origin ); - return nph; -} - - -void NotePlayHandleManager::release( NotePlayHandle * nph ) -{ - nph->NotePlayHandle::~NotePlayHandle(); - s_mutex.lockForRead(); - s_available[++s_availableIndex] = nph; - s_mutex.unlock(); -} - - -void NotePlayHandleManager::extend( int c ) -{ - s_size += c; - NotePlayHandle ** tmp = MM_ALLOC( NotePlayHandle*, s_size ); - MM_FREE( s_available ); - s_available = tmp; - - NotePlayHandle * n = MM_ALLOC( NotePlayHandle, c ); - - for( int i=0; i < c; ++i ) - { - s_available[++s_availableIndex] = n; - ++n; - } -} +const int INITIAL_NPH_CACHE = 256; +MemoryPool NotePlayHandlePool{INITIAL_NPH_CACHE}; diff --git a/src/core/PresetPreviewPlayHandle.cpp b/src/core/PresetPreviewPlayHandle.cpp index ca0e52194..afb5a029c 100644 --- a/src/core/PresetPreviewPlayHandle.cpp +++ b/src/core/PresetPreviewPlayHandle.cpp @@ -182,7 +182,7 @@ PresetPreviewPlayHandle::PresetPreviewPlayHandle( const QString & _preset_file, Engine::mixer()->requestChangeInModel(); // create note-play-handle for it - m_previewNote = NotePlayHandleManager::acquire( + m_previewNote = NotePlayHandlePool.construct( s_previewTC->previewInstrumentTrack(), 0, typeInfo::max() / 2, Note( 0, 0, DefaultKey, 100 ) ); diff --git a/src/core/main.cpp b/src/core/main.cpp index f579ec8f6..1c4ba94af 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -286,9 +286,6 @@ int main( int argc, char * * argv ) qInstallMessageHandler(consoleMessageHandler); #endif - // initialize memory managers - NotePlayHandleManager::init(); - // intialize RNG srand( getpid() + time( 0 ) ); diff --git a/src/tracks/InstrumentTrack.cpp b/src/tracks/InstrumentTrack.cpp index 730ab97bd..18c73d49a 100644 --- a/src/tracks/InstrumentTrack.cpp +++ b/src/tracks/InstrumentTrack.cpp @@ -261,11 +261,11 @@ void InstrumentTrack::processInEvent( const MidiEvent& event, const MidiTime& ti if( m_notes[event.key()] == NULL ) { NotePlayHandle* nph = - NotePlayHandleManager::acquire( + NotePlayHandlePool.construct( this, offset, typeInfo::max() / 2, Note( MidiTime(), MidiTime(), event.key(), event.volume( midiPort()->baseVelocity() ) ), - NULL, event.channel(), + nullptr, event.channel(), NotePlayHandle::OriginMidiInput ); m_notes[event.key()] = nph; if( ! Engine::mixer()->addPlayHandle( nph ) ) @@ -691,7 +691,7 @@ bool InstrumentTrack::play( const MidiTime & _start, const fpp_t _frames, const f_cnt_t note_frames = cur_note->length().frames( frames_per_tick ); - NotePlayHandle* notePlayHandle = NotePlayHandleManager::acquire( this, _offset, note_frames, *cur_note ); + NotePlayHandle* notePlayHandle = NotePlayHandlePool.construct( this, _offset, note_frames, *cur_note ); notePlayHandle->setBBTrack( bb_track ); // are we playing global song? if( _tco_num < 0 )