|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- /*
- * LV2 Atom Ring Buffer
- * Copyright (C) 2012-2014 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 doc/GPL.txt file.
- */
-
- #ifndef LV2_ATOM_RING_BUFFER_HPP_INCLUDED
- #define LV2_ATOM_RING_BUFFER_HPP_INCLUDED
-
- #include "CarlaMutex.hpp"
- #include "CarlaRingBuffer.hpp"
-
- #include "lv2/atom.h"
-
- // -----------------------------------------------------------------------
-
- class Lv2AtomRingBuffer : public CarlaRingBufferControl<HeapBuffer>
- {
- public:
- Lv2AtomRingBuffer() noexcept
- : fMutex(),
- fHeapBuffer(HeapBuffer_INIT),
- fNeedsDataDelete(true)
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- , fRetAtom{{0,0}, {0}}
- #endif
- {
- carla_zeroStruct(fHeapBuffer);
- }
-
- Lv2AtomRingBuffer(Lv2AtomRingBuffer& ringBuf, uint8_t buf[]) noexcept
- : fMutex(),
- fHeapBuffer(HeapBuffer_INIT),
- fNeedsDataDelete(false)
- #ifdef CARLA_PROPER_CPP11_SUPPORT
- , fRetAtom{{0,0}, {0}}
- #endif
- {
- carla_zeroStruct(fHeapBuffer);
-
- fHeapBuffer.buf = buf;
- fHeapBuffer.size = ringBuf.fHeapBuffer.size;
-
- {
- const CarlaMutexLocker cml(ringBuf.fMutex);
- fHeapBuffer.copyDataFrom(ringBuf.fHeapBuffer);
- ringBuf.clear();
- }
-
- setRingBuffer(&fHeapBuffer, false);
- }
-
- ~Lv2AtomRingBuffer() noexcept
- {
- if (fHeapBuffer.buf == nullptr || ! fNeedsDataDelete)
- return;
-
- delete[] fHeapBuffer.buf;
- fHeapBuffer.buf = nullptr;
- }
-
- // -------------------------------------------------------------------
-
- void createBuffer(const uint32_t size) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf == nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fNeedsDataDelete,);
- CARLA_SAFE_ASSERT_RETURN(size > 0,);
-
- const uint32_t p2size(carla_nextPowerOf2(size));
-
- try {
- fHeapBuffer.buf = new uint8_t[p2size];
- } CARLA_SAFE_EXCEPTION_RETURN("Lv2AtomRingBuffer::createBuffer",);
-
- fHeapBuffer.size = p2size;
- setRingBuffer(&fHeapBuffer, true);
- }
-
- void deleteBuffer() noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(fHeapBuffer.buf != nullptr,);
- CARLA_SAFE_ASSERT_RETURN(fNeedsDataDelete,);
-
- setRingBuffer(nullptr, false);
-
- delete[] fHeapBuffer.buf;
- fHeapBuffer.buf = nullptr;
- fHeapBuffer.size = 0;
- }
-
- uint32_t getSize() const noexcept
- {
- return fHeapBuffer.size;
- }
-
- // -------------------------------------------------------------------
-
- bool tryLock() const noexcept
- {
- return fMutex.tryLock();
- }
-
- void unlock() const noexcept
- {
- fMutex.unlock();
- }
-
- // -------------------------------------------------------------------
-
- // NOTE: must have been locked before
- bool get(const LV2_Atom*& atom, uint32_t& portIndex) noexcept
- {
- if (const LV2_Atom* const retAtom = readAtom(portIndex))
- {
- atom = retAtom;
- return true;
- }
-
- return false;
- }
-
- // NOTE: must NOT been locked, we do that here
- bool put(const LV2_Atom* const atom, const uint32_t portIndex) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(atom != nullptr && atom->size > 0, false);
-
- const CarlaMutexLocker cml(fMutex);
-
- return writeAtom(atom, static_cast<int32_t>(portIndex));
- }
-
- // NOTE: must NOT been locked, we do that here
- bool putChunk(const LV2_Atom* const atom, const void* const data, const uint32_t portIndex) noexcept
- {
- CARLA_SAFE_ASSERT_RETURN(atom != nullptr && atom->size > 0, false);
- CARLA_SAFE_ASSERT_RETURN(data != nullptr, false);
-
- const CarlaMutexLocker cml(fMutex);
-
- return writeAtomChunk(atom, data, static_cast<int32_t>(portIndex));
- }
-
- protected:
- // -------------------------------------------------------------------
-
- const LV2_Atom* readAtom(uint32_t& portIndex) noexcept
- {
- fRetAtom.atom.size = 0;
- fRetAtom.atom.type = 0;
-
- if (! tryRead(&fRetAtom.atom, sizeof(LV2_Atom)))
- return nullptr;
- if (fRetAtom.atom.size == 0 || fRetAtom.atom.type == 0)
- return nullptr;
-
- CARLA_SAFE_ASSERT_RETURN(fRetAtom.atom.size < kMaxAtomDataSize, nullptr);
-
- int32_t index = -1;
- if (! tryRead(&index, sizeof(int32_t)))
- return nullptr;
- if (index < 0)
- return nullptr;
-
- if (! tryRead(fRetAtom.data, fRetAtom.atom.size))
- return nullptr;
-
- portIndex = static_cast<uint32_t>(index);
- return &fRetAtom.atom;
- }
-
- // -------------------------------------------------------------------
-
- bool writeAtom(const LV2_Atom* const atom, const int32_t portIndex) noexcept
- {
- tryWrite(atom, sizeof(LV2_Atom));
- tryWrite(&portIndex, sizeof(int32_t));
- tryWrite(LV2_ATOM_BODY_CONST(atom), atom->size);
- return commitWrite();
- }
-
- bool writeAtomChunk(const LV2_Atom* const atom, const void* const data, const int32_t portIndex) noexcept
- {
- tryWrite(atom, sizeof(LV2_Atom));
- tryWrite(&portIndex, sizeof(int32_t));
- tryWrite(data, atom->size);
- return commitWrite();
- }
-
- // -------------------------------------------------------------------
-
- private:
- CarlaMutex fMutex;
- HeapBuffer fHeapBuffer;
- const bool fNeedsDataDelete;
-
- static const std::size_t kMaxAtomDataSize = 8192;
-
- struct RetAtom {
- LV2_Atom atom;
- char data[kMaxAtomDataSize];
- } fRetAtom;
-
- friend class Lv2AtomQueue;
-
- CARLA_PREVENT_VIRTUAL_HEAP_ALLOCATION
- CARLA_DECLARE_NON_COPY_CLASS(Lv2AtomRingBuffer)
- };
-
- // -----------------------------------------------------------------------
-
- #endif // LV2_ATOM_RING_BUFFER_HPP_INCLUDED
|