diff options
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-riff.cpp | 25 | ||||
| -rw-r--r-- | source/core/slang-riff.h | 38 |
2 files changed, 51 insertions, 12 deletions
diff --git a/source/core/slang-riff.cpp b/source/core/slang-riff.cpp index 9fc23fbc8..2162c40be 100644 --- a/source/core/slang-riff.cpp +++ b/source/core/slang-riff.cpp @@ -313,6 +313,11 @@ struct DumpVisitor : public RiffContainer::Visitor return SLANG_OK; } +/* static */SlangResult RiffUtil::write(RiffContainer* container, Stream* stream) +{ + return write(container->getRoot(), true, stream); +} + /* static */SlangResult RiffUtil::read(Stream* stream, RiffContainer& outContainer) { typedef RiffContainer::ScopeChunk ScopeChunk; @@ -561,6 +566,20 @@ void RiffContainer::ListChunk::findContained(FourCC type, List<DataChunk*>& out) } } +RiffContainer::ListChunk* RiffContainer::ListChunk::findContainedList(FourCC type) +{ + Chunk* chunk = m_containedChunks; + while (chunk) + { + if (chunk->m_fourCC == type && chunk->m_kind == Chunk::Kind::List) + { + return static_cast<ListChunk*>(chunk); + } + chunk = chunk->m_next; + } + return nullptr; +} + RiffContainer::Data* RiffContainer::ListChunk::findContainedData(FourCC type) const { Chunk* found = findContained(type); @@ -841,11 +860,11 @@ void RiffContainer::setPayload(Data* data, const void* payload, size_t size) // Add current chunks data m_dataChunk->m_payloadSize += size; - + data->m_ownership = Ownership::Arena; data->m_size = size; - data->m_payload = m_arena.allocate(size); + data->m_payload = m_arena.allocateAligned(size, PAYLOAD_MIN_ALIGNMENT); if (payload) { @@ -922,7 +941,7 @@ RiffContainer::Data* RiffContainer::makeSingleData(DataChunk* dataChunk) // Okay lets combine all into one block const size_t payloadSize = dataChunk->calcPayloadSize(); - void* dst = m_arena.allocate(payloadSize); + void* dst = m_arena.allocateAligned(payloadSize, PAYLOAD_MIN_ALIGNMENT); dataChunk->getPayload(dst); // Remove other datas diff --git a/source/core/slang-riff.h b/source/core/slang-riff.h index 9bdbcfdeb..43c5e285d 100644 --- a/source/core/slang-riff.h +++ b/source/core/slang-riff.h @@ -177,6 +177,14 @@ need to be recalculated, before serialization. class RiffContainer { public: + enum + { + // This alignment is only made for arena based allocations. + // For external blocks it's client code to have appropriate alignment. + // This is needed because when reading a RiffContainer, all allocation is arena based, and + // if the payload contains 8 byte aligned data, the overall payload needs to be 8 byte aligned. + PAYLOAD_MIN_ALIGNMENT = 8, + }; enum class Ownership { @@ -186,10 +194,6 @@ public: Owned, ///< It's owned, but wasn't allocated on the arena }; - struct ListChunk; - struct DataChunk; - - struct Data { /// Get the payload @@ -214,8 +218,11 @@ public: }; struct Chunk; - typedef SlangResult(*VisitorCallback)(Chunk* chunk, void* data); + struct ListChunk; + struct DataChunk; + typedef SlangResult(*VisitorCallback)(Chunk* chunk, void* data); + class Visitor; struct Chunk { @@ -270,6 +277,8 @@ public: void* findContainedData(FourCC type, size_t minSize) const; + ListChunk* findContainedList(FourCC type); + /// Finds the contained data. NOTE! Assumes that there is only as single data block, and will return nullptr if there is not Data* findContainedData(FourCC type) const; @@ -291,6 +300,9 @@ public: /// Get the sub type FourCC getSubType() const { return m_fourCC; } + /// A singly linked list of contained chunks directly contained in this chunk + Chunk* getFirstContainedChunk() const { return m_containedChunks; } + Chunk* m_containedChunks; ///< The contained chunks Chunk* m_endChunk; ///< The last chunk (only set when pushed, and used when popped) }; @@ -416,9 +428,14 @@ protected: template <typename T> T* as(RiffContainer::Chunk* chunk) { - return T::isType(chunk) ? static_cast<T*>(chunk) : nullptr; + return chunk && T::isType(chunk) ? static_cast<T*>(chunk) : nullptr; +} +// ----------------------------------------------------------------------------- +template <typename T> +T* as(RiffContainer::Chunk* chunk, FourCC fourCC) +{ + return chunk && chunk->m_fourCC == fourCC && T::isType(chunk) ? static_cast<T*>(chunk) : nullptr; } - struct RiffUtil { @@ -449,8 +466,11 @@ struct RiffUtil /// Get the size taking into account padding static size_t getPadSize(size_t in) { return (in + kRiffPadMask) & ~size_t(kRiffPadMask); } - /// Write a container and contents to a stream - static SlangResult write(ListChunk* container, bool isRoot, Stream* stream); + /// Write a chunk list and contents to a stream + static SlangResult write(ListChunk* listChunk, bool isRoot, Stream* stream); + /// Write a container to the stream + static SlangResult write(RiffContainer* container, Stream* stream); + /// Read the stream into the container static SlangResult read(Stream* stream, RiffContainer& outContainer); }; |
