Move aligned malloc to MemoryManager.h, port to stl allocator
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Simon Symeonidis <lethaljellybean/at/gmail/com>
|
||||
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* 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
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
* MemoryManager.h
|
||||
*
|
||||
* Copyright (c) 2017 Lukas W <lukaswhl/at/gmail.com>
|
||||
* Copyright (c) 2014 Simon Symeonidis <lethaljellybean/at/gmail/com>
|
||||
* Copyright (c) 2014 Vesa Kivimäki
|
||||
* Copyright (c) 2007-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* This file is part of LMMS - https://lmms.io
|
||||
*
|
||||
@@ -62,8 +63,41 @@ struct MmAllocator
|
||||
}
|
||||
|
||||
typedef std::vector<T, MmAllocator<T> > 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<typename T>
|
||||
class AlignedAllocator : _AlignedAllocator_Base
|
||||
{
|
||||
public:
|
||||
typedef T value_type;
|
||||
template<class U> struct rebind { typedef MmAllocator<U> other; };
|
||||
|
||||
AlignedAllocator( size_t alignment = 16 )
|
||||
: alignment(alignment) {}
|
||||
|
||||
T* allocate( std::size_t n )
|
||||
{
|
||||
return reinterpret_cast<T*>( 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: \
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2014 Simon Symeonidis <lethaljellybean/at/gmail/com>
|
||||
* Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* 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 <stdlib.h>
|
||||
|
||||
#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<char*>( 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<int*>( _buffer ) - 1;
|
||||
_buffer = static_cast<char*>( _buffer ) - *ptr2;
|
||||
free( _buffer );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
* MemoryManager.cpp
|
||||
*
|
||||
* Copyright (c) 2017 Lukas W <lukaswhl/at/gmail.com>
|
||||
* Copyright (c) 2014 Simon Symeonidis <lethaljellybean/at/gmail/com>
|
||||
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
||||
*
|
||||
* 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<char*>( 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<int*>( p ) - 1;
|
||||
p = static_cast<char*>( p ) - *ptr2;
|
||||
free( p );
|
||||
}
|
||||
|
||||
@@ -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<surroundSampleFrame> 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<surroundSampleFrame> 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 )
|
||||
|
||||
Reference in New Issue
Block a user