| // Copyright 2022 The Dawn & Tint Authors |
| // |
| // Redistribution and use in source and binary forms, with or without |
| // modification, are permitted provided that the following conditions are met: |
| // |
| // 1. Redistributions of source code must retain the above copyright notice, this |
| // list of conditions and the following disclaimer. |
| // |
| // 2. Redistributions in binary form must reproduce the above copyright notice, |
| // this list of conditions and the following disclaimer in the documentation |
| // and/or other materials provided with the distribution. |
| // |
| // 3. Neither the name of the copyright holder nor the names of its |
| // contributors may be used to endorse or promote products derived from |
| // this software without specific prior written permission. |
| // |
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| |
| #ifdef UNSAFE_BUFFERS_BUILD |
| // TODO(crbug.com/40285824): Remove this and convert code to safer constructs. |
| #pragma allow_unsafe_buffers |
| #endif |
| |
| #include "dawn/native/Blob.h" |
| |
| #include <algorithm> |
| #include <span> |
| #include <utility> |
| |
| #include "dawn/common/Assert.h" |
| #include "dawn/common/Math.h" |
| #include "dawn/native/stream/Stream.h" |
| |
| namespace dawn::native { |
| |
| // static |
| Blob Blob::Create(size_t size) { |
| if (size > 0) { |
| uint8_t* ptr = new uint8_t[size]; |
| return Blob::UnsafeCreateWithDeleter(ptr, size, [=] { delete[] ptr; }); |
| } else { |
| return Blob(); |
| } |
| } |
| |
| // static |
| Blob Blob::Create(Blob&& original, size_t offset) { |
| Blob result(original.mData.subspan(offset), std::move(original.mDeleter)); |
| original.mData = {}; |
| original.mDeleter = nullptr; |
| return result; |
| } |
| |
| // static |
| Blob Blob::UnsafeCreateWithDeleter(uint8_t* data, size_t size, std::function<void()> deleter) { |
| return Blob({reinterpret_cast<std::byte*>(data), size}, deleter); |
| } |
| |
| Blob::Blob() : mData({}), mDeleter({}) {} |
| |
| Blob::Blob(std::span<std::byte> data, std::function<void()> deleter) |
| : mData(data), mDeleter(std::move(deleter)) {} |
| |
| Blob::Blob(Blob&& rhs) : mData(rhs.mData) { |
| mDeleter = std::move(rhs.mDeleter); |
| rhs.mData = {}; |
| rhs.mDeleter = nullptr; |
| } |
| |
| Blob& Blob::operator=(Blob&& rhs) { |
| if (mDeleter) { |
| mDeleter(); |
| } |
| mData = rhs.mData; |
| mDeleter = std::move(rhs.mDeleter); |
| rhs.mData = {}; |
| rhs.mDeleter = nullptr; |
| return *this; |
| } |
| |
| Blob::~Blob() { |
| if (mDeleter) { |
| mDeleter(); |
| } |
| } |
| |
| bool Blob::Empty() const { |
| return mData.empty(); |
| } |
| |
| std::span<const std::byte> Blob::Data() const { |
| return mData; |
| } |
| |
| std::span<std::byte> Blob::Data() { |
| return mData; |
| } |
| |
| const std::byte* Blob::DataPtr() const { |
| return mData.data(); |
| } |
| |
| std::byte* Blob::DataPtr() { |
| return mData.data(); |
| } |
| |
| size_t Blob::Size() const { |
| return mData.size(); |
| } |
| |
| bool Blob::operator==(const Blob& other) const { |
| return std::ranges::equal(Data(), other.Data()); |
| } |
| |
| template <> |
| void stream::Stream<Blob>::Write(stream::Sink* s, const Blob& b) { |
| size_t size = b.Size(); |
| StreamIn(s, size); |
| if (size > 0) { |
| std::ranges::copy(b.Data(), s->GetSpace(size).begin()); |
| } |
| } |
| |
| template <> |
| MaybeError stream::Stream<Blob>::Read(stream::Source* s, Blob* b) { |
| size_t size; |
| DAWN_TRY(StreamOut(s, &size)); |
| if (size > 0) { |
| std::span<const std::byte> src; |
| DAWN_TRY_ASSIGN(src, s->Read(size)); |
| *b = Blob::Create(size); |
| std::ranges::copy(src, b->Data().begin()); |
| } else { |
| *b = Blob(); |
| } |
| return {}; |
| } |
| |
| } // namespace dawn::native |