Bug 736706 - Part 3: add Buf.copyIncomingToOutgoing(), r=philikon
authorVicamo Yang <vyang@mozilla.com>
Wed, 26 Sep 2012 14:56:21 +0800
changeset 108115 e814f7254496ad7a424d8585f574052724380b30
parent 108114 73d41a3aa7d980061a1bacd6ac481ede0b775b1b
child 108116 c3db7fb3a77187923fa7f2aad65ed7dd82b70f89
push id23539
push userryanvm@gmail.com
push dateWed, 26 Sep 2012 22:55:55 +0000
treeherdermozilla-central@ec079fd92224 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersphilikon
bugs736706
milestone18.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 736706 - Part 3: add Buf.copyIncomingToOutgoing(), r=philikon
dom/system/gonk/ril_worker.js
--- a/dom/system/gonk/ril_worker.js
+++ b/dom/system/gonk/ril_worker.js
@@ -307,20 +307,32 @@ let Buf = {
            this.readUint8Unchecked() <<  8 |
            this.readUint8Unchecked();
   },
 
   /**
    * Functions for writing data to the outgoing buffer.
    */
 
+  /**
+   * Ensure position specified is writable.
+   *
+   * @param index
+   *        Data position in outgoing parcel, valid from 0 to
+   *        this.OUTGOING_BUFFER_LENGTH.
+   */
+  ensureOutgoingAvailable: function ensureOutgoingAvailable(index) {
+    if (index >= this.OUTGOING_BUFFER_LENGTH) {
+      this.growOutgoingBuffer(index + 1);
+    }
+  },
+
   writeUint8: function writeUint8(value) {
-    if (this.outgoingIndex >= this.OUTGOING_BUFFER_LENGTH) {
-      this.growOutgoingBuffer(this.outgoingIndex + 1);
-    }
+    this.ensureOutgoingAvailable(this.outgoingIndex);
+
     this.outgoingBytes[this.outgoingIndex] = value;
     this.outgoingIndex++;
   },
 
   writeUint16: function writeUint16(value) {
     this.writeUint8(value & 0xff);
     this.writeUint8((value >> 8) & 0xff);
   },
@@ -371,16 +383,48 @@ let Buf = {
     this.outgoingIndex = 0;
     this.writeUint8((value >> 24) & 0xff);
     this.writeUint8((value >> 16) & 0xff);
     this.writeUint8((value >> 8) & 0xff);
     this.writeUint8(value & 0xff);
     this.outgoingIndex = currentIndex;
   },
 
+  copyIncomingToOutgoing: function copyIncomingToOutgoing(length) {
+    if (!length || (length < 0)) {
+      return;
+    }
+
+    let translatedReadIndexEnd = this.currentParcelSize - this.readAvailable + length - 1;
+    this.ensureIncomingAvailable(translatedReadIndexEnd);
+
+    let translatedWriteIndexEnd = this.outgoingIndex + length - 1
+    this.ensureOutgoingAvailable(translatedWriteIndexEnd);
+
+    let newIncomingReadIndex = this.incomingReadIndex + length;
+    if (newIncomingReadIndex < this.INCOMING_BUFFER_LENGTH) {
+      // Reading won't cause wrapping, go ahead with builtin copy.
+      this.outgoingBytes.set(this.incomingBytes.subarray(this.incomingReadIndex, newIncomingReadIndex),
+                             this.outgoingIndex);
+    } else {
+      // Not so lucky.
+      newIncomingReadIndex %= this.INCOMING_BUFFER_LENGTH;
+      this.outgoingBytes.set(this.incomingBytes.subarray(this.incomingReadIndex, this.INCOMING_BUFFER_LENGTH),
+                             this.outgoingIndex);
+      if (newIncomingReadIndex) {
+        let firstPartLength = this.INCOMING_BUFFER_LENGTH - this.incomingReadIndex;
+        this.outgoingBytes.set(this.incomingBytes.subarray(0, newIncomingReadIndex),
+                               this.outgoingIndex + firstPartLength);
+      }
+    }
+
+    this.incomingReadIndex = newIncomingReadIndex;
+    this.readAvailable -= length;
+    this.outgoingIndex += length;
+  },
 
   /**
    * Parcel management
    */
 
   /**
    * Write incoming data to the circular buffer.
    *