blob: 8cd2a9694735b16e326b7efc99386c008d07bab4 [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
Reid Spencer97374922004-10-04 11:08:32 +000019#include "Unix.h"
Reid Spencercdf54d02004-12-27 06:16:52 +000020#include "llvm/System/Process.h"
21
22#ifdef HAVE_FCNTL_H
Tanya Lattnerc5a0bbf2004-10-05 00:51:26 +000023#include <fcntl.h>
Reid Spencercdf54d02004-12-27 06:16:52 +000024#endif
25
26#ifdef HAVE_SYS_MMAN_H
Reid Spencer97374922004-10-04 11:08:32 +000027#include <sys/mman.h>
Reid Spencercdf54d02004-12-27 06:16:52 +000028#endif
29
30#ifdef HAVE_SYS_STAT_H
31#include <sys/stat.h>
32#endif
Reid Spencer97374922004-10-04 11:08:32 +000033
34namespace llvm {
35using namespace sys;
36
37struct sys::MappedFileInfo {
38 int fd_;
39 struct stat sbuf_;
40};
41
42void MappedFile::initialize() {
Chris Lattnerc22c7d32006-07-18 06:52:52 +000043 if (!path_.exists())
Reid Spencer1fce0912004-12-11 00:14:15 +000044 throw std::string("Can't open file: ") + path_.toString();
Chris Lattnerc22c7d32006-07-18 06:52:52 +000045
Chris Lattnerc22c7d32006-07-18 06:52:52 +000046 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;
Chris Lattnerc22c7d32006-07-18 06:52:52 +000054
Chris Lattnercca68fa2006-07-18 06:57:51 +000055 int FD = ::open(path_.c_str(), mode);
56 if (FD < 0)
Chris Lattnerc22c7d32006-07-18 06:52:52 +000057 ThrowErrno(std::string("Can't open file: ") + path_.toString());
Chris Lattnercca68fa2006-07-18 06:57:51 +000058
Chris Lattnerc22c7d32006-07-18 06:52:52 +000059 struct stat sbuf;
Chris Lattnercca68fa2006-07-18 06:57:51 +000060 if(::fstat(FD, &sbuf) < 0) {
61 ::close(FD);
Chris Lattnerc22c7d32006-07-18 06:52:52 +000062 ThrowErrno(std::string("Can't stat file: ") + path_.toString());
Reid Spencer97374922004-10-04 11:08:32 +000063 }
Chris Lattnercca68fa2006-07-18 06:57:51 +000064 info_ = new MappedFileInfo;
65 info_->fd_ = FD;
66 info_->sbuf_ = sbuf;
Reid Spencer97374922004-10-04 11:08:32 +000067}
68
69void MappedFile::terminate() {
70 assert(info_ && "MappedFile not initialized");
71 if (info_->fd_ >= 0)
72 ::close(info_->fd_);
73 delete info_;
74 info_ = 0;
75}
76
77void MappedFile::unmap() {
78 assert(info_ && "MappedFile not initialized");
79 if (isMapped()) {
80 if (options_ & WRITE_ACCESS)
81 ::msync(base_, info_->sbuf_.st_size, MS_SYNC);
82 ::munmap(base_, info_->sbuf_.st_size);
83 }
84}
85
86void* MappedFile::map() {
Reid Spencer083507c2004-11-14 22:07:50 +000087 assert(info_ && "MappedFile not initialized");
Reid Spencer97374922004-10-04 11:08:32 +000088 if (!isMapped()) {
89 int prot = PROT_NONE;
Chris Lattner341e1da2004-10-05 00:46:21 +000090 int flags = 0;
91#ifdef MAP_FILE
92 flags |= MAP_FILE;
93#endif
Reid Spencer97374922004-10-04 11:08:32 +000094 if (options_ == 0) {
95 prot = PROT_READ;
96 flags = MAP_PRIVATE;
97 } else {
98 if (options_ & READ_ACCESS)
99 prot |= PROT_READ;
100 if (options_ & WRITE_ACCESS)
101 prot |= PROT_WRITE;
102 if (options_ & EXEC_ACCESS)
103 prot |= PROT_EXEC;
104 if (options_ & SHARED_MAPPING)
105 flags |= MAP_SHARED;
106 else
107 flags |= MAP_PRIVATE;
108 }
109 size_t map_size = ((info_->sbuf_.st_size / Process::GetPageSize())+1) *
110 Process::GetPageSize();
111
112 base_ = ::mmap(0, map_size, prot, flags, info_->fd_, 0);
113 if (base_ == MAP_FAILED)
Reid Spencer1fce0912004-12-11 00:14:15 +0000114 ThrowErrno(std::string("Can't map file:") + path_.toString());
Reid Spencer97374922004-10-04 11:08:32 +0000115 }
116 return base_;
117}
118
Reid Spencer56c3ed82004-12-13 02:58:51 +0000119size_t MappedFile::size() const {
Reid Spencer97374922004-10-04 11:08:32 +0000120 assert(info_ && "MappedFile not initialized");
121 return info_->sbuf_.st_size;
122}
123
124void MappedFile::size(size_t new_size) {
125 assert(info_ && "MappedFile not initialized");
126
127 // Take the mapping out of memory
128 this->unmap();
129
130 // Adjust the current size to a page boundary
131 size_t cur_size = ((info_->sbuf_.st_size / Process::GetPageSize())+1) *
132 Process::GetPageSize();
133
134 // Adjust the new_size to a page boundary
135 new_size = ((new_size / Process::GetPageSize())+1) *
136 Process::GetPageSize();
137
138 // If the file needs to be extended
139 if (new_size > cur_size) {
140 // Ensure we can allocate at least the idodes necessary to handle the
141 // file size requested.
142 ::lseek(info_->fd_, new_size, SEEK_SET);
143 ::write(info_->fd_, "\0", 1);
144 }
145
146 // Seek to current end of file.
147 this->map();
148}
149
150}
151