LLVM  4.0.0
StreamRef.h
Go to the documentation of this file.
1 //===- StreamRef.h - A copyable reference to a stream -----------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_DEBUGINFO_MSF_STREAMREF_H
11 #define LLVM_DEBUGINFO_MSF_STREAMREF_H
12 
13 #include "llvm/ADT/ArrayRef.h"
16 #include "llvm/Support/Error.h"
17 #include <algorithm>
18 #include <cstdint>
19 
20 namespace llvm {
21 namespace msf {
22 
23 template <class StreamType, class RefType> class StreamRefBase {
24 public:
25  StreamRefBase() : Stream(nullptr), ViewOffset(0), Length(0) {}
27  : Stream(&Stream), ViewOffset(Offset), Length(Length) {}
28 
29  uint32_t getLength() const { return Length; }
30  const StreamType *getStream() const { return Stream; }
31 
32  RefType drop_front(uint32_t N) const {
33  if (!Stream)
34  return RefType();
35 
36  N = std::min(N, Length);
37  return RefType(*Stream, ViewOffset + N, Length - N);
38  }
39 
40  RefType keep_front(uint32_t N) const {
41  if (!Stream)
42  return RefType();
43  N = std::min(N, Length);
44  return RefType(*Stream, ViewOffset, N);
45  }
46 
47  RefType slice(uint32_t Offset, uint32_t Len) const {
48  return drop_front(Offset).keep_front(Len);
49  }
50 
51  bool operator==(const RefType &Other) const {
52  if (Stream != Other.Stream)
53  return false;
54  if (ViewOffset != Other.ViewOffset)
55  return false;
56  if (Length != Other.Length)
57  return false;
58  return true;
59  }
60 
61 protected:
62  const StreamType *Stream;
65 };
66 
68  : public StreamRefBase<ReadableStream, ReadableStreamRef> {
69 public:
70  ReadableStreamRef() = default;
72  : StreamRefBase(Stream, 0, Stream.getLength()) {}
75  : StreamRefBase(Stream, Offset, Length) {}
76 
77  // Use StreamRef.slice() instead.
79  uint32_t Length) = delete;
80 
82  ArrayRef<uint8_t> &Buffer) const {
83  if (ViewOffset + Offset < Offset)
84  return make_error<MSFError>(msf_error_code::insufficient_buffer);
85  if (Size + Offset > Length)
86  return make_error<MSFError>(msf_error_code::insufficient_buffer);
87  return Stream->readBytes(ViewOffset + Offset, Size, Buffer);
88  }
89 
90  // Given an offset into the stream, read as much as possible without copying
91  // any data.
93  ArrayRef<uint8_t> &Buffer) const {
94  if (Offset >= Length)
95  return make_error<MSFError>(msf_error_code::insufficient_buffer);
96 
97  if (auto EC = Stream->readLongestContiguousChunk(Offset, Buffer))
98  return EC;
99  // This StreamRef might refer to a smaller window over a larger stream. In
100  // that case we will have read out more bytes than we should return, because
101  // we should not read past the end of the current view.
102  uint32_t MaxLength = Length - Offset;
103  if (Buffer.size() > MaxLength)
104  Buffer = Buffer.slice(0, MaxLength);
105  return Error::success();
106  }
107 };
108 
110  : public StreamRefBase<WritableStream, WritableStreamRef> {
111 public:
112  WritableStreamRef() = default;
114  : StreamRefBase(Stream, 0, Stream.getLength()) {}
117  : StreamRefBase(Stream, Offset, Length) {}
118 
119  // Use StreamRef.slice() instead.
121  uint32_t Length) = delete;
122 
124  if (Data.size() + Offset > Length)
125  return make_error<MSFError>(msf_error_code::insufficient_buffer);
126  return Stream->writeBytes(ViewOffset + Offset, Data);
127  }
128 
129  Error commit() const { return Stream->commit(); }
130 };
131 
132 } // end namespace msf
133 } // end namespace llvm
134 
135 #endif // LLVM_DEBUGINFO_MSF_STREAMREF_H
RefType slice(uint32_t Offset, uint32_t Len) const
Definition: StreamRef.h:47
const StreamType * Stream
Definition: StreamRef.h:62
WritableStreamRef(const WritableStream &Stream, uint32_t Offset, uint32_t Length)
Definition: StreamRef.h:115
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:171
RefType keep_front(uint32_t N) const
Definition: StreamRef.h:40
WritableStreamRef(const WritableStream &Stream)
Definition: StreamRef.h:113
ReadableStreamRef(const ReadableStream &Stream)
Definition: StreamRef.h:71
Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const
Definition: StreamRef.h:81
ReadableStreamRef(const ReadableStream &Stream, uint32_t Offset, uint32_t Length)
Definition: StreamRef.h:73
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:141
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with etc Experimental Use value profile to guide fuzzing Number of simultaneous worker processes to run the jobs If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
RefType drop_front(uint32_t N) const
Definition: StreamRef.h:32
uint32_t getLength() const
Definition: StreamRef.h:29
bool operator==(const RefType &Other) const
Definition: StreamRef.h:51
uint32_t Offset
virtual Error commit() const =0
Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const
Definition: StreamRef.h:123
const StreamType * getStream() const
Definition: StreamRef.h:30
static ErrorSuccess success()
Create a success value.
virtual Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef< uint8_t > &Buffer) const =0
StreamRefBase(const StreamType &Stream, uint32_t Offset, uint32_t Length)
Definition: StreamRef.h:26
#define N
Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const
Definition: StreamRef.h:92
Lightweight error class with error context and mandatory checking.
virtual Error readLongestContiguousChunk(uint32_t Offset, ArrayRef< uint8_t > &Buffer) const =0
virtual Error writeBytes(uint32_t Offset, ArrayRef< uint8_t > Data) const =0