mem-helper: move align-mem functionality to class
There was some memory alignment logic inside the Mixer cpp file. To break down the code and separate things into smaller modules, the aligned memory stuff was crammed into a new class called MemoryHelper. The MemoryHelper can be reused for any other aligned memory that may be needed by another component.
This commit is contained in:
42
include/MemoryHelper.h
Normal file
42
include/MemoryHelper.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* 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 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
|
||||
|
||||
45
src/core/MemoryHelper.cpp
Normal file
45
src/core/MemoryHelper.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#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(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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -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 )
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user