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