From 369118b48747b8b3b8eaf14b2e7f51549720d5ab Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 12 Feb 2017 18:01:23 +0100 Subject: Fix various cast alignment warnings --- ingen/AtomForgeSink.hpp | 97 +++++++++++++++++++++++++++++++++++++++++++++++++ ingen/AtomReader.hpp | 1 - ingen/AtomWriter.hpp | 6 +-- 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 ingen/AtomForgeSink.hpp (limited to 'ingen') diff --git a/ingen/AtomForgeSink.hpp b/ingen/AtomForgeSink.hpp new file mode 100644 index 00000000..5520906c --- /dev/null +++ b/ingen/AtomForgeSink.hpp @@ -0,0 +1,97 @@ +/* + This file is part of Ingen. + Copyright 2007-2017 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 . +*/ + +#ifndef INGEN_ATOMFORGESINK_HPP +#define INGEN_ATOMFORGESINK_HPP + +#include + +#include "lv2/lv2plug.in/ns/ext/atom/forge.h" + +namespace Ingen { + +/** A resizing sink for LV2_Atom_Forge. */ +class AtomForgeSink +{ +public: + AtomForgeSink(LV2_Atom_Forge* forge = nullptr) + : _capacity(8 * sizeof(LV2_Atom)) + , _size(0) + , _buf((LV2_Atom*)calloc(8, sizeof(LV2_Atom))) + { + if (forge) { + set_forge_sink(forge); + } + } + + ~AtomForgeSink() { free(_buf); } + + void set_forge_sink(LV2_Atom_Forge* forge) { + lv2_atom_forge_set_sink(forge, c_append, c_deref, this); + } + + /** Append some data and return a reference to its start. */ + intptr_t append(const void* buf, uint32_t len) { + // Record offset of the start of this write (+1 to avoid NULL) + const intptr_t ref = _size + 1; + + // Update size and reallocate if necessary + if (lv2_atom_pad_size(_size + len) > _capacity) { + _capacity = lv2_atom_pad_size(_size + len); + _buf = (LV2_Atom*)realloc(_buf, _capacity); + } + + // Append new data + memcpy((uint8_t*)_buf + _size, buf, len); + _size += len; + return ref; + } + + /** Dereference a reference previously returned by append. */ + LV2_Atom* deref(intptr_t ref) { + /* Make some assumptions and do unnecessary math to appease + -Wcast-align. This is questionable at best, though the forge should + only dereference references to aligned atoms. */ + assert((ref - 1) % sizeof(LV2_Atom) == 0); + return (LV2_Atom*)(_buf + (ref - 1) / sizeof(LV2_Atom)); + + // Alternatively: + // return (LV2_Atom*)((uint8_t*)_buf + ref - 1); + } + + const LV2_Atom* atom() const { return _buf; } + + void clear() { _buf->type = 0; _buf->size = 0; _size = 0; } + + static LV2_Atom_Forge_Ref + c_append(void* handle, const void* buf, uint32_t len) { + return ((AtomForgeSink*)handle)->append(buf, len); + } + + static LV2_Atom* + c_deref(void* handle, LV2_Atom_Forge_Ref ref) { + return ((AtomForgeSink*)handle)->deref(ref); + } + +private: + size_t _capacity; + size_t _size; + LV2_Atom* _buf; +}; + +} // namespace Ingen + +#endif // INGEN_ATOMFORGESINK_HPP diff --git a/ingen/AtomReader.hpp b/ingen/AtomReader.hpp index 09aa672a..1c1dea5b 100644 --- a/ingen/AtomReader.hpp +++ b/ingen/AtomReader.hpp @@ -23,7 +23,6 @@ #include "ingen/AtomSink.hpp" #include "ingen/URIs.hpp" #include "ingen/ingen.h" -#include "serd/serd.h" namespace Ingen { diff --git a/ingen/AtomWriter.hpp b/ingen/AtomWriter.hpp index 9ea5fa27..4246e884 100644 --- a/ingen/AtomWriter.hpp +++ b/ingen/AtomWriter.hpp @@ -1,6 +1,6 @@ /* This file is part of Ingen. - Copyright 2007-2016 David Robillard + Copyright 2007-2017 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 @@ -19,11 +19,11 @@ #include +#include "ingen/AtomForgeSink.hpp" #include "ingen/Interface.hpp" #include "ingen/URIs.hpp" #include "ingen/ingen.h" #include "lv2/lv2plug.in/ns/ext/atom/forge.h" -#include "serd/serd.h" namespace Ingen { @@ -98,7 +98,7 @@ private: URIMap& _map; URIs& _uris; AtomSink& _sink; - SerdChunk _out; + AtomForgeSink _out; LV2_Atom_Forge _forge; int32_t _id; }; -- cgit v1.2.1