Move aligned malloc to MemoryManager.h, port to stl allocator

This commit is contained in:
Lukas W
2017-11-30 12:39:15 +01:00
parent a863830795
commit 2a08909380
6 changed files with 69 additions and 114 deletions

View File

@@ -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

View File

@@ -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 );
}
}

View File

@@ -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 );
}

View File

@@ -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 )