Home | History | Annotate | Download | only in Support
      1 //===- StreamableMemoryObject.h - Streamable data interface -----*- 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 
     11 #ifndef LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
     12 #define LLVM_SUPPORT_STREAMABLEMEMORYOBJECT_H
     13 
     14 #include "llvm/ADT/OwningPtr.h"
     15 #include "llvm/Support/Compiler.h"
     16 #include "llvm/Support/DataStream.h"
     17 #include "llvm/Support/MemoryObject.h"
     18 #include <vector>
     19 
     20 namespace llvm {
     21 
     22 /// StreamableMemoryObject - Interface to data which might be streamed.
     23 /// Streamability has 2 important implications/restrictions. First, the data
     24 /// might not yet exist in memory when the request is made. This just means
     25 /// that readByte/readBytes might have to block or do some work to get it.
     26 /// More significantly, the exact size of the object might not be known until
     27 /// it has all been fetched. This means that to return the right result,
     28 /// getExtent must also wait for all the data to arrive; therefore it should
     29 /// not be called on objects which are actually streamed (this would defeat
     30 /// the purpose of streaming). Instead, isValidAddress and isObjectEnd can be
     31 /// used to test addresses without knowing the exact size of the stream.
     32 /// Finally, getPointer can be used instead of readBytes to avoid extra copying.
     33 class StreamableMemoryObject : public MemoryObject {
     34  public:
     35   /// Destructor      - Override as necessary.
     36   virtual ~StreamableMemoryObject();
     37 
     38   /// getBase         - Returns the lowest valid address in the region.
     39   ///
     40   /// @result         - The lowest valid address.
     41   virtual uint64_t getBase() const = 0;
     42 
     43   /// getExtent       - Returns the size of the region in bytes.  (The region is
     44   ///                   contiguous, so the highest valid address of the region
     45   ///                   is getBase() + getExtent() - 1).
     46   ///                   May block until all bytes in the stream have been read
     47   ///
     48   /// @result         - The size of the region.
     49   virtual uint64_t getExtent() const = 0;
     50 
     51   /// readByte        - Tries to read a single byte from the region.
     52   ///                   May block until (address - base) bytes have been read
     53   /// @param address  - The address of the byte, in the same space as getBase().
     54   /// @param ptr      - A pointer to a byte to be filled in.  Must be non-NULL.
     55   /// @result         - 0 if successful; -1 if not.  Failure may be due to a
     56   ///                   bounds violation or an implementation-specific error.
     57   virtual int readByte(uint64_t address, uint8_t* ptr) const = 0;
     58 
     59   /// readBytes       - Tries to read a contiguous range of bytes from the
     60   ///                   region, up to the end of the region.
     61   ///                   May block until (address - base + size) bytes have
     62   ///                   been read. Additionally, StreamableMemoryObjects will
     63   ///                   not do partial reads - if size bytes cannot be read,
     64   ///                   readBytes will fail.
     65   ///
     66   /// @param address  - The address of the first byte, in the same space as
     67   ///                   getBase().
     68   /// @param size     - The maximum number of bytes to copy.
     69   /// @param buf      - A pointer to a buffer to be filled in.  Must be non-NULL
     70   ///                   and large enough to hold size bytes.
     71   /// @param copied   - A pointer to a nunber that is filled in with the number
     72   ///                   of bytes actually read.  May be NULL.
     73   /// @result         - 0 if successful; -1 if not.  Failure may be due to a
     74   ///                   bounds violation or an implementation-specific error.
     75   virtual int readBytes(uint64_t address,
     76                         uint64_t size,
     77                         uint8_t* buf,
     78                         uint64_t* copied) const = 0;
     79 
     80   /// getPointer  - Ensures that the requested data is in memory, and returns
     81   ///               A pointer to it. More efficient than using readBytes if the
     82   ///               data is already in memory.
     83   ///               May block until (address - base + size) bytes have been read
     84   /// @param address - address of the byte, in the same space as getBase()
     85   /// @param size    - amount of data that must be available on return
     86   /// @result        - valid pointer to the requested data
     87   virtual const uint8_t *getPointer(uint64_t address, uint64_t size) const = 0;
     88 
     89   /// isValidAddress - Returns true if the address is within the object
     90   ///                  (i.e. between base and base + extent - 1 inclusive)
     91   ///                  May block until (address - base) bytes have been read
     92   /// @param address - address of the byte, in the same space as getBase()
     93   /// @result        - true if the address may be read with readByte()
     94   virtual bool isValidAddress(uint64_t address) const = 0;
     95 
     96   /// isObjectEnd    - Returns true if the address is one past the end of the
     97   ///                  object (i.e. if it is equal to base + extent)
     98   ///                  May block until (address - base) bytes have been read
     99   /// @param address - address of the byte, in the same space as getBase()
    100   /// @result        - true if the address is equal to base + extent
    101   virtual bool isObjectEnd(uint64_t address) const = 0;
    102 };
    103 
    104 /// StreamingMemoryObject - interface to data which is actually streamed from
    105 /// a DataStreamer. In addition to inherited members, it has the
    106 /// dropLeadingBytes and setKnownObjectSize methods which are not applicable
    107 /// to non-streamed objects.
    108 class StreamingMemoryObject : public StreamableMemoryObject {
    109 public:
    110   StreamingMemoryObject(DataStreamer *streamer);
    111   virtual uint64_t getBase() const LLVM_OVERRIDE { return 0; }
    112   virtual uint64_t getExtent() const LLVM_OVERRIDE;
    113   virtual int readByte(uint64_t address, uint8_t* ptr) const LLVM_OVERRIDE;
    114   virtual int readBytes(uint64_t address,
    115                         uint64_t size,
    116                         uint8_t* buf,
    117                         uint64_t* copied) const LLVM_OVERRIDE;
    118   virtual const uint8_t *getPointer(uint64_t address,
    119                                     uint64_t size) const LLVM_OVERRIDE {
    120     // This could be fixed by ensuring the bytes are fetched and making a copy,
    121     // requiring that the bitcode size be known, or otherwise ensuring that
    122     // the memory doesn't go away/get reallocated, but it's
    123     // not currently necessary. Users that need the pointer don't stream.
    124     assert(0 && "getPointer in streaming memory objects not allowed");
    125     return NULL;
    126   }
    127   virtual bool isValidAddress(uint64_t address) const LLVM_OVERRIDE;
    128   virtual bool isObjectEnd(uint64_t address) const LLVM_OVERRIDE;
    129 
    130   /// Drop s bytes from the front of the stream, pushing the positions of the
    131   /// remaining bytes down by s. This is used to skip past the bitcode header,
    132   /// since we don't know a priori if it's present, and we can't put bytes
    133   /// back into the stream once we've read them.
    134   bool dropLeadingBytes(size_t s);
    135 
    136   /// If the data object size is known in advance, many of the operations can
    137   /// be made more efficient, so this method should be called before reading
    138   /// starts (although it can be called anytime).
    139   void setKnownObjectSize(size_t size);
    140 
    141 private:
    142   const static uint32_t kChunkSize = 4096 * 4;
    143   mutable std::vector<unsigned char> Bytes;
    144   OwningPtr<DataStreamer> Streamer;
    145   mutable size_t BytesRead;   // Bytes read from stream
    146   size_t BytesSkipped;// Bytes skipped at start of stream (e.g. wrapper/header)
    147   mutable size_t ObjectSize; // 0 if unknown, set if wrapper seen or EOF reached
    148   mutable bool EOFReached;
    149 
    150   // Fetch enough bytes such that Pos can be read or EOF is reached
    151   // (i.e. BytesRead > Pos). Return true if Pos can be read.
    152   // Unlike most of the functions in BitcodeReader, returns true on success.
    153   // Most of the requests will be small, but we fetch at kChunkSize bytes
    154   // at a time to avoid making too many potentially expensive GetBytes calls
    155   bool fetchToPos(size_t Pos) const {
    156     if (EOFReached) return Pos < ObjectSize;
    157     while (Pos >= BytesRead) {
    158       Bytes.resize(BytesRead + BytesSkipped + kChunkSize);
    159       size_t bytes = Streamer->GetBytes(&Bytes[BytesRead + BytesSkipped],
    160                                         kChunkSize);
    161       BytesRead += bytes;
    162       if (bytes < kChunkSize) {
    163         if (ObjectSize && BytesRead < Pos)
    164           assert(0 && "Unexpected short read fetching bitcode");
    165         if (BytesRead <= Pos) { // reached EOF/ran out of bytes
    166           ObjectSize = BytesRead;
    167           EOFReached = true;
    168           return false;
    169         }
    170       }
    171     }
    172     return true;
    173   }
    174 
    175   StreamingMemoryObject(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
    176   void operator=(const StreamingMemoryObject&) LLVM_DELETED_FUNCTION;
    177 };
    178 
    179 StreamableMemoryObject *getNonStreamedMemoryObject(
    180     const unsigned char *Start, const unsigned char *End);
    181 
    182 }
    183 #endif  // STREAMABLEMEMORYOBJECT_H_
    184