1193323Sed//===--- MemoryBuffer.h - Memory Buffer Interface ---------------*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the MemoryBuffer interface. 11193323Sed// 12193323Sed//===----------------------------------------------------------------------===// 13193323Sed 14193323Sed#ifndef LLVM_SUPPORT_MEMORYBUFFER_H 15193323Sed#define LLVM_SUPPORT_MEMORYBUFFER_H 16193323Sed 17263509Sdim#include "llvm/ADT/Twine.h" 18252723Sdim#include "llvm/Support/CBindingWrapping.h" 19245431Sdim#include "llvm/Support/Compiler.h" 20218893Sdim#include "llvm/Support/DataTypes.h" 21252723Sdim#include "llvm-c/Core.h" 22193323Sed 23193323Sednamespace llvm { 24193323Sed 25218893Sdimclass error_code; 26218893Sdimtemplate<class T> class OwningPtr; 27218893Sdim 28193323Sed/// MemoryBuffer - This interface provides simple read-only access to a block 29193323Sed/// of memory, and provides simple methods for reading files and standard input 30193323Sed/// into a memory buffer. In addition to basic access to the characters in the 31193323Sed/// file, this interface guarantees you can read one character past the end of 32199511Srdivacky/// the file, and that this character will read as '\0'. 33210299Sed/// 34210299Sed/// The '\0' guarantee is needed to support an optimization -- it's intended to 35210299Sed/// be more efficient for clients which are reading all the data to stop 36210299Sed/// reading when they encounter a '\0' than to continually check the file 37210299Sed/// position to see if it has reached the end of the file. 38193323Sedclass MemoryBuffer { 39193323Sed const char *BufferStart; // Start of the buffer. 40193323Sed const char *BufferEnd; // End of the buffer. 41193323Sed 42245431Sdim MemoryBuffer(const MemoryBuffer &) LLVM_DELETED_FUNCTION; 43245431Sdim MemoryBuffer &operator=(const MemoryBuffer &) LLVM_DELETED_FUNCTION; 44193323Sedprotected: 45210299Sed MemoryBuffer() {} 46221345Sdim void init(const char *BufStart, const char *BufEnd, 47221345Sdim bool RequiresNullTerminator); 48193323Sedpublic: 49193323Sed virtual ~MemoryBuffer(); 50193323Sed 51193323Sed const char *getBufferStart() const { return BufferStart; } 52193323Sed const char *getBufferEnd() const { return BufferEnd; } 53193323Sed size_t getBufferSize() const { return BufferEnd-BufferStart; } 54193323Sed 55218893Sdim StringRef getBuffer() const { 56218893Sdim return StringRef(BufferStart, getBufferSize()); 57198090Srdivacky } 58198090Srdivacky 59193323Sed /// getBufferIdentifier - Return an identifier for this buffer, typically the 60193323Sed /// filename it was read from. 61193323Sed virtual const char *getBufferIdentifier() const { 62193323Sed return "Unknown buffer"; 63193323Sed } 64193323Sed 65193323Sed /// getFile - Open the specified file as a MemoryBuffer, returning a new 66193323Sed /// MemoryBuffer if successful, otherwise returning null. If FileSize is 67193323Sed /// specified, this means that the client knows that the file exists and that 68193323Sed /// it has the specified size. 69263509Sdim static error_code getFile(Twine Filename, OwningPtr<MemoryBuffer> &result, 70221345Sdim int64_t FileSize = -1, 71221345Sdim bool RequiresNullTerminator = true); 72193323Sed 73263509Sdim /// Given an already-open file descriptor, map some slice of it into a 74263509Sdim /// MemoryBuffer. The slice is specified by an \p Offset and \p MapSize. 75263509Sdim /// Since this is in the middle of a file, the buffer is not null terminated. 76263509Sdim static error_code getOpenFileSlice(int FD, const char *Filename, 77263509Sdim OwningPtr<MemoryBuffer> &Result, 78263509Sdim uint64_t MapSize, int64_t Offset); 79263509Sdim 80263509Sdim /// Given an already-open file descriptor, read the file and return a 81263509Sdim /// MemoryBuffer. 82218893Sdim static error_code getOpenFile(int FD, const char *Filename, 83263509Sdim OwningPtr<MemoryBuffer> &Result, 84263509Sdim uint64_t FileSize, 85221345Sdim bool RequiresNullTerminator = true); 86218893Sdim 87193323Sed /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note 88223017Sdim /// that InputData must be null terminated if RequiresNullTerminator is true. 89206274Srdivacky static MemoryBuffer *getMemBuffer(StringRef InputData, 90221345Sdim StringRef BufferName = "", 91221345Sdim bool RequiresNullTerminator = true); 92193323Sed 93193323Sed /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, 94218893Sdim /// copying the contents and taking ownership of it. InputData does not 95218893Sdim /// have to be null terminated. 96206274Srdivacky static MemoryBuffer *getMemBufferCopy(StringRef InputData, 97210299Sed StringRef BufferName = ""); 98193323Sed 99193323Sed /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that 100193323Sed /// is completely initialized to zeros. Note that the caller should 101193323Sed /// initialize the memory allocated by this method. The memory is owned by 102193323Sed /// the MemoryBuffer object. 103210299Sed static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = ""); 104193323Sed 105193323Sed /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size 106193323Sed /// that is not initialized. Note that the caller should initialize the 107193323Sed /// memory allocated by this method. The memory is owned by the MemoryBuffer 108193323Sed /// object. 109193323Sed static MemoryBuffer *getNewUninitMemBuffer(size_t Size, 110199481Srdivacky StringRef BufferName = ""); 111193323Sed 112199481Srdivacky /// getSTDIN - Read all of stdin into a file buffer, and return it. 113218893Sdim /// If an error occurs, this returns null and sets ec. 114218893Sdim static error_code getSTDIN(OwningPtr<MemoryBuffer> &result); 115193323Sed 116193323Sed 117193323Sed /// getFileOrSTDIN - Open the specified file as a MemoryBuffer, or open stdin 118218893Sdim /// if the Filename is "-". If an error occurs, this returns null and sets 119218893Sdim /// ec. 120218893Sdim static error_code getFileOrSTDIN(StringRef Filename, 121218893Sdim OwningPtr<MemoryBuffer> &result, 122218893Sdim int64_t FileSize = -1); 123263509Sdim 124221345Sdim //===--------------------------------------------------------------------===// 125221345Sdim // Provided for performance analysis. 126221345Sdim //===--------------------------------------------------------------------===// 127221345Sdim 128221345Sdim /// The kind of memory backing used to support the MemoryBuffer. 129221345Sdim enum BufferKind { 130221345Sdim MemoryBuffer_Malloc, 131221345Sdim MemoryBuffer_MMap 132221345Sdim }; 133221345Sdim 134221345Sdim /// Return information on the memory mechanism used to support the 135221345Sdim /// MemoryBuffer. 136221345Sdim virtual BufferKind getBufferKind() const = 0; 137193323Sed}; 138193323Sed 139252723Sdim// Create wrappers for C Binding types (see CBindingWrapping.h). 140252723SdimDEFINE_SIMPLE_CONVERSION_FUNCTIONS(MemoryBuffer, LLVMMemoryBufferRef) 141252723Sdim 142193323Sed} // end namespace llvm 143193323Sed 144193323Sed#endif 145