Android-cuttlefish cvd tool
mapped_file.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018 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#pragma once
18
19#include <sys/types.h>
20
21#include <memory>
22
23#include "android-base/macros.h"
26
27#if defined(_WIN32)
28#include <windows.h>
29#define PROT_READ 1
30#define PROT_WRITE 2
31using os_handle = HANDLE;
32#else
33#include <sys/mman.h>
34using os_handle = int;
35#endif
36
37namespace android {
38namespace base {
39
44 public:
50 static std::unique_ptr<MappedFile> FromFd(borrowed_fd fd, off64_t offset, size_t length,
51 int prot);
52
56 static std::unique_ptr<MappedFile> FromOsHandle(os_handle h, off64_t offset, size_t length,
57 int prot);
58
63
67 MappedFile(MappedFile&& other);
69
70 char* data() const { return base_ + offset_; }
71 size_t size() const { return size_; }
72
73 private:
75
76 void Close();
77
78 char* base_;
79 size_t size_;
80
81 size_t offset_;
82
83#if defined(_WIN32)
84 MappedFile(char* base, size_t size, size_t offset, HANDLE handle)
85 : base_(base), size_(size), offset_(offset), handle_(handle) {}
86 HANDLE handle_;
87#else
88 MappedFile(char* base, size_t size, size_t offset) : base_(base), size_(size), offset_(offset) {}
89#endif
90};
91
92} // namespace base
93} // namespace android
Definition: mapped_file.h:43
size_t size() const
Definition: mapped_file.h:71
MappedFile(MappedFile &&other)
Definition: mapped_file.cpp:88
void Close()
Definition: mapped_file.cpp:114
~MappedFile()
Definition: mapped_file.cpp:110
size_t size_
Definition: mapped_file.h:79
size_t offset_
Definition: mapped_file.h:81
static std::unique_ptr< MappedFile > FromFd(borrowed_fd fd, off64_t offset, size_t length, int prot)
Definition: mapped_file.cpp:38
char * base_
Definition: mapped_file.h:78
char * data() const
Definition: mapped_file.h:70
MappedFile(char *base, size_t size, size_t offset)
Definition: mapped_file.h:88
MappedFile & operator=(MappedFile &&other)
Definition: mapped_file.cpp:99
DISALLOW_IMPLICIT_CONSTRUCTORS(MappedFile)
static std::unique_ptr< MappedFile > FromOsHandle(os_handle h, off64_t offset, size_t length, int prot)
Definition: mapped_file.cpp:47
int os_handle
Definition: mapped_file.h:34
Definition: map_ptr.h:34
Definition: unique_fd.h:292