Bug 1550108 - Split out Input/OutputBuffer into their own file r=froydnj
authorDoug Thayer <dothayer@mozilla.com>
Fri, 04 Oct 2019 20:44:41 +0000
changeset 496402 fbba9fe4330385dfca0c9065935eb70a9641b5fb
parent 496401 7915439e3c10e850b57bc775f556ac383371af5c
child 496403 8cb90c97dddbe98e8071e72e60fa0d58f4a486d6
push id97222
push userdothayer@mozilla.com
push dateFri, 04 Oct 2019 20:50:04 +0000
treeherderautoland@0c4837041392 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1550108
milestone71.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1550108 - Split out Input/OutputBuffer into their own file r=froydnj This just splits out the InputBuffer and OutputBuffer helper classes to make it cleaner for the StartupCache to include them. Differential Revision: https://phabricator.services.mozilla.com/D34651
dom/ipc/SharedMap.cpp
js/xpconnect/loader/IOBuffers.h
js/xpconnect/loader/ScriptPreloader-inl.h
js/xpconnect/loader/ScriptPreloader.cpp
js/xpconnect/loader/moz.build
--- a/dom/ipc/SharedMap.cpp
+++ b/dom/ipc/SharedMap.cpp
@@ -9,16 +9,17 @@
 
 #include "MemMapSnapshot.h"
 #include "ScriptPreloader-inl.h"
 
 #include "mozilla/dom/ContentParent.h"
 #include "mozilla/dom/ContentProcessMessageManager.h"
 #include "mozilla/dom/IPCBlobUtils.h"
 #include "mozilla/dom/ScriptSettings.h"
+#include "mozilla/IOBuffers.h"
 #include "mozilla/ScriptPreloader.h"
 
 using namespace mozilla::loader;
 
 namespace mozilla {
 
 using namespace ipc;
 
new file mode 100644
--- /dev/null
+++ b/js/xpconnect/loader/IOBuffers.h
@@ -0,0 +1,148 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef IOBuffers_h
+#define IOBuffers_h
+
+#include "mozilla/Assertions.h"
+#include "mozilla/CheckedInt.h"
+#include "mozilla/EndianUtils.h"
+#include "mozilla/EnumSet.h"
+#include "mozilla/Range.h"
+#include "mozilla/Span.h"
+#include "nsString.h"
+#include "nsTArray.h"
+
+namespace mozilla {
+namespace loader {
+
+class OutputBuffer {
+ public:
+  OutputBuffer() {}
+
+  uint8_t* write(size_t size) {
+    auto buf = data.AppendElements(size);
+    cursor_ += size;
+    return buf;
+  }
+
+  void codeUint8(const uint8_t& val) { *write(sizeof val) = val; }
+
+  template <typename T>
+  void codeUint8(const EnumSet<T>& val) {
+    // EnumSets are always represented as uint32_t values, so we need to
+    // assert that the value actually fits in a uint8 before writing it.
+    uint32_t value = val.serialize();
+    codeUint8(CheckedUint8(value).value());
+  }
+
+  void codeUint16(const uint16_t& val) {
+    LittleEndian::writeUint16(write(sizeof val), val);
+  }
+
+  void codeUint32(const uint32_t& val) {
+    LittleEndian::writeUint32(write(sizeof val), val);
+  }
+
+  void codeString(const nsCString& str) {
+    auto len = CheckedUint16(str.Length()).value();
+
+    codeUint16(len);
+    memcpy(write(len), str.BeginReading(), len);
+  }
+
+  size_t cursor() const { return cursor_; }
+
+  uint8_t* Get() { return data.Elements(); }
+
+  const uint8_t* Get() const { return data.Elements(); }
+
+ private:
+  nsTArray<uint8_t> data;
+  size_t cursor_ = 0;
+};
+
+class InputBuffer {
+ public:
+  explicit InputBuffer(const Range<uint8_t>& buffer) : data(buffer) {}
+
+  const uint8_t* read(size_t size) {
+    MOZ_ASSERT(checkCapacity(size));
+
+    auto buf = &data[cursor_];
+    cursor_ += size;
+    return buf;
+  }
+
+  bool codeUint8(uint8_t& val) {
+    if (checkCapacity(sizeof val)) {
+      val = *read(sizeof val);
+    }
+    return !error_;
+  }
+
+  template <typename T>
+  bool codeUint8(EnumSet<T>& val) {
+    uint8_t value;
+    if (codeUint8(value)) {
+      val.deserialize(value);
+    }
+    return !error_;
+  }
+
+  bool codeUint16(uint16_t& val) {
+    if (checkCapacity(sizeof val)) {
+      val = LittleEndian::readUint16(read(sizeof val));
+    }
+    return !error_;
+  }
+
+  bool codeUint32(uint32_t& val) {
+    if (checkCapacity(sizeof val)) {
+      val = LittleEndian::readUint32(read(sizeof val));
+    }
+    return !error_;
+  }
+
+  bool codeString(nsCString& str) {
+    uint16_t len;
+    if (codeUint16(len)) {
+      if (checkCapacity(len)) {
+        str.SetLength(len);
+        memcpy(str.BeginWriting(), read(len), len);
+      }
+    }
+    return !error_;
+  }
+
+  bool error() { return error_; }
+
+  bool finished() { return error_ || !remainingCapacity(); }
+
+  size_t remainingCapacity() { return data.length() - cursor_; }
+
+  size_t cursor() const { return cursor_; }
+
+  const uint8_t* Get() const { return data.begin().get(); }
+
+ private:
+  bool checkCapacity(size_t size) {
+    if (size > remainingCapacity()) {
+      error_ = true;
+    }
+    return !error_;
+  }
+
+  bool error_ = false;
+
+ public:
+  const Range<uint8_t>& data;
+  size_t cursor_ = 0;
+};
+
+} // namespace loader
+} // namespace mozilla
+
+#endif  // IOBuffers_h
--- a/js/xpconnect/loader/ScriptPreloader-inl.h
+++ b/js/xpconnect/loader/ScriptPreloader-inl.h
@@ -33,140 +33,16 @@ static inline Result<Ok, nsresult> Write
   }
   return Ok();
 }
 
 struct MOZ_RAII AutoSafeJSAPI : public AutoJSAPI {
   AutoSafeJSAPI() { Init(); }
 };
 
