llvm.org GIT mirror llvm / 98b7e61
MappedFile is dead, remove it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@49035 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
4 changed file(s) with 0 addition(s) and 320 deletion(s). Raw diff Collapse all Expand all
+0
-85
include/llvm/System/MappedFile.h less more
None //===- llvm/System/MappedFile.h - MappedFile OS Concept ---------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the llvm::sys::MappedFile class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SYSTEM_MAPPEDFILE_H
14 #define LLVM_SYSTEM_MAPPEDFILE_H
15
16 #include "llvm/System/Path.h"
17
18 namespace llvm {
19 namespace sys {
20
21 /// Forward declare a class used for holding platform specific information
22 /// that needs to be
23 struct MappedFileInfo;
24
25 /// This class provides an abstraction for a memory mapped file in the
26 /// operating system's filesystem. It provides platform independent operations
27 /// for mapping a file into memory for read access.
28 class MappedFile {
29 sys::PathWithStatus Path; ///< Path to the file.
30 void *BasePtr; ///< Pointer to the base memory address
31 mutable MappedFileInfo *MapInfo; ///< Platform specific info for the mapping
32
33 MappedFile& operator=(const MappedFile &that); // DO NOT IMPLEMENT
34 MappedFile(const MappedFile &that); // DO NOT IMPLEMENT
35 public:
36 MappedFile() : BasePtr(0), MapInfo(0) {}
37
38 /// Destruct a MappedFile and release all memory associated with it.
39 ~MappedFile() { close(); }
40
41 public: // Accessors
42
43 /// This function determines if the file is currently mapped or not.
44 bool isMapped() const { return BasePtr != 0; }
45
46 /// getBase - Returns a const void* pointer to the base address of the file
47 /// mapping. This is the memory address of the first byte in the file.
48 const void *getBase() const { return BasePtr; }
49
50 /// This function returns a reference to the sys::Path object kept by the
51 /// MappedFile object. This contains the path to the file that is or
52 /// will be mapped.
53 const sys::PathWithStatus &path() const { return Path; }
54
55 /// This function returns the number of bytes in the file.
56 size_t size() const;
57
58 public: // Mutators
59
60 /// Open a file to be mapped and get its size but don't map it yet. Return
61 /// true on error.
62 bool open(const sys::Path &P, std::string *ErrMsg = 0) {
63 Path = P;
64 return initialize(ErrMsg);
65 }
66
67 /// unmap - Remove the mapped file from memory.
68 void unmap();
69
70 /// map - Reserve space for the file, map it into memory, and return a
71 /// pointer to it. This returns the base memory address of the mapped file
72 /// or 0 if an error occurred.
73 const void *map(std::string* ErrMsg = 0);
74
75 void close() { if (MapInfo) terminate(); }
76
77 private:
78 bool initialize(std::string *ErrMsg);
79 void terminate();
80 };
81 } // end namespace sys
82 } // end namespace llvm
83
84 #endif
+0
-34
lib/System/MappedFile.cpp less more
None //===- MappedFile.cpp - MappedFile Support ----------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the mapped file concept.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/System/MappedFile.h"
14 #include "llvm/Config/config.h"
15
16 namespace llvm {
17 using namespace sys;
18
19 //===----------------------------------------------------------------------===//
20 //=== WARNING: Implementation here must contain only TRULY operating system
21 //=== independent code.
22 //===----------------------------------------------------------------------===//
23
24 }
25
26 // Include the platform-specific parts of this class.
27 #ifdef LLVM_ON_UNIX
28 #include "Unix/MappedFile.inc"
29 #endif
30 #ifdef LLVM_ON_WIN32
31 #include "Win32/MappedFile.inc"
32 #endif
33
+0
-97
lib/System/Unix/MappedFile.inc less more
None //===- Unix/MappedFile.inc - Unix MappedFile Implementation -----*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides the generic Unix implementation of the MappedFile concept.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "Unix.h"
14 #include "llvm/System/Process.h"
15
16 #ifdef HAVE_FCNTL_H
17 #include
18 #endif
19
20 #ifdef HAVE_SYS_MMAN_H
21 #include
22 #endif
23
24 #ifdef HAVE_SYS_STAT_H
25 #include
26 #endif
27
28 using namespace llvm;
29 using namespace sys;
30
31 namespace llvm {
32 namespace sys {
33 struct MappedFileInfo {
34 int FD;
35 off_t Size;
36 };
37 }
38 }
39
40 bool MappedFile::initialize(std::string* ErrMsg) {
41 int FD = ::open(Path.c_str(), O_RDONLY);
42 if (FD < 0) {
43 MakeErrMsg(ErrMsg, "can't open file '" + Path.toString() + "'");
44 return true;
45 }
46 const FileStatus *Status = Path.getFileStatus(false, ErrMsg);
47 if (!Status) {
48 ::close(FD);
49 return true;
50 }
51 MapInfo = new MappedFileInfo();
52 MapInfo->FD = FD;
53 MapInfo->Size = Status->getSize();
54 return false;
55 }
56
57 void MappedFile::terminate() {
58 unmap();
59 assert(MapInfo && "MappedFile not initialized");
60 ::close(MapInfo->FD);
61 delete MapInfo;
62 MapInfo = 0;
63 }
64
65 void MappedFile::unmap() {
66 assert(MapInfo && "MappedFile not initialized");
67 if (!isMapped()) return;
68
69 ::munmap(BasePtr, MapInfo->Size);
70 BasePtr = 0; // Mark this as non-mapped.
71 }
72
73 const void* MappedFile::map(std::string* ErrMsg) {
74 assert(MapInfo && "MappedFile not initialized");
75 if (isMapped()) return BasePtr;
76
77 int flags = MAP_PRIVATE;
78 #ifdef MAP_FILE
79 flags |= MAP_FILE;
80 #endif
81 size_t PageSize = Process::GetPageSize();
82 size_t map_size = ((MapInfo->Size / PageSize)+1) * PageSize;
83
84 BasePtr = ::mmap(0, map_size, PROT_READ, flags, MapInfo->FD, 0);
85 if (BasePtr == MAP_FAILED) {
86 MakeErrMsg(ErrMsg, "Can't map file:" + Path.toString());
87 return 0;
88 }
89 return BasePtr;
90 }
91
92 size_t MappedFile::size() const {
93 assert(MapInfo && "MappedFile not initialized");
94 return MapInfo->Size;
95 }
96
+0
-104
lib/System/Win32/MappedFile.inc less more
None //===- Win32/MappedFile.cpp - Win32 MappedFile Implementation ---*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file provides the Win32 implementation of the MappedFile concept.
10 //
11 //===----------------------------------------------------------------------===//
12
13 //===----------------------------------------------------------------------===//
14 //=== WARNING: Implementation here must contain only Win32 code.
15 //===----------------------------------------------------------------------===//
16
17 #include "Win32.h"
18 #include "llvm/System/Process.h"
19
20 namespace llvm {
21 using namespace sys;
22
23 struct sys::MappedFileInfo {
24 HANDLE hFile;
25 HANDLE hMapping;
26 size_t size;
27 };
28
29 bool MappedFile::initialize(std::string* ErrMsg) {
30 assert(!MapInfo);
31 MapInfo = new MappedFileInfo;
32 MapInfo->hFile = INVALID_HANDLE_VALUE;
33 MapInfo->hMapping = NULL;
34
35 MapInfo->hFile = CreateFile(Path.c_str(), GENERIC_READ, 0, NULL,
36 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
37 if (MapInfo->hFile == INVALID_HANDLE_VALUE) {
38 delete MapInfo;
39 MapInfo = NULL;
40 return MakeErrMsg(ErrMsg,
41 std::string("Can't open file: ") + Path.toString());
42 }
43
44 LARGE_INTEGER size;
45 if (!GetFileSizeEx(MapInfo->hFile, &size) ||
46 (MapInfo->size = size_t(size.QuadPart), MapInfo->size != size.QuadPart)) {
47 CloseHandle(MapInfo->hFile);
48 delete MapInfo;
49 MapInfo = NULL;
50 return MakeErrMsg(ErrMsg,
51 std::string("Can't get size of file: ") + Path.toString());
52 }
53
54 return false;
55 }
56
57 void MappedFile::terminate() {
58 unmap();
59 if (MapInfo->hFile != INVALID_HANDLE_VALUE)
60 CloseHandle(MapInfo->hFile);
61 delete MapInfo;
62 MapInfo = NULL;
63 }
64
65 void MappedFile::unmap() {
66 assert(MapInfo && "MappedFile not initialized");
67 if (isMapped()) {
68 UnmapViewOfFile(BasePtr);
69 BasePtr = NULL;
70 }
71 if (MapInfo->hMapping != INVALID_HANDLE_VALUE) {
72 CloseHandle(MapInfo->hMapping);
73 MapInfo->hMapping = NULL;
74 }
75 }
76
77 const void* MappedFile::map(std::string* ErrMsg) {
78 if (!isMapped()) {
79 MapInfo->hMapping = CreateFileMapping(MapInfo->hFile, NULL, PAGE_READONLY,
80 0, 0, NULL);
81 if (MapInfo->hMapping == NULL) {
82 MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
83 return 0;
84 }
85
86 BasePtr = MapViewOfFileEx(MapInfo->hMapping, FILE_MAP_READ, 0, 0, 0, NULL);
87 if (BasePtr == NULL) {
88 CloseHandle(MapInfo->hMapping);
89 MapInfo->hMapping = NULL;
90 MakeErrMsg(ErrMsg, std::string("Can't map file: ") + Path.toString());
91 return 0;
92 }
93 }
94 return BasePtr;
95 }
96
97 size_t MappedFile::size() const {
98 assert(MapInfo && "MappedFile not initialized");
99 return MapInfo->size;
100 }
101
102 }
103