LMMS Memory Manager

This commit is contained in:
Vesa
2014-08-22 20:52:30 +03:00
parent 6f963159df
commit 9c25be1bde
25 changed files with 415 additions and 33 deletions

View File

@@ -22,19 +22,21 @@
*
*/
#ifndef _AUDIO_PORT_H
#define _AUDIO_PORT_H
#ifndef AUDIO_PORT_H
#define AUDIO_PORT_H
#include <QtCore/QString>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include "Mixer.h"
#include "MemoryManager.h"
class EffectChain;
class AudioPort : public ThreadableJob
{
MM_OPERATORS
public:
AudioPort( const QString & _name, bool _has_effect_chain = true );
virtual ~AudioPort();

View File

@@ -34,7 +34,7 @@
#include <QtCore/QVector>
#include "export.h"
#include "MemoryManager.h"
class engine;
@@ -49,6 +49,7 @@ const QString LOCALE_PATH = "locale/";
class EXPORT ConfigManager
{
MM_OPERATORS
public:
static inline ConfigManager * inst()
{

View File

@@ -23,14 +23,15 @@
*
*/
#ifndef _DETUNING_HELPER_H
#define _DETUNING_HELPER_H
#ifndef DETUNING_HELPER_H
#define DETUNING_HELPER_H
#include "InlineAutomation.h"
#include "MemoryManager.h"
class DetuningHelper : public InlineAutomation
{
MM_OPERATORS
public:
DetuningHelper() :
InlineAutomation()

View File

@@ -31,7 +31,7 @@
#include "Mixer.h"
#include "AutomatableModel.h"
#include "TempoSyncKnobModel.h"
#include "MemoryManager.h"
class EffectChain;
class EffectControls;

130
include/MemoryManager.h Normal file
View File

@@ -0,0 +1,130 @@
/*
* MemoryManager.h - A lightweight, generic memory manager for LMMS
*
* Copyright (c) 2014 Vesa Kivimäki
* Copyright (c) 2007-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_MANAGER_H
#define MEMORY_MANAGER_H
#include <new>
#include <QtCore/QVector>
#include <QtCore/QMutex>
#include <QtCore/QMap>
#include "MemoryHelper.h"
const int MM_CHUNK_SIZE = 64; // granularity of managed memory
const int MM_INITIAL_CHUNKS = 1024 * 1024; // how many chunks to allocate at startup - TODO: make configurable
const int MM_INCREMENT_CHUNKS = 16 * 1024; // min. amount of chunks to increment at a time
struct MemoryPool
{
void * m_pool;
char * m_free;
int m_chunks;
QMutex m_mutex;
MemoryPool() :
m_pool( NULL ),
m_free( NULL ),
m_chunks( 0 )
{}
MemoryPool( int chunks ) :
m_chunks( chunks )
{
m_free = (char*) MemoryHelper::alignedMalloc( chunks );
memset( m_free, 1, chunks );
}
MemoryPool( const MemoryPool & mp ) :
m_pool( mp.m_pool ),
m_free( mp.m_free ),
m_chunks( mp.m_chunks ),
m_mutex()
{}
MemoryPool & operator = ( const MemoryPool & mp )
{
m_pool = mp.m_pool;
m_free = mp.m_free;
m_chunks = mp.m_chunks;
return *this;
}
void * getChunks( int chunksNeeded );
void releaseChunks( void * ptr, int chunks );
};
struct PtrInfo
{
int chunks;
MemoryPool * memPool;
};
typedef QVector<MemoryPool> MemoryPoolVector;
typedef QMap<void*, PtrInfo> PointerInfoMap;
class MemoryManager
{
public:
static bool init();
static void * alloc( size_t size );
static void free( void * ptr );
static int extend( int chunks ); // returns index of created pool (for use by alloc)
static void cleanup();
private:
static MemoryPoolVector s_memoryPools;
static QMutex s_poolMutex;
static PointerInfoMap s_pointerInfo;
static QMutex s_pointerMutex;
};
#define MM_OPERATORS \
public: \
static void * operator new ( size_t size ) \
{ \
return MemoryManager::alloc( size ); \
} \
static void * operator new[] ( size_t size ) \
{ \
return MemoryManager::alloc( size ); \
} \
static void operator delete ( void * ptr ) \
{ \
MemoryManager::free( ptr ); \
} \
static void operator delete[] ( void * ptr ) \
{ \
MemoryManager::free( ptr ); \
}
// for use in cases where overriding new/delete isn't a possibility
#define MM_ALLOC( type, count ) (type*) MemoryManager::alloc( sizeof( type ) * count )
// and just for symmetry...
#define MM_FREE( ptr ) MemoryManager::free( ptr )
#endif

View File

@@ -29,9 +29,11 @@
#include "Midi.h"
#include "panning_constants.h"
#include "volume.h"
#include "MemoryManager.h"
class MidiEvent
{
MM_OPERATORS
public:
MidiEvent( MidiEventTypes type = MidiActiveSensing,
int8_t channel = 0,

View File

@@ -27,11 +27,12 @@
#include "MidiEvent.h"
#include "MidiTime.h"
#include "MemoryManager.h"
// all classes being able to process MIDI-events should inherit from this
class MidiEventProcessor
{
MM_OPERATORS
public:
MidiEventProcessor()
{

View File

@@ -30,7 +30,7 @@
#include "note.h"
#include "PlayHandle.h"
#include "track.h"
#include "MemoryManager.h"
class InstrumentTrack;
class NotePlayHandle;
@@ -42,6 +42,7 @@ typedef QList<const NotePlayHandle *> ConstNotePlayHandleList;
class EXPORT NotePlayHandle : public PlayHandle, public note
{
MM_OPERATORS
public:
void * m_pluginData;
basicFilters<> * m_filter;

View File

@@ -31,6 +31,8 @@
#include "JournallingObject.h"
#include "Model.h"
#include "base64.h"
#include "MemoryManager.h"
class QWidget;
@@ -42,6 +44,7 @@ class AutomatableModel;
class EXPORT Plugin : public JournallingObject, public Model
{
MM_OPERATORS
public:
enum PluginTypes
{

View File

@@ -38,6 +38,7 @@
#include "lmms_math.h"
#include "shared_object.h"
#include "Mixer.h"
#include "MemoryManager.h"
class QPainter;
@@ -59,6 +60,7 @@ public:
};
class EXPORT handleState
{
MM_OPERATORS
public:
handleState( bool _varying_pitch = false, int interpolation_mode = SRC_LINEAR );
virtual ~handleState();

View File

@@ -43,6 +43,7 @@
#include "templates.h"
#include "lmms_constants.h"
#include "interpolation.h"
#include "MemoryManager.h"
//#include <iostream>
//#include <cstdlib>
@@ -50,6 +51,7 @@
template<ch_cnt_t CHANNELS/* = DEFAULT_CHANNELS*/>
class basicFilters
{
MM_OPERATORS
public:
enum FilterTypes
{

View File

@@ -23,10 +23,11 @@
*/
#ifndef _ENGINE_H
#define _ENGINE_H
#ifndef ENGINE_H
#define ENGINE_H
#include "lmmsconfig.h"
#include "MemoryManager.h"
#include <QtCore/QMap>