Thanks, Bill!
[oota-llvm.git] / include / llvm / Support / MemoryObject.h
1 //===- MemoryObject.h - Abstract memory 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 #ifndef MEMORYOBJECT_H
11 #define MEMORYOBJECT_H
12
13 #include "llvm/Support/DataTypes.h"
14
15 namespace llvm {
16
17 /// MemoryObject - Abstract base class for contiguous addressable memory.
18 ///   Necessary for cases in which the memory is in another process, in a
19 ///   file, or on a remote machine.
20 class MemoryObject {
21 public:
22   /// Destructor      - Override as necessary.
23   virtual ~MemoryObject() {
24   }
25   
26   /// getBase         - Returns the lowest valid address in the region.
27   ///
28   /// @result         - The lowest valid address.
29   virtual uintptr_t getBase() const = 0;
30   
31   /// getExtent       - Returns the size of the region in bytes.  (The region is
32   ///                   contiguous, so the highest valid address of the region 
33   ///                   is getBase() + getExtent() - 1).
34   ///
35   /// @result         - The size of the region.
36   virtual uintptr_t getExtent() const = 0;
37   
38   /// readByte        - Tries to read a single byte from the region.
39   ///
40   /// @param address  - The address of the byte, in the same space as getBase().
41   /// @param ptr      - A pointer to a byte to be filled in.  Must be non-NULL.
42   /// @result         - 0 if successful; -1 if not.  Failure may be due to a
43   ///                   bounds violation or an implementation-specific error.
44   virtual int readByte(uintptr_t address, uint8_t* ptr) const = 0;
45   
46   /// readByte        - Tries to read a contiguous range of bytes from the
47   ///                   region, up to the end of the region.
48   ///                   You should override this function if there is a quicker
49   ///                   way than going back and forth with individual bytes.
50   ///
51   /// @param address  - The address of the first byte, in the same space as 
52   ///                   getBase().
53   /// @param size     - The maximum number of bytes to copy.
54   /// @param buf      - A pointer to a buffer to be filled in.  Must be non-NULL
55   ///                   and large enough to hold size bytes.
56   /// @result         - The number of bytes copied if successful; (uintptr_t)-1
57   ///                   if not.
58   ///                   Failure may be due to a bounds violation or an
59   ///                   implementation-specific error.
60   virtual uintptr_t readBytes(uintptr_t address,
61                               uintptr_t size,
62                               uint8_t* buf) const {
63     uintptr_t current = address;
64     uintptr_t limit = getBase() + getExtent();
65     
66     while(current - address < size && current < limit) {
67       if(readByte(current, &buf[(current - address)]))
68         return (uintptr_t)-1;
69       
70       current++;
71     }
72     
73     return current - address;
74   }
75 };
76
77 }
78
79 #endif
80