LLVM API Documentation

MemoryObject.h

Go to the documentation of this file.
00001 //===- MemoryObject.h - Abstract memory interface ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #ifndef MEMORYOBJECT_H
00011 #define MEMORYOBJECT_H
00012 
00013 #include "llvm/Support/DataTypes.h"
00014 
00015 namespace llvm {
00016 
00017 /// MemoryObject - Abstract base class for contiguous addressable memory.
00018 ///   Necessary for cases in which the memory is in another process, in a
00019 ///   file, or on a remote machine.
00020 class MemoryObject {
00021 public:
00022   /// Destructor      - Override as necessary.
00023   virtual ~MemoryObject() {
00024   }
00025   
00026   /// getBase         - Returns the lowest valid address in the region.
00027   ///
00028   /// @result         - The lowest valid address.
00029   virtual uintptr_t getBase() const = 0;
00030   
00031   /// getExtent       - Returns the size of the region in bytes.  (The region is
00032   ///                   contiguous, so the highest valid address of the region 
00033   ///                   is getBase() + getExtent() - 1).
00034   ///
00035   /// @result         - The size of the region.
00036   virtual uintptr_t getExtent() const = 0;
00037   
00038   /// readByte        - Tries to read a single byte from the region.
00039   ///
00040   /// @param address  - The address of the byte, in the same space as getBase().
00041   /// @param ptr      - A pointer to a byte to be filled in.  Must be non-NULL.
00042   /// @result         - 0 if successful; -1 if not.  Failure may be due to a
00043   ///                   bounds violation or an implementation-specific error.
00044   virtual int readByte(uintptr_t address, uint8_t* ptr) const = 0;
00045   
00046   /// readByte        - Tries to read a contiguous range of bytes from the
00047   ///                   region, up to the end of the region.
00048   ///                   You should override this function if there is a quicker
00049   ///                   way than going back and forth with individual bytes.
00050   ///
00051   /// @param address  - The address of the first byte, in the same space as 
00052   ///                   getBase().
00053   /// @param size     - The maximum number of bytes to copy.
00054   /// @param buf      - A pointer to a buffer to be filled in.  Must be non-NULL
00055   ///                   and large enough to hold size bytes.
00056   /// @result         - The number of bytes copied if successful; (uintptr_t)-1
00057   ///                   if not.
00058   ///                   Failure may be due to a bounds violation or an
00059   ///                   implementation-specific error.
00060   virtual uintptr_t readBytes(uintptr_t address,
00061                               uintptr_t size,
00062                               uint8_t* buf) const {
00063     uintptr_t current = address;
00064     uintptr_t limit = getBase() + getExtent();
00065     
00066     while(current - address < size && current < limit) {
00067       if(readByte(current, &buf[(current - address)]))
00068         return (uintptr_t)-1;
00069       
00070       current++;
00071     }
00072     
00073     return current - address;
00074   }
00075 };
00076 
00077 }
00078 
00079 #endif
00080