blob: d853acec4f45b4c50a462f5ca75f62f07b77d65e [file] [log] [blame]
Reid Spencer97374922004-10-04 11:08:32 +00001//===- Unix/MappedFile.cpp - Unix MappedFile Implementation -----*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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 "llvm/System/Process.h"
20#include "Unix.h"
21#include <sys/fcntl.h>
22#include <sys/mman.h>
23
24namespace llvm {
25using namespace sys;
26
27struct sys::MappedFileInfo {
28 int fd_;
29 struct stat sbuf_;
30};
31
32void MappedFile::initialize() {
33 if (path_.exists()) {
34 info_ = new MappedFileInfo;
35 int mode = 0;
36 if (options_&READ_ACCESS)
37 if (options_&WRITE_ACCESS)
38 mode = O_RDWR;
39 else
40 mode = O_RDONLY;
41 else if (options_&WRITE_ACCESS)
42 mode = O_WRONLY;
43 info_->fd_ = ::open(path_.c_str(),mode);
44 if (info_->fd_ < 0) {
45 delete info_;
46 info_ = 0;
47 ThrowErrno(std::string("Can't open file: ") + path_.get());
48 }
49 struct stat sbuf;
50 if(::fstat(info_->fd_, &info_->sbuf_) < 0) {
51 ::close(info_->fd_);
52 delete info_;
53 info_ = 0;
54 ThrowErrno(std::string("Can't stat file: ") + path_.get());
55 }
56 }
57}
58
59void MappedFile::terminate() {
60 assert(info_ && "MappedFile not initialized");
61 if (info_->fd_ >= 0)
62 ::close(info_->fd_);
63 delete info_;
64 info_ = 0;
65}
66
67void MappedFile::unmap() {
68 assert(info_ && "MappedFile not initialized");
69 if (isMapped()) {
70 if (options_ & WRITE_ACCESS)
71 ::msync(base_, info_->sbuf_.st_size, MS_SYNC);
72 ::munmap(base_, info_->sbuf_.st_size);
73 }
74}
75
76void* MappedFile::map() {
77 if (!isMapped()) {
78 int prot = PROT_NONE;
79 int flags = MAP_FILE;
80 if (options_ == 0) {
81 prot = PROT_READ;
82 flags = MAP_PRIVATE;
83 } else {
84 if (options_ & READ_ACCESS)
85 prot |= PROT_READ;
86 if (options_ & WRITE_ACCESS)
87 prot |= PROT_WRITE;
88 if (options_ & EXEC_ACCESS)
89 prot |= PROT_EXEC;
90 if (options_ & SHARED_MAPPING)
91 flags |= MAP_SHARED;
92 else
93 flags |= MAP_PRIVATE;
94 }
95 size_t map_size = ((info_->sbuf_.st_size / Process::GetPageSize())+1) *
96 Process::GetPageSize();
97
98 base_ = ::mmap(0, map_size, prot, flags, info_->fd_, 0);
99 if (base_ == MAP_FAILED)
100 ThrowErrno(std::string("Can't map file:") + path_.get());
101 }
102 return base_;
103}
104
105size_t MappedFile::size() {
106 assert(info_ && "MappedFile not initialized");
107 return info_->sbuf_.st_size;
108}
109
110void MappedFile::size(size_t new_size) {
111 assert(info_ && "MappedFile not initialized");
112
113 // Take the mapping out of memory
114 this->unmap();
115
116 // Adjust the current size to a page boundary
117 size_t cur_size = ((info_->sbuf_.st_size / Process::GetPageSize())+1) *
118 Process::GetPageSize();
119
120 // Adjust the new_size to a page boundary
121 new_size = ((new_size / Process::GetPageSize())+1) *
122 Process::GetPageSize();
123
124 // If the file needs to be extended
125 if (new_size > cur_size) {
126 // Ensure we can allocate at least the idodes necessary to handle the
127 // file size requested.
128 ::lseek(info_->fd_, new_size, SEEK_SET);
129 ::write(info_->fd_, "\0", 1);
130 }
131
132 // Seek to current end of file.
133 this->map();
134}
135
136}
137
138// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab