2017-02-27 23:11:43 +01:00
|
|
|
//===- BinaryByteStream.h ---------------------------------------*- C++ -*-===//
|
2016-04-29 19:22:58 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// 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
|
2017-02-27 23:11:43 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// A BinaryStream which stores data in a single continguous memory buffer.
|
2016-04-29 19:22:58 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-02 21:52:51 +01:00
|
|
|
#ifndef LLVM_SUPPORT_BINARYBYTESTREAM_H
|
|
|
|
#define LLVM_SUPPORT_BINARYBYTESTREAM_H
|
2016-04-29 19:22:58 +02:00
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-03-02 21:52:51 +01:00
|
|
|
#include "llvm/Support/BinaryStream.h"
|
|
|
|
#include "llvm/Support/BinaryStreamError.h"
|
2016-05-10 01:11:38 +02:00
|
|
|
#include "llvm/Support/Error.h"
|
2016-07-28 21:12:28 +02:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-11-18 19:00:19 +01:00
|
|
|
#include <algorithm>
|
2016-05-10 01:11:38 +02:00
|
|
|
#include <cstdint>
|
2016-11-18 19:00:19 +01:00
|
|
|
#include <cstring>
|
2016-05-10 01:11:38 +02:00
|
|
|
#include <memory>
|
2016-04-29 19:22:58 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
2016-06-10 07:09:12 +02:00
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// An implementation of BinaryStream which holds its entire data set
|
2017-02-27 23:11:43 +01:00
|
|
|
/// in a single contiguous buffer. BinaryByteStream guarantees that no read
|
|
|
|
/// operation will ever incur a copy. Note that BinaryByteStream does not
|
|
|
|
/// own the underlying buffer.
|
|
|
|
class BinaryByteStream : public BinaryStream {
|
2016-04-29 19:22:58 +02:00
|
|
|
public:
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryByteStream() = default;
|
2017-02-28 01:04:07 +01:00
|
|
|
BinaryByteStream(ArrayRef<uint8_t> Data, llvm::support::endianness Endian)
|
|
|
|
: Endian(Endian), Data(Data) {}
|
|
|
|
BinaryByteStream(StringRef Data, llvm::support::endianness Endian)
|
|
|
|
: Endian(Endian), Data(Data.bytes_begin(), Data.bytes_end()) {}
|
|
|
|
|
|
|
|
llvm::support::endianness getEndian() const override { return Endian; }
|
2016-04-29 19:22:58 +02:00
|
|
|
|
2016-05-27 03:54:44 +02:00
|
|
|
Error readBytes(uint32_t Offset, uint32_t Size,
|
2017-02-27 23:11:43 +01:00
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
2017-11-27 19:48:37 +01:00
|
|
|
if (auto EC = checkOffsetForRead(Offset, Size))
|
2017-02-28 18:49:34 +01:00
|
|
|
return EC;
|
2016-07-28 21:12:28 +02:00
|
|
|
Buffer = Data.slice(Offset, Size);
|
|
|
|
return Error::success();
|
|
|
|
}
|
2016-11-18 19:00:19 +01:00
|
|
|
|
2016-06-10 07:09:12 +02:00
|
|
|
Error readLongestContiguousChunk(uint32_t Offset,
|
2017-02-27 23:11:43 +01:00
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
2017-11-27 19:48:37 +01:00
|
|
|
if (auto EC = checkOffsetForRead(Offset, 1))
|
2017-02-28 18:49:34 +01:00
|
|
|
return EC;
|
2016-07-28 21:12:28 +02:00
|
|
|
Buffer = Data.slice(Offset);
|
|
|
|
return Error::success();
|
|
|
|
}
|
2016-06-10 07:09:12 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
uint32_t getLength() override { return Data.size(); }
|
2016-05-03 02:28:21 +02:00
|
|
|
|
2016-07-28 21:12:28 +02:00
|
|
|
ArrayRef<uint8_t> data() const { return Data; }
|
2016-04-29 19:22:58 +02:00
|
|
|
|
2016-07-28 21:12:28 +02:00
|
|
|
StringRef str() const {
|
|
|
|
const char *CharData = reinterpret_cast<const char *>(Data.data());
|
|
|
|
return StringRef(CharData, Data.size());
|
|
|
|
}
|
2016-06-30 19:43:00 +02:00
|
|
|
|
2016-07-28 21:12:28 +02:00
|
|
|
protected:
|
2017-02-28 01:04:07 +01:00
|
|
|
llvm::support::endianness Endian;
|
2016-07-28 21:12:28 +02:00
|
|
|
ArrayRef<uint8_t> Data;
|
|
|
|
};
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// An implementation of BinaryStream whose data is backed by an llvm
|
2017-02-27 23:11:43 +01:00
|
|
|
/// MemoryBuffer object. MemoryBufferByteStream owns the MemoryBuffer in
|
|
|
|
/// question. As with BinaryByteStream, reading from a MemoryBufferByteStream
|
|
|
|
/// will never cause a copy.
|
|
|
|
class MemoryBufferByteStream : public BinaryByteStream {
|
2016-07-28 21:12:28 +02:00
|
|
|
public:
|
2017-02-28 01:04:07 +01:00
|
|
|
MemoryBufferByteStream(std::unique_ptr<MemoryBuffer> Buffer,
|
|
|
|
llvm::support::endianness Endian)
|
|
|
|
: BinaryByteStream(Buffer->getBuffer(), Endian),
|
|
|
|
MemBuffer(std::move(Buffer)) {}
|
2016-07-28 21:12:28 +02:00
|
|
|
|
|
|
|
std::unique_ptr<MemoryBuffer> MemBuffer;
|
|
|
|
};
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// An implementation of BinaryStream which holds its entire data set
|
2017-02-27 23:11:43 +01:00
|
|
|
/// in a single contiguous buffer. As with BinaryByteStream, the mutable
|
|
|
|
/// version also guarantees that no read operation will ever incur a copy,
|
|
|
|
/// and similarly it does not own the underlying buffer.
|
|
|
|
class MutableBinaryByteStream : public WritableBinaryStream {
|
2016-07-28 21:12:28 +02:00
|
|
|
public:
|
2017-02-27 23:11:43 +01:00
|
|
|
MutableBinaryByteStream() = default;
|
2017-02-28 01:04:07 +01:00
|
|
|
MutableBinaryByteStream(MutableArrayRef<uint8_t> Data,
|
|
|
|
llvm::support::endianness Endian)
|
|
|
|
: Data(Data), ImmutableStream(Data, Endian) {}
|
|
|
|
|
|
|
|
llvm::support::endianness getEndian() const override {
|
|
|
|
return ImmutableStream.getEndian();
|
|
|
|
}
|
2016-07-28 21:12:28 +02:00
|
|
|
|
|
|
|
Error readBytes(uint32_t Offset, uint32_t Size,
|
2017-02-27 23:11:43 +01:00
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
2016-07-28 21:12:28 +02:00
|
|
|
return ImmutableStream.readBytes(Offset, Size, Buffer);
|
|
|
|
}
|
2016-11-18 19:00:19 +01:00
|
|
|
|
2016-07-28 21:12:28 +02:00
|
|
|
Error readLongestContiguousChunk(uint32_t Offset,
|
2017-02-27 23:11:43 +01:00
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
2016-07-28 21:12:28 +02:00
|
|
|
return ImmutableStream.readLongestContiguousChunk(Offset, Buffer);
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
uint32_t getLength() override { return ImmutableStream.getLength(); }
|
2016-07-28 21:12:28 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) override {
|
2016-11-14 23:05:19 +01:00
|
|
|
if (Buffer.empty())
|
|
|
|
return Error::success();
|
|
|
|
|
2017-11-27 19:48:37 +01:00
|
|
|
if (auto EC = checkOffsetForWrite(Offset, Buffer.size()))
|
2017-02-28 18:49:34 +01:00
|
|
|
return EC;
|
2016-07-28 21:12:28 +02:00
|
|
|
|
|
|
|
uint8_t *DataPtr = const_cast<uint8_t *>(Data.data());
|
|
|
|
::memcpy(DataPtr + Offset, Buffer.data(), Buffer.size());
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error commit() override { return Error::success(); }
|
2016-07-28 21:12:28 +02:00
|
|
|
|
|
|
|
MutableArrayRef<uint8_t> data() const { return Data; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
MutableArrayRef<uint8_t> Data;
|
2017-02-27 23:11:43 +01:00
|
|
|
BinaryByteStream ImmutableStream;
|
2016-07-28 21:12:28 +02:00
|
|
|
};
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// An implementation of WritableBinaryStream which can write at its end
|
2017-11-27 19:48:37 +01:00
|
|
|
/// causing the underlying data to grow. This class owns the underlying data.
|
|
|
|
class AppendingBinaryByteStream : public WritableBinaryStream {
|
|
|
|
std::vector<uint8_t> Data;
|
[CodeView] Refactor / Rewrite TypeSerializer and TypeTableBuilder.
The motivation behind this patch is that future directions require us to
be able to compute the hash value of records independently of actually
using them for de-duplication.
The current structure of TypeSerializer / TypeTableBuilder being a
single entry point that takes an unserialized type record, and then
hashes and de-duplicates it is not flexible enough to allow this.
At the same time, the existing TypeSerializer is already extremely
complex for this very reason -- it tries to be too many things. In
addition to serializing, hashing, and de-duplicating, ti also supports
splitting up field list records and adding continuations. All of this
functionality crammed into this one class makes it very complicated to
work with and hard to maintain.
To solve all of these problems, I've re-written everything from scratch
and split the functionality into separate pieces that can easily be
reused. The end result is that one class TypeSerializer is turned into 3
new classes SimpleTypeSerializer, ContinuationRecordBuilder, and
TypeTableBuilder, each of which in isolation is simple and
straightforward.
A quick summary of these new classes and their responsibilities are:
- SimpleTypeSerializer : Turns a non-FieldList leaf type into a series of
bytes. Does not do any hashing. Every time you call it, it will
re-serialize and return bytes again. The same instance can be re-used
over and over to avoid re-allocations, and in exchange for this
optimization the bytes returned by the serializer only live until the
caller attempts to serialize a new record.
- ContinuationRecordBuilder : Turns a FieldList-like record into a series
of fragments. Does not do any hashing. Like SimpleTypeSerializer,
returns references to privately owned bytes, so the storage is
invalidated as soon as the caller tries to re-use the instance. Works
equally well for LF_FIELDLIST as it does for LF_METHODLIST, solving a
long-standing theoretical limitation of the previous implementation.
- TypeTableBuilder : Accepts sequences of bytes that the user has already
serialized, and inserts them by de-duplicating with a hash table. For
the sake of convenience and efficiency, this class internally stores a
SimpleTypeSerializer so that it can accept unserialized records. The
same is not true of ContinuationRecordBuilder. The user is required to
create their own instance of ContinuationRecordBuilder.
Differential Revision: https://reviews.llvm.org/D40518
llvm-svn: 319198
2017-11-28 19:33:17 +01:00
|
|
|
llvm::support::endianness Endian = llvm::support::little;
|
2017-11-27 19:48:37 +01:00
|
|
|
|
|
|
|
public:
|
|
|
|
AppendingBinaryByteStream() = default;
|
|
|
|
AppendingBinaryByteStream(llvm::support::endianness Endian)
|
|
|
|
: Endian(Endian) {}
|
|
|
|
|
|
|
|
void clear() { Data.clear(); }
|
|
|
|
|
|
|
|
llvm::support::endianness getEndian() const override { return Endian; }
|
|
|
|
|
|
|
|
Error readBytes(uint32_t Offset, uint32_t Size,
|
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
|
|
|
if (auto EC = checkOffsetForWrite(Offset, Buffer.size()))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
Buffer = makeArrayRef(Data).slice(Offset, Size);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
[CodeView] Refactor / Rewrite TypeSerializer and TypeTableBuilder.
The motivation behind this patch is that future directions require us to
be able to compute the hash value of records independently of actually
using them for de-duplication.
The current structure of TypeSerializer / TypeTableBuilder being a
single entry point that takes an unserialized type record, and then
hashes and de-duplicates it is not flexible enough to allow this.
At the same time, the existing TypeSerializer is already extremely
complex for this very reason -- it tries to be too many things. In
addition to serializing, hashing, and de-duplicating, ti also supports
splitting up field list records and adding continuations. All of this
functionality crammed into this one class makes it very complicated to
work with and hard to maintain.
To solve all of these problems, I've re-written everything from scratch
and split the functionality into separate pieces that can easily be
reused. The end result is that one class TypeSerializer is turned into 3
new classes SimpleTypeSerializer, ContinuationRecordBuilder, and
TypeTableBuilder, each of which in isolation is simple and
straightforward.
A quick summary of these new classes and their responsibilities are:
- SimpleTypeSerializer : Turns a non-FieldList leaf type into a series of
bytes. Does not do any hashing. Every time you call it, it will
re-serialize and return bytes again. The same instance can be re-used
over and over to avoid re-allocations, and in exchange for this
optimization the bytes returned by the serializer only live until the
caller attempts to serialize a new record.
- ContinuationRecordBuilder : Turns a FieldList-like record into a series
of fragments. Does not do any hashing. Like SimpleTypeSerializer,
returns references to privately owned bytes, so the storage is
invalidated as soon as the caller tries to re-use the instance. Works
equally well for LF_FIELDLIST as it does for LF_METHODLIST, solving a
long-standing theoretical limitation of the previous implementation.
- TypeTableBuilder : Accepts sequences of bytes that the user has already
serialized, and inserts them by de-duplicating with a hash table. For
the sake of convenience and efficiency, this class internally stores a
SimpleTypeSerializer so that it can accept unserialized records. The
same is not true of ContinuationRecordBuilder. The user is required to
create their own instance of ContinuationRecordBuilder.
Differential Revision: https://reviews.llvm.org/D40518
llvm-svn: 319198
2017-11-28 19:33:17 +01:00
|
|
|
void insert(uint32_t Offset, ArrayRef<uint8_t> Bytes) {
|
|
|
|
Data.insert(Data.begin() + Offset, Bytes.begin(), Bytes.end());
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:48:37 +01:00
|
|
|
Error readLongestContiguousChunk(uint32_t Offset,
|
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
|
|
|
if (auto EC = checkOffsetForWrite(Offset, 1))
|
|
|
|
return EC;
|
|
|
|
|
|
|
|
Buffer = makeArrayRef(Data).slice(Offset);
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getLength() override { return Data.size(); }
|
|
|
|
|
|
|
|
Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Buffer) override {
|
|
|
|
if (Buffer.empty())
|
|
|
|
return Error::success();
|
|
|
|
|
|
|
|
// This is well-defined for any case except where offset is strictly
|
|
|
|
// greater than the current length. If offset is equal to the current
|
|
|
|
// length, we can still grow. If offset is beyond the current length, we
|
|
|
|
// would have to decide how to deal with the intermediate uninitialized
|
|
|
|
// bytes. So we punt on that case for simplicity and just say it's an
|
|
|
|
// error.
|
|
|
|
if (Offset > getLength())
|
|
|
|
return make_error<BinaryStreamError>(stream_error_code::invalid_offset);
|
|
|
|
|
|
|
|
uint32_t RequiredSize = Offset + Buffer.size();
|
|
|
|
if (RequiredSize > Data.size())
|
|
|
|
Data.resize(RequiredSize);
|
|
|
|
|
|
|
|
::memcpy(Data.data() + Offset, Buffer.data(), Buffer.size());
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
Error commit() override { return Error::success(); }
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// Return the properties of this stream.
|
2017-11-27 19:48:37 +01:00
|
|
|
virtual BinaryStreamFlags getFlags() const override {
|
|
|
|
return BSF_Write | BSF_Append;
|
|
|
|
}
|
|
|
|
|
|
|
|
MutableArrayRef<uint8_t> data() { return Data; }
|
|
|
|
};
|
|
|
|
|
2018-05-01 17:54:18 +02:00
|
|
|
/// An implementation of WritableBinaryStream backed by an llvm
|
2017-02-27 23:11:43 +01:00
|
|
|
/// FileOutputBuffer.
|
|
|
|
class FileBufferByteStream : public WritableBinaryStream {
|
2016-07-28 21:12:28 +02:00
|
|
|
private:
|
2017-02-27 23:11:43 +01:00
|
|
|
class StreamImpl : public MutableBinaryByteStream {
|
2016-07-28 21:12:28 +02:00
|
|
|
public:
|
2017-02-28 01:04:07 +01:00
|
|
|
StreamImpl(std::unique_ptr<FileOutputBuffer> Buffer,
|
|
|
|
llvm::support::endianness Endian)
|
|
|
|
: MutableBinaryByteStream(
|
|
|
|
MutableArrayRef<uint8_t>(Buffer->getBufferStart(),
|
|
|
|
Buffer->getBufferEnd()),
|
|
|
|
Endian),
|
2016-07-28 21:12:28 +02:00
|
|
|
FileBuffer(std::move(Buffer)) {}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error commit() override {
|
2016-07-28 21:12:28 +02:00
|
|
|
if (FileBuffer->commit())
|
2017-02-28 18:49:34 +01:00
|
|
|
return make_error<BinaryStreamError>(
|
|
|
|
stream_error_code::filesystem_error);
|
2016-07-28 21:12:28 +02:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
2018-06-27 23:18:15 +02:00
|
|
|
/// Returns a pointer to the start of the buffer.
|
|
|
|
uint8_t *getBufferStart() const { return FileBuffer->getBufferStart(); }
|
|
|
|
|
|
|
|
/// Returns a pointer to the end of the buffer.
|
|
|
|
uint8_t *getBufferEnd() const { return FileBuffer->getBufferEnd(); }
|
|
|
|
|
2016-07-28 21:12:28 +02:00
|
|
|
private:
|
|
|
|
std::unique_ptr<FileOutputBuffer> FileBuffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2017-02-28 01:04:07 +01:00
|
|
|
FileBufferByteStream(std::unique_ptr<FileOutputBuffer> Buffer,
|
|
|
|
llvm::support::endianness Endian)
|
|
|
|
: Impl(std::move(Buffer), Endian) {}
|
|
|
|
|
|
|
|
llvm::support::endianness getEndian() const override {
|
|
|
|
return Impl.getEndian();
|
|
|
|
}
|
2016-07-28 21:12:28 +02:00
|
|
|
|
|
|
|
Error readBytes(uint32_t Offset, uint32_t Size,
|
2017-02-27 23:11:43 +01:00
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
2016-07-28 21:12:28 +02:00
|
|
|
return Impl.readBytes(Offset, Size, Buffer);
|
|
|
|
}
|
2016-11-18 19:00:19 +01:00
|
|
|
|
2016-07-28 21:12:28 +02:00
|
|
|
Error readLongestContiguousChunk(uint32_t Offset,
|
2017-02-27 23:11:43 +01:00
|
|
|
ArrayRef<uint8_t> &Buffer) override {
|
2016-07-28 21:12:28 +02:00
|
|
|
return Impl.readLongestContiguousChunk(Offset, Buffer);
|
|
|
|
}
|
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
uint32_t getLength() override { return Impl.getLength(); }
|
2016-07-28 21:12:28 +02:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error writeBytes(uint32_t Offset, ArrayRef<uint8_t> Data) override {
|
2016-07-28 21:12:28 +02:00
|
|
|
return Impl.writeBytes(Offset, Data);
|
|
|
|
}
|
2016-11-18 19:00:19 +01:00
|
|
|
|
2017-02-27 23:11:43 +01:00
|
|
|
Error commit() override { return Impl.commit(); }
|
2016-04-29 19:22:58 +02:00
|
|
|
|
2018-06-27 23:18:15 +02:00
|
|
|
/// Returns a pointer to the start of the buffer.
|
|
|
|
uint8_t *getBufferStart() const { return Impl.getBufferStart(); }
|
|
|
|
|
|
|
|
/// Returns a pointer to the end of the buffer.
|
|
|
|
uint8_t *getBufferEnd() const { return Impl.getBufferEnd(); }
|
|
|
|
|
2016-04-29 19:22:58 +02:00
|
|
|
private:
|
2016-07-28 21:12:28 +02:00
|
|
|
StreamImpl Impl;
|
2016-04-29 19:22:58 +02:00
|
|
|
};
|
|
|
|
|
2016-05-10 01:11:38 +02:00
|
|
|
} // end namespace llvm
|
|
|
|
|
2021-02-06 06:02:06 +01:00
|
|
|
#endif // LLVM_SUPPORT_BINARYBYTESTREAM_H
|