From ac778d258e0866d1117aadf576de2ff89df3e84d Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 21 Jan 2018 14:28:34 +0100 Subject: Clean up Buffer class and factor out allocation --- src/server/Buffer.cpp | 38 ++++++++++++++++++++------------------ src/server/Buffer.hpp | 33 ++++++++++++++++----------------- 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/server/Buffer.cpp b/src/server/Buffer.cpp index d609a2af..1952beb2 100644 --- a/src/server/Buffer.cpp +++ b/src/server/Buffer.cpp @@ -47,30 +47,18 @@ Buffer::Buffer(BufferFactory& bufs, bool external, void* buf) : _factory(bufs) - , _buf(nullptr) + , _next(nullptr) + , _buf(external ? nullptr : aligned_alloc(capacity)) + , _latest_event(0) , _type(type) , _value_type(value_type) , _capacity(capacity) - , _latest_event(0) - , _next(nullptr) , _refs(0) , _external(external) { - if (!external) { -#ifdef HAVE_POSIX_MEMALIGN - int ret = posix_memalign((void**)&_buf, 16, capacity); - if (!ret) { - memset(_buf, 0, capacity); - } -#else - _buf = (LV2_buf*)calloc(1, capacity); - int ret = (_buf != NULL) ? 0 : -1; -#endif - - if (ret) { - bufs.engine().log().rt_error("Failed to allocate event buffer\n"); - throw std::bad_alloc(); - } + if (!external && !_buf) { + bufs.engine().log().rt_error("Failed to allocate buffer\n"); + throw std::bad_alloc(); } if (type != bufs.uris().atom_Sound) { @@ -450,6 +438,20 @@ Buffer::dump_cv(const RunContext& context) const } #endif +void* Buffer::aligned_alloc(size_t size) +{ +#ifdef HAVE_POSIX_MEMALIGN + void* buf; + if (!posix_memalign((void**)&buf, 16, size)) { + memset(buf, 0, size); + return buf; + } +#else + return (LV2_buf*)calloc(1, size); +#endif + return nullptr; +} + void intrusive_ptr_add_ref(Buffer* b) { diff --git a/src/server/Buffer.hpp b/src/server/Buffer.hpp index 6d42b979..a95fcd3c 100644 --- a/src/server/Buffer.hpp +++ b/src/server/Buffer.hpp @@ -47,6 +47,9 @@ public: bool external = false, void* buf = nullptr); + Buffer(const Buffer&) = delete; + Buffer& operator=(const Buffer&) = delete; + void clear(); void resize(uint32_t capacity); void copy(const RunContext& context, const Buffer* src); @@ -204,6 +207,8 @@ public: void set_buffer(void* buf) { assert(_external); _buf = buf; } + static void* aligned_alloc(size_t size); + template const T* get() const { return reinterpret_cast(_buf); } template T* get() { return reinterpret_cast(_buf); } @@ -215,28 +220,22 @@ public: } } -protected: - BufferFactory& _factory; - void* _buf; - LV2_URID _type; - LV2_URID _value_type; - uint32_t _capacity; - int64_t _latest_event; - - BufferRef _value_buffer; ///< Value buffer for numeric sequences - +private: friend class BufferFactory; ~Buffer(); -private: - Buffer(const Buffer&) = delete; - Buffer& operator=(const Buffer&) = delete; - void recycle(); - Buffer* _next; ///< Intrusive linked list for BufferFactory - std::atomic _refs; ///< Intrusive reference count - bool _external; ///< Buffer is externally allocated + BufferFactory& _factory; + Buffer* _next; ///< Intrusive linked list for BufferFactory + void* _buf; ///< Actual buffer memory + BufferRef _value_buffer; ///< Value buffer for numeric sequences + int64_t _latest_event; + LV2_URID _type; + LV2_URID _value_type; + uint32_t _capacity; + std::atomic _refs; ///< Intrusive reference count + bool _external; ///< Buffer is externally allocated }; } // namespace Server -- cgit v1.2.1