<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Sat, Feb 18, 2017 at 5:09 PM, Zachary Turner via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Some background:</div><div><br></div>A while back while working on code to read / write PDB files, I came up with Yet Another Stream Abstraction.  Note that LLVM already has a few.  Off the top of my head, theres:<div><br></div><div>1) `MemoryBuffer` and its associated class hierarchy</div><div>2) `raw_ostream` and it's associated classes.</div><div>3) `DataExtractor` which is used for reading from a StringRef.</div><div><br></div><div>There's probably more, and indivdiual subprojects might have even created their own.</div><div><br></div><div>The reason I couldn't use any of these and needed to invent another is because PDB files are not laid out contiguously in memory.  You can think of it as a file system where there is an MFT that defines the blocks that individual files live on, and then you have to re-sequence the blocks in order to read out a "file".</div><div><br></div><div>To add to the complexity, each "file" inside of here is actually a list of variable length records where records, or even individual fields of records might cross a block boundary and require multiple reads to piece together.  I needed a way to view these streams as being contiguous, and reading data out of them allowing all the magic of broken fields and records, discontiguous records, etc to just disappear behind the interface.  Also, I needed the ability to read and write using a single interface without worrying about the details of the block structure.  None of LLVM's existing abstractions provided convenient mechanisms for writing this kind of data.</div><div><br></div><div>So I came up with the following set of abstractions:</div><div><br></div><div><b>ThinStream</b> - An abstract base class that provides read-only access to data.  The interface is:</div><div>  virtual Error readBytes(uint32_t Offset, uint32_t Size, ArrayRef<uint8_t> &Buffer) const = 0;</div><div><div>  virtual Error readLongestContiguousChunk(<wbr>uint32_t Offset, ArrayRef<uint8_t> &Buffer) const = 0;</div><div>  virtual uint32_t getLength() const = 0;<br></div></div><div><br></div><div>An important distinction between `ThinStream` and existing stream implementations is that API encourages implementations to be <b>zero copy</b>.  Instead of giving it a buffer to write into, it just returns you a slice of the existing buffer.  This makes it <b>very efficient</b>.  Similar to `ArrayRef` / `MutableArrayRef`, I also provide <b>WritableThinStream</b> for cases where your data is not read-only.  This is another area where functionality is provided that was not present in existing abstractions (i.e. writeability).</div><div><b><br></b></div><div>I have several implementations of this class and some abstractions for working with them.  <b>ByteStream</b> and <b>MutableByteStream</b> provide a concrete implementatino where the backing store is an `ArrayRef` or `MutableArrayRef`.  <b>MappedBlockStream</b> (which is PDB specific) provides an implementation that seeks around a file, piecing together blocks from various locations in a PDB file.  When a call to `readBytes` spans a block boundary, it does multiple reads, allocates a contiguous buffer from a `BumpPtrAllocator` and returns a reference to that (subsequent requests for the same offset return from the cached allocation).  There is also <b>FileBufferThinStream</b> which adapts an `llvm::FileOutputBuffer` so you can write to a file system object.  One could easily imagine an implementation that adapts `llvm::MemoryBuffer` so that you could read and write from mmap'ed files.</div><div><br></div><div>But all of these just allow reading and writing raw bytes.  To handle reading and writing semantic data, there are two additional classes.  <b>ThinStreamReader</b> and <b>ThinStreamWriter.</b></div><div><b><br></b></div><div>These accept any subclass of `ThinStream` and maintain an offset and allow you to read integers in any endianness, strings, objects, arrays of objects (both fixed length records and variable length records), and various other things.</div><div><br></div><div>Finally, there are <b>ThinStreamRef</b> and <b>WritableThinStreamRef</b> which you can think of as `ArrayRef` and `MutableArrayRef` for ThinStreams.  They allow slicing, dropping, etc and copy-semantics so that you can easily pass streams around without worrying about reference lifetime issues.</div><div><br></div><div>To re-iterate: <b>When using a ThinStreamReader, copies are the exception, not the rule and for most implementations of ThinStream will never occur.</b>  Suppose you have a struct:</div><div><br></div><div>struct Header {</div><div>  char Magic[48];</div><div>  ulittle16_t A;</div><div>  ulittle16_t B;  <br></div><div>  ulittle64_t C;</div><div>};<br></div><div><br></div><div>To read this using a `ThinStreamReader`, you would write this:</div><div><br></div><div>ThinStreamReader Reader(Stream);</div><div>const Header *H;</div><div>if (auto EC = Reader.readObject(H))</div><div>  return EC;</div><div><br></div><div>and `ThinStreamReader` just reinterpret_casts the underlying bytes to your structure.  The same is true for null terminated strings, arrays of objects, and everything else.  <b>It is up to the user to ensure that reads and writes happen at proper alignments.</b>  (LLVM can still assert though if you do a misaligned read/write).  But when reading and writing records in binary file formats, this is not a new issue.</div><div><br></div><div>The proposal:</div><div>This code has been used in LLVM's PDB library for some time, and I want to move it up to Support.  </div><div><br></div><div>I've discussed this with some people offline.  beanz@ has expressed interest for some work he wants to do on libobject.</div></div></blockquote><div><br></div><div>Out of curiosity, how do we expect this to be useful in libobject?</div><div><br></div><div>Peter</div></div>
</div></div>