MemoryBuffer.h revision 221345
1185923Simp//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2185923Simp//
3185923Simp//                     The LLVM Compiler Infrastructure
4185923Simp//
5208737Sjmallett// This file is distributed under the University of Illinois Open Source
6208737Sjmallett// License. See LICENSE.TXT for details.
7185923Simp//
8208737Sjmallett//===----------------------------------------------------------------------===//
9185923Simp//
10185923Simp//  This file defines the MemoryBuffer interface.
11208737Sjmallett//
12208737Sjmallett//===----------------------------------------------------------------------===//
13208737Sjmallett
14208737Sjmallett#ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15208737Sjmallett#define LLVM_SUPPORT_MEMORYBUFFER_H
16208737Sjmallett
17208737Sjmallett#include "llvm/ADT/StringRef.h"
18208737Sjmallett#include "llvm/Support/DataTypes.h"
19208737Sjmallett
20204548Simpnamespace llvm {
21204548Simp
22201851Simpclass error_code;
23201851Simptemplate<class T> class OwningPtr;
24201851Simp
25201851Simp/// MemoryBuffer - This interface provides simple read-only access to a block
26201851Simp/// of memory, and provides simple methods for reading files and standard input
27201851Simp/// into a memory buffer.  In addition to basic access to the characters in the
28201851Simp/// file, this interface guarantees you can read one character past the end of
29201851Simp/// the file, and that this character will read as '\0'.
30201851Simp///
31185923Simp/// The '\0' guarantee is needed to support an optimization -- it's intended to
32201851Simp/// be more efficient for clients which are reading all the data to stop
33185923Simp/// reading when they encounter a '\0' than to continually check the file
34185923Simp/// position to see if it has reached the end of the file.
35185923Simpclass MemoryBuffer {
36185923Simp  const char *BufferStart; // Start of the buffer.
37201851Simp  const char *BufferEnd;   // End of the buffer.
38201851Simp
39201851Simp  MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
40  MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
41protected:
42  MemoryBuffer() {}
43  void init(const char *BufStart, const char *BufEnd,
44            bool RequiresNullTerminator);
45public:
46  virtual ~MemoryBuffer();
47
48  const char *getBufferStart() const { return BufferStart; }
49  const char *getBufferEnd() const   { return BufferEnd; }
50  size_t getBufferSize() const { return BufferEnd-BufferStart; }
51
52  StringRef getBuffer() const {
53    return StringRef(BufferStart, getBufferSize());
54  }
55
56  /// getBufferIdentifier - Return an identifier for this buffer, typically the
57  /// filename it was read from.
58  virtual const char *getBufferIdentifier() const {
59    return "Unknown buffer";
60  }
61
62  /// getFile - Open the specified file as a MemoryBuffer, returning a new
63  /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
64  /// specified, this means that the client knows that the file exists and that
65  /// it has the specified size.
66  static error_code getFile(StringRef Filename, OwningPtr<MemoryBuffer> &result,
67                            int64_t FileSize = -1,
68                            bool RequiresNullTerminator = true);
69  static error_code getFile(const char *Filename,
70                            OwningPtr<MemoryBuffer> &result,
71                            int64_t FileSize = -1,
72                            bool RequiresNullTerminator = true);
73
74  /// getOpenFile - Given an already-open file descriptor, read the file and
75  /// return a MemoryBuffer.
76  static error_code getOpenFile(int FD, const char *Filename,
77                                OwningPtr<MemoryBuffer> &result,
78                                size_t FileSize = -1,
79                                size_t MapSize = -1,
80                                off_t Offset = 0,
81                                bool RequiresNullTerminator = true);
82
83  /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
84  /// that InputData must be null terminated.
85  static MemoryBuffer *getMemBuffer(StringRef InputData,
86                                    StringRef BufferName = "",
87                                    bool RequiresNullTerminator = true);
88
89  /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
90  /// copying the contents and taking ownership of it.  InputData does not
91  /// have to be null terminated.
92  static MemoryBuffer *getMemBufferCopy(StringRef InputData,
93                                        StringRef BufferName = "");
94
95  /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
96  /// is completely initialized to zeros.  Note that the caller should
97  /// initialize the memory allocated by this method.  The memory is owned by
98  /// the MemoryBuffer object.
99  static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
100
101  /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
102  /// that is not initialized.  Note that the caller should initialize the
103  /// memory allocated by this method.  The memory is owned by the MemoryBuffer
104  /// object.
105  static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
106                                             StringRef BufferName = "");
107
108  /// getSTDIN - Read all of stdin into a file buffer, and return it.
109  /// If an error occurs, this returns null and sets ec.
110  static error_code getSTDIN(OwningPtr<MemoryBuffer> &result);
111
112
113  /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
114  /// if the Filename is "-".  If an error occurs, this returns null and sets
115  /// ec.
116  static error_code getFileOrSTDIN(StringRef Filename,
117                                   OwningPtr<MemoryBuffer> &result,
118                                   int64_t FileSize = -1);
119  static error_code getFileOrSTDIN(const char *Filename,
120                                   OwningPtr<MemoryBuffer> &result,
121                                   int64_t FileSize = -1);
122
123
124  //===--------------------------------------------------------------------===//
125  // Provided for performance analysis.
126  //===--------------------------------------------------------------------===//
127
128  /// The kind of memory backing used to support the MemoryBuffer.
129  enum BufferKind {
130    MemoryBuffer_Malloc,
131    MemoryBuffer_MMap
132  };
133
134  /// Return information on the memory mechanism used to support the
135  /// MemoryBuffer.
136  virtual BufferKind getBufferKind() const = 0;
137};
138
139} // end namespace llvm
140
141#endif
142