2009-03-04 11:31:44 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Encapsulate a shared file mapping.
|
|
|
|
//
|
|
|
|
#ifndef __LIBS_FILE_MAP_H
|
|
|
|
#define __LIBS_FILE_MAP_H
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2010-11-25 04:56:06 +08:00
|
|
|
#include <utils/Compat.h>
|
|
|
|
|
2014-11-12 02:31:30 +08:00
|
|
|
#if defined(__MINGW32__)
|
2014-11-09 11:30:05 +08:00
|
|
|
// Ensure that we always pull in winsock2.h before windows.h
|
2015-07-30 08:45:24 +08:00
|
|
|
#if defined(_WIN32)
|
2014-11-09 11:30:05 +08:00
|
|
|
#include <winsock2.h>
|
|
|
|
#endif
|
2009-03-04 11:31:44 +08:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This represents a memory-mapped file. It might be the entire file or
|
|
|
|
* only part of it. This requires a little bookkeeping because the mapping
|
|
|
|
* needs to be aligned on page boundaries, and in some cases we'd like to
|
|
|
|
* have multiple references to the mapped area without creating additional
|
|
|
|
* maps.
|
|
|
|
*
|
|
|
|
* This always uses MAP_SHARED.
|
|
|
|
*
|
|
|
|
* TODO: we should be able to create a new FileMap that is a subset of
|
|
|
|
* an existing FileMap and shares the underlying mapped pages. Requires
|
|
|
|
* completing the refcounting stuff and possibly introducing the notion
|
|
|
|
* of a FileMap hierarchy.
|
|
|
|
*/
|
|
|
|
class FileMap {
|
|
|
|
public:
|
|
|
|
FileMap(void);
|
|
|
|
|
2015-09-23 09:42:19 +08:00
|
|
|
FileMap(FileMap&& f);
|
|
|
|
FileMap& operator=(FileMap&& f);
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
/*
|
|
|
|
* Create a new mapping on an open file.
|
|
|
|
*
|
|
|
|
* Closing the file descriptor does not unmap the pages, so we don't
|
|
|
|
* claim ownership of the fd.
|
|
|
|
*
|
|
|
|
* Returns "false" on failure.
|
|
|
|
*/
|
|
|
|
bool create(const char* origFileName, int fd,
|
2010-11-25 04:56:06 +08:00
|
|
|
off64_t offset, size_t length, bool readOnly);
|
2009-03-04 11:31:44 +08:00
|
|
|
|
2015-02-23 23:43:35 +08:00
|
|
|
~FileMap(void);
|
|
|
|
|
2009-03-04 11:31:44 +08:00
|
|
|
/*
|
|
|
|
* Return the name of the file this map came from, if known.
|
|
|
|
*/
|
|
|
|
const char* getFileName(void) const { return mFileName; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a pointer to the piece of the file we requested.
|
|
|
|
*/
|
|
|
|
void* getDataPtr(void) const { return mDataPtr; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the length we requested.
|
|
|
|
*/
|
|
|
|
size_t getDataLength(void) const { return mDataLength; }
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the data offset used to create this map.
|
|
|
|
*/
|
2010-11-25 04:56:06 +08:00
|
|
|
off64_t getDataOffset(void) const { return mDataOffset; }
|
2009-03-04 11:31:44 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This maps directly to madvise() values, but allows us to avoid
|
|
|
|
* including <sys/mman.h> everywhere.
|
|
|
|
*/
|
|
|
|
enum MapAdvice {
|
|
|
|
NORMAL, RANDOM, SEQUENTIAL, WILLNEED, DONTNEED
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Apply an madvise() call to the entire file.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
int advise(MapAdvice advice);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
private:
|
|
|
|
// these are not implemented
|
|
|
|
FileMap(const FileMap& src);
|
|
|
|
const FileMap& operator=(const FileMap& src);
|
|
|
|
|
|
|
|
char* mFileName; // original file name, if known
|
|
|
|
void* mBasePtr; // base of mmap area; page aligned
|
|
|
|
size_t mBaseLength; // length, measured from "mBasePtr"
|
2010-11-25 04:56:06 +08:00
|
|
|
off64_t mDataOffset; // offset used when map was created
|
2009-03-04 11:31:44 +08:00
|
|
|
void* mDataPtr; // start of requested data, offset from base
|
|
|
|
size_t mDataLength; // length, measured from "mDataPtr"
|
2014-11-12 02:31:30 +08:00
|
|
|
#if defined(__MINGW32__)
|
2009-03-04 11:31:44 +08:00
|
|
|
HANDLE mFileHandle; // Win32 file handle
|
|
|
|
HANDLE mFileMapping; // Win32 file mapping handle
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static long mPageSize;
|
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
#endif // __LIBS_FILE_MAP_H
|