Add a new memorybuffer class, to unify all the file reading code in the system
[oota-llvm.git] / include / llvm / Support / MemoryBuffer.h
1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source 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/Support/DataTypes.h"
18
19 namespace llvm {
20
21 /// MemoryBuffer - This interface provides simple read-only access to a block
22 /// of memory, and provides simple methods for reading files and standard input
23 /// into a memory buffer.  In addition to basic access to the characters in the
24 /// file, this interface guarantees you can read one character past the end of
25 /// the file, and that this character will read as '\0'.
26 class MemoryBuffer {
27   const char *BufferStart; // Start of the buffer.
28   const char *BufferEnd;   // End of the buffer.
29
30   /// MustDeleteBuffer - True if we allocated this buffer.  If so, the
31   /// destructor must know the delete[] it.
32   bool MustDeleteBuffer;
33 protected:
34   MemoryBuffer() : MustDeleteBuffer(false) {}
35   void init(const char *BufStart, const char *BufEnd);
36   void initCopyOf(const char *BufStart, const char *BufEnd);
37 public:
38   virtual ~MemoryBuffer();
39   
40   const char *getBufferStart() const { return BufferStart; }
41   const char *getBufferEnd() const   { return BufferEnd; }
42   unsigned getBufferSize() const { return BufferEnd-BufferStart; }
43   
44   /// getBufferIdentifier - Return an identifier for this buffer, typically the
45   /// filename it was read from.
46   virtual const char *getBufferIdentifier() const {
47     return "Unknown buffer";
48   }
49
50   /// getFile - Open the specified file as a MemoryBuffer, returning a new
51   /// MemoryBuffer if successful, otherwise returning null.  If FileSize is
52   /// specified, this means that the client knows that the file exists and that
53   /// it has the specified size.
54   static MemoryBuffer *getFile(const char *FilenameStart, unsigned FnSize,
55                                int64_t FileSize = -1);
56
57   /// getMemBuffer - Open the specified memory range as a MemoryBuffer.  Note
58   /// that EndPtr[0] must be a null byte and be accessible!
59   static MemoryBuffer *getMemBuffer(const char *StartPtr, const char *EndPtr,
60                                     const char *BufferName = "");
61   
62   /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
63   /// is completely initialized to zeros.  Note that the caller should
64   /// initialize the memory allocated by this method.  The memory is owned by
65   /// the MemoryBuffer object.
66   static MemoryBuffer *getNewMemBuffer(unsigned Size,
67                                        const char *BufferName = "");
68   
69   /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
70   /// that is not initialized.  Note that the caller should initialize the
71   /// memory allocated by this method.  The memory is owned by the MemoryBuffer
72   /// object.
73   static MemoryBuffer *getNewUninitMemBuffer(unsigned Size,
74                                              const char *BufferName = "");
75   
76   /// getSTDIN - Read all of stdin into a file buffer, and return it.  This
77   /// fails if stdin is empty.
78   static MemoryBuffer *getSTDIN();
79 };
80
81 } // end namespace llvm
82
83 #endif