diff --git a/include/MemoryHelper.h b/include/MemoryHelper.h new file mode 100644 index 000000000..624f326ec --- /dev/null +++ b/include/MemoryHelper.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014 Simon Symeonidis + * Copyright (c) 2004-2014 Tobias Doerffel + * + * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net + * + * 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(int); + + static void alignedFree(void*); + +private: +}; + +#endif + diff --git a/src/core/MemoryHelper.cpp b/src/core/MemoryHelper.cpp new file mode 100644 index 000000000..86a03061e --- /dev/null +++ b/src/core/MemoryHelper.cpp @@ -0,0 +1,45 @@ +#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(int _byteNum) { + char *ptr,*ptr2,*aligned_ptr; + int align_mask = ALIGN_SIZE- 1; + + ptr = (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 != NULL ) + { + int *ptr2=(int *)_buffer - 1; + _buffer = (char *)_buffer - *ptr2; + free(_buffer); + } +} + + + + diff --git a/src/core/Mixer.cpp b/src/core/Mixer.cpp index ddc3f075d..9c35ee852 100644 --- a/src/core/Mixer.cpp +++ b/src/core/Mixer.cpp @@ -57,37 +57,10 @@ #include "MidiWinMM.h" #include "MidiDummy.h" +#include "MemoryHelper.h" -static void aligned_free( void * _buf ) -{ - if( _buf != NULL ) - { - int *ptr2=(int *)_buf - 1; - _buf = (char *)_buf- *ptr2; - free(_buf); - } -} - -static void * aligned_malloc( int _bytes ) -{ - char *ptr,*ptr2,*aligned_ptr; - int align_mask = ALIGN_SIZE- 1; - ptr=(char *)malloc(_bytes +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); -} - - Mixer::Mixer() : m_framesPerPeriod( DEFAULT_BUFFER_SIZE ), @@ -146,12 +119,12 @@ Mixer::Mixer() : m_fifo = new fifo( 1 ); } - m_workingBuf = (sampleFrame*) aligned_malloc( m_framesPerPeriod * + m_workingBuf = (sampleFrame*) MemoryHelper::alignedMalloc( m_framesPerPeriod * sizeof( sampleFrame ) ); for( int i = 0; i < 3; i++ ) { m_readBuf = (surroundSampleFrame*) - aligned_malloc( m_framesPerPeriod * + MemoryHelper::alignedMalloc( m_framesPerPeriod * sizeof( surroundSampleFrame ) ); clearAudioBuffer( m_readBuf, m_framesPerPeriod ); @@ -201,10 +174,10 @@ Mixer::~Mixer() for( int i = 0; i < 3; i++ ) { - aligned_free( m_bufferPool[i] ); + MemoryHelper::alignedFree( m_bufferPool[i] ); } - aligned_free( m_workingBuf ); + MemoryHelper::alignedFree( m_workingBuf ); for( int i = 0; i < 2; ++i ) {