| @@ -55,6 +55,7 @@ BUILD_CXX_FLAGS += -DVESTIGE_HEADER | |||||
| # -------------------------------------------------------------- | # -------------------------------------------------------------- | ||||
| ifeq ($(CARLA_PLUGIN_SUPPORT),true) | ifeq ($(CARLA_PLUGIN_SUPPORT),true) | ||||
| BUILD_C_FLAGS += -DWANT_LV2 | |||||
| BUILD_CXX_FLAGS += -DWANT_LADSPA -DWANT_DSSI -DWANT_LV2 -DWANT_VST | BUILD_CXX_FLAGS += -DWANT_LADSPA -DWANT_DSSI -DWANT_LV2 -DWANT_VST | ||||
| HAVE_SUIL = $(shell pkg-config --exists suil-0 && echo true) | HAVE_SUIL = $(shell pkg-config --exists suil-0 && echo true) | ||||
| endif | endif | ||||
| @@ -21,7 +21,9 @@ | |||||
| #include "carla_backend.hpp" | #include "carla_backend.hpp" | ||||
| #include "carla_utils.hpp" | #include "carla_utils.hpp" | ||||
| #ifndef BUILD_BRIDGE | |||||
| class QProcessEnvironment; | class QProcessEnvironment; | ||||
| #endif | |||||
| CARLA_BACKEND_START_NAMESPACE | CARLA_BACKEND_START_NAMESPACE | ||||
| @@ -138,6 +140,15 @@ struct CarlaEngineControlEvent { | |||||
| channel(0), | channel(0), | ||||
| parameter(0), | parameter(0), | ||||
| value(0.0) {} | value(0.0) {} | ||||
| void clear() | |||||
| { | |||||
| type = CarlaEngineNullEvent; | |||||
| time = 0; | |||||
| channel = 0; | |||||
| parameter = 0; | |||||
| value = 0.0; | |||||
| } | |||||
| }; | }; | ||||
| /*! | /*! | ||||
| @@ -152,6 +163,13 @@ struct CarlaEngineMidiEvent { | |||||
| : time(0), | : time(0), | ||||
| size(0), | size(0), | ||||
| data{0} {} | data{0} {} | ||||
| void clear() | |||||
| { | |||||
| time = 0; | |||||
| size = 0; | |||||
| data[0] = data[1] = data[2] = 0; | |||||
| } | |||||
| }; | }; | ||||
| /*! | /*! | ||||
| @@ -0,0 +1,209 @@ | |||||
| /* | |||||
| * Carla Native Plugin API | |||||
| * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com> | |||||
| * | |||||
| * 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 | |||||
| * 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. | |||||
| * | |||||
| * For a full copy of the GNU General Public License see the COPYING file | |||||
| */ | |||||
| #ifndef CARLA_NATIVE_H | |||||
| #define CARLA_NATIVE_H | |||||
| #ifdef __cplusplus | |||||
| extern "C" { | |||||
| #else | |||||
| # include <stdbool.h> | |||||
| #endif | |||||
| #include <stddef.h> | |||||
| #include <stdint.h> | |||||
| /*! | |||||
| * @defgroup CarlaNativeAPI Carla Native API | |||||
| * | |||||
| * The Carla Native API | |||||
| * | |||||
| * @{ | |||||
| */ | |||||
| typedef void* HostHandle; | |||||
| typedef void* PluginHandle; | |||||
| const uint32_t PLUGIN_IS_SYNTH = 1 << 0; | |||||
| const uint32_t PLUGIN_HAS_GUI = 1 << 1; | |||||
| const uint32_t PLUGIN_USES_SINGLE_THREAD = 1 << 2; | |||||
| const uint32_t PARAMETER_IS_OUTPUT = 1 << 0; | |||||
| const uint32_t PARAMETER_IS_ENABLED = 1 << 1; | |||||
| const uint32_t PARAMETER_IS_AUTOMABLE = 1 << 2; | |||||
| const uint32_t PARAMETER_IS_BOOLEAN = 1 << 3; | |||||
| const uint32_t PARAMETER_IS_INTEGER = 1 << 4; | |||||
| const uint32_t PARAMETER_IS_LOGARITHMIC = 1 << 5; | |||||
| const uint32_t PARAMETER_USES_SAMPLE_RATE = 1 << 6; | |||||
| const uint32_t PARAMETER_USES_SCALEPOINTS = 1 << 7; | |||||
| const uint32_t PARAMETER_USES_CUSTOM_TEXT = 1 << 8; | |||||
| typedef enum _PluginCategory { | |||||
| PLUGIN_CATEGORY_NONE = 0, //!< Null plugin category. | |||||
| PLUGIN_CATEGORY_SYNTH = 1, //!< A synthesizer or generator. | |||||
| PLUGIN_CATEGORY_DELAY = 2, //!< A delay or reverberator. | |||||
| PLUGIN_CATEGORY_EQ = 3, //!< An equalizer. | |||||
| PLUGIN_CATEGORY_FILTER = 4, //!< A filter. | |||||
| PLUGIN_CATEGORY_DYNAMICS = 5, //!< A 'dynamic' plugin (amplifier, compressor, gate, etc). | |||||
| PLUGIN_CATEGORY_MODULATOR = 6, //!< A 'modulator' plugin (chorus, flanger, phaser, etc). | |||||
| PLUGIN_CATEGORY_UTILITY = 7, //!< An 'utility' plugin (analyzer, converter, mixer, etc). | |||||
| PLUGIN_CATEGORY_OTHER = 8 //!< Misc plugin (used to check if the plugin has a category). | |||||
| } PluginCategory; | |||||
| typedef struct _ParameterScalePoint { | |||||
| const char* label; | |||||
| float value; | |||||
| } ParameterScalePoint; | |||||
| typedef struct _ParameterRanges { | |||||
| float def; | |||||
| float min; | |||||
| float max; | |||||
| float step; | |||||
| float stepSmall; | |||||
| float stepLarge; | |||||
| } ParameterRanges; | |||||
| #define PARAMETER_RANGES_DEFAULT_STEP 0.01f | |||||
| #define PARAMETER_RANGES_DEFAULT_STEP_SMALL 0.0001f | |||||
| #define PARAMETER_RANGES_DEFAULT_STEP_LARGE 0.1f | |||||
| typedef struct _Parameter { | |||||
| uint32_t hints; | |||||
| const char* name; | |||||
| const char* unit; | |||||
| ParameterRanges ranges; | |||||
| uint32_t scalePointCount; | |||||
| ParameterScalePoint* scalePoints; | |||||
| } Parameter; | |||||
| typedef struct _MidiEvent { | |||||
| uint32_t port; | |||||
| uint32_t time; | |||||
| uint8_t data[3]; | |||||
| } MidiEvent; | |||||
| typedef struct _MidiProgram { | |||||
| uint32_t bank; | |||||
| uint32_t program; | |||||
| const char* name; | |||||
| } MidiProgram; | |||||
| typedef struct _TimeInfoBBT { | |||||
| bool valid; | |||||
| int32_t bar; | |||||
| int32_t beat; | |||||
| int32_t tick; | |||||
| double bar_start_tick; | |||||
| float beats_per_bar; | |||||
| float beat_type; | |||||
| double ticks_per_beat; | |||||
| double beats_per_minute; | |||||
| } TimeInfoBBT; | |||||
| typedef struct _TimeInfo { | |||||
| bool playing; | |||||
| uint32_t frame; | |||||
| uint32_t time; | |||||
| TimeInfoBBT bbt; | |||||
| } TimeInfo; | |||||
| typedef struct _HostDescriptor { | |||||
| HostHandle handle; | |||||
| uint32_t (*get_buffer_size)(HostHandle handle); | |||||
| double (*get_sample_rate)(HostHandle handle); | |||||
| const TimeInfo* (*get_time_info)(HostHandle handle); | |||||
| bool (*write_midi_event)(HostHandle handle, MidiEvent* event); | |||||
| void (*ui_parameter_changed)(HostHandle handle, uint32_t index, float value); | |||||
| void (*ui_midi_program_changed)(HostHandle handle, uint32_t bank, uint32_t program); | |||||
| void (*ui_custom_data_changed)(HostHandle handle, const char* key, const char* value); | |||||
| void (*ui_closed)(HostHandle handle); | |||||
| } HostDescriptor; | |||||
| typedef struct _PluginDescriptor { | |||||
| const PluginCategory category; | |||||
| const uint32_t hints; | |||||
| const uint32_t audioIns; | |||||
| const uint32_t audioOuts; | |||||
| const uint32_t midiIns; | |||||
| const uint32_t midiOuts; | |||||
| const uint32_t parameterIns; | |||||
| const uint32_t parameterOuts; | |||||
| const char* const name; | |||||
| const char* const label; | |||||
| const char* const maker; | |||||
| const char* const copyright; | |||||
| PluginHandle (*instantiate)(const struct _PluginDescriptor* _this_, HostDescriptor* host); | |||||
| uint32_t (*get_parameter_count)(PluginHandle handle); | |||||
| const Parameter* (*get_parameter_info)(PluginHandle handle, uint32_t index); | |||||
| float (*get_parameter_value)(PluginHandle handle, uint32_t index); | |||||
| const char* (*get_parameter_text)(PluginHandle handle, uint32_t index); | |||||
| uint32_t (*get_midi_program_count)(PluginHandle handle); | |||||
| const MidiProgram* (*get_midi_program_info)(PluginHandle handle, uint32_t index); | |||||
| void (*set_parameter_value)(PluginHandle handle, uint32_t index, float value); | |||||
| void (*set_midi_program)(PluginHandle handle, uint32_t bank, uint32_t program); | |||||
| void (*set_custom_data)(PluginHandle handle, const char* key, const char* value); | |||||
| void (*ui_show)(PluginHandle handle, bool show); | |||||
| void (*ui_idle)(PluginHandle handle); | |||||
| void (*ui_set_parameter_value)(PluginHandle handle, uint32_t index, float value); | |||||
| void (*ui_set_midi_program)(PluginHandle handle, uint32_t bank, uint32_t program); | |||||
| void (*ui_set_custom_data)(PluginHandle handle, const char* key, const char* value); | |||||
| void (*activate)(PluginHandle handle); | |||||
| void (*deactivate)(PluginHandle handle); | |||||
| void (*cleanup)(PluginHandle handle); | |||||
| void (*process)(PluginHandle handle, float** inBuffer, float** outBuffer, uint32_t frames, uint32_t midiEventCount, const MidiEvent* midiEvents); | |||||
| } PluginDescriptor; | |||||
| // ----------------------------------------------------------------------- | |||||
| // Register plugin | |||||
| void carla_register_native_plugin(const PluginDescriptor* desc); | |||||
| // Simple plugins | |||||
| void carla_register_native_plugin_bypass(); | |||||
| void carla_register_native_plugin_midiSplit(); | |||||
| // DISTRHO plugins | |||||
| void carla_register_native_plugin_3BandEQ(); | |||||
| void carla_register_native_plugin_3BandSplitter(); | |||||
| void carla_register_native_plugin_PingPongPan(); | |||||
| #ifdef WANT_ZYNADDSUBFX | |||||
| // ZynAddSubFX | |||||
| void carla_register_native_plugin_zynaddsubfx(); | |||||
| #endif | |||||
| // ----------------------------------------------------------------------- | |||||
| /**@}*/ | |||||
| #ifdef __cplusplus | |||||
| } // extern "C" | |||||
| #endif | |||||
| #endif // CARLA_NATIVE_H | |||||
| @@ -180,6 +180,7 @@ void CarlaEngineControlPort::writeEvent(const CarlaEngineControlEventType type, | |||||
| CARLA_ASSERT(buffer); | CARLA_ASSERT(buffer); | ||||
| CARLA_ASSERT(type != CarlaEngineNullEvent); | CARLA_ASSERT(type != CarlaEngineNullEvent); | ||||
| CARLA_ASSERT(channel < 16); | |||||
| if (! buffer) | if (! buffer) | ||||
| return; | return; | ||||
| @@ -187,6 +188,8 @@ void CarlaEngineControlPort::writeEvent(const CarlaEngineControlEventType type, | |||||
| return; | return; | ||||
| if (type == CarlaEngineParameterChangeEvent) | if (type == CarlaEngineParameterChangeEvent) | ||||
| CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(parameter)); | CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(parameter)); | ||||
| if (channel >= 16) | |||||
| return; | |||||
| #ifndef BUILD_BRIDGE | #ifndef BUILD_BRIDGE | ||||
| if (processMode == PROCESS_MODE_CONTINUOUS_RACK || processMode == PROCESS_MODE_PATCHBAY) | if (processMode == PROCESS_MODE_CONTINUOUS_RACK || processMode == PROCESS_MODE_PATCHBAY) | ||||
| @@ -323,11 +326,7 @@ void CarlaEngineMidiPort::writeEvent(const uint32_t time, const uint8_t* const d | |||||
| CARLA_ASSERT(data); | CARLA_ASSERT(data); | ||||
| CARLA_ASSERT(size > 0); | CARLA_ASSERT(size > 0); | ||||
| if (! buffer) | |||||
| return; | |||||
| if (! data) | |||||
| return; | |||||
| if (size == 0) | |||||
| if (! (buffer && data && size > 0)) | |||||
| return; | return; | ||||
| #ifndef BUILD_BRIDGE | #ifndef BUILD_BRIDGE | ||||
| @@ -28,16 +28,6 @@ | |||||
| # include <QtCore/QProcessEnvironment> | # include <QtCore/QProcessEnvironment> | ||||
| #endif | #endif | ||||
| #ifdef CARLA_ENGINE_RTAUDIO | |||||
| # if defined(Q_OS_MAC) && ! defined(__MACOSX_CORE__) | |||||
| # define __MACOSX_CORE__ | |||||
| # endif | |||||
| # if defined(Q_OS_WIN) && ! (defined(__WINDOWS_ASIO__) || defined(__WINDOWS_DS__)) | |||||
| # define __WINDOWS_ASIO__ | |||||
| # define __WINDOWS_DS__ | |||||
| # endif | |||||
| #endif | |||||
| CARLA_BACKEND_START_NAMESPACE | CARLA_BACKEND_START_NAMESPACE | ||||
| // ------------------------------------------------------------------------------------------------------------------- | // ------------------------------------------------------------------------------------------------------------------- | ||||
| @@ -128,9 +118,10 @@ struct CarlaEnginePrivateData { | |||||
| QProcessEnvironment procEnv; | QProcessEnvironment procEnv; | ||||
| #endif | #endif | ||||
| QMutex procLock; | |||||
| QMutex midiLock; | |||||
| CarlaMutex procLock; | |||||
| CarlaMutex midiLock; | |||||
| // TODO - use ListHead for pointers, remove maximum static value | |||||
| CarlaPlugin* carlaPlugins[MAX_PLUGINS]; | CarlaPlugin* carlaPlugins[MAX_PLUGINS]; | ||||
| const char* uniqueNames[MAX_PLUGINS]; | const char* uniqueNames[MAX_PLUGINS]; | ||||
| @@ -17,9 +17,7 @@ | |||||
| #ifdef CARLA_ENGINE_JACK | #ifdef CARLA_ENGINE_JACK | ||||
| #include "carla_engine.hpp" | |||||
| #include "carla_plugin.hpp" | |||||
| #include "carla_engine_internal.hpp" | |||||
| #include "carla_backend_utils.hpp" | #include "carla_backend_utils.hpp" | ||||
| #include "carla_midi.h" | #include "carla_midi.h" | ||||
| @@ -38,7 +36,7 @@ public: | |||||
| m_client(client), | m_client(client), | ||||
| m_port(port) | m_port(port) | ||||
| { | { | ||||
| qDebug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %s)", bool2str(isInput), ProcessMode2Str(processMode)); | |||||
| qDebug("CarlaEngineJackAudioPort::CarlaEngineJackAudioPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port); | |||||
| if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| CARLA_ASSERT(m_client && m_port); | CARLA_ASSERT(m_client && m_port); | ||||
| @@ -56,6 +54,14 @@ public: | |||||
| void initBuffer(CarlaEngine* const engine) | void initBuffer(CarlaEngine* const engine) | ||||
| { | { | ||||
| CARLA_ASSERT(engine); | |||||
| if (! engine) | |||||
| { | |||||
| buffer = nullptr; | |||||
| return; | |||||
| } | |||||
| if (! m_port) | if (! m_port) | ||||
| return CarlaEngineAudioPort::initBuffer(engine); | return CarlaEngineAudioPort::initBuffer(engine); | ||||
| @@ -70,8 +76,6 @@ public: | |||||
| private: | private: | ||||
| jack_client_t* const m_client; | jack_client_t* const m_client; | ||||
| jack_port_t* const m_port; | jack_port_t* const m_port; | ||||
| friend class CarlaEngineJack; | |||||
| }; | }; | ||||
| // ------------------------------------------------------------------------------------------------------------------- | // ------------------------------------------------------------------------------------------------------------------- | ||||
| @@ -85,7 +89,7 @@ public: | |||||
| m_client(client), | m_client(client), | ||||
| m_port(port) | m_port(port) | ||||
| { | { | ||||
| qDebug("CarlaEngineJackControlPort::CarlaEngineJackControlPort(%s, %s)", bool2str(isInput), ProcessMode2Str(processMode)); | |||||
| qDebug("CarlaEngineJackControlPort::CarlaEngineJackControlPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port); | |||||
| if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| CARLA_ASSERT(m_client && m_port); | CARLA_ASSERT(m_client && m_port); | ||||
| @@ -103,11 +107,17 @@ public: | |||||
| void initBuffer(CarlaEngine* const engine) | void initBuffer(CarlaEngine* const engine) | ||||
| { | { | ||||
| CARLA_ASSERT(engine); | |||||
| if (! engine) | |||||
| { | |||||
| buffer = nullptr; | |||||
| return; | |||||
| } | |||||
| if (! m_port) | if (! m_port) | ||||
| return CarlaEngineControlPort::initBuffer(engine); | return CarlaEngineControlPort::initBuffer(engine); | ||||
| CARLA_ASSERT(engine); | |||||
| buffer = jackbridge_port_get_buffer(m_port, engine->getBufferSize()); | buffer = jackbridge_port_get_buffer(m_port, engine->getBufferSize()); | ||||
| if (! isInput) | if (! isInput) | ||||
| @@ -124,6 +134,9 @@ public: | |||||
| CARLA_ASSERT(buffer); | CARLA_ASSERT(buffer); | ||||
| if (! buffer) | |||||
| return 0; | |||||
| return jackbridge_midi_get_event_count(buffer); | return jackbridge_midi_get_event_count(buffer); | ||||
| } | } | ||||
| @@ -137,55 +150,60 @@ public: | |||||
| CARLA_ASSERT(buffer); | CARLA_ASSERT(buffer); | ||||
| static jack_midi_event_t jackEvent; | |||||
| static CarlaEngineControlEvent carlaEvent; | |||||
| if (! buffer) | |||||
| return nullptr; | |||||
| jack_midi_event_t jackEvent; | |||||
| if (jackbridge_midi_event_get(&jackEvent, buffer, index) != 0) | if (jackbridge_midi_event_get(&jackEvent, buffer, index) != 0) | ||||
| return nullptr; | return nullptr; | ||||
| memset(&carlaEvent, 0, sizeof(CarlaEngineControlEvent)); | |||||
| const uint8_t midiStatus = jackEvent.buffer[0]; | |||||
| const uint8_t midiChannel = midiStatus & 0x0F; | |||||
| uint8_t midiStatus = jackEvent.buffer[0]; | |||||
| uint8_t midiChannel = midiStatus & 0x0F; | |||||
| carlaEvent.time = jackEvent.time; | |||||
| carlaEvent.channel = midiChannel; | |||||
| m_retEvent.clear(); | |||||
| m_retEvent.time = jackEvent.time; | |||||
| m_retEvent.channel = midiChannel; | |||||
| if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus)) | if (MIDI_IS_STATUS_CONTROL_CHANGE(midiStatus)) | ||||
| { | { | ||||
| uint8_t midiControl = jackEvent.buffer[1]; | |||||
| const uint8_t midiControl = jackEvent.buffer[1]; | |||||
| if (MIDI_IS_CONTROL_BANK_SELECT(midiControl)) | if (MIDI_IS_CONTROL_BANK_SELECT(midiControl)) | ||||
| { | { | ||||
| uint8_t midiBank = jackEvent.buffer[2]; | |||||
| carlaEvent.type = CarlaEngineMidiBankChangeEvent; | |||||
| carlaEvent.value = midiBank; | |||||
| const uint8_t midiBank = jackEvent.buffer[2]; | |||||
| m_retEvent.type = CarlaEngineMidiBankChangeEvent; | |||||
| m_retEvent.value = midiBank; | |||||
| } | } | ||||
| else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF) | else if (midiControl == MIDI_CONTROL_ALL_SOUND_OFF) | ||||
| { | { | ||||
| carlaEvent.type = CarlaEngineAllSoundOffEvent; | |||||
| m_retEvent.type = CarlaEngineAllSoundOffEvent; | |||||
| } | } | ||||
| else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF) | else if (midiControl == MIDI_CONTROL_ALL_NOTES_OFF) | ||||
| { | { | ||||
| carlaEvent.type = CarlaEngineAllNotesOffEvent; | |||||
| m_retEvent.type = CarlaEngineAllNotesOffEvent; | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| uint8_t midiValue = jackEvent.buffer[2]; | |||||
| carlaEvent.type = CarlaEngineParameterChangeEvent; | |||||
| carlaEvent.parameter = midiControl; | |||||
| carlaEvent.value = double(midiValue)/127; | |||||
| const uint8_t midiValue = jackEvent.buffer[2]; | |||||
| m_retEvent.type = CarlaEngineParameterChangeEvent; | |||||
| m_retEvent.parameter = midiControl; | |||||
| m_retEvent.value = double(midiValue)/127; | |||||
| } | } | ||||
| return &carlaEvent; | |||||
| return &m_retEvent; | |||||
| } | } | ||||
| if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus)) | if (MIDI_IS_STATUS_PROGRAM_CHANGE(midiStatus)) | ||||
| { | { | ||||
| uint8_t midiProgram = jackEvent.buffer[1]; | |||||
| carlaEvent.type = CarlaEngineMidiProgramChangeEvent; | |||||
| carlaEvent.value = midiProgram; | |||||
| const uint8_t midiProgram = jackEvent.buffer[1]; | |||||
| m_retEvent.type = CarlaEngineMidiProgramChangeEvent; | |||||
| m_retEvent.value = midiProgram; | |||||
| return &carlaEvent; | |||||
| return &m_retEvent; | |||||
| } | } | ||||
| return nullptr; | return nullptr; | ||||
| @@ -201,13 +219,19 @@ public: | |||||
| CARLA_ASSERT(buffer); | CARLA_ASSERT(buffer); | ||||
| CARLA_ASSERT(type != CarlaEngineNullEvent); | CARLA_ASSERT(type != CarlaEngineNullEvent); | ||||
| CARLA_ASSERT(channel < 16); | |||||
| if (! buffer) | |||||
| return; | |||||
| if (type == CarlaEngineNullEvent) | if (type == CarlaEngineNullEvent) | ||||
| return; | return; | ||||
| if (type == CarlaEngineParameterChangeEvent) | if (type == CarlaEngineParameterChangeEvent) | ||||
| CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(parameter)); | CARLA_ASSERT(! MIDI_IS_CONTROL_BANK_SELECT(parameter)); | ||||
| if (channel >= 16) | |||||
| return; | |||||
| uint8_t data[3] = { 0 }; | uint8_t data[3] = { 0 }; | ||||
| uint8_t size = 0; | |||||
| switch (type) | switch (type) | ||||
| { | { | ||||
| @@ -217,35 +241,40 @@ public: | |||||
| data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | ||||
| data[1] = parameter; | data[1] = parameter; | ||||
| data[2] = value * 127; | data[2] = value * 127; | ||||
| jackbridge_midi_event_write(buffer, time, data, 3); | |||||
| size = 3; | |||||
| break; | break; | ||||
| case CarlaEngineMidiBankChangeEvent: | case CarlaEngineMidiBankChangeEvent: | ||||
| data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | ||||
| data[1] = MIDI_CONTROL_BANK_SELECT; | data[1] = MIDI_CONTROL_BANK_SELECT; | ||||
| data[2] = value; | data[2] = value; | ||||
| jackbridge_midi_event_write(buffer, time, data, 3); | |||||
| size = 3; | |||||
| break; | break; | ||||
| case CarlaEngineMidiProgramChangeEvent: | case CarlaEngineMidiProgramChangeEvent: | ||||
| data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel; | data[0] = MIDI_STATUS_PROGRAM_CHANGE + channel; | ||||
| data[1] = value; | data[1] = value; | ||||
| jackbridge_midi_event_write(buffer, time, data, 2); | |||||
| size = 2; | |||||
| break; | break; | ||||
| case CarlaEngineAllSoundOffEvent: | case CarlaEngineAllSoundOffEvent: | ||||
| data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | ||||
| data[1] = MIDI_CONTROL_ALL_SOUND_OFF; | data[1] = MIDI_CONTROL_ALL_SOUND_OFF; | ||||
| jackbridge_midi_event_write(buffer, time, data, 2); | |||||
| size = 2; | |||||
| break; | break; | ||||
| case CarlaEngineAllNotesOffEvent: | case CarlaEngineAllNotesOffEvent: | ||||
| data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | data[0] = MIDI_STATUS_CONTROL_CHANGE + channel; | ||||
| data[1] = MIDI_CONTROL_ALL_NOTES_OFF; | data[1] = MIDI_CONTROL_ALL_NOTES_OFF; | ||||
| jackbridge_midi_event_write(buffer, time, data, 2); | |||||
| size = 2; | |||||
| break; | break; | ||||
| } | } | ||||
| if (size > 0) | |||||
| jackbridge_midi_event_write(buffer, time, data, size); | |||||
| } | } | ||||
| private: | private: | ||||
| jack_client_t* const m_client; | jack_client_t* const m_client; | ||||
| jack_port_t* const m_port; | jack_port_t* const m_port; | ||||
| CarlaEngineControlEvent m_retEvent; | |||||
| }; | }; | ||||
| // ------------------------------------------------------------------------------------------------------------------- | // ------------------------------------------------------------------------------------------------------------------- | ||||
| @@ -259,7 +288,7 @@ public: | |||||
| m_client(client), | m_client(client), | ||||
| m_port(port) | m_port(port) | ||||
| { | { | ||||
| qDebug("CarlaEngineJackMidiPort::CarlaEngineJackMidiPort(%s, %s)", bool2str(isInput), ProcessMode2Str(processMode)); | |||||
| qDebug("CarlaEngineJackMidiPort::CarlaEngineJackMidiPort(%s, %s, %p, %p)", bool2str(isInput), ProcessMode2Str(processMode), client, port); | |||||
| if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | if (processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| CARLA_ASSERT(m_client && m_port); | CARLA_ASSERT(m_client && m_port); | ||||
| @@ -277,11 +306,17 @@ public: | |||||
| void initBuffer(CarlaEngine* const engine) | void initBuffer(CarlaEngine* const engine) | ||||
| { | { | ||||
| CARLA_ASSERT(engine); | |||||
| if (! engine) | |||||
| { | |||||
| buffer = nullptr; | |||||
| return; | |||||
| } | |||||
| if (! m_port) | if (! m_port) | ||||
| return CarlaEngineMidiPort::initBuffer(engine); | return CarlaEngineMidiPort::initBuffer(engine); | ||||
| CARLA_ASSERT(engine); | |||||
| buffer = jackbridge_port_get_buffer(m_port, engine->getBufferSize()); | buffer = jackbridge_port_get_buffer(m_port, engine->getBufferSize()); | ||||
| if (! isInput) | if (! isInput) | ||||
| @@ -311,18 +346,20 @@ public: | |||||
| CARLA_ASSERT(buffer); | CARLA_ASSERT(buffer); | ||||
| static jack_midi_event_t jackEvent; | |||||
| static CarlaEngineMidiEvent carlaEvent; | |||||
| if (! buffer) | |||||
| return nullptr; | |||||
| if (jackbridge_midi_event_get(&jackEvent, buffer, index) == 0 && jackEvent.size <= 4) | |||||
| { | |||||
| carlaEvent.time = jackEvent.time; | |||||
| carlaEvent.size = jackEvent.size; | |||||
| memcpy(carlaEvent.data, jackEvent.buffer, jackEvent.size); | |||||
| return &carlaEvent; | |||||
| } | |||||
| jack_midi_event_t jackEvent; | |||||
| return nullptr; | |||||
| if (jackbridge_midi_event_get(&jackEvent, buffer, index) != 0 || jackEvent.size > 3) | |||||
| return nullptr; | |||||
| m_retEvent.clear(); | |||||
| m_retEvent.time = jackEvent.time; | |||||
| m_retEvent.size = jackEvent.size; | |||||
| memcpy(m_retEvent.data, jackEvent.buffer, jackEvent.size); | |||||
| return &m_retEvent; | |||||
| } | } | ||||
| void writeEvent(const uint32_t time, const uint8_t* const data, const uint8_t size) | void writeEvent(const uint32_t time, const uint8_t* const data, const uint8_t size) | ||||
| @@ -337,12 +374,17 @@ public: | |||||
| CARLA_ASSERT(data); | CARLA_ASSERT(data); | ||||
| CARLA_ASSERT(size > 0); | CARLA_ASSERT(size > 0); | ||||
| if (! (buffer && data && size > 0)) | |||||
| return; | |||||
| jackbridge_midi_event_write(buffer, time, data, size); | jackbridge_midi_event_write(buffer, time, data, size); | ||||
| } | } | ||||
| private: | private: | ||||
| jack_client_t* const m_client; | jack_client_t* const m_client; | ||||
| jack_port_t* const m_port; | jack_port_t* const m_port; | ||||
| CarlaEngineMidiEvent m_retEvent; | |||||
| }; | }; | ||||
| // ------------------------------------------------------------------------------------------------------------------- | // ------------------------------------------------------------------------------------------------------------------- | ||||
| @@ -351,11 +393,13 @@ private: | |||||
| class CarlaEngineJackClient : public CarlaEngineClient | class CarlaEngineJackClient : public CarlaEngineClient | ||||
| { | { | ||||
| public: | public: | ||||
| CarlaEngineJackClient(jack_client_t* const client, const CarlaEngineType engineType, const ProcessMode processMode) | |||||
| CarlaEngineJackClient(const CarlaEngineType engineType, const ProcessMode processMode, jack_client_t* const client) | |||||
| : CarlaEngineClient(engineType, processMode), | : CarlaEngineClient(engineType, processMode), | ||||
| m_client(client), | m_client(client), | ||||
| m_usesClient(processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | m_usesClient(processMode == PROCESS_MODE_SINGLE_CLIENT || processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| { | { | ||||
| qDebug("CarlaEngineJackClient::CarlaEngineJackClient(%s, %s, %p)", CarlaEngineType2Str(engineType), ProcessMode2Str(processMode), client); | |||||
| if (m_usesClient) | if (m_usesClient) | ||||
| CARLA_ASSERT(m_client); | CARLA_ASSERT(m_client); | ||||
| else | else | ||||
| @@ -364,6 +408,8 @@ public: | |||||
| ~CarlaEngineJackClient() | ~CarlaEngineJackClient() | ||||
| { | { | ||||
| qDebug("CarlaEngineClient::~CarlaEngineClient()"); | |||||
| if (processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | if (processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| { | { | ||||
| if (m_client) | if (m_client) | ||||
| @@ -373,6 +419,8 @@ public: | |||||
| void activate() | void activate() | ||||
| { | { | ||||
| qDebug("CarlaEngineClient::activate()"); | |||||
| if (processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | if (processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| { | { | ||||
| if (m_client && ! isActive()) | if (m_client && ! isActive()) | ||||
| @@ -384,6 +432,8 @@ public: | |||||
| void deactivate() | void deactivate() | ||||
| { | { | ||||
| qDebug("CarlaEngineClient::deactivate()"); | |||||
| if (processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | if (processMode == PROCESS_MODE_MULTIPLE_CLIENTS) | ||||
| { | { | ||||
| if (m_client && isActive()) | if (m_client && isActive()) | ||||
| @@ -395,6 +445,8 @@ public: | |||||
| bool isOk() const | bool isOk() const | ||||
| { | { | ||||
| qDebug("CarlaEngineClient::isOk()"); | |||||
| if (m_usesClient) | if (m_usesClient) | ||||
| return bool(m_client); | return bool(m_client); | ||||
| @@ -409,9 +461,9 @@ public: | |||||
| jackbridge_recompute_total_latencies(m_client); | jackbridge_recompute_total_latencies(m_client); | ||||
| } | } | ||||
| const CarlaEngineBasePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput) | |||||
| const CarlaEnginePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput) | |||||
| { | { | ||||
| qDebug("CarlaJackEngineClient::addPort(%i, \"%s\", %s)", portType, name, bool2str(isInput)); | |||||
| qDebug("CarlaJackEngineClient::addPort(%s, \"%s\", %s)", CarlaEnginePortType2Str(portType), name, bool2str(isInput)); | |||||
| jack_port_t* port = nullptr; | jack_port_t* port = nullptr; | ||||
| @@ -17,16 +17,9 @@ | |||||
| #ifdef CARLA_ENGINE_RTAUDIO | #ifdef CARLA_ENGINE_RTAUDIO | ||||
| #if defined(Q_OS_MAC) | |||||
| # define __MACOSX_CORE__ | |||||
| #elif defined(Q_OS_WIN) | |||||
| # define __WINDOWS_DS__ | |||||
| # define __WINDOWS_ASIO__ | |||||
| # define __WINDOWS_MM__ | |||||
| #endif | |||||
| #include "carla_engine.hpp" | |||||
| #include "carla_plugin.hpp" | |||||
| #include "carla_engine_internal.hpp" | |||||
| #include "carla_backend_utils.hpp" | |||||
| #include "carla_midi.h" | |||||
| #include "RtAudio.h" | #include "RtAudio.h" | ||||
| #include "RtMidi.h" | #include "RtMidi.h" | ||||
| @@ -48,9 +41,9 @@ public: | |||||
| { | { | ||||
| } | } | ||||
| const CarlaEngineBasePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput) | |||||
| const CarlaEnginePort* addPort(const CarlaEnginePortType portType, const char* const name, const bool isInput) | |||||
| { | { | ||||
| qDebug("CarlaEngineRtAudioClient::addPort(%i, \"%s\", %s)", portType, name, bool2str(isInput)); | |||||
| qDebug("CarlaEngineRtAudioClient::addPort(%s, \"%s\", %s)", CarlaEnginePortType2Str(portType), name, bool2str(isInput)); | |||||
| switch (portType) | switch (portType) | ||||
| { | { | ||||
| @@ -1,4 +1,3 @@ | |||||
| /* -*- Mode: C ; c-basic-offset: 2 -*- */ | |||||
| /***************************************************************************** | /***************************************************************************** | ||||
| * | * | ||||
| * Linux kernel header adapted for user-mode | * Linux kernel header adapted for user-mode | ||||
| @@ -6,7 +5,7 @@ | |||||
| * | * | ||||
| * Original copyright holders of this code are unknown, they were not | * Original copyright holders of this code are unknown, they were not | ||||
| * mentioned in the original file. | * mentioned in the original file. | ||||
| * | |||||
| * | |||||
| * This program is free software; you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
| * the Free Software Foundation; version 2 of the License | * the Free Software Foundation; version 2 of the License | ||||
| @@ -22,23 +21,31 @@ | |||||
| * | * | ||||
| *****************************************************************************/ | *****************************************************************************/ | ||||
| #ifndef _LINUX_LIST_H | |||||
| #define _LINUX_LIST_H | |||||
| #ifndef __LINUX_LIST_H__ | |||||
| #define __LINUX_LIST_H__ | |||||
| /* This file is from Linux Kernel (include/linux/list.h) | |||||
| * and modified by simply removing hardware prefetching of list items. | |||||
| * Here by copyright, credits attributed to wherever they belong. | |||||
| * Filipe Coelho (aka falkTX <falktx@falktx.com>) | |||||
| */ | |||||
| #include <stddef.h> | #include <stddef.h> | ||||
| #if !defined(offsetof) | #if !defined(offsetof) | ||||
| #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |||||
| # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |||||
| #endif | #endif | ||||
| /** | /** | ||||
| * container_of - cast a member of a structure out to the containing structure | * container_of - cast a member of a structure out to the containing structure | ||||
| * @ptr: the pointer to the member. | |||||
| * @type: the type of the container struct this is embedded in. | |||||
| * @member: the name of the member within the struct. | |||||
| * @ptr: the pointer to the member. | |||||
| * @type: the type of the container struct this is embedded in. | |||||
| * @member: the name of the member within the struct. | |||||
| * | * | ||||
| */ | */ | ||||
| #define container_of(ptr, type, member) (type *)((char *)(ptr) - offsetof(type,member)) | |||||
| #define container_of(ptr, type, member) ({ \ | |||||
| const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |||||
| (type *)( (char *)__mptr - offsetof(type,member) );}) | |||||
| #define prefetch(x) (x = x) | #define prefetch(x) (x = x) | ||||
| @@ -61,18 +68,18 @@ | |||||
| */ | */ | ||||
| struct list_head { | struct list_head { | ||||
| struct list_head *next, *prev; | |||||
| struct list_head *next, *prev; | |||||
| }; | }; | ||||
| #define LIST_HEAD_INIT(name) { &(name), &(name) } | #define LIST_HEAD_INIT(name) { &(name), &(name) } | ||||
| #define LIST_HEAD(name) \ | #define LIST_HEAD(name) \ | ||||
| struct list_head name = LIST_HEAD_INIT(name) | |||||
| struct list_head name = LIST_HEAD_INIT(name) | |||||
| static inline void INIT_LIST_HEAD(struct list_head *list) | static inline void INIT_LIST_HEAD(struct list_head *list) | ||||
| { | { | ||||
| list->next = list; | |||||
| list->prev = list; | |||||
| list->next = list; | |||||
| list->prev = list; | |||||
| } | } | ||||
| /* | /* | ||||
| @@ -81,99 +88,38 @@ static inline void INIT_LIST_HEAD(struct list_head *list) | |||||
| * This is only for internal list manipulation where we know | * This is only for internal list manipulation where we know | ||||
| * the prev/next entries already! | * the prev/next entries already! | ||||
| */ | */ | ||||
| static inline void __list_add(struct list_head *new, | |||||
| struct list_head *prev, | |||||
| struct list_head *next) | |||||
| static inline void __list_add(struct list_head *new_, struct list_head *prev, struct list_head *next) | |||||
| { | { | ||||
| next->prev = new; | |||||
| new->next = next; | |||||
| new->prev = prev; | |||||
| prev->next = new; | |||||
| next->prev = new_; | |||||
| new_->next = next; | |||||
| new_->prev = prev; | |||||
| prev->next = new_; | |||||
| } | } | ||||
| /** | /** | ||||
| * list_add - add a new entry | * list_add - add a new entry | ||||
| * @new: new entry to be added | |||||
| * @new: new entry to be added | |||||
| * @head: list head to add it after | * @head: list head to add it after | ||||
| * | * | ||||
| * Insert a new entry after the specified head. | * Insert a new entry after the specified head. | ||||
| * This is good for implementing stacks. | * This is good for implementing stacks. | ||||
| */ | */ | ||||
| static inline void list_add(struct list_head *new, struct list_head *head) | |||||
| static inline void list_add(struct list_head *new_, struct list_head *head) | |||||
| { | { | ||||
| __list_add(new, head, head->next); | |||||
| __list_add(new_, head, head->next); | |||||
| } | } | ||||
| /** | /** | ||||
| * list_add_tail - add a new entry | * list_add_tail - add a new entry | ||||
| * @new: new entry to be added | |||||
| * @new: new entry to be added | |||||
| * @head: list head to add it before | * @head: list head to add it before | ||||
| * | * | ||||
| * Insert a new entry before the specified head. | * Insert a new entry before the specified head. | ||||
| * This is useful for implementing queues. | * This is useful for implementing queues. | ||||
| */ | */ | ||||
| static inline void list_add_tail(struct list_head *new, struct list_head *head) | |||||
| static inline void list_add_tail(struct list_head *new_, struct list_head *head) | |||||
| { | { | ||||
| __list_add(new, head->prev, head); | |||||
| } | |||||
| /* | |||||
| * Insert a new entry between two known consecutive entries. | |||||
| * | |||||
| * This is only for internal list manipulation where we know | |||||
| * the prev/next entries already! | |||||
| */ | |||||
| static inline void __list_add_rcu(struct list_head * new, | |||||
| struct list_head * prev, struct list_head * next) | |||||
| { | |||||
| new->next = next; | |||||
| new->prev = prev; | |||||
| // smp_wmb(); | |||||
| next->prev = new; | |||||
| prev->next = new; | |||||
| } | |||||
| /** | |||||
| * list_add_rcu - add a new entry to rcu-protected list | |||||
| * @new: new entry to be added | |||||
| * @head: list head to add it after | |||||
| * | |||||
| * Insert a new entry after the specified head. | |||||
| * This is good for implementing stacks. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as list_add_rcu() | |||||
| * or list_del_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * list_for_each_entry_rcu(). | |||||
| */ | |||||
| static inline void list_add_rcu(struct list_head *new, struct list_head *head) | |||||
| { | |||||
| __list_add_rcu(new, head, head->next); | |||||
| } | |||||
| /** | |||||
| * list_add_tail_rcu - add a new entry to rcu-protected list | |||||
| * @new: new entry to be added | |||||
| * @head: list head to add it before | |||||
| * | |||||
| * Insert a new entry before the specified head. | |||||
| * This is useful for implementing queues. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as list_add_tail_rcu() | |||||
| * or list_del_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * list_for_each_entry_rcu(). | |||||
| */ | |||||
| static inline void list_add_tail_rcu(struct list_head *new, | |||||
| struct list_head *head) | |||||
| { | |||||
| __list_add_rcu(new, head->prev, head); | |||||
| __list_add(new_, head->prev, head); | |||||
| } | } | ||||
| /* | /* | ||||
| @@ -183,10 +129,10 @@ static inline void list_add_tail_rcu(struct list_head *new, | |||||
| * This is only for internal list manipulation where we know | * This is only for internal list manipulation where we know | ||||
| * the prev/next entries already! | * the prev/next entries already! | ||||
| */ | */ | ||||
| static inline void __list_del(struct list_head * prev, struct list_head * next) | |||||
| static inline void __list_del(struct list_head *prev, struct list_head *next) | |||||
| { | { | ||||
| next->prev = prev; | |||||
| prev->next = next; | |||||
| next->prev = prev; | |||||
| prev->next = next; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -197,57 +143,9 @@ static inline void __list_del(struct list_head * prev, struct list_head * next) | |||||
| */ | */ | ||||
| static inline void list_del(struct list_head *entry) | static inline void list_del(struct list_head *entry) | ||||
| { | { | ||||
| __list_del(entry->prev, entry->next); | |||||
| entry->next = LIST_POISON1; | |||||
| entry->prev = LIST_POISON2; | |||||
| } | |||||
| /** | |||||
| * list_del_rcu - deletes entry from list without re-initialization | |||||
| * @entry: the element to delete from the list. | |||||
| * | |||||
| * Note: list_empty on entry does not return true after this, | |||||
| * the entry is in an undefined state. It is useful for RCU based | |||||
| * lockfree traversal. | |||||
| * | |||||
| * In particular, it means that we can not poison the forward | |||||
| * pointers that may still be used for walking the list. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as list_del_rcu() | |||||
| * or list_add_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * list_for_each_entry_rcu(). | |||||
| * | |||||
| * Note that the caller is not permitted to immediately free | |||||
| * the newly deleted entry. Instead, either synchronize_rcu() | |||||
| * or call_rcu() must be used to defer freeing until an RCU | |||||
| * grace period has elapsed. | |||||
| */ | |||||
| static inline void list_del_rcu(struct list_head *entry) | |||||
| { | |||||
| __list_del(entry->prev, entry->next); | |||||
| entry->prev = LIST_POISON2; | |||||
| } | |||||
| /* | |||||
| * list_replace_rcu - replace old entry by new one | |||||
| * @old : the element to be replaced | |||||
| * @new : the new element to insert | |||||
| * | |||||
| * The old entry will be replaced with the new entry atomically. | |||||
| */ | |||||
| static inline void list_replace_rcu(struct list_head *old, | |||||
| struct list_head *new) | |||||
| { | |||||
| new->next = old->next; | |||||
| new->prev = old->prev; | |||||
| // smp_wmb(); | |||||
| new->next->prev = new; | |||||
| new->prev->next = new; | |||||
| old->prev = LIST_POISON2; | |||||
| __list_del(entry->prev, entry->next); | |||||
| entry->next = (struct list_head*)LIST_POISON1; | |||||
| entry->prev = (struct list_head*)LIST_POISON2; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -256,8 +154,8 @@ static inline void list_replace_rcu(struct list_head *old, | |||||
| */ | */ | ||||
| static inline void list_del_init(struct list_head *entry) | static inline void list_del_init(struct list_head *entry) | ||||
| { | { | ||||
| __list_del(entry->prev, entry->next); | |||||
| INIT_LIST_HEAD(entry); | |||||
| __list_del(entry->prev, entry->next); | |||||
| INIT_LIST_HEAD(entry); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -267,8 +165,8 @@ static inline void list_del_init(struct list_head *entry) | |||||
| */ | */ | ||||
| static inline void list_move(struct list_head *list, struct list_head *head) | static inline void list_move(struct list_head *list, struct list_head *head) | ||||
| { | { | ||||
| __list_del(list->prev, list->next); | |||||
| list_add(list, head); | |||||
| __list_del(list->prev, list->next); | |||||
| list_add(list, head); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -276,11 +174,10 @@ static inline void list_move(struct list_head *list, struct list_head *head) | |||||
| * @list: the entry to move | * @list: the entry to move | ||||
| * @head: the head that will follow our entry | * @head: the head that will follow our entry | ||||
| */ | */ | ||||
| static inline void list_move_tail(struct list_head *list, | |||||
| struct list_head *head) | |||||
| static inline void list_move_tail(struct list_head *list, struct list_head *head) | |||||
| { | { | ||||
| __list_del(list->prev, list->next); | |||||
| list_add_tail(list, head); | |||||
| __list_del(list->prev, list->next); | |||||
| list_add_tail(list, head); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -289,7 +186,7 @@ static inline void list_move_tail(struct list_head *list, | |||||
| */ | */ | ||||
| static inline int list_empty(const struct list_head *head) | static inline int list_empty(const struct list_head *head) | ||||
| { | { | ||||
| return head->next == head; | |||||
| return head->next == head; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -306,22 +203,21 @@ static inline int list_empty(const struct list_head *head) | |||||
| */ | */ | ||||
| static inline int list_empty_careful(const struct list_head *head) | static inline int list_empty_careful(const struct list_head *head) | ||||
| { | { | ||||
| struct list_head *next = head->next; | |||||
| return (next == head) && (next == head->prev); | |||||
| struct list_head *next = head->next; | |||||
| return (next == head) && (next == head->prev); | |||||
| } | } | ||||
| static inline void __list_splice(struct list_head *list, | |||||
| struct list_head *head) | |||||
| static inline void __list_splice(struct list_head *list, struct list_head *head) | |||||
| { | { | ||||
| struct list_head *first = list->next; | |||||
| struct list_head *last = list->prev; | |||||
| struct list_head *at = head->next; | |||||
| struct list_head *first = list->next; | |||||
| struct list_head *last = list->prev; | |||||
| struct list_head *at = head->next; | |||||
| first->prev = head; | |||||
| head->next = first; | |||||
| first->prev = head; | |||||
| head->next = first; | |||||
| last->next = at; | |||||
| at->prev = last; | |||||
| last->next = at; | |||||
| at->prev = last; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -331,8 +227,8 @@ static inline void __list_splice(struct list_head *list, | |||||
| */ | */ | ||||
| static inline void list_splice(struct list_head *list, struct list_head *head) | static inline void list_splice(struct list_head *list, struct list_head *head) | ||||
| { | { | ||||
| if (!list_empty(list)) | |||||
| __list_splice(list, head); | |||||
| if (!list_empty(list)) | |||||
| __list_splice(list, head); | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -342,32 +238,32 @@ static inline void list_splice(struct list_head *list, struct list_head *head) | |||||
| * | * | ||||
| * The list at @list is reinitialised | * The list at @list is reinitialised | ||||
| */ | */ | ||||
| static inline void list_splice_init(struct list_head *list, | |||||
| struct list_head *head) | |||||
| static inline void list_splice_init(struct list_head *list, struct list_head *head) | |||||
| { | { | ||||
| if (!list_empty(list)) { | |||||
| __list_splice(list, head); | |||||
| INIT_LIST_HEAD(list); | |||||
| } | |||||
| if (!list_empty(list)) { | |||||
| __list_splice(list, head); | |||||
| INIT_LIST_HEAD(list); | |||||
| } | |||||
| } | } | ||||
| /** | /** | ||||
| * list_entry - get the struct for this entry | * list_entry - get the struct for this entry | ||||
| * @ptr: the &struct list_head pointer. | |||||
| * @type: the type of the struct this is embedded in. | |||||
| * @ptr: the &struct list_head pointer. | |||||
| * @type: the type of the struct this is embedded in. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_entry(ptr, type, member) \ | #define list_entry(ptr, type, member) \ | ||||
| container_of(ptr, type, member) | |||||
| ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) | |||||
| //container_of(ptr, type, member) | |||||
| /** | /** | ||||
| * list_for_each - iterate over a list | |||||
| * list_for_each - iterate over a list | |||||
| * @pos: the &struct list_head to use as a loop counter. | * @pos: the &struct list_head to use as a loop counter. | ||||
| * @head: the head for your list. | * @head: the head for your list. | ||||
| */ | */ | ||||
| #define list_for_each(pos, head) \ | #define list_for_each(pos, head) \ | ||||
| for (pos = (head)->next; prefetch(pos->next), pos != (head); \ | |||||
| pos = pos->next) | |||||
| for (pos = (head)->next; prefetch(pos->next), pos != (head); \ | |||||
| pos = pos->next) | |||||
| /** | /** | ||||
| * __list_for_each - iterate over a list | * __list_for_each - iterate over a list | ||||
| @@ -380,7 +276,7 @@ static inline void list_splice_init(struct list_head *list, | |||||
| * or 1 entry) most of the time. | * or 1 entry) most of the time. | ||||
| */ | */ | ||||
| #define __list_for_each(pos, head) \ | #define __list_for_each(pos, head) \ | ||||
| for (pos = (head)->next; pos != (head); pos = pos->next) | |||||
| for (pos = (head)->next; pos != (head); pos = pos->next) | |||||
| /** | /** | ||||
| * list_for_each_prev - iterate over a list backwards | * list_for_each_prev - iterate over a list backwards | ||||
| @@ -388,8 +284,8 @@ static inline void list_splice_init(struct list_head *list, | |||||
| * @head: the head for your list. | * @head: the head for your list. | ||||
| */ | */ | ||||
| #define list_for_each_prev(pos, head) \ | #define list_for_each_prev(pos, head) \ | ||||
| for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ | |||||
| pos = pos->prev) | |||||
| for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ | |||||
| pos = pos->prev) | |||||
| /** | /** | ||||
| * list_for_each_safe - iterate over a list safe against removal of list entry | * list_for_each_safe - iterate over a list safe against removal of list entry | ||||
| @@ -398,475 +294,110 @@ static inline void list_splice_init(struct list_head *list, | |||||
| * @head: the head for your list. | * @head: the head for your list. | ||||
| */ | */ | ||||
| #define list_for_each_safe(pos, n, head) \ | #define list_for_each_safe(pos, n, head) \ | ||||
| for (pos = (head)->next, n = pos->next; pos != (head); \ | |||||
| for (pos = (head)->next, n = pos->next; pos != (head); \ | |||||
| pos = n, n = pos->next) | pos = n, n = pos->next) | ||||
| /** | /** | ||||
| * list_for_each_entry - iterate over list of given type | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * list_for_each_entry - iterate over list of given type | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry(pos, head, member) \ | #define list_for_each_entry(pos, head, member) \ | ||||
| for (pos = list_entry((head)->next, typeof(*pos), member); \ | |||||
| prefetch(pos->member.next), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| for (pos = list_entry((head)->next, typeof(*pos), member); \ | |||||
| prefetch(pos->member.next), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_reverse - iterate backwards over list of given type. | * list_for_each_entry_reverse - iterate backwards over list of given type. | ||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_reverse(pos, head, member) \ | #define list_for_each_entry_reverse(pos, head, member) \ | ||||
| for (pos = list_entry((head)->prev, typeof(*pos), member); \ | |||||
| prefetch(pos->member.prev), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.prev, typeof(*pos), member)) | |||||
| for (pos = list_entry((head)->prev, typeof(*pos), member); \ | |||||
| prefetch(pos->member.prev), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.prev, typeof(*pos), member)) | |||||
| /** | /** | ||||
| * list_prepare_entry - prepare a pos entry for use as a start point in | |||||
| * list_for_each_entry_continue | |||||
| * @pos: the type * to use as a start point | |||||
| * @head: the head of the list | |||||
| * list_prepare_entry - prepare a pos entry for use as a start point in list_for_each_entry_continue | |||||
| * @pos: the type * to use as a start point | |||||
| * @head: the head of the list | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_prepare_entry(pos, head, member) \ | #define list_prepare_entry(pos, head, member) \ | ||||
| ((pos) ? : list_entry(head, typeof(*pos), member)) | |||||
| ((pos) ? : list_entry(head, typeof(*pos), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_continue - iterate over list of given type | |||||
| * continuing after existing point | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * list_for_each_entry_continue - iterate over list of given type continuing after existing point | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_continue(pos, head, member) \ | #define list_for_each_entry_continue(pos, head, member) \ | ||||
| for (pos = list_entry(pos->member.next, typeof(*pos), member); \ | |||||
| prefetch(pos->member.next), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| for (pos = list_entry(pos->member.next, typeof(*pos), member); \ | |||||
| prefetch(pos->member.next), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_from - iterate over list of given type | |||||
| * continuing from existing point | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * list_for_each_entry_from - iterate over list of given type continuing from existing point | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_from(pos, head, member) \ | #define list_for_each_entry_from(pos, head, member) \ | ||||
| for (; prefetch(pos->member.next), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| for (; prefetch(pos->member.next), &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry | ||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_safe(pos, n, head, member) \ | #define list_for_each_entry_safe(pos, n, head, member) \ | ||||
| for (pos = list_entry((head)->next, typeof(*pos), member), \ | |||||
| for (pos = list_entry((head)->next, typeof(*pos), member), \ | |||||
| n = list_entry(pos->member.next, typeof(*pos), member); \ | n = list_entry(pos->member.next, typeof(*pos), member); \ | ||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_safe_continue - iterate over list of given type | |||||
| * continuing after existing point safe against removal of list entry | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * list_for_each_entry_safe_continue - iterate over list of given type continuing after existing point safe against removal of list entry | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_safe_continue(pos, n, head, member) \ | #define list_for_each_entry_safe_continue(pos, n, head, member) \ | ||||
| for (pos = list_entry(pos->member.next, typeof(*pos), member), \ | |||||
| for (pos = list_entry(pos->member.next, typeof(*pos), member), \ | |||||
| n = list_entry(pos->member.next, typeof(*pos), member); \ | n = list_entry(pos->member.next, typeof(*pos), member); \ | ||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_safe_from - iterate over list of given type | |||||
| * from existing point safe against removal of list entry | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * list_for_each_entry_safe_from - iterate over list of given type from existing point safe against removal of list entry | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_safe_from(pos, n, head, member) \ | #define list_for_each_entry_safe_from(pos, n, head, member) \ | ||||
| for (n = list_entry(pos->member.next, typeof(*pos), member); \ | |||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |||||
| for (n = list_entry(pos->member.next, typeof(*pos), member); \ | |||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |||||
| /** | /** | ||||
| * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against | |||||
| * removal of list entry | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against removal of list entry | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @n: another type * to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | * @member: the name of the list_struct within the struct. | ||||
| */ | */ | ||||
| #define list_for_each_entry_safe_reverse(pos, n, head, member) \ | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ | ||||
| for (pos = list_entry((head)->prev, typeof(*pos), member), \ | |||||
| for (pos = list_entry((head)->prev, typeof(*pos), member), \ | |||||
| n = list_entry(pos->member.prev, typeof(*pos), member); \ | n = list_entry(pos->member.prev, typeof(*pos), member); \ | ||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.prev, typeof(*n), member)) | |||||
| /** | |||||
| * list_for_each_rcu - iterate over an rcu-protected list | |||||
| * @pos: the &struct list_head to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * | |||||
| * This list-traversal primitive may safely run concurrently with | |||||
| * the _rcu list-mutation primitives such as list_add_rcu() | |||||
| * as long as the traversal is guarded by rcu_read_lock(). | |||||
| */ | |||||
| #define list_for_each_rcu(pos, head) \ | |||||
| for (pos = (head)->next; \ | |||||
| prefetch(rcu_dereference(pos)->next), pos != (head); \ | |||||
| pos = pos->next) | |||||
| #define __list_for_each_rcu(pos, head) \ | |||||
| for (pos = (head)->next; \ | |||||
| rcu_dereference(pos) != (head); \ | |||||
| pos = pos->next) | |||||
| /** | |||||
| * list_for_each_safe_rcu - iterate over an rcu-protected list safe | |||||
| * against removal of list entry | |||||
| * @pos: the &struct list_head to use as a loop counter. | |||||
| * @n: another &struct list_head to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * | |||||
| * This list-traversal primitive may safely run concurrently with | |||||
| * the _rcu list-mutation primitives such as list_add_rcu() | |||||
| * as long as the traversal is guarded by rcu_read_lock(). | |||||
| */ | |||||
| #define list_for_each_safe_rcu(pos, n, head) \ | |||||
| for (pos = (head)->next; \ | |||||
| n = rcu_dereference(pos)->next, pos != (head); \ | |||||
| pos = n) | |||||
| /** | |||||
| * list_for_each_entry_rcu - iterate over rcu list of given type | |||||
| * @pos: the type * to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the list_struct within the struct. | |||||
| * | |||||
| * This list-traversal primitive may safely run concurrently with | |||||
| * the _rcu list-mutation primitives such as list_add_rcu() | |||||
| * as long as the traversal is guarded by rcu_read_lock(). | |||||
| */ | |||||
| #define list_for_each_entry_rcu(pos, head, member) \ | |||||
| for (pos = list_entry((head)->next, typeof(*pos), member); \ | |||||
| prefetch(rcu_dereference(pos)->member.next), \ | |||||
| &pos->member != (head); \ | |||||
| pos = list_entry(pos->member.next, typeof(*pos), member)) | |||||
| /** | |||||
| * list_for_each_continue_rcu - iterate over an rcu-protected list | |||||
| * continuing after existing point. | |||||
| * @pos: the &struct list_head to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * | |||||
| * This list-traversal primitive may safely run concurrently with | |||||
| * the _rcu list-mutation primitives such as list_add_rcu() | |||||
| * as long as the traversal is guarded by rcu_read_lock(). | |||||
| */ | |||||
| #define list_for_each_continue_rcu(pos, head) \ | |||||
| for ((pos) = (pos)->next; \ | |||||
| prefetch(rcu_dereference((pos))->next), (pos) != (head); \ | |||||
| (pos) = (pos)->next) | |||||
| /* | |||||
| * Double linked lists with a single pointer list head. | |||||
| * Mostly useful for hash tables where the two pointer list head is | |||||
| * too wasteful. | |||||
| * You lose the ability to access the tail in O(1). | |||||
| */ | |||||
| struct hlist_head { | |||||
| struct hlist_node *first; | |||||
| }; | |||||
| struct hlist_node { | |||||
| struct hlist_node *next, **pprev; | |||||
| }; | |||||
| #define HLIST_HEAD_INIT { .first = NULL } | |||||
| #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } | |||||
| #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) | |||||
| static inline void INIT_HLIST_NODE(struct hlist_node *h) | |||||
| { | |||||
| h->next = NULL; | |||||
| h->pprev = NULL; | |||||
| } | |||||
| static inline int hlist_unhashed(const struct hlist_node *h) | |||||
| { | |||||
| return !h->pprev; | |||||
| } | |||||
| &pos->member != (head); \ | |||||
| pos = n, n = list_entry(n->member.prev, typeof(*n), member)) | |||||
| static inline int hlist_empty(const struct hlist_head *h) | |||||
| { | |||||
| return !h->first; | |||||
| } | |||||
| static inline void __hlist_del(struct hlist_node *n) | |||||
| { | |||||
| struct hlist_node *next = n->next; | |||||
| struct hlist_node **pprev = n->pprev; | |||||
| *pprev = next; | |||||
| if (next) | |||||
| next->pprev = pprev; | |||||
| } | |||||
| static inline void hlist_del(struct hlist_node *n) | |||||
| { | |||||
| __hlist_del(n); | |||||
| n->next = LIST_POISON1; | |||||
| n->pprev = LIST_POISON2; | |||||
| } | |||||
| /** | |||||
| * hlist_del_rcu - deletes entry from hash list without re-initialization | |||||
| * @n: the element to delete from the hash list. | |||||
| * | |||||
| * Note: list_unhashed() on entry does not return true after this, | |||||
| * the entry is in an undefined state. It is useful for RCU based | |||||
| * lockfree traversal. | |||||
| * | |||||
| * In particular, it means that we can not poison the forward | |||||
| * pointers that may still be used for walking the hash list. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as hlist_add_head_rcu() | |||||
| * or hlist_del_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * hlist_for_each_entry(). | |||||
| */ | |||||
| static inline void hlist_del_rcu(struct hlist_node *n) | |||||
| { | |||||
| __hlist_del(n); | |||||
| n->pprev = LIST_POISON2; | |||||
| } | |||||
| static inline void hlist_del_init(struct hlist_node *n) | |||||
| { | |||||
| if (!hlist_unhashed(n)) { | |||||
| __hlist_del(n); | |||||
| INIT_HLIST_NODE(n); | |||||
| } | |||||
| } | |||||
| /* | |||||
| * hlist_replace_rcu - replace old entry by new one | |||||
| * @old : the element to be replaced | |||||
| * @new : the new element to insert | |||||
| * | |||||
| * The old entry will be replaced with the new entry atomically. | |||||
| */ | |||||
| static inline void hlist_replace_rcu(struct hlist_node *old, | |||||
| struct hlist_node *new) | |||||
| { | |||||
| struct hlist_node *next = old->next; | |||||
| new->next = next; | |||||
| new->pprev = old->pprev; | |||||
| // smp_wmb(); | |||||
| if (next) | |||||
| new->next->pprev = &new->next; | |||||
| *new->pprev = new; | |||||
| old->pprev = LIST_POISON2; | |||||
| } | |||||
| static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) | |||||
| { | |||||
| struct hlist_node *first = h->first; | |||||
| n->next = first; | |||||
| if (first) | |||||
| first->pprev = &n->next; | |||||
| h->first = n; | |||||
| n->pprev = &h->first; | |||||
| } | |||||
| /** | |||||
| * hlist_add_head_rcu - adds the specified element to the specified hlist, | |||||
| * while permitting racing traversals. | |||||
| * @n: the element to add to the hash list. | |||||
| * @h: the list to add to. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as hlist_add_head_rcu() | |||||
| * or hlist_del_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * hlist_for_each_entry_rcu(), used to prevent memory-consistency | |||||
| * problems on Alpha CPUs. Regardless of the type of CPU, the | |||||
| * list-traversal primitive must be guarded by rcu_read_lock(). | |||||
| */ | |||||
| static inline void hlist_add_head_rcu(struct hlist_node *n, | |||||
| struct hlist_head *h) | |||||
| { | |||||
| struct hlist_node *first = h->first; | |||||
| n->next = first; | |||||
| n->pprev = &h->first; | |||||
| // smp_wmb(); | |||||
| if (first) | |||||
| first->pprev = &n->next; | |||||
| h->first = n; | |||||
| } | |||||
| /* next must be != NULL */ | |||||
| static inline void hlist_add_before(struct hlist_node *n, | |||||
| struct hlist_node *next) | |||||
| { | |||||
| n->pprev = next->pprev; | |||||
| n->next = next; | |||||
| next->pprev = &n->next; | |||||
| *(n->pprev) = n; | |||||
| } | |||||
| static inline void hlist_add_after(struct hlist_node *n, | |||||
| struct hlist_node *next) | |||||
| { | |||||
| next->next = n->next; | |||||
| n->next = next; | |||||
| next->pprev = &n->next; | |||||
| if(next->next) | |||||
| next->next->pprev = &next->next; | |||||
| } | |||||
| /** | |||||
| * hlist_add_before_rcu - adds the specified element to the specified hlist | |||||
| * before the specified node while permitting racing traversals. | |||||
| * @n: the new element to add to the hash list. | |||||
| * @next: the existing element to add the new element before. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as hlist_add_head_rcu() | |||||
| * or hlist_del_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * hlist_for_each_entry_rcu(), used to prevent memory-consistency | |||||
| * problems on Alpha CPUs. | |||||
| */ | |||||
| static inline void hlist_add_before_rcu(struct hlist_node *n, | |||||
| struct hlist_node *next) | |||||
| { | |||||
| n->pprev = next->pprev; | |||||
| n->next = next; | |||||
| // smp_wmb(); | |||||
| next->pprev = &n->next; | |||||
| *(n->pprev) = n; | |||||
| } | |||||
| /** | |||||
| * hlist_add_after_rcu - adds the specified element to the specified hlist | |||||
| * after the specified node while permitting racing traversals. | |||||
| * @prev: the existing element to add the new element after. | |||||
| * @n: the new element to add to the hash list. | |||||
| * | |||||
| * The caller must take whatever precautions are necessary | |||||
| * (such as holding appropriate locks) to avoid racing | |||||
| * with another list-mutation primitive, such as hlist_add_head_rcu() | |||||
| * or hlist_del_rcu(), running on this same list. | |||||
| * However, it is perfectly legal to run concurrently with | |||||
| * the _rcu list-traversal primitives, such as | |||||
| * hlist_for_each_entry_rcu(), used to prevent memory-consistency | |||||
| * problems on Alpha CPUs. | |||||
| */ | |||||
| static inline void hlist_add_after_rcu(struct hlist_node *prev, | |||||
| struct hlist_node *n) | |||||
| { | |||||
| n->next = prev->next; | |||||
| n->pprev = &prev->next; | |||||
| // smp_wmb(); | |||||
| prev->next = n; | |||||
| if (n->next) | |||||
| n->next->pprev = &n->next; | |||||
| } | |||||
| #define hlist_entry(ptr, type, member) container_of(ptr,type,member) | |||||
| #define hlist_for_each(pos, head) \ | |||||
| for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ | |||||
| pos = pos->next) | |||||
| #define hlist_for_each_safe(pos, n, head) \ | |||||
| for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ | |||||
| pos = n) | |||||
| /** | |||||
| * hlist_for_each_entry - iterate over list of given type | |||||
| * @tpos: the type * to use as a loop counter. | |||||
| * @pos: the &struct hlist_node to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the hlist_node within the struct. | |||||
| */ | |||||
| #define hlist_for_each_entry(tpos, pos, head, member) \ | |||||
| for (pos = (head)->first; \ | |||||
| pos && ({ prefetch(pos->next); 1;}) && \ | |||||
| ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |||||
| pos = pos->next) | |||||
| /** | |||||
| * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point | |||||
| * @tpos: the type * to use as a loop counter. | |||||
| * @pos: the &struct hlist_node to use as a loop counter. | |||||
| * @member: the name of the hlist_node within the struct. | |||||
| */ | |||||
| #define hlist_for_each_entry_continue(tpos, pos, member) \ | |||||
| for (pos = (pos)->next; \ | |||||
| pos && ({ prefetch(pos->next); 1;}) && \ | |||||
| ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |||||
| pos = pos->next) | |||||
| /** | |||||
| * hlist_for_each_entry_from - iterate over a hlist continuing from existing point | |||||
| * @tpos: the type * to use as a loop counter. | |||||
| * @pos: the &struct hlist_node to use as a loop counter. | |||||
| * @member: the name of the hlist_node within the struct. | |||||
| */ | |||||
| #define hlist_for_each_entry_from(tpos, pos, member) \ | |||||
| for (; pos && ({ prefetch(pos->next); 1;}) && \ | |||||
| ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |||||
| pos = pos->next) | |||||
| /** | |||||
| * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry | |||||
| * @tpos: the type * to use as a loop counter. | |||||
| * @pos: the &struct hlist_node to use as a loop counter. | |||||
| * @n: another &struct hlist_node to use as temporary storage | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the hlist_node within the struct. | |||||
| */ | |||||
| #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ | |||||
| for (pos = (head)->first; \ | |||||
| pos && ({ n = pos->next; 1; }) && \ | |||||
| ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |||||
| pos = n) | |||||
| /** | |||||
| * hlist_for_each_entry_rcu - iterate over rcu list of given type | |||||
| * @tpos: the type * to use as a loop counter. | |||||
| * @pos: the &struct hlist_node to use as a loop counter. | |||||
| * @head: the head for your list. | |||||
| * @member: the name of the hlist_node within the struct. | |||||
| * | |||||
| * This list-traversal primitive may safely run concurrently with | |||||
| * the _rcu list-mutation primitives such as hlist_add_head_rcu() | |||||
| * as long as the traversal is guarded by rcu_read_lock(). | |||||
| */ | |||||
| #define hlist_for_each_entry_rcu(tpos, pos, head, member) \ | |||||
| for (pos = (head)->first; \ | |||||
| rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \ | |||||
| ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |||||
| pos = pos->next) | |||||
| #endif | |||||
| #endif // __LINUX_LIST_H__ | |||||
| @@ -1,4 +0,0 @@ | |||||
| /* simple file for rtmempool compatibility */ | |||||
| #define LOG_DEBUG(format, arg...) | |||||
| #define LOG_WARNING(format, arg...) | |||||
| @@ -0,0 +1,31 @@ | |||||
| /* | |||||
| * RealTime Memory Pool, heavily based on work by Nedko Arnaudov | |||||
| * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com> | |||||
| * | |||||
| * 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 | |||||
| * 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. | |||||
| * | |||||
| * For a full copy of the GNU General Public License see the COPYING file | |||||
| */ | |||||
| #ifndef __RTMEMPOOL_LV2_H__ | |||||
| #define __RTMEMPOOL_LV2_H__ | |||||
| #include "rtmempool.h" | |||||
| #include "lv2/lv2_rtmempool.h" | |||||
| /** | |||||
| * Initialize LV2_RTSAFE_MEMORY_POOL__Pool feature | |||||
| * | |||||
| * @param poolPtr host allocated pointer to LV2_RtMemPool_Pool | |||||
| */ | |||||
| void lv2_rtmempool_init(LV2_RtMemPool_Pool* poolPtr); | |||||
| #endif // __RTMEMPOOL_LV2_H__ | |||||
| @@ -1,397 +1,379 @@ | |||||
| /* -*- Mode: C ; c-basic-offset: 2 -*- */ | |||||
| /***************************************************************************** | |||||
| /* | |||||
| * RealTime Memory Pool, heavily based on work by Nedko Arnaudov | |||||
| * Copyright (C) 2006-2009 Nedko Arnaudov <nedko@arnaudov.name> | |||||
| * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com> | |||||
| * | * | ||||
| * This file is part of zynjacku | |||||
| * 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 | |||||
| * any later version. | |||||
| * | * | ||||
| * Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name> | |||||
| * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com> | |||||
| * 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. | |||||
| * | * | ||||
| * 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; version 2 of the License | |||||
| * | |||||
| * 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; if not, write to the Free Software | |||||
| * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
| * | |||||
| *****************************************************************************/ | |||||
| * For a full copy of the GNU General Public License see the COPYING file | |||||
| */ | |||||
| #include "rtmempool.h" | |||||
| #include "list.h" | |||||
| #include <stddef.h> | |||||
| #include <stdbool.h> | |||||
| #include <string.h> | |||||
| #include <stdio.h> /* sprintf */ | |||||
| #include <stdlib.h> | |||||
| #include <assert.h> | #include <assert.h> | ||||
| #include <pthread.h> | #include <pthread.h> | ||||
| #include <stdio.h> | |||||
| #include <stdlib.h> | |||||
| #include <string.h> | |||||
| #include "rtmempool.h" | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| #include "list.h" | |||||
| //#define LOG_LEVEL LOG_LEVEL_DEBUG | |||||
| #include "log.h" | |||||
| typedef struct list_head k_list_head; | |||||
| struct rtsafe_memory_pool | |||||
| { | |||||
| char name[LV2_RTSAFE_MEMORY_POOL_NAME_MAX]; | |||||
| size_t data_size; | |||||
| size_t min_preallocated; | |||||
| size_t max_preallocated; | |||||
| unsigned int used_count; | |||||
| struct list_head unused; | |||||
| struct list_head used; | |||||
| unsigned int unused_count; | |||||
| bool enforce_thread_safety; | |||||
| /* next members are initialized/used only if enforce_thread_safety is true */ | |||||
| pthread_mutex_t mutex; | |||||
| unsigned int unused_count2; | |||||
| struct list_head pending; | |||||
| size_t used_size; | |||||
| }; | |||||
| static | |||||
| void | |||||
| rtsafe_memory_pool_sleepy( | |||||
| LV2_RtMemPool_Handle pool_handle); | |||||
| static | |||||
| bool | |||||
| rtsafe_memory_pool_create( | |||||
| LV2_RtMemPool_Handle * pool_handle_ptr, | |||||
| const char * pool_name, | |||||
| size_t data_size, | |||||
| size_t min_preallocated, | |||||
| size_t max_preallocated, | |||||
| bool enforce_thread_safety) | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| typedef struct _RtMemPool | |||||
| { | { | ||||
| int ret; | |||||
| struct rtsafe_memory_pool * pool_ptr; | |||||
| assert(min_preallocated <= max_preallocated); | |||||
| assert(pool_name == NULL || strlen(pool_name) < LV2_RTSAFE_MEMORY_POOL_NAME_MAX); | |||||
| LOG_DEBUG( | |||||
| "creating pool \"%s\" (size %u, min = %u, max = %u, enforce = %s)", | |||||
| pool_name, | |||||
| (unsigned int)data_size, | |||||
| (unsigned int)min_preallocated, | |||||
| (unsigned int)max_preallocated, | |||||
| enforce_thread_safety ? "true" : "false"); | |||||
| pool_ptr = malloc(sizeof(struct rtsafe_memory_pool)); | |||||
| if (pool_ptr == NULL) | |||||
| { | |||||
| return false; | |||||
| } | |||||
| if (pool_name != NULL) | |||||
| { | |||||
| strcpy(pool_ptr->name, pool_name); | |||||
| } | |||||
| else | |||||
| { | |||||
| sprintf(pool_ptr->name, "%p", pool_ptr); | |||||
| } | |||||
| pool_ptr->data_size = data_size; | |||||
| pool_ptr->min_preallocated = min_preallocated; | |||||
| pool_ptr->max_preallocated = max_preallocated; | |||||
| INIT_LIST_HEAD(&pool_ptr->used); | |||||
| pool_ptr->used_count = 0; | |||||
| INIT_LIST_HEAD(&pool_ptr->unused); | |||||
| pool_ptr->unused_count = 0; | |||||
| pool_ptr->enforce_thread_safety = enforce_thread_safety; | |||||
| if (enforce_thread_safety) | |||||
| { | |||||
| ret = pthread_mutex_init(&pool_ptr->mutex, NULL); | |||||
| if (ret != 0) | |||||
| { | |||||
| free(pool_ptr); | |||||
| return false; | |||||
| } | |||||
| char name[RTSAFE_MEMORY_POOL_NAME_MAX]; | |||||
| INIT_LIST_HEAD(&pool_ptr->pending); | |||||
| pool_ptr->unused_count2 = 0; | |||||
| } | |||||
| size_t dataSize; | |||||
| size_t minPreallocated; | |||||
| size_t maxPreallocated; | |||||
| pool_ptr->used_size = 0; | |||||
| k_list_head used; | |||||
| unsigned int usedCount; | |||||
| rtsafe_memory_pool_sleepy((LV2_RtMemPool_Handle)pool_ptr); | |||||
| *pool_handle_ptr = (LV2_RtMemPool_Handle)pool_ptr; | |||||
| k_list_head unused; | |||||
| unsigned int unusedCount; | |||||
| return true; | |||||
| } | |||||
| bool enforceThreadSafety; | |||||
| #define pool_ptr ((struct rtsafe_memory_pool *)pool_handle) | |||||
| // next members are initialized/used only if enforceThreadSafety is true | |||||
| pthread_mutex_t mutex; | |||||
| unsigned int unusedCount2; | |||||
| k_list_head pending; | |||||
| size_t usedSize; | |||||
| static | |||||
| void | |||||
| rtsafe_memory_pool_destroy( | |||||
| LV2_RtMemPool_Handle pool_handle) | |||||
| { | |||||
| int ret; | |||||
| struct list_head * node_ptr; | |||||
| } RtMemPool; | |||||
| LOG_DEBUG("destroying pool \"%s\"", pool_ptr->name); | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| // adjust unused list size | |||||
| /* caller should deallocate all chunks prior releasing pool itself */ | |||||
| if (pool_ptr->used_count != 0) | |||||
| { | |||||
| LOG_WARNING("Deallocating non-empty pool \"%s\", leaking %u entries:", pool_ptr->name, pool_ptr->used_count); | |||||
| void rtsafe_memory_pool_sleepy(RtMemPool* poolPtr) | |||||
| { | |||||
| k_list_head* nodePtr; | |||||
| unsigned int count; | |||||
| list_for_each(node_ptr, &pool_ptr->used) | |||||
| if (poolPtr->enforceThreadSafety) | |||||
| { | { | ||||
| LOG_WARNING(" %p", node_ptr + 1); | |||||
| } | |||||
| pthread_mutex_lock(&poolPtr->mutex); | |||||
| assert(0); | |||||
| } | |||||
| count = poolPtr->unusedCount2; | |||||
| while (pool_ptr->unused_count != 0) | |||||
| { | |||||
| assert(!list_empty(&pool_ptr->unused)); | |||||
| assert(poolPtr->minPreallocated < poolPtr->maxPreallocated); | |||||
| node_ptr = pool_ptr->unused.next; | |||||
| while (count < poolPtr->minPreallocated) | |||||
| { | |||||
| nodePtr = malloc(sizeof(k_list_head) + poolPtr->dataSize); | |||||
| list_del(node_ptr); | |||||
| pool_ptr->unused_count--; | |||||
| if (nodePtr == NULL) | |||||
| { | |||||
| break; | |||||
| } | |||||
| free(node_ptr); | |||||
| } | |||||
| list_add_tail(nodePtr, &poolPtr->pending); | |||||
| assert(list_empty(&pool_ptr->unused)); | |||||
| count++; | |||||
| if (pool_ptr->enforce_thread_safety) | |||||
| { | |||||
| while (!list_empty(&pool_ptr->pending)) | |||||
| { | |||||
| node_ptr = pool_ptr->pending.next; | |||||
| poolPtr->usedSize += poolPtr->dataSize; | |||||
| } | |||||
| list_del(node_ptr); | |||||
| while (count > poolPtr->maxPreallocated && ! list_empty(&poolPtr->pending)) | |||||
| { | |||||
| nodePtr = poolPtr->pending.next; | |||||
| free(node_ptr); | |||||
| } | |||||
| list_del(nodePtr); | |||||
| ret = pthread_mutex_destroy(&pool_ptr->mutex); | |||||
| assert(ret == 0); | |||||
| } | |||||
| free(nodePtr); | |||||
| free(pool_ptr); | |||||
| count--; | |||||
| // unused variable | |||||
| (void)ret; | |||||
| } | |||||
| poolPtr->usedSize -= poolPtr->dataSize; | |||||
| } | |||||
| /* adjust unused list size */ | |||||
| static | |||||
| void | |||||
| rtsafe_memory_pool_sleepy( | |||||
| LV2_RtMemPool_Handle pool_handle) | |||||
| { | |||||
| struct list_head * node_ptr; | |||||
| unsigned int count; | |||||
| pthread_mutex_unlock(&poolPtr->mutex); | |||||
| } | |||||
| else | |||||
| { | |||||
| while (poolPtr->unusedCount < poolPtr->minPreallocated) | |||||
| { | |||||
| nodePtr = malloc(sizeof(k_list_head) + poolPtr->dataSize); | |||||
| LOG_DEBUG("pool \"%s\", sleepy", pool_ptr->name); | |||||
| if (nodePtr == NULL) | |||||
| { | |||||
| return; | |||||
| } | |||||
| if (pool_ptr->enforce_thread_safety) | |||||
| { | |||||
| pthread_mutex_lock(&pool_ptr->mutex); | |||||
| list_add_tail(nodePtr, &poolPtr->unused); | |||||
| poolPtr->unusedCount++; | |||||
| poolPtr->usedSize += poolPtr->dataSize; | |||||
| } | |||||
| count = pool_ptr->unused_count2; | |||||
| while (poolPtr->unusedCount > poolPtr->maxPreallocated) | |||||
| { | |||||
| assert(! list_empty(&poolPtr->unused)); | |||||
| assert(pool_ptr->min_preallocated < pool_ptr->max_preallocated); | |||||
| nodePtr = poolPtr->unused.next; | |||||
| while (count < pool_ptr->min_preallocated) | |||||
| { | |||||
| node_ptr = malloc(sizeof(struct list_head) + pool_ptr->data_size); | |||||
| if (node_ptr == NULL) | |||||
| { | |||||
| LOG_DEBUG("malloc() failed (%u)", (unsigned int)pool_ptr->used_size); | |||||
| break; | |||||
| } | |||||
| list_del(nodePtr); | |||||
| poolPtr->unusedCount--; | |||||
| free(nodePtr); | |||||
| poolPtr->usedSize -= poolPtr->dataSize; | |||||
| } | |||||
| } | |||||
| } | |||||
| list_add_tail(node_ptr, &pool_ptr->pending); | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| count++; | |||||
| bool rtsafe_memory_pool_create2(RtMemPool_Handle* handlePtr, | |||||
| const char* poolName, | |||||
| size_t dataSize, | |||||
| size_t minPreallocated, | |||||
| size_t maxPreallocated, | |||||
| bool enforceThreadSafety) | |||||
| { | |||||
| assert(minPreallocated <= maxPreallocated); | |||||
| assert(poolName == NULL || strlen(poolName) < RTSAFE_MEMORY_POOL_NAME_MAX); | |||||
| RtMemPool* poolPtr; | |||||
| poolPtr = malloc(sizeof(RtMemPool)); | |||||
| pool_ptr->used_size += pool_ptr->data_size; | |||||
| if (poolPtr == NULL) | |||||
| { | |||||
| return false; | |||||
| } | } | ||||
| while (count > pool_ptr->max_preallocated && !list_empty(&pool_ptr->pending)) | |||||
| if (poolName != NULL) | |||||
| { | |||||
| strcpy(poolPtr->name, poolName); | |||||
| } | |||||
| else | |||||
| { | { | ||||
| node_ptr = pool_ptr->pending.next; | |||||
| sprintf(poolPtr->name, "%p", poolPtr); | |||||
| } | |||||
| list_del(node_ptr); | |||||
| poolPtr->dataSize = dataSize; | |||||
| poolPtr->minPreallocated = minPreallocated; | |||||
| poolPtr->maxPreallocated = maxPreallocated; | |||||
| free(node_ptr); | |||||
| INIT_LIST_HEAD(&poolPtr->used); | |||||
| poolPtr->usedCount = 0; | |||||
| count--; | |||||
| INIT_LIST_HEAD(&poolPtr->unused); | |||||
| poolPtr->unusedCount = 0; | |||||
| pool_ptr->used_size -= pool_ptr->data_size; | |||||
| } | |||||
| poolPtr->enforceThreadSafety = enforceThreadSafety; | |||||
| pthread_mutex_unlock(&pool_ptr->mutex); | |||||
| } | |||||
| else | |||||
| { | |||||
| while (pool_ptr->unused_count < pool_ptr->min_preallocated) | |||||
| if (enforceThreadSafety) | |||||
| { | { | ||||
| node_ptr = malloc(sizeof(struct list_head) + pool_ptr->data_size); | |||||
| if (node_ptr == NULL) | |||||
| { | |||||
| LOG_DEBUG("malloc() failed (%u)", (unsigned int)pool_ptr->used_size); | |||||
| return; | |||||
| } | |||||
| list_add_tail(node_ptr, &pool_ptr->unused); | |||||
| pool_ptr->unused_count++; | |||||
| pool_ptr->used_size += pool_ptr->data_size; | |||||
| if (pthread_mutex_init(&poolPtr->mutex, NULL) != 0) | |||||
| { | |||||
| free(poolPtr); | |||||
| return false; | |||||
| } | |||||
| INIT_LIST_HEAD(&poolPtr->pending); | |||||
| } | } | ||||
| while (pool_ptr->unused_count > pool_ptr->max_preallocated) | |||||
| { | |||||
| assert(!list_empty(&pool_ptr->unused)); | |||||
| poolPtr->unusedCount2 = 0; | |||||
| poolPtr->usedSize = 0; | |||||
| node_ptr = pool_ptr->unused.next; | |||||
| rtsafe_memory_pool_sleepy(poolPtr); | |||||
| *handlePtr = (RtMemPool_Handle)poolPtr; | |||||
| list_del(node_ptr); | |||||
| pool_ptr->unused_count--; | |||||
| return true; | |||||
| } | |||||
| free(node_ptr); | |||||
| pool_ptr->used_size -= pool_ptr->data_size; | |||||
| } | |||||
| } | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| bool rtsafe_memory_pool_create(RtMemPool_Handle* handlePtr, | |||||
| const char* poolName, | |||||
| size_t dataSize, | |||||
| size_t minPreallocated, | |||||
| size_t maxPreallocated) | |||||
| { | |||||
| return rtsafe_memory_pool_create2(handlePtr, poolName, dataSize, minPreallocated, maxPreallocated, false); | |||||
| } | } | ||||
| /* find entry in unused list, fail if it is empty */ | |||||
| static | |||||
| void * | |||||
| rtsafe_memory_pool_allocate_atomic( | |||||
| LV2_RtMemPool_Handle pool_handle) | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| bool rtsafe_memory_pool_create_safe(RtMemPool_Handle* handlePtr, | |||||
| const char* poolName, | |||||
| size_t dataSize, | |||||
| size_t minPreallocated, | |||||
| size_t maxPreallocated) | |||||
| { | { | ||||
| struct list_head * node_ptr; | |||||
| return rtsafe_memory_pool_create2(handlePtr, poolName, dataSize, minPreallocated, maxPreallocated, true); | |||||
| } | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| LOG_DEBUG("pool \"%s\", allocate (%u, %u)", pool_ptr->name, pool_ptr->used_count, pool_ptr->unused_count); | |||||
| void rtsafe_memory_pool_destroy(RtMemPool_Handle handle) | |||||
| { | |||||
| assert(handle); | |||||
| if (list_empty(&pool_ptr->unused)) | |||||
| { | |||||
| return NULL; | |||||
| } | |||||
| k_list_head* nodePtr; | |||||
| RtMemPool* poolPtr = (RtMemPool*)handle; | |||||
| node_ptr = pool_ptr->unused.next; | |||||
| list_del(node_ptr); | |||||
| pool_ptr->unused_count--; | |||||
| pool_ptr->used_count++; | |||||
| list_add_tail(node_ptr, &pool_ptr->used); | |||||
| // caller should deallocate all chunks prior releasing pool itself | |||||
| if (poolPtr->usedCount != 0) | |||||
| { | |||||
| assert(0); | |||||
| } | |||||
| if (pool_ptr->enforce_thread_safety && | |||||
| pthread_mutex_trylock(&pool_ptr->mutex) == 0) | |||||
| { | |||||
| while (pool_ptr->unused_count < pool_ptr->min_preallocated && !list_empty(&pool_ptr->pending)) | |||||
| while (poolPtr->unusedCount != 0) | |||||
| { | { | ||||
| node_ptr = pool_ptr->pending.next; | |||||
| assert(! list_empty(&poolPtr->unused)); | |||||
| list_del(node_ptr); | |||||
| list_add_tail(node_ptr, &pool_ptr->unused); | |||||
| pool_ptr->unused_count++; | |||||
| nodePtr = poolPtr->unused.next; | |||||
| list_del(nodePtr); | |||||
| poolPtr->unusedCount--; | |||||
| free(nodePtr); | |||||
| } | } | ||||
| pool_ptr->unused_count2 = pool_ptr->unused_count; | |||||
| assert(list_empty(&poolPtr->unused)); | |||||
| if (poolPtr->enforceThreadSafety) | |||||
| { | |||||
| while (! list_empty(&poolPtr->pending)) | |||||
| { | |||||
| nodePtr = poolPtr->pending.next; | |||||
| list_del(nodePtr); | |||||
| pthread_mutex_unlock(&pool_ptr->mutex); | |||||
| } | |||||
| free(nodePtr); | |||||
| } | |||||
| LOG_DEBUG("pool \"%s\", allocated %p (%u)", pool_ptr->name, node_ptr + 1, pool_ptr->used_count); | |||||
| return (node_ptr + 1); | |||||
| int ret = pthread_mutex_destroy(&poolPtr->mutex); | |||||
| #ifdef DEBUG | |||||
| assert(ret == 0); | |||||
| #else | |||||
| // unused | |||||
| (void)ret; | |||||
| #endif | |||||
| } | |||||
| free(poolPtr); | |||||
| } | } | ||||
| /* move from used to unused list */ | |||||
| static | |||||
| void | |||||
| rtsafe_memory_pool_deallocate( | |||||
| LV2_RtMemPool_Handle pool_handle, | |||||
| void * data) | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| // find entry in unused list, fail if it is empty | |||||
| void* rtsafe_memory_pool_allocate_atomic(RtMemPool_Handle handle) | |||||
| { | { | ||||
| struct list_head * node_ptr; | |||||
| assert(handle); | |||||
| LOG_DEBUG("pool \"%s\", deallocate %p (%u)", pool_ptr->name, (struct list_head *)data - 1, pool_ptr->used_count); | |||||
| k_list_head* nodePtr; | |||||
| RtMemPool* poolPtr = (RtMemPool*)handle; | |||||
| list_del((struct list_head *)data - 1); | |||||
| list_add_tail((struct list_head *)data - 1, &pool_ptr->unused); | |||||
| pool_ptr->used_count--; | |||||
| pool_ptr->unused_count++; | |||||
| if (list_empty(&poolPtr->unused)) | |||||
| { | |||||
| return NULL; | |||||
| } | |||||
| nodePtr = poolPtr->unused.next; | |||||
| list_del(nodePtr); | |||||
| poolPtr->unusedCount--; | |||||
| poolPtr->usedCount++; | |||||
| if (pool_ptr->enforce_thread_safety && | |||||
| pthread_mutex_trylock(&pool_ptr->mutex) == 0) | |||||
| { | |||||
| while (pool_ptr->unused_count > pool_ptr->max_preallocated) | |||||
| list_add_tail(nodePtr, &poolPtr->used); | |||||
| if (poolPtr->enforceThreadSafety && pthread_mutex_trylock(&poolPtr->mutex) == 0) | |||||
| { | { | ||||
| assert(!list_empty(&pool_ptr->unused)); | |||||
| while (poolPtr->unusedCount < poolPtr->minPreallocated && ! list_empty(&poolPtr->pending)) | |||||
| { | |||||
| nodePtr = poolPtr->pending.next; | |||||
| node_ptr = pool_ptr->unused.next; | |||||
| list_del(nodePtr); | |||||
| list_add_tail(nodePtr, &poolPtr->unused); | |||||
| list_del(node_ptr); | |||||
| list_add_tail(node_ptr, &pool_ptr->pending); | |||||
| pool_ptr->unused_count--; | |||||
| } | |||||
| poolPtr->unusedCount++; | |||||
| } | |||||
| pool_ptr->unused_count2 = pool_ptr->unused_count; | |||||
| poolPtr->unusedCount2 = poolPtr->unusedCount; | |||||
| pthread_mutex_unlock(&pool_ptr->mutex); | |||||
| } | |||||
| pthread_mutex_unlock(&poolPtr->mutex); | |||||
| } | |||||
| return (nodePtr + 1); | |||||
| } | } | ||||
| static | |||||
| void * | |||||
| rtsafe_memory_pool_allocate_sleepy( | |||||
| LV2_RtMemPool_Handle pool_handle) | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| void* rtsafe_memory_pool_allocate_sleepy(RtMemPool_Handle handle) | |||||
| { | { | ||||
| void * data; | |||||
| assert(handle); | |||||
| LOG_DEBUG("pool \"%s\", allocate sleepy", pool_ptr->name); | |||||
| void* data; | |||||
| RtMemPool* poolPtr = (RtMemPool*)handle; | |||||
| do | |||||
| { | |||||
| rtsafe_memory_pool_sleepy(pool_handle); | |||||
| data = rtsafe_memory_pool_allocate_atomic(pool_handle); | |||||
| } | |||||
| while (data == NULL); | |||||
| do { | |||||
| rtsafe_memory_pool_sleepy(poolPtr); | |||||
| data = rtsafe_memory_pool_allocate_atomic((RtMemPool_Handle)poolPtr); | |||||
| } | |||||
| while (data == NULL); | |||||
| return data; | |||||
| return data; | |||||
| } | } | ||||
| #undef pool_ptr | |||||
| // ------------------------------------------------------------------------------------------------ | |||||
| // move from used to unused list | |||||
| static | |||||
| bool | |||||
| rtsafe_memory_pool_create2( | |||||
| LV2_RtMemPool_Handle * pool_handle_ptr, | |||||
| const char * pool_name, | |||||
| size_t data_size, | |||||
| size_t min_preallocated, | |||||
| size_t max_preallocated) | |||||
| void rtsafe_memory_pool_deallocate(RtMemPool_Handle handle, void* memoryPtr) | |||||
| { | { | ||||
| return rtsafe_memory_pool_create(pool_handle_ptr, pool_name, data_size, min_preallocated, max_preallocated, false); | |||||
| assert(handle); | |||||
| k_list_head* nodePtr; | |||||
| RtMemPool* poolPtr = (RtMemPool*)handle; | |||||
| list_del((k_list_head*)memoryPtr - 1); | |||||
| list_add_tail((k_list_head*)memoryPtr - 1, &poolPtr->unused); | |||||
| poolPtr->usedCount--; | |||||
| poolPtr->unusedCount++; | |||||
| if (poolPtr->enforceThreadSafety && pthread_mutex_trylock(&poolPtr->mutex) == 0) | |||||
| { | |||||
| while (poolPtr->unusedCount > poolPtr->maxPreallocated) | |||||
| { | |||||
| assert(! list_empty(&poolPtr->unused)); | |||||
| nodePtr = poolPtr->unused.next; | |||||
| list_del(nodePtr); | |||||
| list_add_tail(nodePtr, &poolPtr->pending); | |||||
| poolPtr->unusedCount--; | |||||
| } | |||||
| poolPtr->unusedCount2 = poolPtr->unusedCount; | |||||
| pthread_mutex_unlock(&poolPtr->mutex); | |||||
| } | |||||
| } | } | ||||
| void | |||||
| rtmempool_allocator_init( | |||||
| struct _LV2_RtMemPool_Pool * allocator_ptr) | |||||
| #ifdef WANT_LV2 | |||||
| #include "lv2/lv2_rtmempool.h" | |||||
| void lv2_rtmempool_init(LV2_RtMemPool_Pool* poolPtr) | |||||
| { | { | ||||
| allocator_ptr->create = rtsafe_memory_pool_create2; | |||||
| allocator_ptr->destroy = rtsafe_memory_pool_destroy; | |||||
| allocator_ptr->allocate_atomic = rtsafe_memory_pool_allocate_atomic; | |||||
| allocator_ptr->allocate_sleepy = rtsafe_memory_pool_allocate_sleepy; | |||||
| allocator_ptr->deallocate = rtsafe_memory_pool_deallocate; | |||||
| poolPtr->create = rtsafe_memory_pool_create; | |||||
| poolPtr->destroy = rtsafe_memory_pool_destroy; | |||||
| poolPtr->allocate_atomic = rtsafe_memory_pool_allocate_atomic; | |||||
| poolPtr->allocate_sleepy = rtsafe_memory_pool_allocate_sleepy; | |||||
| poolPtr->deallocate = rtsafe_memory_pool_deallocate; | |||||
| } | } | ||||
| #endif | |||||
| @@ -1,41 +1,108 @@ | |||||
| /* -*- Mode: C ; c-basic-offset: 2 -*- */ | |||||
| /***************************************************************************** | |||||
| /* | |||||
| * RealTime Memory Pool, heavily based on work by Nedko Arnaudov | |||||
| * Copyright (C) 2006-2009 Nedko Arnaudov <nedko@arnaudov.name> | |||||
| * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com> | |||||
| * | * | ||||
| * This file is part of zynjacku | |||||
| * 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 | |||||
| * any later version. | |||||
| * | * | ||||
| * Copyright (C) 2006,2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name> | |||||
| * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com> | |||||
| * 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. | |||||
| * | * | ||||
| * 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; version 2 of the License | |||||
| * For a full copy of the GNU General Public License see the COPYING file | |||||
| */ | |||||
| #ifndef __RTMEMPOOL_H__ | |||||
| #define __RTMEMPOOL_H__ | |||||
| #ifdef __cplusplus | |||||
| # include <cstddef> | |||||
| #else | |||||
| # include <stdbool.h> | |||||
| # include <stddef.h> | |||||
| #endif | |||||
| /** max size of memory pool name, in chars, including terminating zero char */ | |||||
| #define RTSAFE_MEMORY_POOL_NAME_MAX 128 | |||||
| /** | |||||
| * Opaque data for RtMemPool_Pool. | |||||
| */ | |||||
| typedef void* RtMemPool_Handle; | |||||
| /** | |||||
| * Create new memory pool | |||||
| * | * | ||||
| * 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. | |||||
| * <b>may/will sleep</b> | |||||
| * | * | ||||
| * You should have received a copy of the GNU General Public License | |||||
| * along with this program; if not, write to the Free Software | |||||
| * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |||||
| * @param poolName pool name, for debug purposes, max RTSAFE_MEMORY_POOL_NAME_MAX chars, including terminating zero char. May be NULL. | |||||
| * @param dataSize memory chunk size | |||||
| * @param minPreallocated min chunks preallocated | |||||
| * @param maxPreallocated max chunks preallocated | |||||
| * | * | ||||
| *****************************************************************************/ | |||||
| * @return Success status, true if successful | |||||
| */ | |||||
| bool rtsafe_memory_pool_create(RtMemPool_Handle* handlePtr, | |||||
| const char* poolName, | |||||
| size_t dataSize, | |||||
| size_t minPreallocated, | |||||
| size_t maxPreallocated); | |||||
| #ifndef RTMEMPOOL_H__1FA54215_11CF_4659_9CF3_C17A10A67A1F__INCLUDED | |||||
| #define RTMEMPOOL_H__1FA54215_11CF_4659_9CF3_C17A10A67A1F__INCLUDED | |||||
| /** | |||||
| * Create new memory pool, thread-safe version | |||||
| * | |||||
| * <b>may/will sleep</b> | |||||
| * | |||||
| * @param poolName pool name, for debug purposes, max RTSAFE_MEMORY_POOL_NAME_MAX chars, including terminating zero char. May be NULL. | |||||
| * @param dataSize memory chunk size | |||||
| * @param minPreallocated min chunks preallocated | |||||
| * @param maxPreallocated max chunks preallocated | |||||
| * | |||||
| * @return Success status, true if successful | |||||
| */ | |||||
| bool rtsafe_memory_pool_create_safe(RtMemPool_Handle* handlePtr, | |||||
| const char* poolName, | |||||
| size_t dataSize, | |||||
| size_t minPreallocated, | |||||
| size_t maxPreallocated); | |||||
| #include "lv2/lv2_rtmempool.h" | |||||
| /** | |||||
| * Destroy previously created memory pool | |||||
| * | |||||
| * <b>may/will sleep</b> | |||||
| */ | |||||
| void rtsafe_memory_pool_destroy(RtMemPool_Handle handle); | |||||
| #ifdef __cplusplus | |||||
| extern "C" { | |||||
| #endif | |||||
| /** | |||||
| * Allocate memory in context where sleeping is not allowed | |||||
| * | |||||
| * <b>will not sleep</b> | |||||
| * | |||||
| * @return Pointer to allocated memory or NULL if memory no memory is available | |||||
| */ | |||||
| void* rtsafe_memory_pool_allocate_atomic(RtMemPool_Handle handle); | |||||
| void | |||||
| rtmempool_allocator_init( | |||||
| struct _LV2_RtMemPool_Pool * allocator_ptr); | |||||
| /** | |||||
| * Allocate memory in context where sleeping is allowed | |||||
| * | |||||
| * <b>may/will sleep</b> | |||||
| * | |||||
| * @return Pointer to allocated memory or NULL if memory no memory is available (should not happen under normal conditions) | |||||
| */ | |||||
| void* rtsafe_memory_pool_allocate_sleepy(RtMemPool_Handle handle); | |||||
| #ifdef __cplusplus | |||||
| } /* extern "C" */ | |||||
| #endif | |||||
| /** | |||||
| * Deallocate previously allocated memory | |||||
| * | |||||
| * <b>will not sleep</b> | |||||
| * | |||||
| * @param memoryPtr pointer to previously allocated memory chunk | |||||
| */ | |||||
| void rtsafe_memory_pool_deallocate(RtMemPool_Handle handle, | |||||
| void* memoryPtr); | |||||
| #endif /* #ifndef RTMEMPOOL_H__1FA54215_11CF_4659_9CF3_C17A10A67A1F__INCLUDED */ | |||||
| #endif // __RTMEMPOOL_H__ | |||||
| @@ -23,6 +23,7 @@ | |||||
| #include <cstdio> | #include <cstdio> | ||||
| #include <cstdlib> | #include <cstdlib> | ||||
| #include <cstring> | #include <cstring> | ||||
| #include <pthread.h> | |||||
| #if defined(Q_OS_HAIKU) | #if defined(Q_OS_HAIKU) | ||||
| # include <kernel/OS.h> | # include <kernel/OS.h> | ||||
| @@ -164,6 +165,60 @@ const char* bool2str(const bool yesNo) | |||||
| static inline | static inline | ||||
| void pass() {} | void pass() {} | ||||
| // ------------------------------------------------- | |||||
| // CarlaMutex class | |||||
| class CarlaMutex | |||||
| { | |||||
| public: | |||||
| CarlaMutex() | |||||
| : pmutex(PTHREAD_MUTEX_INITIALIZER) | |||||
| { | |||||
| pthread_mutex_init(&pmutex, nullptr); | |||||
| } | |||||
| ~CarlaMutex() | |||||
| { | |||||
| pthread_mutex_destroy(&pmutex); | |||||
| } | |||||
| bool lock() | |||||
| { | |||||
| return (pthread_mutex_lock(&pmutex) == 0); | |||||
| } | |||||
| bool tryLock() | |||||
| { | |||||
| return (pthread_mutex_trylock(&pmutex) == 0); | |||||
| } | |||||
| bool unlock() | |||||
| { | |||||
| return (pthread_mutex_unlock(&pmutex) == 0); | |||||
| } | |||||
| class ScopedLocker | |||||
| { | |||||
| public: | |||||
| ScopedLocker(CarlaMutex* const mutex_) | |||||
| : mutex(mutex_) | |||||
| { | |||||
| mutex->lock(); | |||||
| } | |||||
| ~ScopedLocker() | |||||
| { | |||||
| mutex->unlock(); | |||||
| } | |||||
| private: | |||||
| CarlaMutex* const mutex; | |||||
| }; | |||||
| private: | |||||
| pthread_mutex_t pmutex; | |||||
| }; | |||||
| // ------------------------------------------------- | // ------------------------------------------------- | ||||
| // CarlaString class | // CarlaString class | ||||
| @@ -0,0 +1,192 @@ | |||||
| /* | |||||
| * Simple Queue, specially developed for Atom types | |||||
| * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com> | |||||
| * | |||||
| * 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 | |||||
| * 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. | |||||
| * | |||||
| * For a full copy of the GNU General Public License see the COPYING file | |||||
| */ | |||||
| #ifndef __LV2_ATOM_QUEUE_HPP__ | |||||
| #define __LV2_ATOM_QUEUE_HPP__ | |||||
| #include "carla_utils.hpp" | |||||
| #include "lv2/atom.h" | |||||
| #include <cstring> // memcpy, memset | |||||
| #include <pthread.h> | |||||
| class Lv2AtomQueue | |||||
| { | |||||
| public: | |||||
| Lv2AtomQueue() | |||||
| : mutex(PTHREAD_MUTEX_INITIALIZER) | |||||
| { | |||||
| index = indexPool = 0; | |||||
| empty = true; | |||||
| full = false; | |||||
| ::memset(dataPool, 0, sizeof(unsigned char)*MAX_POOL_SIZE); | |||||
| } | |||||
| void copyDataFrom(Lv2AtomQueue* const queue) | |||||
| { | |||||
| // lock mutexes | |||||
| queue->lock(); | |||||
| lock(); | |||||
| // copy data from queue | |||||
| ::memcpy(data, queue->data, sizeof(datatype)*MAX_SIZE); | |||||
| ::memcpy(dataPool, queue->dataPool, sizeof(unsigned char)*MAX_POOL_SIZE); | |||||
| index = queue->index; | |||||
| indexPool = queue->indexPool; | |||||
| empty = queue->empty; | |||||
| full = queue->full; | |||||
| // unlock our mutex, no longer needed | |||||
| unlock(); | |||||
| // reset queque | |||||
| ::memset(queue->data, 0, sizeof(datatype)*MAX_SIZE); | |||||
| ::memset(queue->dataPool, 0, sizeof(unsigned char)*MAX_POOL_SIZE); | |||||
| queue->index = queue->indexPool = 0; | |||||
| queue->empty = true; | |||||
| queue->full = false; | |||||
| // unlock queque mutex | |||||
| queue->unlock(); | |||||
| } | |||||
| bool isEmpty() | |||||
| { | |||||
| return empty; | |||||
| } | |||||
| bool isFull() | |||||
| { | |||||
| return full; | |||||
| } | |||||
| bool lock() | |||||
| { | |||||
| return (pthread_mutex_lock(&mutex) == 0); | |||||
| } | |||||
| bool tryLock() | |||||
| { | |||||
| return (pthread_mutex_trylock(&mutex) == 0); | |||||
| } | |||||
| bool unlock() | |||||
| { | |||||
| return (pthread_mutex_unlock(&mutex) == 0); | |||||
| } | |||||
| void put(const uint32_t portIndex, const LV2_Atom* const atom) | |||||
| { | |||||
| CARLA_ASSERT(atom && atom->size > 0); | |||||
| CARLA_ASSERT(indexPool + atom->size < MAX_POOL_SIZE); // overflow | |||||
| if (full || atom->size == 0 || indexPool + atom->size >= MAX_POOL_SIZE) | |||||
| return; | |||||
| lock(); | |||||
| for (unsigned short i=0; i < MAX_SIZE; i++) | |||||
| { | |||||
| if (data[i].size == 0) | |||||
| { | |||||
| data[i].portIndex = portIndex; | |||||
| data[i].size = atom->size; | |||||
| data[i].type = atom->type; | |||||
| data[i].poolOffset = indexPool; | |||||
| ::memcpy(dataPool + indexPool, (const unsigned char*)LV2_ATOM_BODY_CONST(atom), atom->size); | |||||
| empty = false; | |||||
| full = (i == MAX_SIZE-1); | |||||
| indexPool += atom->size; | |||||
| break; | |||||
| } | |||||
| } | |||||
| unlock(); | |||||
| } | |||||
| bool get(uint32_t* const portIndex, const LV2_Atom** const atom) | |||||
| { | |||||
| CARLA_ASSERT(portIndex && atom); | |||||
| if (empty || ! (portIndex && atom)) | |||||
| return false; | |||||
| if (! tryLock()) | |||||
| return false; | |||||
| full = false; | |||||
| if (data[index].size == 0) | |||||
| { | |||||
| index = indexPool = 0; | |||||
| empty = true; | |||||
| unlock(); | |||||
| return false; | |||||
| } | |||||
| retAtom.atom.size = data[index].size; | |||||
| retAtom.atom.type = data[index].type; | |||||
| ::memcpy(retAtom.data, dataPool + data[index].poolOffset, data[index].size); | |||||
| *portIndex = data[index].portIndex; | |||||
| *atom = (LV2_Atom*)&retAtom; | |||||
| data[index].portIndex = 0; | |||||
| data[index].size = 0; | |||||
| data[index].type = 0; | |||||
| data[index].poolOffset = 0; | |||||
| index++; | |||||
| empty = false; | |||||
| unlock(); | |||||
| return true; | |||||
| } | |||||
| private: | |||||
| struct datatype { | |||||
| size_t size; | |||||
| uint32_t type; | |||||
| uint32_t portIndex; | |||||
| uint32_t poolOffset; | |||||
| datatype() | |||||
| : size(0), | |||||
| type(0), | |||||
| portIndex(0), | |||||
| poolOffset(0) {} | |||||
| }; | |||||
| static const unsigned short MAX_SIZE = 128; | |||||
| static const unsigned short MAX_POOL_SIZE = 8192; | |||||
| datatype data[MAX_SIZE]; | |||||
| unsigned char dataPool[MAX_POOL_SIZE]; | |||||
| struct { | |||||
| LV2_Atom atom; | |||||
| unsigned char data[MAX_POOL_SIZE]; | |||||
| } retAtom; | |||||
| unsigned short index, indexPool; | |||||
| bool empty, full; | |||||
| pthread_mutex_t mutex; | |||||
| }; | |||||
| #endif // __LV2_ATOM_QUEUE_HPP__ | |||||
| @@ -0,0 +1,214 @@ | |||||
| /* | |||||
| * High-level, real-time safe, templated C++ doubly linked list | |||||
| * Copyright (C) 2013 Filipe Coelho <falktx@falktx.com> | |||||
| * | |||||
| * 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 | |||||
| * 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. | |||||
| * | |||||
| * For a full copy of the GNU General Public License see the COPYING file | |||||
| */ | |||||
| #ifndef __RT_LIST_HPP__ | |||||
| #define __RT_LIST_HPP__ | |||||
| extern "C" { | |||||
| #include "rtmempool/list.h" | |||||
| #include "rtmempool/rtmempool.h" | |||||
| } | |||||
| #include <cassert> | |||||
| typedef struct list_head k_list_head; | |||||
| template<typename T> | |||||
| class RtList | |||||
| { | |||||
| public: | |||||
| RtList(const size_t minPreallocated, const size_t maxPreallocated) | |||||
| { | |||||
| qcount = 0; | |||||
| INIT_LIST_HEAD(&queue); | |||||
| rtsafe_memory_pool_create(&mempool, nullptr, sizeof(ListHeadData), minPreallocated, maxPreallocated); | |||||
| assert(mempool); | |||||
| } | |||||
| ~RtList() | |||||
| { | |||||
| clear(); | |||||
| rtsafe_memory_pool_destroy(mempool); | |||||
| } | |||||
| void resize(const size_t minPreallocated, const size_t maxPreallocated) | |||||
| { | |||||
| clear(); | |||||
| rtsafe_memory_pool_destroy(mempool); | |||||
| rtsafe_memory_pool_create(&mempool, nullptr, sizeof(ListHeadData), minPreallocated, maxPreallocated); | |||||
| assert(mempool); | |||||
| } | |||||
| void clear() | |||||
| { | |||||
| if (! isEmpty()) | |||||
| { | |||||
| ListHeadData* data; | |||||
| k_list_head* entry; | |||||
| list_for_each(entry, &queue) | |||||
| { | |||||
| data = list_entry(entry, ListHeadData, siblings); | |||||
| rtsafe_memory_pool_deallocate(mempool, data); | |||||
| } | |||||
| } | |||||
| qcount = 0; | |||||
| INIT_LIST_HEAD(&queue); | |||||
| } | |||||
| size_t count() const | |||||
| { | |||||
| return qcount; | |||||
| } | |||||
| bool isEmpty() const | |||||
| { | |||||
| return (list_empty(&queue) != 0); | |||||
| } | |||||
| void append(const T& value, const bool sleepy = false) | |||||
| { | |||||
| ListHeadData* data; | |||||
| if (sleepy) | |||||
| data = (ListHeadData*)rtsafe_memory_pool_allocate_sleepy(mempool); | |||||
| else | |||||
| data = (ListHeadData*)rtsafe_memory_pool_allocate_atomic(mempool); | |||||
| if (data) | |||||
| { | |||||
| memcpy(&data->value, &value, sizeof(T)); | |||||
| list_add_tail(&data->siblings, &queue); | |||||
| qcount++; | |||||
| } | |||||
| } | |||||
| T& getFirst() | |||||
| { | |||||
| return __get(true, false); | |||||
| } | |||||
| T& getFirstAndRemove() | |||||
| { | |||||
| return __get(true, true); | |||||
| } | |||||
| T& getLast() | |||||
| { | |||||
| return __get(false, false); | |||||
| } | |||||
| T& getLastAndRemove() | |||||
| { | |||||
| return __get(false, true); | |||||
| } | |||||
| bool removeOne(const T& value) | |||||
| { | |||||
| ListHeadData* data; | |||||
| k_list_head* entry; | |||||
| list_for_each(entry, &queue) | |||||
| { | |||||
| data = list_entry(entry, ListHeadData, siblings); | |||||
| if (data->value == value) | |||||
| { | |||||
| qcount--; | |||||
| list_del(entry); | |||||
| rtsafe_memory_pool_deallocate(mempool, data); | |||||
| return true; | |||||
| } | |||||
| } | |||||
| return false; | |||||
| } | |||||
| void removeAll(const T& value) | |||||
| { | |||||
| ListHeadData* data; | |||||
| k_list_head* entry; | |||||
| k_list_head* tmp; | |||||
| list_for_each_safe(entry, tmp, &queue) | |||||
| { | |||||
| data = list_entry(entry, ListHeadData, siblings); | |||||
| if (data->value == value) | |||||
| { | |||||
| qcount--; | |||||
| list_del(entry); | |||||
| rtsafe_memory_pool_deallocate(mempool, data); | |||||
| } | |||||
| } | |||||
| } | |||||
| private: | |||||
| size_t qcount; | |||||
| k_list_head queue; | |||||
| RtMemPool_Handle mempool; | |||||
| struct ListHeadData { | |||||
| T value; | |||||
| k_list_head siblings; | |||||
| }; | |||||
| T& __get(const bool first, const bool doDelete) | |||||
| { | |||||
| if (isEmpty()) | |||||
| { | |||||
| // FIXME ? | |||||
| static T value; | |||||
| static bool reset = true; | |||||
| if (reset) | |||||
| { | |||||
| reset = false; | |||||
| memset(&value, 0, sizeof(T)); | |||||
| } | |||||
| return value; | |||||
| } | |||||
| k_list_head* entry = first ? queue.next : queue.prev; | |||||
| ListHeadData* data = list_entry(entry, ListHeadData, siblings); | |||||
| T& ret = data->value; | |||||
| if (data && doDelete) | |||||
| { | |||||
| qcount--; | |||||
| list_del(entry); | |||||
| rtsafe_memory_pool_deallocate(mempool, data); | |||||
| } | |||||
| return ret; | |||||
| } | |||||
| // Non-copyable | |||||
| RtList(const RtList&); | |||||
| RtList& operator= (const RtList&); | |||||
| }; | |||||
| #endif // __RT_LIST_HPP__ | |||||