JSBigString.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <folly/Exception.h>
  9. #ifndef RN_EXPORT
  10. #ifdef _MSC_VER
  11. #define RN_EXPORT
  12. #else
  13. #define RN_EXPORT __attribute__((visibility("default")))
  14. #endif
  15. #endif
  16. namespace facebook {
  17. namespace react {
  18. // JSExecutor functions sometimes take large strings, on the order of
  19. // megabytes. Copying these can be expensive. Introducing a
  20. // move-only, non-CopyConstructible type will let the compiler ensure
  21. // that no copies occur. folly::MoveWrapper should be used when a
  22. // large string needs to be curried into a std::function<>, which must
  23. // by CopyConstructible.
  24. class JSBigString {
  25. public:
  26. JSBigString() = default;
  27. // Not copyable
  28. JSBigString(const JSBigString &) = delete;
  29. JSBigString &operator=(const JSBigString &) = delete;
  30. virtual ~JSBigString() {}
  31. virtual bool isAscii() const = 0;
  32. // This needs to be a \0 terminated string
  33. virtual const char *c_str() const = 0;
  34. // Length of the c_str without the NULL byte.
  35. virtual size_t size() const = 0;
  36. };
  37. // Concrete JSBigString implementation which holds a std::string
  38. // instance.
  39. class JSBigStdString : public JSBigString {
  40. public:
  41. JSBigStdString(std::string str, bool isAscii = false)
  42. : m_isAscii(isAscii), m_str(std::move(str)) {}
  43. bool isAscii() const override {
  44. return m_isAscii;
  45. }
  46. const char *c_str() const override {
  47. return m_str.c_str();
  48. }
  49. size_t size() const override {
  50. return m_str.size();
  51. }
  52. private:
  53. bool m_isAscii;
  54. std::string m_str;
  55. };
  56. // Concrete JSBigString implementation which holds a heap-allocated
  57. // buffer, and provides an accessor for writing to it. This can be
  58. // used to construct a JSBigString in place, such as by reading from a
  59. // file.
  60. class RN_EXPORT JSBigBufferString : public JSBigString {
  61. public:
  62. JSBigBufferString(size_t size) : m_data(new char[size + 1]), m_size(size) {
  63. // Guarantee nul-termination. The caller is responsible for
  64. // filling in the rest of m_data.
  65. m_data[m_size] = '\0';
  66. }
  67. ~JSBigBufferString() {
  68. delete[] m_data;
  69. }
  70. bool isAscii() const override {
  71. return true;
  72. }
  73. const char *c_str() const override {
  74. return m_data;
  75. }
  76. size_t size() const override {
  77. return m_size;
  78. }
  79. char *data() {
  80. return m_data;
  81. }
  82. private:
  83. char *m_data;
  84. size_t m_size;
  85. };
  86. // JSBigString interface implemented by a file-backed mmap region.
  87. class RN_EXPORT JSBigFileString : public JSBigString {
  88. public:
  89. JSBigFileString(int fd, size_t size, off_t offset = 0);
  90. ~JSBigFileString();
  91. bool isAscii() const override {
  92. return true;
  93. }
  94. const char *c_str() const override;
  95. size_t size() const override;
  96. int fd() const;
  97. static std::unique_ptr<const JSBigFileString> fromPath(
  98. const std::string &sourceURL);
  99. private:
  100. int m_fd; // The file descriptor being mmaped
  101. size_t m_size; // The size of the mmaped region
  102. mutable off_t m_pageOff; // The offset in the mmaped region to the data.
  103. off_t m_mapOff; // The offset in the file to the mmaped region.
  104. mutable const char *m_data; // Pointer to the mmaped region.
  105. };
  106. } // namespace react
  107. } // namespace facebook