blob: 6c0aa52742f6933046d7144d029aa89d5063719a [file] [log] [blame]
The Android Open Source Projectcbb10112009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17//
18// Encapsulate a shared file mapping.
19//
20#ifndef __LIBS_FILE_MAP_H
21#define __LIBS_FILE_MAP_H
22
23#include <sys/types.h>
24
Kenny Roote2fa7dc2010-11-24 12:56:06 -080025#include <utils/Compat.h>
26
Yabin Cui266092c2014-11-11 10:31:30 -080027#if defined(__MINGW32__)
Stephen Hines7e341212014-11-08 19:30:05 -080028// Ensure that we always pull in winsock2.h before windows.h
29#ifdef HAVE_WINSOCK
30#include <winsock2.h>
31#endif
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080032#include <windows.h>
33#endif
34
35namespace android {
36
37/*
38 * This represents a memory-mapped file. It might be the entire file or
39 * only part of it. This requires a little bookkeeping because the mapping
40 * needs to be aligned on page boundaries, and in some cases we'd like to
41 * have multiple references to the mapped area without creating additional
42 * maps.
43 *
44 * This always uses MAP_SHARED.
45 *
46 * TODO: we should be able to create a new FileMap that is a subset of
47 * an existing FileMap and shares the underlying mapped pages. Requires
48 * completing the refcounting stuff and possibly introducing the notion
49 * of a FileMap hierarchy.
50 */
51class FileMap {
52public:
53 FileMap(void);
54
55 /*
56 * Create a new mapping on an open file.
57 *
58 * Closing the file descriptor does not unmap the pages, so we don't
59 * claim ownership of the fd.
60 *
61 * Returns "false" on failure.
62 */
63 bool create(const char* origFileName, int fd,
Kenny Roote2fa7dc2010-11-24 12:56:06 -080064 off64_t offset, size_t length, bool readOnly);
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080065
66 /*
67 * Return the name of the file this map came from, if known.
68 */
69 const char* getFileName(void) const { return mFileName; }
70
71 /*
72 * Get a pointer to the piece of the file we requested.
73 */
74 void* getDataPtr(void) const { return mDataPtr; }
75
76 /*
77 * Get the length we requested.
78 */
79 size_t getDataLength(void) const { return mDataLength; }
80
81 /*
82 * Get the data offset used to create this map.
83 */
Kenny Roote2fa7dc2010-11-24 12:56:06 -080084 off64_t getDataOffset(void) const { return mDataOffset; }
The Android Open Source Projectcbb10112009-03-03 19:31:44 -080085
86 /*
87 * Get a "copy" of the object.
88 */
89 FileMap* acquire(void) { mRefCount++; return this; }
90
91 /*
92 * Call this when mapping is no longer needed.
93 */
94 void release(void) {
95 if (--mRefCount <= 0)
96 delete this;
97 }
98
99 /*
100 * This maps directly to madvise() values, but allows us to avoid
101 * including <sys/mman.h> everywhere.
102 */
103 enum MapAdvice {
104 NORMAL, RANDOM, SEQUENTIAL, WILLNEED, DONTNEED
105 };
106
107 /*
108 * Apply an madvise() call to the entire file.
109 *
110 * Returns 0 on success, -1 on failure.
111 */
112 int advise(MapAdvice advice);
113
114protected:
115 // don't delete objects; call release()
116 ~FileMap(void);
117
118private:
119 // these are not implemented
120 FileMap(const FileMap& src);
121 const FileMap& operator=(const FileMap& src);
122
123 int mRefCount; // reference count
124 char* mFileName; // original file name, if known
125 void* mBasePtr; // base of mmap area; page aligned
126 size_t mBaseLength; // length, measured from "mBasePtr"
Kenny Roote2fa7dc2010-11-24 12:56:06 -0800127 off64_t mDataOffset; // offset used when map was created
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800128 void* mDataPtr; // start of requested data, offset from base
129 size_t mDataLength; // length, measured from "mDataPtr"
Yabin Cui266092c2014-11-11 10:31:30 -0800130#if defined(__MINGW32__)
The Android Open Source Projectcbb10112009-03-03 19:31:44 -0800131 HANDLE mFileHandle; // Win32 file handle
132 HANDLE mFileMapping; // Win32 file mapping handle
133#endif
134
135 static long mPageSize;
136};
137
138}; // namespace android
139
140#endif // __LIBS_FILE_MAP_H