CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
124 lines
4.7 KiB
C++
124 lines
4.7 KiB
C++
//===- BinaryStreamRef.cpp - ----------------------------------------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/Support/BinaryStreamRef.h"
|
|
#include "llvm/Support/BinaryByteStream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
|
|
class ArrayRefImpl : public BinaryStream {
|
|
public:
|
|
ArrayRefImpl(ArrayRef<uint8_t> Data, endianness Endian) : BBS(Data, Endian) {}
|
|
|
|
llvm::endianness getEndian() const override { return BBS.getEndian(); }
|
|
Error readBytes(uint64_t Offset, uint64_t Size,
|
|
ArrayRef<uint8_t> &Buffer) override {
|
|
return BBS.readBytes(Offset, Size, Buffer);
|
|
}
|
|
Error readLongestContiguousChunk(uint64_t Offset,
|
|
ArrayRef<uint8_t> &Buffer) override {
|
|
return BBS.readLongestContiguousChunk(Offset, Buffer);
|
|
}
|
|
uint64_t getLength() override { return BBS.getLength(); }
|
|
|
|
private:
|
|
BinaryByteStream BBS;
|
|
};
|
|
|
|
class MutableArrayRefImpl : public WritableBinaryStream {
|
|
public:
|
|
MutableArrayRefImpl(MutableArrayRef<uint8_t> Data, endianness Endian)
|
|
: BBS(Data, Endian) {}
|
|
|
|
// Inherited via WritableBinaryStream
|
|
llvm::endianness getEndian() const override { return BBS.getEndian(); }
|
|
Error readBytes(uint64_t Offset, uint64_t Size,
|
|
ArrayRef<uint8_t> &Buffer) override {
|
|
return BBS.readBytes(Offset, Size, Buffer);
|
|
}
|
|
Error readLongestContiguousChunk(uint64_t Offset,
|
|
ArrayRef<uint8_t> &Buffer) override {
|
|
return BBS.readLongestContiguousChunk(Offset, Buffer);
|
|
}
|
|
uint64_t getLength() override { return BBS.getLength(); }
|
|
|
|
Error writeBytes(uint64_t Offset, ArrayRef<uint8_t> Data) override {
|
|
return BBS.writeBytes(Offset, Data);
|
|
}
|
|
Error commit() override { return BBS.commit(); }
|
|
|
|
private:
|
|
MutableBinaryByteStream BBS;
|
|
};
|
|
} // namespace
|
|
|
|
BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream)
|
|
: BinaryStreamRefBase(Stream) {}
|
|
BinaryStreamRef::BinaryStreamRef(BinaryStream &Stream, uint64_t Offset,
|
|
std::optional<uint64_t> Length)
|
|
: BinaryStreamRefBase(Stream, Offset, Length) {}
|
|
BinaryStreamRef::BinaryStreamRef(ArrayRef<uint8_t> Data, endianness Endian)
|
|
: BinaryStreamRefBase(std::make_shared<ArrayRefImpl>(Data, Endian), 0,
|
|
Data.size()) {}
|
|
BinaryStreamRef::BinaryStreamRef(StringRef Data, endianness Endian)
|
|
: BinaryStreamRef(ArrayRef(Data.bytes_begin(), Data.bytes_end()), Endian) {}
|
|
|
|
Error BinaryStreamRef::readBytes(uint64_t Offset, uint64_t Size,
|
|
ArrayRef<uint8_t> &Buffer) const {
|
|
if (auto EC = checkOffsetForRead(Offset, Size))
|
|
return EC;
|
|
return BorrowedImpl->readBytes(ViewOffset + Offset, Size, Buffer);
|
|
}
|
|
|
|
Error BinaryStreamRef::readLongestContiguousChunk(
|
|
uint64_t Offset, ArrayRef<uint8_t> &Buffer) const {
|
|
if (auto EC = checkOffsetForRead(Offset, 1))
|
|
return EC;
|
|
|
|
if (auto EC =
|
|
BorrowedImpl->readLongestContiguousChunk(ViewOffset + Offset, Buffer))
|
|
return EC;
|
|
// This StreamRef might refer to a smaller window over a larger stream. In
|
|
// that case we will have read out more bytes than we should return, because
|
|
// we should not read past the end of the current view.
|
|
uint64_t MaxLength = getLength() - Offset;
|
|
if (Buffer.size() > MaxLength)
|
|
Buffer = Buffer.slice(0, MaxLength);
|
|
return Error::success();
|
|
}
|
|
|
|
WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream)
|
|
: BinaryStreamRefBase(Stream) {}
|
|
|
|
WritableBinaryStreamRef::WritableBinaryStreamRef(WritableBinaryStream &Stream,
|
|
uint64_t Offset,
|
|
std::optional<uint64_t> Length)
|
|
: BinaryStreamRefBase(Stream, Offset, Length) {}
|
|
|
|
WritableBinaryStreamRef::WritableBinaryStreamRef(MutableArrayRef<uint8_t> Data,
|
|
endianness Endian)
|
|
: BinaryStreamRefBase(std::make_shared<MutableArrayRefImpl>(Data, Endian),
|
|
0, Data.size()) {}
|
|
|
|
Error WritableBinaryStreamRef::writeBytes(uint64_t Offset,
|
|
ArrayRef<uint8_t> Data) const {
|
|
if (auto EC = checkOffsetForWrite(Offset, Data.size()))
|
|
return EC;
|
|
|
|
return BorrowedImpl->writeBytes(ViewOffset + Offset, Data);
|
|
}
|
|
|
|
WritableBinaryStreamRef::operator BinaryStreamRef() const {
|
|
return BinaryStreamRef(*BorrowedImpl, ViewOffset, Length);
|
|
}
|
|
|
|
/// For buffered streams, commits changes to the backing store.
|
|
Error WritableBinaryStreamRef::commit() { return BorrowedImpl->commit(); }
|