Flutter Engine
fml::FileMapping Class Referencefinal

#include <mapping.h>

Inheritance diagram for fml::FileMapping:
fml::Mapping

Public Types

enum  Protection {
  Protection::kRead,
  Protection::kWrite,
  Protection::kExecute
}
 

Public Member Functions

 FileMapping (const fml::UniqueFD &fd, std::initializer_list< Protection > protection={ Protection::kRead})
 
 ~FileMapping () override
 
size_t GetSize () const override
 
const uint8_t * GetMapping () const override
 
bool IsDontNeedSafe () const override
 
uint8_t * GetMutableMapping ()
 
bool IsValid () const
 
- Public Member Functions inherited from fml::Mapping
 Mapping ()
 
virtual ~Mapping ()
 

Static Public Member Functions

static std::unique_ptr< FileMappingCreateReadOnly (const std::string &path)
 
static std::unique_ptr< FileMappingCreateReadOnly (const fml::UniqueFD &base_fd, const std::string &sub_path="")
 
static std::unique_ptr< FileMappingCreateReadExecute (const std::string &path)
 
static std::unique_ptr< FileMappingCreateReadExecute (const fml::UniqueFD &base_fd, const std::string &sub_path="")
 

Detailed Description

Definition at line 39 of file mapping.h.

Member Enumeration Documentation

◆ Protection

Enumerator
kRead 
kWrite 
kExecute 

Definition at line 41 of file mapping.h.

Constructor & Destructor Documentation

◆ FileMapping()

fml::FileMapping::FileMapping ( const fml::UniqueFD fd,
std::initializer_list< Protection protection = Protection::kRead} 
)
explicit

Definition at line 53 of file mapping_posix.cc.

Referenced by fml::IsExecutable().

55  : size_(0), mapping_(nullptr) {
56  if (!handle.is_valid()) {
57  return;
58  }
59 
60  struct stat stat_buffer = {};
61 
62  if (::fstat(handle.get(), &stat_buffer) != 0) {
63  return;
64  }
65 
66  if (stat_buffer.st_size == 0) {
67  valid_ = true;
68  return;
69  }
70 
71  const auto is_writable = IsWritable(protection);
72 
73  auto* mapping =
74  ::mmap(nullptr, stat_buffer.st_size, ToPosixProtectionFlags(protection),
75  is_writable ? MAP_SHARED : MAP_PRIVATE, handle.get(), 0);
76 
77  if (mapping == MAP_FAILED) {
78  return;
79  }
80 
81  mapping_ = static_cast<uint8_t*>(mapping);
82  size_ = stat_buffer.st_size;
83  valid_ = true;
84  if (is_writable) {
85  mutable_mapping_ = mapping_;
86  }
87 }
static bool IsWritable(std::initializer_list< FileMapping::Protection > protection_flags)
static int ToPosixProtectionFlags(std::initializer_list< FileMapping::Protection > protection_flags)

◆ ~FileMapping()

fml::FileMapping::~FileMapping ( )
override

Definition at line 89 of file mapping_posix.cc.

89  {
90  if (mapping_ != nullptr) {
91  ::munmap(mapping_, size_);
92  }
93 }

Member Function Documentation

◆ CreateReadExecute() [1/2]

std::unique_ptr< FileMapping > fml::FileMapping::CreateReadExecute ( const std::string &  path)
static

Definition at line 42 of file mapping.cc.

References fml::kRead, and fml::OpenFile().

Referenced by CreateReadExecute(), and flutter::GetFileMapping().

43  {
44  return CreateReadExecute(
45  OpenFile(path.c_str(), false, FilePermission::kRead));
46 }
static std::unique_ptr< FileMapping > CreateReadExecute(const std::string &path)
Definition: mapping.cc:42
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:65
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57

◆ CreateReadExecute() [2/2]

