From 2a0890938049bbe5ddef25fc36f4f6b515bea3e9 Mon Sep 17 00:00:00 2001 From: Lukas W Date: Thu, 30 Nov 2017 12:39:15 +0100 Subject: [PATCH] Move aligned malloc to MemoryManager.h, port to stl allocator --- include/MemoryHelper.h | 42 ------------------------ include/MemoryManager.h | 36 ++++++++++++++++++++- src/core/CMakeLists.txt | 1 - src/core/MemoryHelper.cpp | 65 -------------------------------------- src/core/MemoryManager.cpp | 29 +++++++++++++++++ src/core/Mixer.cpp | 10 +++--- 6 files changed, 69 insertions(+), 114 deletions(-) delete mode 100644 include/MemoryHelper.h delete mode 100644 src/core/MemoryHelper.cpp diff --git a/include/MemoryHelper.h b/include/MemoryHelper.h deleted file mode 100644 index 7bd31bf2b..000000000 --- a/include/MemoryHelper.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2014 Simon Symeonidis - * Copyright (c) 2004-2014 Tobias Doerffel - * - * This file is part of LMMS - https://lmms.io - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program (see COPYING); if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - */ - - -#ifndef MEMORY_HELPER_H -#define MEMORY_HELPER_H - -/** - * Helper class to alocate aligned memory and free it. - */ -class MemoryHelper { -public: - - static void* alignedMalloc( size_t ); - - static void alignedFree( void* ); - -private: -}; - -#endif - diff --git a/include/MemoryManager.h b/include/MemoryManager.h index 4c225e026..a739cae27 100644 --- a/include/MemoryManager.h +++ b/include/MemoryManager.h @@ -2,8 +2,9 @@ * MemoryManager.h * * Copyright (c) 2017 Lukas W + * Copyright (c) 2014 Simon Symeonidis * Copyright (c) 2014 Vesa Kivimäki - * Copyright (c) 2007-2014 Tobias Doerffel + * Copyright (c) 2004-2014 Tobias Doerffel * * This file is part of LMMS - https://lmms.io * @@ -62,8 +63,41 @@ struct MmAllocator } typedef std::vector > vector; + +private: + MemoryManager::ThreadGuard m_threadGuard; }; +class _AlignedAllocator_Base +{ +protected: + void* alloc_impl( size_t n, size_t alignment ); + void dealloc_impl( void* p ); +}; + +template +class AlignedAllocator : _AlignedAllocator_Base +{ +public: + typedef T value_type; + template struct rebind { typedef MmAllocator other; }; + + AlignedAllocator( size_t alignment = 16 ) + : alignment(alignment) {} + + T* allocate( std::size_t n ) + { + return reinterpret_cast( alloc_impl( sizeof(T) * n, alignment ) ); + } + + void deallocate( T* p, std::size_t ) + { + dealloc_impl( p ); + } + +private: + std::size_t alignment; +}; #define MM_OPERATORS \ public: \ diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index ba41e089c..7a3484149 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -31,7 +31,6 @@ set(LMMS_SRCS core/LadspaManager.cpp core/LfoController.cpp core/LocklessAllocator.cpp - core/MemoryHelper.cpp core/MemoryManager.cpp core/MeterModel.cpp core/MicroTimer.cpp diff --git a/src/core/MemoryHelper.cpp b/src/core/MemoryHelper.cpp deleted file mode 100644 index eb5a24d44..000000000 --- a/src/core/MemoryHelper.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2014 Simon Symeonidis - * Copyright (c) 2008-2014 Tobias Doerffel - * - * This file is part of LMMS - https://lmms.io - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program (see COPYING); if not, write to the - * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, - * Boston, MA 02110-1301 USA. - * - */ - -#include - -#include "lmms_basics.h" -#include "MemoryHelper.h" - -/** - * Allocate a number of bytes and return them. - * @param byteNum is the number of bytes - */ -void* MemoryHelper::alignedMalloc( size_t byteNum ) -{ - char *ptr, *ptr2, *aligned_ptr; - int align_mask = ALIGN_SIZE - 1; - - ptr = static_cast( malloc( byteNum + ALIGN_SIZE + sizeof( int ) ) ); - - if( ptr == NULL ) return NULL; - - ptr2 = ptr + sizeof( int ); - aligned_ptr = ptr2 + ( ALIGN_SIZE - ( ( size_t ) ptr2 & align_mask ) ); - - ptr2 = aligned_ptr - sizeof( int ); - *( ( int* ) ptr2 ) = ( int )( aligned_ptr - ptr ); - - return aligned_ptr; -} - - -/** - * Free an aligned buffer - * @param _buffer is the buffer to free - */ -void MemoryHelper::alignedFree( void* _buffer ) -{ - if( _buffer ) - { - int *ptr2 = static_cast( _buffer ) - 1; - _buffer = static_cast( _buffer ) - *ptr2; - free( _buffer ); - } -} - diff --git a/src/core/MemoryManager.cpp b/src/core/MemoryManager.cpp index d0932e4f1..06368172e 100644 --- a/src/core/MemoryManager.cpp +++ b/src/core/MemoryManager.cpp @@ -2,6 +2,8 @@ * MemoryManager.cpp * * Copyright (c) 2017 Lukas W + * Copyright (c) 2014 Simon Symeonidis + * Copyright (c) 2004-2014 Tobias Doerffel * * This file is part of LMMS - https://lmms.io * @@ -75,3 +77,30 @@ void MemoryManager::free(void * ptr) Q_ASSERT_X(rpmalloc_is_thread_initialized(), "MemoryManager::free", "Thread not initialized"); return rpfree(ptr); } + +void* _AlignedAllocator_Base::alloc_impl( size_t n , size_t alignment) +{ + char *ptr, *ptr2, *aligned_ptr; + int align_mask = alignment - 1; + + ptr = static_cast( malloc( n + alignment + sizeof( int ) ) ); + + if( ptr == NULL ) std::bad_alloc; + + ptr2 = ptr + sizeof( int ); + aligned_ptr = ptr2 + ( alignment - ( ( size_t ) ptr2 & align_mask ) ); + + ptr2 = aligned_ptr - sizeof( int ); + *( ( int* ) ptr2 ) = ( int )( aligned_ptr - ptr ); + + return aligned_ptr; +} + +void _AlignedAllocator_Base::dealloc_impl(void* p) +{ + if ( !p ) return; + + int *ptr2 = static_cast( p ) - 1; + p = static_cast( p ) - *ptr2; + free( p ); +} diff --git a/src/core/Mixer.cpp b/src/core/Mixer.cpp index 2550b072e..8d3a6c2cb 100644 --- a/src/core/Mixer.cpp +++ b/src/core/Mixer.cpp @@ -36,7 +36,7 @@ #include "NotePlayHandle.h" #include "ConfigManager.h" #include "SamplePlayHandle.h" -#include "MemoryHelper.h" +#include "MemoryManager.h" // platform-specific audio-interface-classes #include "AudioAlsa.h" @@ -136,11 +136,10 @@ Mixer::Mixer( bool renderOnly ) : // now that framesPerPeriod is fixed initialize global BufferManager BufferManager::init( m_framesPerPeriod ); + AlignedAllocator alloc; for( int i = 0; i < 3; i++ ) { - m_readBuf = (surroundSampleFrame*) - MemoryHelper::alignedMalloc( m_framesPerPeriod * - sizeof( surroundSampleFrame ) ); + m_readBuf = alloc.allocate( m_framesPerPeriod ); BufferManager::clear( m_readBuf, m_framesPerPeriod ); m_bufferPool.push_back( m_readBuf ); @@ -189,9 +188,10 @@ Mixer::~Mixer() delete m_midiClient; delete m_audioDev; + AlignedAllocator alloc; for( int i = 0; i < 3; i++ ) { - MemoryHelper::alignedFree( m_bufferPool[i] ); + alloc.deallocate( m_bufferPool[i], m_framesPerPeriod ); } for( int i = 0; i < 2; ++i )