Changeset View
Changeset View
Standalone View
Standalone View
llvm/lib/Support/BinaryStreamWriter.cpp
Show First 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | Error BinaryStreamWriter::writeFixedString(StringRef Str) { | ||||
return writeBytes(arrayRefFromStringRef(Str)); | return writeBytes(arrayRefFromStringRef(Str)); | ||||
} | } | ||||
Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) { | Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) { | ||||
return writeStreamRef(Ref, Ref.getLength()); | return writeStreamRef(Ref, Ref.getLength()); | ||||
} | } | ||||
Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) { | Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint64_t Length) { | ||||
BinaryStreamReader SrcReader(Ref.slice(0, Length)); | BinaryStreamReader SrcReader(Ref.slice(0, Length)); | ||||
// This is a bit tricky. If we just call readBytes, we are requiring that it | // This is a bit tricky. If we just call readBytes, we are requiring that it | ||||
// return us the entire stream as a contiguous buffer. There is no guarantee | // return us the entire stream as a contiguous buffer. There is no guarantee | ||||
// this can be satisfied by returning a reference straight from the buffer, as | // this can be satisfied by returning a reference straight from the buffer, as | ||||
// an implementation may not store all data in a single contiguous buffer. So | // an implementation may not store all data in a single contiguous buffer. So | ||||
// we iterate over each contiguous chunk, writing each one in succession. | // we iterate over each contiguous chunk, writing each one in succession. | ||||
while (SrcReader.bytesRemaining() > 0) { | while (SrcReader.bytesRemaining() > 0) { | ||||
ArrayRef<uint8_t> Chunk; | ArrayRef<uint8_t> Chunk; | ||||
if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) | if (auto EC = SrcReader.readLongestContiguousChunk(Chunk)) | ||||
return EC; | return EC; | ||||
if (auto EC = writeBytes(Chunk)) | if (auto EC = writeBytes(Chunk)) | ||||
return EC; | return EC; | ||||
} | } | ||||
return Error::success(); | return Error::success(); | ||||
} | } | ||||
std::pair<BinaryStreamWriter, BinaryStreamWriter> | std::pair<BinaryStreamWriter, BinaryStreamWriter> | ||||
BinaryStreamWriter::split(uint32_t Off) const { | BinaryStreamWriter::split(uint64_t Off) const { | ||||
assert(getLength() >= Off); | assert(getLength() >= Off); | ||||
WritableBinaryStreamRef First = Stream.drop_front(Offset); | WritableBinaryStreamRef First = Stream.drop_front(Offset); | ||||
WritableBinaryStreamRef Second = First.drop_front(Off); | WritableBinaryStreamRef Second = First.drop_front(Off); | ||||
First = First.keep_front(Off); | First = First.keep_front(Off); | ||||
BinaryStreamWriter W1{First}; | BinaryStreamWriter W1{First}; | ||||
BinaryStreamWriter W2{Second}; | BinaryStreamWriter W2{Second}; | ||||
return std::make_pair(W1, W2); | return std::make_pair(W1, W2); | ||||
} | } | ||||
Error BinaryStreamWriter::padToAlignment(uint32_t Align) { | Error BinaryStreamWriter::padToAlignment(uint32_t Align) { | ||||
uint32_t NewOffset = alignTo(Offset, Align); | uint64_t NewOffset = alignTo(Offset, Align); | ||||
if (NewOffset > getLength()) | if (NewOffset > getLength()) | ||||
return make_error<BinaryStreamError>(stream_error_code::stream_too_short); | return make_error<BinaryStreamError>(stream_error_code::stream_too_short); | ||||
while (Offset < NewOffset) | while (Offset < NewOffset) | ||||
if (auto EC = writeInteger('\0')) | if (auto EC = writeInteger('\0')) | ||||
return EC; | return EC; | ||||
return Error::success(); | return Error::success(); | ||||
} | } |