MemoryBuffer.h revision 60e6f3d4123a01babeb2c1a0e00d0a2b109008e5
1//===--- MemoryBuffer.h - Memory Buffer 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//  This file defines the MemoryBuffer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15#define LLVM_SUPPORT_MEMORYBUFFER_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/System/DataTypes.h"
19#include <string>
20#include <sys/stat.h>
21
22namespace llvm {
23
24/// MemoryBuffer - This interface provides simple read-only access to a block
25/// of memory, and provides simple methods for reading files and standard input
26/// into a memory buffer.  In addition to basic access to the characters in the
27/// file, this interface guarantees you can read one character past the end of
28/// the file, and that this character will read as '\0'.
29///
30/// The '\0' guarantee is needed to support an optimization -- it's intended to
31/// be more efficient for clients which are reading all the data to stop
32/// reading when they encounter a '\0' than to continually check the file
33/// position to see if it has reached the end of the file.
34class MemoryBuffer {
35  const char *BufferStart; // Start of the buffer.
36  const char *BufferEnd;   // End of the buffer.
37
38  /// MustDeleteBuffer - True if we allocated this buffer.  If so, the
39  /// destructor must know the delete[] it.
40  bool MustDeleteBuffer;
41protected:
42  MemoryBuffer() : MustDeleteBuffer(false) {}
43  void init(const char *BufStart, const char *BufEnd);
44  void initCopyOf(const char *BufStart, const char *BufEnd);
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 MemoryBuffer *getFile(StringRef Filename,
67                               std::string *ErrStr = 0,
68                               int64_t FileSize = -1,
69                               struct stat *FileInfo = 0);
70  static MemoryBuffer *getFile(const char *Filename,
71                               std::string *ErrStr = 0,
72                               int64_t FileSize = -1,
73                               struct stat *FileInfo = 0);
74
75  /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
76  /// that EndPtr[0] must be a null byte and be accessible!
77  static MemoryBuffer *getMemBuffer(StringRef InputData,
78                                    const char *BufferName = "");
79
80  /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
81  /// copying the contents and taking ownership of it.  This has no requirements
82  /// on EndPtr[0].
83  static MemoryBuffer *getMemBufferCopy(StringRef InputData,
84                                        const char *BufferName = "");
85
86  /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
87  /// is completely initialized to zeros.  Note that the caller should
88  /// initialize the memory allocated by this method.  The memory is owned by
89  /// the MemoryBuffer object.
90  static MemoryBuffer *getNewMemBuffer(size_t Size,
91                                       const char *BufferName = "");
92
93  /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
94  /// that is not initialized.  Note that the caller should initialize the
95  /// memory allocated by this method.  The memory is owned by the MemoryBuffer
96  /// object.
97  static MemoryBuffer *getNewUninitMemBuffer(size_t Size,
98                                             StringRef BufferName = "");
99
100  /// getSTDIN - Read all of stdin into a file buffer, and return it.
101  /// If an error occurs, this returns null and fills in *ErrStr with a reason.
102  static MemoryBuffer *getSTDIN(std::string *ErrStr = 0);
103
104
105  /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin
106  /// if the Filename is "-".  If an error occurs, this returns null and fills
107  /// in *ErrStr with a reason.
108  static MemoryBuffer *getFileOrSTDIN(StringRef Filename,
109                                      std::string *ErrStr = 0,
110                                      int64_t FileSize = -1,
111                                      struct stat *FileInfo = 0);
112  static MemoryBuffer *getFileOrSTDIN(const char *Filename,
113                                      std::string *ErrStr = 0,
114                                      int64_t FileSize = -1,
115                                      struct stat *FileInfo = 0);
116};
117
118} // end namespace llvm
119
120#endif
121