+++ /dev/null
-//===- llvm/System/MappedFile.h - MappedFile OS Concept ---------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file declares the llvm::sys::MappedFile class.
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_SYSTEM_MAPPEDFILE_H
-#define LLVM_SYSTEM_MAPPEDFILE_H
-
-#include "llvm/System/Path.h"
-
-namespace llvm {
-namespace sys {
-
- /// Forward declare a class used for holding platform specific information
- /// that needs to be
- struct MappedFileInfo;
-
- /// This class provides an abstraction for a memory mapped file in the
- /// operating system's filesystem. It provides platform independent operations
- /// for mapping a file into memory for read access.
- class MappedFile {
- sys::PathWithStatus Path; ///< Path to the file.
- void *BasePtr; ///< Pointer to the base memory address
- mutable MappedFileInfo *MapInfo; ///< Platform specific info for the mapping
-
- MappedFile& operator=(const MappedFile &that); // DO NOT IMPLEMENT
- MappedFile(const MappedFile &that); // DO NOT IMPLEMENT
- public:
- MappedFile() : BasePtr(0), MapInfo(0) {}
-
- /// Destruct a MappedFile and release all memory associated with it.
- ~MappedFile() { close(); }
-
- public: // Accessors
-
- /// This function determines if the file is currently mapped or not.
- bool isMapped() const { return BasePtr != 0; }
-
- /// getBase - Returns a const void* pointer to the base address of the file
- /// mapping. This is the memory address of the first byte in the file.
- const void *getBase() const { return BasePtr; }
-
- /// This function returns a reference to the sys::Path object kept by the
- /// MappedFile object. This contains the path to the file that is or
- /// will be mapped.
- const sys::PathWithStatus &path() const { return Path; }
-
- /// This function returns the number of bytes in the file.
- size_t size() const;
-
- public: // Mutators
-
- /// Open a file to be mapped and get its size but don't map it yet. Return
- /// true on error.
- bool open(const sys::Path &P, std::string *ErrMsg = 0) {
- Path = P;
- return initialize(ErrMsg);
- }
-
- /// unmap - Remove the mapped file from memory.
- void unmap();
-
- /// map - Reserve space for the file, map it into memory, and return a
- /// pointer to it. This returns the base memory address of the mapped file
- /// or 0 if an error occurred.
- const void *map(std::string* ErrMsg = 0);
-
- void close() { if (MapInfo) terminate(); }
-
- private:
- bool initialize(std::string *ErrMsg);
- void terminate();
- };
-} // end namespace sys
-} // end namespace llvm
-
-#endif
+++ /dev/null
-//===- MappedFile.cpp - MappedFile Support ----------------------*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file implements the mapped file concept.
-//
-//===----------------------------------------------------------------------===//
-
-#include "llvm/System/MappedFile.h"
-#include "llvm/Config/config.h"
-
-namespace llvm {
-using namespace sys;
-
-//===----------------------------------------------------------------------===//
-//=== WARNING: Implementation here must contain only TRULY operating system
-//=== independent code.
-//===----------------------------------------------------------------------===//
-
-}
-
-// Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
-#include "Unix/MappedFile.inc"
-#endif
-#ifdef LLVM_ON_WIN32
-#include "Win32/MappedFile.inc"
-#endif
-
+++ /dev/null
-//===- Unix/MappedFile.inc - Unix MappedFile Implementation -----*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file provides the generic Unix implementation of the MappedFile concept.
-//
-//===----------------------------------------------------------------------===//
-
-#include "Unix.h"
-#include "llvm/System/Process.h"
-
-#ifdef HAVE_FCNTL_H
-#include <fcntl.h>
-#endif
-
-#ifdef HAVE_SYS_MMAN_H
-#include <sys/mman.h>
-#endif
-
-#ifdef HAVE_SYS_STAT_H
-#include <sys/stat.h>
-#endif
-
-using namespace llvm;
-using namespace sys;
-
-namespace llvm {
- namespace sys {
- struct MappedFileInfo {
- int FD;
- off_t Size;
- };
- }
-}
-
-bool MappedFile::initialize(std::string* ErrMsg) {
- int FD = ::open(Path.c_str(), O_RDONLY);
- if (FD < 0) {
- MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
- return true;
- }
- const FileStatus *Status = Path.getFileStatus(false, ErrMsg);
- if (!Status) {
- ::close(FD);
- return true;
- }
- MapInfo = new MappedFileInfo();
- MapInfo->FD = FD;
- MapInfo->Size = Status->getSize();
- return false;
-}
-
-void MappedFile::terminate() {
- unmap();
- assert(MapInfo && "MappedFile not initialized");
- ::close(MapInfo->FD);
- delete MapInfo;
- MapInfo = 0;
-}
-
-void MappedFile::unmap() {
- assert(MapInfo && "MappedFile not initialized");
- if (!isMapped()) return;
-
- ::munmap(BasePtr, MapInfo->Size);
- BasePtr = 0; // Mark this as non-mapped.
-}
-
-const void* MappedFile::map(std::string* ErrMsg) {
- assert(MapInfo && "MappedFile not initialized");
- if (isMapped()) return BasePtr;
-
- int flags = MAP_PRIVATE;
-#ifdef MAP_FILE
- flags |= MAP_FILE;
-#endif
- size_t PageSize = Process::GetPageSize();
- size_t map_size = ((MapInfo->Size / PageSize)+1) * PageSize;
-
- BasePtr = ::mmap(0, map_size, PROT_READ, flags, MapInfo->FD, 0);
- if (BasePtr == MAP_FAILED) {
- MakeErrMsg(ErrMsg, "Can't map file:" + Path.toString());
- return 0;
- }
- return BasePtr;
-}
-
-size_t MappedFile::size() const {
- assert(MapInfo && "MappedFile not initialized");
- return MapInfo->Size;
-}
-
+++ /dev/null
-//===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===//
-//
-// The LLVM Compiler Infrastructure
-//
-// This file is distributed under the University of Illinois Open Source
-// License. See LICENSE.TXT for details.
-//
-//===----------------------------------------------------------------------===//
-//
-// This file provides the Win32 implementation of the MappedFile concept.
-//
-//===----------------------------------------------------------------------===//
-
-//===----------------------------------------------------------------------===//
-//=== WARNING: Implementation here must contain only Win32 code.
-//===----------------------------------------------------------------------===//
-
-#include "Win32.h"
-#include "llvm/System/Process.h"
-
-namespace llvm {
-using namespace sys;
-
-struct sys::MappedFileInfo {
- HANDLE hFile;
- HANDLE hMapping;
- size_t size;
-};
-
-bool MappedFile::initialize(std::string* ErrMsg) {
- assert(!MapInfo);
- MapInfo = new MappedFileInfo;
- MapInfo->hFile = INVALID_HANDLE_VALUE;
- MapInfo->hMapping = NULL;
-
- MapInfo->hFile = CreateFile(Path.c_str(), GENERIC_READ, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- if (MapInfo->hFile == INVALID_HANDLE_VALUE) {
- delete MapInfo;
- MapInfo = NULL;
- return MakeErrMsg(ErrMsg,
- std::string("Can't open file: ") + Path.toString());
- }
-
- LARGE_INTEGER size;
- if (!GetFileSizeEx(MapInfo->hFile, &size) ||
- (MapInfo->size = size_t(size.QuadPart), MapInfo->size != size.QuadPart)) {
- CloseHandle(MapInfo->hFile);
- delete MapInfo;
- MapInfo = NULL;
- return MakeErrMsg(ErrMsg,
- std::string("Can't get size of file: ") + Path.toString());
- }
-
- return false;
-}
-
-void MappedFile::terminate() {
- unmap();
- if (MapInfo->hFile != INVALID_HANDLE_VALUE)
- CloseHandle(MapInfo->hFile);
- delete MapInfo;
- MapInfo = NULL;
-}
-
-void MappedFile::unmap() {
- assert(MapInfo && "MappedFile not initialized");
- if (isMapped()) {
- UnmapViewOfFile(BasePtr);
- BasePtr = NULL;
- }
- if (MapInfo->hMapping != INVALID_HANDLE_VALUE) {
- CloseHandle(MapInfo->hMapping);
- MapInfo->hMapping = NULL;
- }
-}
-
-const void* MappedFile::map(std::string* ErrMsg) {
- if (!isMapped()) {
- MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, PAGE_READONLY,
- 0, 0, NULL);
- if (MapInfo->hMapping == NULL) {
- MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
- return 0;
- }
-
- BasePtr = MapViewOfFileEx(MapInfo->hMapping, FILE_MAP_READ, 0, 0, 0, NULL);
- if (BasePtr == NULL) {
- CloseHandle(MapInfo->hMapping);
- MapInfo->hMapping = NULL;
- MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
- return 0;
- }
- }
- return BasePtr;
-}
-
-size_t MappedFile::size() const {
- assert(MapInfo && "MappedFile not initialized");
- return MapInfo->size;
-}
-
-}
-