f8f29be5a7b354a3be55dbf058bfd60adf967c71
[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   /// Constructor     - Override as necessary.
23   MemoryObject() {
24   }
25   
26   /// getBase         - Returns the lowest valid address in the region.
27   ///
28   /// @result         - The lowest valid address.
29   virtual uint64_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 uint64_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(uint64_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; (uint64_t)-1
57   ///                   if not.
58   ///                   Failure may be due to a bounds violation or an
59   ///                   implementation-specific error.
60   virtual uint64_t readBytes(uint64_t address,
61                              uint64_t size,
62                              uint8_t* buf) const {
63     uint64_t current = address;
64     uint64_t limit = getBase() + getExtent();
65     uint64_t ret = 0;
66     
67     while(current - address < size && current < limit) {
68       if(readByte(current, &buf[(current - address)]))
69         return (uint64_t)-1;
70       
71       ret++;
72     }
73     
74     return ret;
75   }
76 };
77
78 }
79
80 #endif
81