blob: 92dc6660439e43c9596a0d4f5ebc7d9d5b04ec34 [file] [log] [blame]
Chris Lattnerb342b7c2008-04-01 03:10:22 +00001//===- Unix/MappedFile.inc - Unix MappedFile Implementation -----*- C++ -*-===//
Dan Gohmanf17a25c2007-07-18 16:29:46 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file provides the generic Unix implementation of the MappedFile concept.
11//
12//===----------------------------------------------------------------------===//
13
Dan Gohmanf17a25c2007-07-18 16:29:46 +000014#include "Unix.h"
15#include "llvm/System/Process.h"
16
17#ifdef HAVE_FCNTL_H
18#include <fcntl.h>
19#endif
20
21#ifdef HAVE_SYS_MMAN_H
22#include <sys/mman.h>
23#endif
24
25#ifdef HAVE_SYS_STAT_H
26#include <sys/stat.h>
27#endif
28
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000029using namespace llvm;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace sys;
31
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000032namespace llvm {
33 namespace sys {
34 struct MappedFileInfo {
35 int FD;
36 off_t Size;
37 };
38 }
39}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41bool MappedFile::initialize(std::string* ErrMsg) {
Chris Lattnerb342b7c2008-04-01 03:10:22 +000042 int FD = ::open(Path.c_str(), O_RDONLY);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043 if (FD < 0) {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000044 MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 return true;
46 }
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000047 const FileStatus *Status = Path.getFileStatus(false, ErrMsg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048 if (!Status) {
49 ::close(FD);
50 return true;
51 }
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000052 MapInfo = new MappedFileInfo();
53 MapInfo->FD = FD;
54 MapInfo->Size = Status->getSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 return false;
56}
57
58void MappedFile::terminate() {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000059 assert(MapInfo && "MappedFile not initialized");
60 ::close(MapInfo->FD);
61 delete MapInfo;
62 MapInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063}
64
65void MappedFile::unmap() {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000066 assert(MapInfo && "MappedFile not initialized");
67 if (!isMapped()) return;
68
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000069 ::munmap(BasePtr, MapInfo->Size);
70 BasePtr = 0; // Mark this as non-mapped.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071}
72
73void* MappedFile::map(std::string* ErrMsg) {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000074 assert(MapInfo && "MappedFile not initialized");
75 if (isMapped()) return BasePtr;
76
Chris Lattnerb342b7c2008-04-01 03:10:22 +000077 int prot = PROT_READ;
78 int flags = MAP_PRIVATE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079#ifdef MAP_FILE
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000080 flags |= MAP_FILE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081#endif
Chris Lattnerb342b7c2008-04-01 03:10:22 +000082 size_t PageSize = Process::GetPageSize();
83 size_t map_size = ((MapInfo->Size / PageSize)+1) * PageSize;
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000084
85 BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0);
86 if (BasePtr == MAP_FAILED) {
87 MakeErrMsg(ErrMsg, "Can't map file:" + Path.toString());
88 return 0;
89 }
90 return BasePtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091}
92
93size_t MappedFile::size() const {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000094 assert(MapInfo && "MappedFile not initialized");
95 return MapInfo->Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096}
97