Skip to content

Commit 2de1c35

Browse files
Ashok MenonFacebook Github Bot
authored andcommitted
Introducing JSBigFileString
Reviewed By: michalgr Differential Revision: D4189896 fbshipit-source-id: 0f3cebcd7e76d5c763c29ebc1119ae24b643e5ad
1 parent 79fa6d4 commit 2de1c35

File tree

3 files changed

+118
-1
lines changed

3 files changed

+118
-1
lines changed

ReactCommon/cxxreact/Executor.h

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,61 @@ class JSBigBufferString : public facebook::react::JSBigString {
148148
size_t m_size;
149149
};
150150

151+
// JSBigString interface implemented by a file-backed mmap region.
152+
class JSBigFileString : public JSBigString {
153+
public:
154+
155+
JSBigFileString(int fd, size_t size, off_t offset = 0)
156+
: m_fd {fd}
157+
, m_data {nullptr}
158+
{
159+
// Offsets given to mmap must be page aligend. We abstract away that
160+
// restriction by sending a page aligned offset to mmap, and keeping track
161+
// of the offset within the page that we must alter the mmap pointer by to
162+
// get the final desired offset.
163+
auto ps = getpagesize();
164+
auto d = lldiv(offset, ps);
165+
166+
m_mapOff = d.quot;
167+
m_pageOff = d.rem;
168+
m_size = size + m_pageOff;
169+
}
170+
171+
~JSBigFileString() {
172+
if (m_data) {
173+
munmap((void *)m_data, m_size);
174+
}
175+
close(m_fd);
176+
}
177+
178+
bool isAscii() const override {
179+
return true;
180+
}
181+
182+
const char *c_str() const override {
183+
if (!m_data) {
184+
m_data = (const char *)mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, m_mapOff);
185+
CHECK(m_data != MAP_FAILED);
186+
}
187+
return m_data + m_pageOff;
188+
}
189+
190+
size_t size() const override {
191+
return m_size - m_pageOff;
192+
}
193+
194+
int fd() const {
195+
return m_fd;
196+
}
197+
198+
private:
199+
int m_fd; // The file descriptor being mmaped
200+
size_t m_size; // The size of the mmaped region
201+
size_t m_pageOff; // The offset in the mmaped region to the data.
202+
off_t m_mapOff; // The offset in the file to the mmaped region.
203+
mutable const char *m_data; // Pointer to the mmaped region.
204+
};
205+
151206
class JSBigOptimizedBundleString : public JSBigString {
152207
public:
153208
enum class Encoding {
@@ -157,7 +212,6 @@ class JSBigOptimizedBundleString : public JSBigString {
157212
Utf16,
158213
};
159214

160-
161215
JSBigOptimizedBundleString(int fd, size_t size, const uint8_t sha1[20], Encoding encoding) :
162216
m_fd(fd),
163217
m_size(size),

ReactCommon/cxxreact/tests/BUCK

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
TEST_SRCS = [
22
'CxxMessageQueueTest.cpp',
33
'jsarg_helpers.cpp',
4+
'jsbigstring.cpp',
45
'jscexecutor.cpp',
56
'jsclogging.cpp',
67
'methodcall.cpp',
@@ -35,6 +36,7 @@ if THIS_IS_FBOBJC:
3536
'-fexceptions',
3637
],
3738
deps = [
39+
'//xplat/folly:molly',
3840
'//xplat/third-party/gmock:gtest',
3941
react_native_xplat_target('cxxreact:bridge'),
4042
react_native_xplat_target('jschelpers:jschelpers'),
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
// Copyright 2004-present Facebook. All Rights Reserved.
3+
#include <sys/mman.h>
4+
#include <fcntl.h>
5+
6+
#include <folly/File.h>
7+
#include <gtest/gtest.h>
8+
#include <cxxreact/Executor.h>
9+
#include <cxxreact/MessageQueueThread.h>
10+
#include <cxxreact/MethodCall.h>
11+
12+
using namespace facebook;
13+
using namespace facebook::react;
14+
15+
namespace {
16+
int tempFileFromString(std::string contents)
17+
{
18+
std::string tmp {getenv("TMPDIR")};
19+
tmp += "/temp.XXXXX";
20+
21+
std::vector<char> tmpBuf {tmp.begin(), tmp.end()};
22+
tmpBuf.push_back('\0');
23+
24+
const int fd = mkstemp(tmpBuf.data());
25+
write(fd, contents.c_str(), contents.size() + 1);
26+
27+
return fd;
28+
}
29+
};
30+
31+
TEST(JSBigFileString, MapWholeFileTest) {
32+
std::string data {"Hello, world"};
33+
const auto size = data.length() + 1;
34+
35+
// Initialise Big String
36+
int fd = tempFileFromString("Hello, world");
37+
JSBigFileString bigStr {fd, size};
38+
39+
// Test
40+
ASSERT_EQ(fd, bigStr.fd());
41+
ASSERT_STREQ(data.c_str(), bigStr.c_str());
42+
}
43+
44+
TEST(JSBigFileString, MapPartTest) {
45+
std::string data {"Hello, world"};
46+
47+
// Sub-string to actually map
48+
std::string needle {"or"};
49+
off_t offset = data.find(needle);
50+
51+
// Initialise Big String
52+
int fd = tempFileFromString(data);
53+
JSBigFileString bigStr {fd, needle.size(), offset};
54+
55+
// Test
56+
ASSERT_EQ(fd, bigStr.fd());
57+
ASSERT_EQ(needle.length(), bigStr.size());
58+
for (unsigned int i = 0; i < needle.length(); ++i) {
59+
ASSERT_EQ(needle[i], bigStr.c_str()[i]);
60+
}
61+
}

0 commit comments

Comments
 (0)