Remove MemoryHelper (#7335)

* remove memory helper and replace alignedMalloc with stl version

* it's aligned_alloc

* forgot about destructor

* switch to unique pointers

Co-authored-by: saker <sakertooth@gmail.com>

* compile fix

---------

Co-authored-by: saker <sakertooth@gmail.com>
This commit is contained in:
Rossmaxx
2024-06-24 21:22:19 +05:30
committed by GitHub
parent a1f7753a94
commit aaca5fbd0e
6 changed files with 11 additions and 137 deletions

View File

@@ -34,6 +34,7 @@
#include <QThread>
#include <samplerate.h>
#include <memory>
#include <vector>
#include "lmms_basics.h"
@@ -380,8 +381,8 @@ private:
int m_inputBufferRead;
int m_inputBufferWrite;
surroundSampleFrame * m_outputBufferRead;
surroundSampleFrame * m_outputBufferWrite;
std::unique_ptr<surroundSampleFrame[]> m_outputBufferRead;
std::unique_ptr<surroundSampleFrame[]> m_outputBufferWrite;
// worker thread stuff
std::vector<AudioEngineWorkerThread *> m_workers;

View File

@@ -1,49 +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 LMMS_MEMORY_HELPER_H
#define LMMS_MEMORY_HELPER_H
#include <cstddef>
namespace lmms
{
/**
* Helper class to alocate aligned memory and free it.
*/
class MemoryHelper {
public:
static void* alignedMalloc( size_t );
static void alignedFree( void* );
private:
};
} // namespace lmms
#endif // LMMS_MEMORY_HELPER_H

View File

@@ -128,7 +128,6 @@ constexpr char LADSPA_PATH_SEPERATOR =
using sampleFrame = std::array<sample_t, DEFAULT_CHANNELS>;
using surroundSampleFrame = std::array<sample_t, SURROUND_CHANNELS>;
constexpr std::size_t LMMS_ALIGN_SIZE = 16;
#define LMMS_STRINGIFY(s) LMMS_STR(s)

View File

@@ -37,7 +37,6 @@
#include "NotePlayHandle.h"
#include "ConfigManager.h"
#include "SamplePlayHandle.h"
#include "MemoryHelper.h"
// platform-specific audio-interface-classes
#include "AudioAlsa.h"
@@ -137,12 +136,9 @@ AudioEngine::AudioEngine( bool renderOnly ) :
// now that framesPerPeriod is fixed initialize global BufferManager
BufferManager::init( m_framesPerPeriod );
int outputBufferSize = m_framesPerPeriod * sizeof(surroundSampleFrame);
m_outputBufferRead = static_cast<surroundSampleFrame *>(MemoryHelper::alignedMalloc(outputBufferSize));
m_outputBufferWrite = static_cast<surroundSampleFrame *>(MemoryHelper::alignedMalloc(outputBufferSize));
m_outputBufferRead = std::make_unique<surroundSampleFrame[]>(m_framesPerPeriod);
m_outputBufferWrite = std::make_unique<surroundSampleFrame[]>(m_framesPerPeriod);
BufferManager::clear(m_outputBufferRead, m_framesPerPeriod);
BufferManager::clear(m_outputBufferWrite, m_framesPerPeriod);
for( int i = 0; i < m_numWorkers+1; ++i )
{
@@ -181,8 +177,6 @@ AudioEngine::~AudioEngine()
delete m_midiClient;
delete m_audioDev;
MemoryHelper::alignedFree(m_outputBufferRead);
MemoryHelper::alignedFree(m_outputBufferWrite);
for (const auto& input : m_inputBuffer)
{
@@ -421,11 +415,11 @@ void AudioEngine::renderStageMix()
AudioEngineProfiler::Probe profilerProbe(m_profiler, AudioEngineProfiler::DetailType::Mixing);
Mixer *mixer = Engine::mixer();
mixer->masterMix(m_outputBufferWrite);
mixer->masterMix(m_outputBufferWrite.get());
MixHelpers::multiply(m_outputBufferWrite, m_masterGain, m_framesPerPeriod);
MixHelpers::multiply(m_outputBufferWrite.get(), m_masterGain, m_framesPerPeriod);
emit nextAudioBuffer(m_outputBufferRead);
emit nextAudioBuffer(m_outputBufferRead.get());
// and trigger LFOs
EnvelopeAndLfoParameters::instances()->trigger();
@@ -435,7 +429,7 @@ void AudioEngine::renderStageMix()
const surroundSampleFrame *AudioEngine::renderNextBuffer()
const surroundSampleFrame* AudioEngine::renderNextBuffer()
{
const auto lock = std::lock_guard{m_changeMutex};
@@ -450,7 +444,7 @@ const surroundSampleFrame *AudioEngine::renderNextBuffer()
s_renderingThread = false;
m_profiler.finishPeriod(outputSampleRate(), m_framesPerPeriod);
return m_outputBufferRead;
return m_outputBufferRead.get();
}
@@ -463,7 +457,7 @@ void AudioEngine::swapBuffers()
m_inputBufferFrames[m_inputBufferWrite] = 0;
std::swap(m_outputBufferRead, m_outputBufferWrite);
BufferManager::clear(m_outputBufferWrite, m_framesPerPeriod);
std::fill_n(m_outputBufferWrite.get(), m_framesPerPeriod, surroundSampleFrame{});
}

View File

@@ -39,7 +39,6 @@ set(LMMS_SRCS
core/LfoController.cpp
core/LinkedModelGroups.cpp
core/LocklessAllocator.cpp
core/MemoryHelper.cpp
core/MeterModel.cpp
core/MicroTimer.cpp
core/Microtuner.cpp

View File

@@ -1,70 +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 <cstdlib>
#include "lmms_basics.h"
#include "MemoryHelper.h"
namespace lmms
{
/**
* Allocate a number of bytes and return them.
* @param byteNum is the number of bytes
*/
void* MemoryHelper::alignedMalloc( size_t byteNum )
{
int align_mask = LMMS_ALIGN_SIZE - 1;
char* ptr = static_cast<char*>(malloc(byteNum + LMMS_ALIGN_SIZE + sizeof(int)));
if( ptr == nullptr ) return nullptr;
char* ptr2 = ptr + sizeof(int);
char* aligned_ptr = ptr2 + (LMMS_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 );
}
}
} // namespace lmms