Skip to content

Commit

Permalink
LMMS Memory Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
diizy committed Nov 18, 2014
1 parent 6f96315 commit 9c25be1
Show file tree
Hide file tree
Showing 25 changed files with 415 additions and 33 deletions.
6 changes: 4 additions & 2 deletions include/AudioPort.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion include/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <QtCore/QVector>

#include "export.h"

#include "MemoryManager.h"

class engine;

Expand All @@ -49,6 +49,7 @@ const QString LOCALE_PATH = "locale/";

class EXPORT ConfigManager
{
MM_OPERATORS
public:
static inline ConfigManager * inst()
{
Expand Down
7 changes: 4 additions & 3 deletions include/DetuningHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion include/Effect.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include "Mixer.h"
#include "AutomatableModel.h"
#include "TempoSyncKnobModel.h"

#include "MemoryManager.h"

class EffectChain;
class EffectControls;
Expand Down
130 changes: 130 additions & 0 deletions include/MemoryManager.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions include/MidiEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion include/MidiEventProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
3 changes: 2 additions & 1 deletion include/NotePlayHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "note.h"
#include "PlayHandle.h"
#include "track.h"

#include "MemoryManager.h"

class InstrumentTrack;
class NotePlayHandle;
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions include/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

#include "JournallingObject.h"
#include "Model.h"
#include "base64.h"
#include "MemoryManager.h"


class QWidget;
Expand All @@ -42,6 +44,7 @@ class AutomatableModel;

class EXPORT Plugin : public JournallingObject, public Model
{
MM_OPERATORS
public:
enum PluginTypes
{
Expand Down
2 changes: 2 additions & 0 deletions include/SampleBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "lmms_math.h"
#include "shared_object.h"
#include "Mixer.h"
#include "MemoryManager.h"


class QPainter;
Expand All @@ -59,6 +60,7 @@ class EXPORT SampleBuffer : public QObject, public sharedObject
};
class EXPORT handleState
{
MM_OPERATORS
public:
handleState( bool _varying_pitch = false, int interpolation_mode = SRC_LINEAR );
virtual ~handleState();
Expand Down
2 changes: 2 additions & 0 deletions include/basic_filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@
#include "templates.h"
#include "lmms_constants.h"
#include "interpolation.h"
#include "MemoryManager.h"

//#include <iostream>
//#include <cstdlib>

template<ch_cnt_t CHANNELS/* = DEFAULT_CHANNELS*/>
class basicFilters
{
MM_OPERATORS
public:
enum FilterTypes
{
Expand Down
5 changes: 3 additions & 2 deletions include/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
6 changes: 4 additions & 2 deletions plugins/bit_invader/bit_invader.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@
*/


#ifndef _BIT_INVADER_H
#define _BIT_INVADER_H
#ifndef BIT_INVADER_H
#define BIT_INVADER_H

#include "Instrument.h"
#include "InstrumentView.h"
#include "graph.h"
#include "knob.h"
#include "pixmap_button.h"
#include "led_checkbox.h"
#include "MemoryManager.h"

class oscillator;
class bitInvaderView;

class bSynth
{
MM_OPERATORS
public:
bSynth( float * sample, int length, NotePlayHandle * _nph,
bool _interpolation, float factor,
Expand Down
2 changes: 2 additions & 0 deletions plugins/kicker/KickerOsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@

#include "lmms_math.h"
#include "interpolation.h"
#include "MemoryManager.h"


template<class FX = DspEffectLibrary::StereoBypass>
class KickerOsc
{
MM_OPERATORS
public:
KickerOsc( const FX & fx, const float start, const float end, const float noise, const float offset,
const float slope, const float env, const float diststart, const float distend, const float length ) :
Expand Down
1 change: 1 addition & 0 deletions plugins/monstro/Monstro.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ class MonstroView;

class MonstroSynth
{
MM_OPERATORS
public:
MonstroSynth( MonstroInstrument * _i, NotePlayHandle * _nph );
virtual ~MonstroSynth();
Expand Down
2 changes: 2 additions & 0 deletions plugins/nes/Nes.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "TempoSyncKnob.h"
#include "NotePlayHandle.h"
#include "pixmap_button.h"
#include "MemoryManager.h"


#define makeknob( name, x, y, hint, unit, oname ) \
Expand Down Expand Up @@ -80,6 +81,7 @@ class NesInstrument;

class NesObject
{
MM_OPERATORS
public:
NesObject( NesInstrument * nes, const sample_rate_t samplerate, NotePlayHandle * nph );
virtual ~NesObject();
Expand Down
2 changes: 2 additions & 0 deletions plugins/papu/Basic_Gb_Apu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include "gb_apu/Gb_Apu.h"
#include "gb_apu/Multi_Buffer.h"
#include "MemoryManager.h"

class Basic_Gb_Apu {
MM_OPERATORS
public:
Basic_Gb_Apu();
~Basic_Gb_Apu();
Expand Down
Loading

0 comments on commit 9c25be1

Please sign in to comment.