• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
25 #ifdef HAVE_WIN32_FILEMAP
26 #include <windows.h>
27 #endif
28 
29 namespace android {
30 
31 /*
32  * This represents a memory-mapped file.  It might be the entire file or
33  * only part of it.  This requires a little bookkeeping because the mapping
34  * needs to be aligned on page boundaries, and in some cases we'd like to
35  * have multiple references to the mapped area without creating additional
36  * maps.
37  *
38  * This always uses MAP_SHARED.
39  *
40  * TODO: we should be able to create a new FileMap that is a subset of
41  * an existing FileMap and shares the underlying mapped pages.  Requires
42  * completing the refcounting stuff and possibly introducing the notion
43  * of a FileMap hierarchy.
44  */
45 class FileMap {
46 public:
47     FileMap(void);
48 
49     /*
50      * Create a new mapping on an open file.
51      *
52      * Closing the file descriptor does not unmap the pages, so we don't
53      * claim ownership of the fd.
54      *
55      * Returns "false" on failure.
56      */
57     bool create(const char* origFileName, int fd,
58                 off_t offset, size_t length, bool readOnly);
59 
60     /*
61      * Return the name of the file this map came from, if known.
62      */
getFileName(void)63     const char* getFileName(void) const { return mFileName; }
64 
65     /*
66      * Get a pointer to the piece of the file we requested.
67      */
getDataPtr(void)68     void* getDataPtr(void) const { return mDataPtr; }
69 
70     /*
71      * Get the length we requested.
72      */
getDataLength(void)73     size_t getDataLength(void) const { return mDataLength; }
74 
75     /*
76      * Get the data offset used to create this map.
77      */
getDataOffset(void)78     off_t getDataOffset(void) const { return mDataOffset; }
79 
80     /*
81      * Get a "copy" of the object.
82      */
acquire(void)83     FileMap* acquire(void) { mRefCount++; return this; }
84 
85     /*
86      * Call this when mapping is no longer needed.
87      */
release(void)88     void release(void) {
89         if (--mRefCount <= 0)
90             delete this;
91     }
92 
93     /*
94      * This maps directly to madvise() values, but allows us to avoid
95      * including <sys/mman.h> everywhere.
96      */
97     enum MapAdvice {
98         NORMAL, RANDOM, SEQUENTIAL, WILLNEED, DONTNEED
99     };
100 
101     /*
102      * Apply an madvise() call to the entire file.
103      *
104      * Returns 0 on success, -1 on failure.
105      */
106     int advise(MapAdvice advice);
107 
108 protected:
109     // don't delete objects; call release()
110     ~FileMap(void);
111 
112 private:
113     // these are not implemented
114     FileMap(const FileMap& src);
115     const FileMap& operator=(const FileMap& src);
116 
117     int         mRefCount;      // reference count
118     char*       mFileName;      // original file name, if known
119     void*       mBasePtr;       // base of mmap area; page aligned
120     size_t      mBaseLength;    // length, measured from "mBasePtr"
121     off_t       mDataOffset;    // offset used when map was created
122     void*       mDataPtr;       // start of requested data, offset from base
123     size_t      mDataLength;    // length, measured from "mDataPtr"
124 #ifdef HAVE_WIN32_FILEMAP
125     HANDLE      mFileHandle;    // Win32 file handle
126     HANDLE      mFileMapping;   // Win32 file mapping handle
127 #endif
128 
129     static long mPageSize;
130 };
131 
132 }; // namespace android
133 
134 #endif // __LIBS_FILE_MAP_H
135