Mac VST-2.x support
[ardour.git] / libs / ardour / buffer_set.cc
index 92f5272b3b2bee945c8723eb0b2188ff58518e9b..982725f491e6de9f4bcf5fb0202b3c2b7a641e70 100644 (file)
@@ -39,7 +39,7 @@
 #include "ardour/lv2_plugin.h"
 #include "lv2_evbuf.h"
 #endif
-#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
+#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
 #include "ardour/vestige/aeffectx.h"
 #endif
 
@@ -79,7 +79,7 @@ BufferSet::clear()
        _count.reset();
        _available.reset();
 
-#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
+#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
        for (VSTBuffers::iterator i = _vst_buffers.begin(); i != _vst_buffers.end(); ++i) {
                delete *i;
        }
@@ -206,7 +206,7 @@ BufferSet::ensure_buffers(DataType type, size_t num_buffers, size_t buffer_capac
        }
 #endif
 
-#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
+#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
        // As above but for VST
        if (type == DataType::MIDI) {
                while (_vst_buffers.size() < _buffers[type].size()) {
@@ -343,12 +343,13 @@ BufferSet::flush_lv2_midi(bool input, size_t i)
 
 #endif /* LV2_SUPPORT */
 
-#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT
+#if defined WINDOWS_VST_SUPPORT || defined LXVST_SUPPORT || defined MACVST_SUPPORT
 
 VstEvents*
 BufferSet::get_vst_midi (size_t b)
 {
        MidiBuffer& m = get_midi (b);
+       assert (b <= _vst_buffers.size());
        VSTBuffer* vst = _vst_buffers[b];
 
        vst->clear ();
@@ -362,13 +363,24 @@ BufferSet::get_vst_midi (size_t b)
 
 BufferSet::VSTBuffer::VSTBuffer (size_t c)
   : _capacity (c)
+  , _events (0)
+  , _midi_events (0)
 {
-       _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
-       _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
+       if (_capacity > 0) {
+               /* from `man malloc`: "If size is 0, then malloc() returns either NULL, or a
+                * unique pointer value that can later be successfully passed to free()."
+                *
+                * The latter will cause trouble here.
+                */
+               _events = static_cast<VstEvents*> (malloc (sizeof (VstEvents) + _capacity * sizeof (VstEvent *)));
+               _midi_events = static_cast<VstMidiEvent*> (malloc (sizeof (VstMidiEvent) * _capacity));
+       }
 
        if (_events == 0 || _midi_events == 0) {
                free (_events);
                free (_midi_events);
+               _events = 0;
+               _midi_events = 0;
                throw failed_constructor ();
        }
 
@@ -399,6 +411,9 @@ BufferSet::VSTBuffer::push_back (Evoral::MIDIEvent<framepos_t> const & ev)
        }
        int const n = _events->numEvents;
        assert (n < (int) _capacity);
+       if (n >= _capacity) {
+               return;
+       }
 
        _events->events[n] = reinterpret_cast<VstEvent*> (_midi_events + n);
        VstMidiEvent* v = reinterpret_cast<VstMidiEvent*> (_events->events[n]);