1 //===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MemoryBuffer interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_MEMORYBUFFER_H
15 #define LLVM_SUPPORT_MEMORYBUFFER_H
17 #include "llvm/Support/DataTypes.h"
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'.
27 const char *BufferStart; // Start of the buffer.
28 const char *BufferEnd; // End of the buffer.
30 /// MustDeleteBuffer - True if we allocated this buffer. If so, the
31 /// destructor must know the delete[] it.
32 bool MustDeleteBuffer;
34 MemoryBuffer() : MustDeleteBuffer(false) {}
35 void init(const char *BufStart, const char *BufEnd);
36 void initCopyOf(const char *BufStart, const char *BufEnd);
38 virtual ~MemoryBuffer();
40 const char *getBufferStart() const { return BufferStart; }
41 const char *getBufferEnd() const { return BufferEnd; }
42 unsigned getBufferSize() const { return BufferEnd-BufferStart; }
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";
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);
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 = "");
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 = "");
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
73 static MemoryBuffer *getNewUninitMemBuffer(unsigned Size,
74 const char *BufferName = "");
76 /// getSTDIN - Read all of stdin into a file buffer, and return it. This
77 /// fails if stdin is empty.
78 static MemoryBuffer *getSTDIN();
81 } // end namespace llvm