blob: 28cc5514f73b65c7f94c41b41e5c50358b86c2a6 [file] [log] [blame]
Elliott Hughes76160052012-12-12 16:31:20 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_UNIX_FILE_MAPPED_FILE_H_
18#define ART_RUNTIME_BASE_UNIX_FILE_MAPPED_FILE_H_
Elliott Hughes76160052012-12-12 16:31:20 -080019
20#include <fcntl.h>
21#include <string>
22#include "base/unix_file/fd_file.h"
23
24namespace unix_file {
25
26// Random access file which handles an mmap(2), munmap(2) pair in C++
27// RAII style. When a file is mmapped, the random access file
28// interface accesses the mmapped memory directly; otherwise, the
29// standard file I/O is used. Whenever a function fails, it returns
30// false and errno is set to the corresponding error code.
31class MappedFile : public FdFile {
32 public:
33 // File modes used in Open().
34 enum FileMode {
35 kReadOnlyMode = O_RDONLY | O_LARGEFILE,
36 kReadWriteMode = O_CREAT | O_RDWR | O_LARGEFILE,
37 };
38
39 MappedFile() : FdFile(), file_size_(-1), mapped_file_(NULL) {
40 }
41 // Creates a MappedFile using the given file descriptor. Takes ownership of
42 // the file descriptor.
43 explicit MappedFile(int fd) : FdFile(fd), file_size_(-1), mapped_file_(NULL) {
44 }
45
46 // Unmaps and closes the file if needed.
47 virtual ~MappedFile();
48
49 // Maps an opened file to memory in the read-only mode.
50 bool MapReadOnly();
51
52 // Maps an opened file to memory in the read-write mode. Before the
53 // file is mapped, it is truncated to 'file_size' bytes.
54 bool MapReadWrite(int64_t file_size);
55
56 // Unmaps a mapped file so that, e.g., SetLength() may be invoked.
57 bool Unmap();
58
59 // RandomAccessFile API.
60 // The functions below require that the file is open, but it doesn't
61 // have to be mapped.
62 virtual int Close();
63 virtual int64_t Read(char* buf, int64_t byte_count, int64_t offset) const;
64 // SetLength() requires that the file is not mmapped.
65 virtual int SetLength(int64_t new_length);
66 virtual int64_t GetLength() const;
67 virtual int Flush();
68 // Write() requires that, if the file is mmapped, it is mmapped in
69 // the read-write mode. Writes past the end of file are discarded.
70 virtual int64_t Write(const char* buf, int64_t byte_count, int64_t offset);
71
72 // A convenience method equivalent to GetLength().
73 int64_t size() const;
74
75 // Returns true if the file has been mmapped.
76 bool IsMapped() const;
77
78 // Returns a pointer to the start of the memory mapping once the
79 // file is successfully mapped; crashes otherwise.
80 char* data() const;
81
82 private:
83 enum MapMode {
84 kMapReadOnly = 1,
85 kMapReadWrite = 2,
86 };
87
88 mutable int64_t file_size_; // May be updated in GetLength().
89 void* mapped_file_;
90 MapMode map_mode_;
91
92 DISALLOW_COPY_AND_ASSIGN(MappedFile);
93};
94
95} // namespace unix_file
96
Brian Carlstromfc0e3212013-07-17 14:40:12 -070097#endif // ART_RUNTIME_BASE_UNIX_FILE_MAPPED_FILE_H_