blob: 148372b0cc30b58633302b905782b6663b4144bc [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 {
Chris Lattner0af70932006-07-18 07:01:08 +000038 int FD;
39 off_t Size;
Reid Spencer97374922004-10-04 11:08:32 +000040};
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;
Chris Lattner84bad2f2006-07-18 07:03:14 +000047 if (options_ & READ_ACCESS)
48 if (options_ & WRITE_ACCESS)
Chris Lattnerc22c7d32006-07-18 06:52:52 +000049 mode = O_RDWR;
50 else
51 mode = O_RDONLY;
Chris Lattner84bad2f2006-07-18 07:03:14 +000052 else if (options_ & WRITE_ACCESS)
Chris Lattnerc22c7d32006-07-18 06:52:52 +000053 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;
Chris Lattner0af70932006-07-18 07:01:08 +000065 info_->FD = FD;
66 info_->Size = sbuf.st_size;
Reid Spencer97374922004-10-04 11:08:32 +000067}
68
69void MappedFile::terminate() {
70 assert(info_ && "MappedFile not initialized");
Chris Lattner84bad2f2006-07-18 07:03:14 +000071 ::close(info_->FD);
Reid Spencer97374922004-10-04 11:08:32 +000072 delete info_;
73 info_ = 0;
74}
75
76void MappedFile::unmap() {
77 assert(info_ && "MappedFile not initialized");
78 if (isMapped()) {
79 if (options_ & WRITE_ACCESS)
Chris Lattner0af70932006-07-18 07:01:08 +000080 ::msync(base_, info_->Size, MS_SYNC);
81 ::munmap(base_, info_->Size);
Reid Spencer97374922004-10-04 11:08:32 +000082 }
83}
84
85void* MappedFile::map() {
Reid Spencer083507c2004-11-14 22:07:50 +000086 assert(info_ && "MappedFile not initialized");
Reid Spencer97374922004-10-04 11:08:32 +000087 if (!isMapped()) {
88 int prot = PROT_NONE;
Chris Lattner341e1da2004-10-05 00:46:21 +000089 int flags = 0;
90#ifdef MAP_FILE
91 flags |= MAP_FILE;
92#endif
Reid Spencer97374922004-10-04 11:08:32 +000093 if (options_ == 0) {
94 prot = PROT_READ;
95 flags = MAP_PRIVATE;
96 } else {
97 if (options_ & READ_ACCESS)
98 prot |= PROT_READ;
99 if (options_ & WRITE_ACCESS)
100 prot |= PROT_WRITE;
101 if (options_ & EXEC_ACCESS)
102 prot |= PROT_EXEC;
103 if (options_ & SHARED_MAPPING)
104 flags |= MAP_SHARED;
105 else
106 flags |= MAP_PRIVATE;
107 }
Chris Lattner0af70932006-07-18 07:01:08 +0000108 size_t map_size = ((info_->Size / Process::GetPageSize())+1) *
Reid Spencer97374922004-10-04 11:08:32 +0000109 Process::GetPageSize();
110
Chris Lattner0af70932006-07-18 07:01:08 +0000111 base_ = ::mmap(0, map_size, prot, flags, info_->FD, 0);
Reid Spencer97374922004-10-04 11:08:32 +0000112 if (base_ == MAP_FAILED)
Reid Spencer1fce0912004-12-11 00:14:15 +0000113 ThrowErrno(std::string("Can't map file:") + path_.toString());
Reid Spencer97374922004-10-04 11:08:32 +0000114 }
115 return base_;
116}
117
Reid Spencer56c3ed82004-12-13 02:58:51 +0000118size_t MappedFile::size() const {
Reid Spencer97374922004-10-04 11:08:32 +0000119 assert(info_ && "MappedFile not initialized");
Chris Lattner0af70932006-07-18 07:01:08 +0000120 return info_->Size;
Reid Spencer97374922004-10-04 11:08:32 +0000121}
122
123void MappedFile::size(size_t new_size) {
124 assert(info_ && "MappedFile not initialized");
125
126 // Take the mapping out of memory
127 this->unmap();
128
129 // Adjust the current size to a page boundary
Chris Lattner0af70932006-07-18 07:01:08 +0000130 size_t cur_size = ((info_->Size / Process::GetPageSize())+1) *
Reid Spencer97374922004-10-04 11:08:32 +0000131 Process::GetPageSize();
132
133 // Adjust the new_size to a page boundary
134 new_size = ((new_size / Process::GetPageSize())+1) *
135 Process::GetPageSize();
136
137 // If the file needs to be extended
138 if (new_size > cur_size) {
139 // Ensure we can allocate at least the idodes necessary to handle the
140 // file size requested.
Chris Lattner0af70932006-07-18 07:01:08 +0000141 ::lseek(info_->FD, new_size, SEEK_SET);
142 ::write(info_->FD, "\0", 1);
Reid Spencer97374922004-10-04 11:08:32 +0000143 }
144
145 // Seek to current end of file.
146 this->map();
147}
148
149}
150