5
5
6
6
#include " JSBigString.h"
7
7
8
- #include < fcntl.h>
9
- #include < sys/stat.h>
10
- #include < unistd.h>
11
-
12
8
#include < glog/logging.h>
13
9
14
10
#include < folly/Memory.h>
15
- #include < folly/portability/SysMman.h>
16
11
#include < folly/ScopeGuard.h>
12
+ #include < folly/portability/Fcntl.h>
13
+ #include < folly/portability/SysMman.h>
14
+ #include < folly/portability/SysStat.h>
15
+ #include < folly/portability/Unistd.h>
17
16
18
17
namespace facebook {
19
18
namespace react {
20
19
21
20
JSBigFileString::JSBigFileString (int fd, size_t size, off_t offset /* = 0*/ )
22
- : m_fd { -1 }
23
- , m_data { nullptr } {
24
- folly::checkUnixError (m_fd = dup (fd),
25
- " Could not duplicate file descriptor" );
21
+ : m_fd{-1 }, m_data{nullptr } {
22
+ folly::checkUnixError (m_fd = dup (fd), " Could not duplicate file descriptor" );
26
23
27
24
// Offsets given to mmap must be page aligned. We abstract away that
28
25
// restriction by sending a page aligned offset to mmap, and keeping track
29
26
// of the offset within the page that we must alter the mmap pointer by to
30
27
// get the final desired offset.
31
28
if (offset != 0 ) {
32
- const static auto ps = getpagesize ( );
29
+ const static auto ps = sysconf (_SC_PAGESIZE );
33
30
auto d = lldiv (offset, ps);
34
31
35
32
m_mapOff = d.quot ;
@@ -69,8 +66,7 @@ static off_t maybeRemap(char *data, size_t size, int fd) {
69
66
}
70
67
const auto begin = data;
71
68
static const uint8_t kRemapMagic [] = {
72
- 0xc6 , 0x1f , 0xbc , 0x03 , 0xc1 , 0x03 , 0x19 , 0x1f , 0xa1 , 0xd0 , 0xeb , 0x73
73
- };
69
+ 0xc6 , 0x1f , 0xbc , 0x03 , 0xc1 , 0x03 , 0x19 , 0x1f , 0xa1 , 0xd0 , 0xeb , 0x73 };
74
70
if (::memcmp (data, kRemapMagic , sizeof (kRemapMagic )) != 0 ) {
75
71
return 0 ;
76
72
}
@@ -82,26 +78,26 @@ static off_t maybeRemap(char *data, size_t size, int fd) {
82
78
{
83
79
// System page size must be at least as granular as the remapping.
84
80
// TODO: Consider fallback that reads entire file into memory.
85
- const size_t systemPS = getpagesize ( );
81
+ const size_t systemPS = sysconf (_SC_PAGESIZE );
86
82
CHECK (filePS >= systemPS)
87
- << " filePS: " << filePS
88
- << " systemPS: " << systemPS;
83
+ << " filePS: " << filePS << " systemPS: " << systemPS;
89
84
}
90
85
const off_t headerPages = read16 (data);
91
86
uint16_t numMappings = read16 (data);
92
87
size_t curFilePage = headerPages;
93
88
while (numMappings--) {
94
89
auto memPage = read16 (data) + headerPages;
95
90
auto numPages = read16 (data);
96
- if (mmap (begin + memPage * filePS, numPages * filePS,
97
- PROT_READ, MAP_FILE | MAP_PRIVATE | MAP_FIXED,
98
- fd, curFilePage * filePS) == MAP_FAILED) {
99
- CHECK (false )
100
- << " memPage: " << memPage
101
- << " numPages: " << numPages
102
- << " curFilePage: " << curFilePage
103
- << " size: " << size
104
- << " error: " << std::strerror (errno);
91
+ if (mmap (
92
+ begin + memPage * filePS,
93
+ numPages * filePS,
94
+ PROT_READ,
95
+ MAP_FILE | MAP_PRIVATE | MAP_FIXED,
96
+ fd,
97
+ curFilePage * filePS) == MAP_FAILED) {
98
+ CHECK (false ) << " memPage: " << memPage << " numPages: " << numPages
99
+ << " curFilePage: " << curFilePage << " size: " << size
100
+ << " error: " << std::strerror (errno);
105
101
}
106
102
curFilePage += numPages;
107
103
}
@@ -115,12 +111,10 @@ const char *JSBigFileString::c_str() const {
115
111
}
116
112
if (!m_data) {
117
113
m_data =
118
- (const char *) mmap (0 , m_size, PROT_READ, MAP_PRIVATE, m_fd, m_mapOff);
114
+ (const char *)mmap (0 , m_size, PROT_READ, MAP_PRIVATE, m_fd, m_mapOff);
119
115
CHECK (m_data != MAP_FAILED)
120
- << " fd: " << m_fd
121
- << " size: " << m_size
122
- << " offset: " << m_mapOff
123
- << " error: " << std::strerror (errno);
116
+ << " fd: " << m_fd << " size: " << m_size << " offset: " << m_mapOff
117
+ << " error: " << std::strerror (errno);
124
118
#ifdef WITH_FBREMAP
125
119
// Remapping is only attempted when the entire file was requested.
126
120
if (m_mapOff == 0 && m_pageOff == 0 ) {
@@ -141,16 +135,19 @@ int JSBigFileString::fd() const {
141
135
return m_fd;
142
136
}
143
137
144
- std::unique_ptr<const JSBigFileString> JSBigFileString::fromPath (const std::string& sourceURL) {
138
+ std::unique_ptr<const JSBigFileString> JSBigFileString::fromPath (
139
+ const std::string &sourceURL) {
145
140
int fd = ::open (sourceURL.c_str (), O_RDONLY);
146
141
folly::checkUnixError (fd, " Could not open file" , sourceURL);
147
- SCOPE_EXIT { CHECK (::close (fd) == 0 ); };
142
+ SCOPE_EXIT {
143
+ CHECK (::close (fd) == 0 );
144
+ };
148
145
149
146
struct stat fileInfo;
150
147
folly::checkUnixError (::fstat (fd, &fileInfo), " fstat on bundle failed." );
151
148
152
149
return folly::make_unique<const JSBigFileString>(fd, fileInfo.st_size );
153
150
}
154
151
155
- } // namespace react
156
- } // namespace facebook
152
+ } // namespace react
153
+ } // namespace facebook
0 commit comments