-class OutputBuffer {
- public:
-  OutputBuffer() {}
-
-  uint8_t* write(size_t size) {
-    auto buf = data.AppendElements(size);
-    cursor_ += size;
-    return buf;
-  }
-
-  void codeUint8(const uint8_t& val) { *write(sizeof val) = val; }
-
-  template <typename T>
-  void codeUint8(const EnumSet<T>& val) {
-    // EnumSets are always represented as uint32_t values, so we need to
-    // assert that the value actually fits in a uint8 before writing it.
-    uint32_t value = val.serialize();
-    codeUint8(CheckedUint8(value).value());
-  }
-
-  void codeUint16(const uint16_t& val) {
-    LittleEndian::writeUint16(write(sizeof val), val);
-  }
-
-  void codeUint32(const uint32_t& val) {
-    LittleEndian::writeUint32(write(sizeof val), val);
-  }
-
-  void codeString(const nsCString& str) {
-    auto len = CheckedUint16(str.Length()).value();
-
-    codeUint16(len);
-    memcpy(write(len), str.BeginReading(), len);
-  }
-
-  size_t cursor() const { return cursor_; }
-
-  uint8_t* Get() { return data.Elements(); }
-
-  const uint8_t* Get() const { return data.Elements(); }
-
- private:
-  nsTArray<uint8_t> data;
-  size_t cursor_ = 0;
-};
-
-class InputBuffer {
- public:
-  explicit InputBuffer(const Range<uint8_t>& buffer) : data(buffer) {}
-
-  const uint8_t* read(size_t size) {
-    MOZ_ASSERT(checkCapacity(size));
-
-    auto buf = &data[cursor_];
-    cursor_ += size;
-    return buf;
-  }
-
-  bool codeUint8(uint8_t& val) {
-    if (checkCapacity(sizeof val)) {
-      val = *read(sizeof val);
-    }
-    return !error_;
-  }
-
-  template <typename T>
-  bool codeUint8(EnumSet<T>& val) {
-    uint8_t value;
-    if (codeUint8(value)) {
-      val.deserialize(value);
-    }
-    return !error_;
-  }
-
-  bool codeUint16(uint16_t& val) {
-    if (checkCapacity(sizeof val)) {
-      val = LittleEndian::readUint16(read(sizeof val));
-    }
-    return !error_;
-  }
-
-  bool codeUint32(uint32_t& val) {
-    if (checkCapacity(sizeof val)) {
-      val = LittleEndian::readUint32(read(sizeof val));
-    }
-    return !error_;
-  }
-
-  bool codeString(nsCString& str) {
-    uint16_t len;
-    if (codeUint16(len)) {
-      if (checkCapacity(len)) {
-        str.SetLength(len);
-        memcpy(str.BeginWriting(), read(len), len);
-      }
-    }
-    return !error_;
-  }
-
-  bool error() { return error_; }
-
-  bool finished() { return error_ || !remainingCapacity(); }
-
-  size_t remainingCapacity() { return data.length() - cursor_; }
-
-  size_t cursor() const { return cursor_; }
-
-  const uint8_t* Get() const { return data.begin().get(); }
-
- private:
-  bool checkCapacity(size_t size) {
-    if (size > remainingCapacity()) {
-      error_ = true;
-    }
-    return !error_;
-  }
-
-  bool error_ = false;
-
- public:
-  const Range<uint8_t>& data;
-  size_t cursor_ = 0;
-};
-
 template <typename T>
 struct Matcher;
 
 // Wraps the iterator for a nsTHashTable so that it may be used as a range
 // iterator. Each iterator result acts as a smart pointer to the hash element,
 // and has a Remove() method which will remove the element from the hash.
 //
 // It also accepts an optional Matcher instance against which to filter the
--- a/js/xpconnect/loader/ScriptPreloader.cpp
+++ b/js/xpconnect/loader/ScriptPreloader.cpp
@@ -9,16 +9,17 @@
 #include "mozilla/loader/ScriptCacheActors.h"
 
 #include "mozilla/URLPreloader.h"
 
 #include "mozilla/ArrayUtils.h"
 #include "mozilla/ClearOnShutdown.h"
 #include "mozilla/Components.h"
 #include "mozilla/FileUtils.h"
+#include "mozilla/IOBuffers.h"
 #include "mozilla/Logging.h"
 #include "mozilla/ScopeExit.h"
 #include "mozilla/Services.h"
 #include "mozilla/Telemetry.h"
 #include "mozilla/Unused.h"
 #include "mozilla/dom/ContentChild.h"
 #include "mozilla/dom/ContentParent.h"
 
--- a/js/xpconnect/loader/moz.build
+++ b/js/xpconnect/loader/moz.build
@@ -26,16 +26,17 @@ IPDL_SOURCES += [
 ]
 
 EXPORTS += [
     'nsImportModule.h'
 ]
 
 EXPORTS.mozilla += [
     'AutoMemMap.h',
+    'IOBuffers.h',
     'ScriptPreloader.h',
     'URLPreloader.h',
 ]
 
 EXPORTS.mozilla.dom += [
     'PrecompiledScript.h',
 ]