std::unique_ptr< FileMapping > fml::FileMapping::CreateReadExecute ( const fml::UniqueFD base_fd,
const std::string &  sub_path = "" 
)
static

Definition at line 48 of file mapping.cc.

References CreateReadExecute(), kExecute, fml::kRead, kRead, and fml::OpenFile().

50  {
51  if (sub_path.size() != 0) {
52  return CreateReadExecute(
53  OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
54  }
55 
56  auto mapping = std::make_unique<FileMapping>(
57  base_fd, std::initializer_list<Protection>{Protection::kRead,
59 
60  if (!mapping->IsValid()) {
61  return nullptr;
62  }
63 
64  return mapping;
65 }
static std::unique_ptr< FileMapping > CreateReadExecute(const std::string &path)
Definition: mapping.cc:42
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:65

◆ CreateReadOnly() [1/2]

std::unique_ptr< FileMapping > fml::FileMapping::CreateReadOnly ( const std::string &  path)
static

Definition at line 18 of file mapping.cc.

References fml::kRead, and fml::OpenFile().

Referenced by CreateReadOnly(), flutter::GetFileMapping(), flutter::AndroidShellHolder::NotifyLowMemoryWarning(), flutter::testing::OpenFixtureAsMapping(), flutter::testing::OpenFixtureAsSkData(), flutter::testing::DartFixture::SetSnapshotsAndAssets(), flutter::StartupAndShutdownShell(), and flutter::testing::TEST_F().

19  {
20  return CreateReadOnly(OpenFile(path.c_str(), false, FilePermission::kRead),
21  "");
22 }
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:65
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:18

◆ CreateReadOnly() [2/2]

std::unique_ptr< FileMapping > fml::FileMapping::CreateReadOnly ( const fml::UniqueFD base_fd,
const std::string &  sub_path = "" 
)
static

Definition at line 24 of file mapping.cc.

References CreateReadOnly(), fml::kRead, kRead, and fml::OpenFile().

26  {
27  if (sub_path.size() != 0) {
28  return CreateReadOnly(
29  OpenFile(base_fd, sub_path.c_str(), false, FilePermission::kRead), "");
30  }
31 
32  auto mapping = std::make_unique<FileMapping>(
33  base_fd, std::initializer_list<Protection>{Protection::kRead});
34 
35  if (!mapping->IsValid()) {
36  return nullptr;
37  }
38 
39  return mapping;
40 }
fml::UniqueFD OpenFile(const char *path, bool create_if_necessary, FilePermission permission)
This can open a directory on POSIX, but not on Windows.
Definition: file_posix.cc:65
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition: mapping.cc:18

◆ GetMapping()

const uint8_t * fml::FileMapping::GetMapping ( ) const
overridevirtual

Implements fml::Mapping.

Definition at line 99 of file mapping_posix.cc.

Referenced by flutter::testing::ImageMatchesFixture(), and ReadStringFromFile().

99  {
100  return mapping_;
101 }

◆ GetMutableMapping()

uint8_t * fml::FileMapping::GetMutableMapping ( )

Definition at line 14 of file mapping.cc.

14  {
15  return mutable_mapping_;
16 }

◆ GetSize()

size_t fml::FileMapping::GetSize ( ) const
overridevirtual

Implements fml::Mapping.

Definition at line 95 of file mapping_posix.cc.

Referenced by flutter::testing::ImageMatchesFixture(), and ReadStringFromFile().

95  {
96  return size_;
97 }

◆ IsDontNeedSafe()

bool fml::FileMapping::IsDontNeedSafe ( ) const
overridevirtual

Implements fml::Mapping.

Definition at line 103 of file mapping_posix.cc.

103  {
104  return mutable_mapping_ == nullptr;
105 }

◆ IsValid()

bool fml::FileMapping::IsValid ( ) const

Definition at line 107 of file mapping_posix.cc.

107  {
108  return valid_;
109 }

The documentation for this class was generated from the following files: