llvm.org GIT mirror llvm / bdbd2d7
Remove MappedFile support for mapping files for write and exec and shared. This complicates the design, is not used, and probably doesn't even work. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49022 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
8 changed file(s) with 23 addition(s) and 137 deletion(s). Raw diff Collapse all Expand all
5151 SourceFile(const std::string &fn, const GlobalVariable *Desc)
5252 : Filename(fn), Descriptor(Desc), File() {
5353 std::string ErrMsg;
54 if (File.open(Filename, sys::MappedFile::READ_ACCESS, &ErrMsg))
54 if (File.open(Filename, &ErrMsg))
5555 throw ErrMsg;
5656 readFile();
5757 }
2424
2525 /// This class provides an abstraction for a memory mapped file in the
2626 /// operating system's filesystem. It provides platform independent operations
27 /// for mapping a file into memory for both read and write access. This class
28 /// does not provide facilities for finding the file or operating on paths to
29 /// files. The sys::Path class is used for that.
27 /// for mapping a file into memory for both read access.
3028 class MappedFile {
3129 sys::PathWithStatus Path; ///< Path to the file.
32 unsigned Options; ///< Options used to create the mapping
3330 void *BasePtr; ///< Pointer to the base memory address
3431 mutable MappedFileInfo *MapInfo; ///< Platform specific info for the mapping
3532
3633 MappedFile& operator=(const MappedFile &that); // DO NOT IMPLEMENT
3734 MappedFile(const MappedFile &that); // DO NOT IMPLEMENT
3835 public:
39 enum MappingOptions {
40 READ_ACCESS = 0x0001, ///< Map the file for reading
41 WRITE_ACCESS = 0x0002, ///< Map the file for write access
42 EXEC_ACCESS = 0x0004, ///< Map the file for execution access
43 SHARED_MAPPING = 0x0008 ///< Map the file shared with other processes
44 };
45
46 MappedFile() : Options(READ_ACCESS), BasePtr(0), MapInfo(0) {}
36 MappedFile() : BasePtr(0), MapInfo(0) {}
4737
4838 /// Destruct a MappedFile and release all memory associated with it.
4939 ~MappedFile() { close(); }
7060 /// This function returns a reference to the sys::Path object kept by the
7161 /// MappedFile object. This contains the path to the file that is or
7262 /// will be mapped.
73 const sys::Path& path() const { return Path; }
63 const sys::PathWithStatus &path() const { return Path; }
7464
7565 /// This function returns the number of bytes in the file.
7666 size_t size() const;
7969
8070 /// Open a file to be mapped and get its size but don't map it yet. Return
8171 /// true on error.
82 bool open(const sys::Path &P, int options = READ_ACCESS,
83 std::string *ErrMsg = 0) {
72 bool open(const sys::Path &P, std::string *ErrMsg = 0) {
8473 Path = P;
85 Options = options;
8674 return initialize(ErrMsg);
8775 }
8876
9684 /// or 0 if an error occurred.
9785 void *map(std::string* ErrMsg = 0);
9886
99 /// resize - This method causes the size of the file, and consequently the
100 /// size of the mapping to be set. This is logically the same as unmap(),
101 /// adjust size of the file, map(). Consequently, when calling this
102 /// function, the caller should not rely on previous results of the
103 /// map(), base(), or baseChar() members as they may point to invalid
104 /// areas of memory after this call.
105 bool resize(size_t new_size, std::string *ErrMsg = 0);
106
10787 void close() { if (MapInfo) terminate(); }
10888
10989 private:
147147 Archive::mapToMemory(std::string* ErrMsg)
148148 {
149149 mapfile = new sys::MappedFile();
150 if (mapfile->open(archPath, sys::MappedFile::READ_ACCESS, ErrMsg))
150 if (mapfile->open(archPath, ErrMsg))
151151 return true;
152152 if (!(base = (char*) mapfile->map(ErrMsg)))
153153 return true;
211211 sys::MappedFile* mFile = 0;
212212 if (!data) {
213213 mFile = new sys::MappedFile();
214 if (mFile->open(member.getPath(), sys::MappedFile::READ_ACCESS, ErrMsg))
214 if (mFile->open(member.getPath(), ErrMsg))
215215 return true;
216216 if (!(data = (const char*) mFile->map(ErrMsg)))
217217 return true;
410410
411411 // Map in the archive we just wrote.
412412 sys::MappedFile arch;
413 if (arch.open(TmpArchive, sys::MappedFile::READ_ACCESS, ErrMsg))
413 if (arch.open(TmpArchive, ErrMsg))
414414 return true;
415415 const char* base;
416416 if (!(base = (const char*) arch.map(ErrMsg)))
191191 // Now its safe to mmap the files into memory becasue both files
192192 // have a non-zero size.
193193 sys::MappedFile F1;
194 if (F1.open(FileA, sys::MappedFile::READ_ACCESS, Error))
194 if (F1.open(FileA, Error))
195195 return 2;
196196 sys::MappedFile F2;
197 if (F2.open(FileB, sys::MappedFile::READ_ACCESS, Error))
197 if (F2.open(FileB, Error))
198198 return 2;
199199 if (!F1.map(Error))
200200 return 2;
158158 std::string *ErrStr) {
159159 // FIXME: This does an extra stat syscall to figure out the size, but we
160160 // already know the size!
161 bool Failure = File.open(Filename, sys::MappedFile::READ_ACCESS, ErrStr);
161 bool Failure = File.open(Filename, ErrStr);
162162 if (Failure) return true;
163163
164164 if (!File.map(ErrStr))
None //===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===//
0 //===- Unix/MappedFile.inc - Unix MappedFile Implementation -----*- C++ -*-===//
11 //
22 // The LLVM Compiler Infrastructure
33 //
88 //
99 // This file provides the generic Unix implementation of the MappedFile concept.
1010 //
11 //===----------------------------------------------------------------------===//
12
13 //===----------------------------------------------------------------------===//
14 //=== WARNING: Implementation here must contain only generic UNIX code that
15 //=== is guaranteed to work on *all* UNIX variants.
1611 //===----------------------------------------------------------------------===//
1712
1813 #include "Unix.h"
4338 }
4439
4540 bool MappedFile::initialize(std::string* ErrMsg) {
46 int mode = 0;
47 if (Options & READ_ACCESS)
48 if (Options & WRITE_ACCESS)
49 mode = O_RDWR;
50 else
51 mode = O_RDONLY;
52 else if (Options & WRITE_ACCESS)
53 mode = O_WRONLY;
54
55 int FD = ::open(Path.c_str(), mode);
41 int FD = ::open(Path.c_str(), O_RDONLY);
5642 if (FD < 0) {
5743 MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
5844 return true;
7965 assert(MapInfo && "MappedFile not initialized");
8066 if (!isMapped()) return;
8167
82 if (Options & WRITE_ACCESS)
83 ::msync(BasePtr, MapInfo->Size, MS_SYNC);
8468 ::munmap(BasePtr, MapInfo->Size);
8569 BasePtr = 0; // Mark this as non-mapped.
8670 }
8973 assert(MapInfo && "MappedFile not initialized");
9074 if (isMapped()) return BasePtr;
9175
92 int prot = PROT_NONE;
93 int flags = 0;
76 int prot = PROT_READ;
77 int flags = MAP_PRIVATE;
9478 #ifdef MAP_FILE
9579 flags |= MAP_FILE;
9680 #endif
97 if (Options == 0) {
98 prot = PROT_READ;
99 flags = MAP_PRIVATE;
100 } else {
101 if (Options & READ_ACCESS)
102 prot |= PROT_READ;
103 if (Options & WRITE_ACCESS)
104 prot |= PROT_WRITE;
105 if (Options & EXEC_ACCESS)
106 prot |= PROT_EXEC;
107 if (Options & SHARED_MAPPING)
108 flags |= MAP_SHARED;
109 else
110 flags |= MAP_PRIVATE;
111 }
112 size_t map_size = ((MapInfo->Size / Process::GetPageSize())+1) *
113 Process::GetPageSize();
81 size_t PageSize = Process::GetPageSize();
82 size_t map_size = ((MapInfo->Size / PageSize)+1) * PageSize;
11483
11584 BasePtr = ::mmap(0, map_size, prot, flags, MapInfo->FD, 0);
11685 if (BasePtr == MAP_FAILED) {
12594 return MapInfo->Size;
12695 }
12796
128 bool MappedFile::resize(size_t new_size, std::string* ErrMsg) {
129 assert(MapInfo && "MappedFile not initialized");
130
131 // Take the mapping out of memory
132 unmap();
133
134 // Adjust the current size to a page boundary
135 size_t cur_size = ((MapInfo->Size / Process::GetPageSize())+1) *
136 Process::GetPageSize();
137
138 // Adjust the new_size to a page boundary
139 new_size = ((new_size / Process::GetPageSize())+1) *
140 Process::GetPageSize();
141
142 // If the file needs to be extended
143 if (new_size > cur_size) {
144 // Ensure we can allocate at least the idodes necessary to handle the
145 // file size requested.
146 if ((off_t)-1 == ::lseek(MapInfo->FD, new_size, SEEK_SET))
147 return MakeErrMsg(ErrMsg, "Can't lseek: ");
148 if (-1 == ::write(MapInfo->FD, "\0", 1))
149 return MakeErrMsg(ErrMsg, "Can't write: ");
150 }
151
152 // Put the mapping back into memory.
153 return map(ErrMsg);
154 }
3232 MapInfo->hFile = INVALID_HANDLE_VALUE;
3333 MapInfo->hMapping = NULL;
3434
35 DWORD mode = Options & WRITE_ACCESS ? GENERIC_WRITE : GENERIC_READ;
36 DWORD disposition = Options & WRITE_ACCESS ? OPEN_ALWAYS : OPEN_EXISTING;
37 DWORD share = Options & WRITE_ACCESS ? FILE_SHARE_WRITE : FILE_SHARE_READ;
38 share = Options & SHARED_MAPPING ? share : 0;
39 MapInfo->hFile = CreateFile(Path.c_str(), mode, share, NULL, disposition,
40 FILE_ATTRIBUTE_NORMAL, NULL);
35 MapInfo->hFile = CreateFile(Path.c_str(), GENERIC_READ, 0, NULL,
36 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
4137 if (MapInfo->hFile == INVALID_HANDLE_VALUE) {
4238 delete MapInfo;
4339 MapInfo = NULL;
4440 return MakeErrMsg(ErrMsg,
45 std::string("Can't open file: ") + Path.toString());
41 std::string("Can't open file: ") + Path.toString());
4642 }
4743
4844 LARGE_INTEGER size;
8076
8177 void* MappedFile::map(std::string* ErrMsg) {
8278 if (!isMapped()) {
83 DWORD prot = PAGE_READONLY;
84 if (Options & EXEC_ACCESS)
85 prot = SEC_IMAGE;
86 else if (Options & WRITE_ACCESS)
87 prot = PAGE_READWRITE;
88 MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, prot, 0, 0, NULL);
79 MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, PAGE_READONLY,
80 0, 0, NULL);
8981 if (MapInfo->hMapping == NULL) {
9082 MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
9183 return 0;
9284 }
9385
94 prot = (Options & WRITE_ACCESS) ? FILE_MAP_WRITE : FILE_MAP_READ;
95 BasePtr = MapViewOfFileEx(MapInfo->hMapping, prot, 0, 0, 0, NULL);
86 BasePtr = MapViewOfFileEx(MapInfo->hMapping, FILE_MAP_READ, 0, 0, 0, NULL);
9687 if (BasePtr == NULL) {
9788 CloseHandle(MapInfo->hMapping);
9889 MapInfo->hMapping = NULL;
10899 return MapInfo->size;
109100 }
110101
111 bool MappedFile::resize(size_t new_size, std::string* ErrMsg) {
112 assert(MapInfo && "MappedFile not initialized");
113
114 // Take the mapping out of memory.
115 unmap();
116
117 // Adjust the new_size to a page boundary.
118 size_t pagesizem1 = Process::GetPageSize() - 1;
119 new_size = (new_size + pagesizem1) & ~pagesizem1;
120
121 // If the file needs to be extended, do so.
122 if (new_size > MapInfo->size) {
123 LARGE_INTEGER eof;
124 eof.QuadPart = new_size;
125 if (!SetFilePointerEx(MapInfo->hFile, eof, NULL, FILE_BEGIN))
126 return MakeErrMsg(ErrMsg,
127 std::string("Can't set end of file: ") + Path.toString());
128 if (!SetEndOfFile(MapInfo->hFile))
129 return MakeErrMsg(ErrMsg,
130 std::string("Can't set end of file: ") + Path.toString());
131 MapInfo->size = new_size;
132 }
133
134 // Remap the file.
135 return map(ErrMsg);
136102 }
137103
138 }
139