blob: dcfd1892f84c72be7be425782626e9f1753ddc26 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===//
2//
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
14//===----------------------------------------------------------------------===//
15//=== WARNING: Implementation here must contain only generic UNIX code that
16//=== is guaranteed to work on *all* UNIX variants.
17//===----------------------------------------------------------------------===//
18
19#include "Unix.h"
20#include "llvm/System/Process.h"
21
22#ifdef HAVE_FCNTL_H
23#include <fcntl.h>
24#endif
25
26#ifdef HAVE_SYS_MMAN_H
27#include <sys/mman.h>
28#endif
29
30#ifdef HAVE_SYS_STAT_H
31#include <sys/stat.h>
32#endif
33
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000034using namespace llvm;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035using namespace sys;
36
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000037namespace llvm {
38 namespace sys {
39 struct MappedFileInfo {
40 int FD;
41 off_t Size;
42 };
43 }
44}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045
46bool MappedFile::initialize(std::string* ErrMsg) {
47 int mode = 0;
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000048 if (Options & READ_ACCESS)
49 if (Options & WRITE_ACCESS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 mode = O_RDWR;
51 else
52 mode = O_RDONLY;
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000053 else if (Options & WRITE_ACCESS)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 mode = O_WRONLY;
55
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000056 int FD = ::open(Path.c_str(), mode);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 if (FD < 0) {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000058 MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 return true;
60 }
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000061 const FileStatus *Status = Path.getFileStatus(false, ErrMsg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 if (!Status) {
63 ::close(FD);
64 return true;
65 }
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000066 MapInfo = new MappedFileInfo();
67 MapInfo->FD = FD;
68 MapInfo->Size = Status->getSize();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069 return false;
70}
71
72void MappedFile::terminate() {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000073 assert(MapInfo && "MappedFile not initialized");
74 ::close(MapInfo->FD);
75 delete MapInfo;
76 MapInfo = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077}
78
79void MappedFile::unmap() {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000080 assert(MapInfo && "MappedFile not initialized");
81 if (!isMapped()) return;
82
83 if (Options & WRITE_ACCESS)
84 ::msync(BasePtr, MapInfo->Size, MS_SYNC);
85 ::munmap(BasePtr, MapInfo->Size);
86 BasePtr = 0; // Mark this as non-mapped.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087}
88
89void* MappedFile::map(std::string* ErrMsg) {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000090 assert(MapInfo && "MappedFile not initialized");
91 if (isMapped()) return BasePtr;
92
93 int prot = PROT_NONE;
94 int flags = 0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095#ifdef MAP_FILE
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000096 flags |= MAP_FILE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097#endif
Chris Lattnerb8a0afe2008-04-01 00:53:25 +000098 if (Options == 0) {
99 prot = PROT_READ;
100 flags = MAP_PRIVATE;
101 } else {
102 if (Options & READ_ACCESS)
103 prot |= PROT_READ;
104 if (Options & WRITE_ACCESS)
105 prot |= PROT_WRITE;
106 if (Options & EXEC_ACCESS)
107 prot |= PROT_EXEC;
108 if (Options & SHARED_MAPPING)
109 flags |= MAP_SHARED;
110 else
111 flags |= MAP_PRIVATE;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 }
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000113 size_t map_size = ((MapInfo->Size / Process::GetPageSize())+1) *
114 Process::GetPageSize();
115
116 BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0);
117 if (BasePtr == MAP_FAILED) {
118 MakeErrMsg(ErrMsg, "Can't map file:" + Path.toString());
119 return 0;
120 }
121 return BasePtr;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122}
123
124size_t MappedFile::size() const {
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000125 assert(MapInfo && "MappedFile not initialized");
126 return MapInfo->Size;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127}
128
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000129bool MappedFile::resize(size_t new_size, std::string* ErrMsg) {
130 assert(MapInfo && "MappedFile not initialized");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
132 // Take the mapping out of memory
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000133 unmap();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134
135 // Adjust the current size to a page boundary
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000136 size_t cur_size = ((MapInfo->Size / Process::GetPageSize())+1) *
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 Process::GetPageSize();
138
139 // Adjust the new_size to a page boundary
140 new_size = ((new_size / Process::GetPageSize())+1) *
141 Process::GetPageSize();
142
143 // If the file needs to be extended
144 if (new_size > cur_size) {
145 // Ensure we can allocate at least the idodes necessary to handle the
146 // file size requested.
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000147 if ((off_t)-1 == ::lseek(MapInfo->FD, new_size, SEEK_SET))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 return MakeErrMsg(ErrMsg, "Can't lseek: ");
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000149 if (-1 == ::write(MapInfo->FD, "\0", 1))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 return MakeErrMsg(ErrMsg, "Can't write: ");
151 }
152
153 // Put the mapping back into memory.
Chris Lattnerb8a0afe2008-04-01 00:53:25 +0000154 return map(ErrMsg);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155}