mirror of
https://github.com/status-im/react-native.git
synced 2025-01-28 02:04:55 +00:00
Introducing JSBigFileString
Reviewed By: michalgr Differential Revision: D4189896 fbshipit-source-id: 0f3cebcd7e76d5c763c29ebc1119ae24b643e5ad
This commit is contained in:
parent
79fa6d41a1
commit
2de1c3507a
@ -148,6 +148,61 @@ private:
|
|||||||
size_t m_size;
|
size_t m_size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// JSBigString interface implemented by a file-backed mmap region.
|
||||||
|
class JSBigFileString : public JSBigString {
|
||||||
|
public:
|
||||||
|
|
||||||
|
JSBigFileString(int fd, size_t size, off_t offset = 0)
|
||||||
|
: m_fd {fd}
|
||||||
|
, m_data {nullptr}
|
||||||
|
{
|
||||||
|
// Offsets given to mmap must be page aligend. We abstract away that
|
||||||
|
// restriction by sending a page aligned offset to mmap, and keeping track
|
||||||
|
// of the offset within the page that we must alter the mmap pointer by to
|
||||||
|
// get the final desired offset.
|
||||||
|
auto ps = getpagesize();
|
||||||
|
auto d = lldiv(offset, ps);
|
||||||
|
|
||||||
|
m_mapOff = d.quot;
|
||||||
|
m_pageOff = d.rem;
|
||||||
|
m_size = size + m_pageOff;
|
||||||
|
}
|
||||||
|
|
||||||
|
~JSBigFileString() {
|
||||||
|
if (m_data) {
|
||||||
|
munmap((void *)m_data, m_size);
|
||||||
|
}
|
||||||
|
close(m_fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isAscii() const override {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *c_str() const override {
|
||||||
|
if (!m_data) {
|
||||||
|
m_data = (const char *)mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, m_mapOff);
|
||||||
|
CHECK(m_data != MAP_FAILED);
|
||||||
|
}
|
||||||
|
return m_data + m_pageOff;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t size() const override {
|
||||||
|
return m_size - m_pageOff;
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd() const {
|
||||||
|
return m_fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_fd; // The file descriptor being mmaped
|
||||||
|
size_t m_size; // The size of the mmaped region
|
||||||
|
size_t m_pageOff; // The offset in the mmaped region to the data.
|
||||||
|
off_t m_mapOff; // The offset in the file to the mmaped region.
|
||||||
|
mutable const char *m_data; // Pointer to the mmaped region.
|
||||||
|
};
|
||||||
|
|
||||||
class JSBigOptimizedBundleString : public JSBigString {
|
class JSBigOptimizedBundleString : public JSBigString {
|
||||||
public:
|
public:
|
||||||
enum class Encoding {
|
enum class Encoding {
|
||||||
@ -157,7 +212,6 @@ public:
|
|||||||
Utf16,
|
Utf16,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
JSBigOptimizedBundleString(int fd, size_t size, const uint8_t sha1[20], Encoding encoding) :
|
JSBigOptimizedBundleString(int fd, size_t size, const uint8_t sha1[20], Encoding encoding) :
|
||||||
m_fd(fd),
|
m_fd(fd),
|
||||||
m_size(size),
|
m_size(size),
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
TEST_SRCS = [
|
TEST_SRCS = [
|
||||||
'CxxMessageQueueTest.cpp',
|
'CxxMessageQueueTest.cpp',
|
||||||
'jsarg_helpers.cpp',
|
'jsarg_helpers.cpp',
|
||||||
|
'jsbigstring.cpp',
|
||||||
'jscexecutor.cpp',
|
'jscexecutor.cpp',
|
||||||
'jsclogging.cpp',
|
'jsclogging.cpp',
|
||||||
'methodcall.cpp',
|
'methodcall.cpp',
|
||||||
@ -35,6 +36,7 @@ if THIS_IS_FBOBJC:
|
|||||||
'-fexceptions',
|
'-fexceptions',
|
||||||
],
|
],
|
||||||
deps = [
|
deps = [
|
||||||
|
'//xplat/folly:molly',
|
||||||
'//xplat/third-party/gmock:gtest',
|
'//xplat/third-party/gmock:gtest',
|
||||||
react_native_xplat_target('cxxreact:bridge'),
|
react_native_xplat_target('cxxreact:bridge'),
|
||||||
react_native_xplat_target('jschelpers:jschelpers'),
|
react_native_xplat_target('jschelpers:jschelpers'),
|
||||||
|
61
ReactCommon/cxxreact/tests/jsbigstring.cpp
Normal file
61
ReactCommon/cxxreact/tests/jsbigstring.cpp
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
|
||||||
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||||
|
#include <sys/mman.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
#include <folly/File.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <cxxreact/Executor.h>
|
||||||
|
#include <cxxreact/MessageQueueThread.h>
|
||||||
|
#include <cxxreact/MethodCall.h>
|
||||||
|
|
||||||
|
using namespace facebook;
|
||||||
|
using namespace facebook::react;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
int tempFileFromString(std::string contents)
|
||||||
|
{
|
||||||
|
std::string tmp {getenv("TMPDIR")};
|
||||||
|
tmp += "/temp.XXXXX";
|
||||||
|
|
||||||
|
std::vector<char> tmpBuf {tmp.begin(), tmp.end()};
|
||||||
|
tmpBuf.push_back('\0');
|
||||||
|
|
||||||
|
const int fd = mkstemp(tmpBuf.data());
|
||||||
|
write(fd, contents.c_str(), contents.size() + 1);
|
||||||
|
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST(JSBigFileString, MapWholeFileTest) {
|
||||||
|
std::string data {"Hello, world"};
|
||||||
|
const auto size = data.length() + 1;
|
||||||
|
|
||||||
|
// Initialise Big String
|
||||||
|
int fd = tempFileFromString("Hello, world");
|
||||||
|
JSBigFileString bigStr {fd, size};
|
||||||
|
|
||||||
|
// Test
|
||||||
|
ASSERT_EQ(fd, bigStr.fd());
|
||||||
|
ASSERT_STREQ(data.c_str(), bigStr.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(JSBigFileString, MapPartTest) {
|
||||||
|
std::string data {"Hello, world"};
|
||||||
|
|
||||||
|
// Sub-string to actually map
|
||||||
|
std::string needle {"or"};
|
||||||
|
off_t offset = data.find(needle);
|
||||||
|
|
||||||
|
// Initialise Big String
|
||||||
|
int fd = tempFileFromString(data);
|
||||||
|
JSBigFileString bigStr {fd, needle.size(), offset};
|
||||||
|
|
||||||
|
// Test
|
||||||
|
ASSERT_EQ(fd, bigStr.fd());
|
||||||
|
ASSERT_EQ(needle.length(), bigStr.size());
|
||||||
|
for (unsigned int i = 0; i < needle.length(); ++i) {
|
||||||
|
ASSERT_EQ(needle[i], bigStr.c_str()[i]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user