/* This file is part of Ingen. Copyright 2007-2015 David Robillard Ingen is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version. Ingen 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 Affero General Public License for details. You should have received a copy of the GNU Affero General Public License along with Ingen. If not, see . */ #include "BufferFactory.hpp" #include "Buffer.hpp" #include "Engine.hpp" #include "ingen/Log.hpp" #include "ingen/URIs.hpp" #include "ingen/World.hpp" #include "lv2/atom/atom.h" #include "lv2/urid/urid.h" #include #include namespace ingen::server { BufferFactory::BufferFactory(Engine& engine, URIs& uris) : _free_audio(nullptr) , _free_control(nullptr) , _free_sequence(nullptr) , _free_object(nullptr) , _engine(engine) , _uris(uris) , _silent_buffer(nullptr) {} BufferFactory::~BufferFactory() { _silent_buffer.reset(); // Run twice to delete value buffer references which are dropped for (unsigned i = 0; i < 2; ++i) { free_list(_free_audio.exchange(nullptr)); free_list(_free_control.exchange(nullptr)); free_list(_free_sequence.exchange(nullptr)); free_list(_free_object.exchange(nullptr)); } } Forge& BufferFactory::forge() { return _engine.world().forge(); } raul::Maid& BufferFactory::maid() { return *_engine.maid(); } void BufferFactory::free_list(Buffer* head) { while (head) { Buffer* next = head->_next; delete head; head = next; } } void BufferFactory::set_block_length(SampleCount block_length) { _silent_buffer = create(_uris.atom_Sound, audio_buffer_size(block_length)); } uint32_t BufferFactory::audio_buffer_size(SampleCount nframes) { return nframes * sizeof(Sample); } uint32_t BufferFactory::audio_buffer_size() const { return _engine.block_length() * sizeof(Sample); } uint32_t BufferFactory::default_size(LV2_URID type) const { if (type == _uris.atom_Float) { return sizeof(LV2_Atom_Float); } if (type == _uris.atom_Sound) { return audio_buffer_size(_engine.block_length()); } if (type == _uris.atom_URID) { return sizeof(LV2_Atom_URID); } if (type == _uris.atom_Sequence) { if (_seq_size == 0U) { return _engine.sequence_size(); } return _seq_size; } return 0; } Buffer* BufferFactory::try_get_buffer(LV2_URID type) { std::atomic& head_ptr = free_list(type); Buffer* head = nullptr; Buffer* next = nullptr; do { head = head_ptr.load(); if (!head) { break; } next = head->_next; } while (!head_ptr.compare_exchange_weak(head, next)); return head; } BufferRef BufferFactory::get_buffer(LV2_URID type, LV2_URID value_type, uint32_t capacity) { Buffer* try_head = try_get_buffer(type); if (!try_head) { return create(type, value_type, capacity); } try_head->_next = nullptr; try_head->set_type(&BufferFactory::get_buffer, type, value_type); try_head->clear(); return {try_head}; } BufferRef BufferFactory::claim_buffer(LV2_URID type, LV2_URID value_type, uint32_t) { Buffer* try_head = try_get_buffer(type); if (!try_head) { _engine.world().log().rt_error("Failed to obtain buffer"); return {}; } try_head->_next = nullptr; try_head->set_type(&BufferFactory::claim_buffer, type, value_type); return {try_head}; } BufferRef BufferFactory::silent_buffer() { return _silent_buffer; } BufferRef BufferFactory::create(LV2_URID type, LV2_URID value_type, uint32_t capacity) { if (capacity == 0) { capacity = default_size(type); } else if (type == _uris.atom_Float) { capacity = std::max(capacity, static_cast(sizeof(LV2_Atom_Float))); } else if (type == _uris.atom_Sound) { capacity = std::max(capacity, default_size(_uris.atom_Sound)); } return {new Buffer(*this, type, value_type, capacity)}; } void BufferFactory::recycle(Buffer* buf) { std::atomic& head_ptr = free_list(buf->type()); Buffer* try_head = nullptr; do { try_head = head_ptr.load(); buf->_next = try_head; } while (!head_ptr.compare_exchange_weak(try_head, buf)); } } // namespace ingen::server