Bug 854169 - Move OS.File's normalizeToPointer() to SharedAll. r=froydnj
☠☠ backed out by 2d4fd5a493b1 ☠ ☠
authorDavid Rajchenbach-Teller <dteller@mozilla.com>
Fri, 08 Nov 2013 09:16:04 -0500
changeset 173017 1b339f171b0af353ca4f0153f5b0404a46873554
parent 173016 0aeb846dc2e7b458651a11df052415cb57eb301a
child 173018 224d0d4df43475781091c2d5f7de4adac9ea1a91
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs854169
milestone28.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 854169 - Move OS.File's normalizeToPointer() to SharedAll. r=froydnj
toolkit/components/osfile/modules/osfile_shared_allthreads.jsm
toolkit/components/osfile/modules/osfile_shared_front.jsm
--- a/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm
+++ b/toolkit/components/osfile/modules/osfile_shared_allthreads.jsm
@@ -38,16 +38,17 @@ let EXPORTED_SYMBOLS = [
   "Config",
   "Constants",
   "Type",
   "HollowStructure",
   "OSError",
   "declareFFI",
   "declareLazy",
   "declareLazyFFI",
+  "normalizeToPointer",
   "projectValue",
   "isTypedArray",
   "defineLazyGetter",
   "offsetBy",
   "OS" // Warning: this exported symbol will disappear
 ];
 
 ////////////////////// Configuration of OS.File
@@ -1042,16 +1043,61 @@ let offsetBy =
       gOffsetByType = ctypes.uint8_t.array(bytes * 2);
     }
     let addr = ctypes.cast(pointer, gOffsetByType.ptr).
       contents.addressOfElement(bytes);
     return ctypes.cast(addr, type);
 };
 exports.offsetBy = offsetBy;
 
+/**
+ * Utility function used to normalize a Typed Array or C
+ * pointer into a uint8_t C pointer.
+ *
+ * Future versions might extend this to other data structures.
+ *
+ * @param {Typed array | C pointer} candidate The buffer. If
+ * a C pointer, it must be non-null.
+ * @param {number} bytes The number of bytes that |candidate| should contain.
+ * Used for sanity checking if the size of |candidate| can be determined.
+ *
+ * @return {ptr:{C pointer}, bytes:number} A C pointer of type uint8_t,
+ * corresponding to the start of |candidate|.
+ */
+function normalizeToPointer(candidate, bytes) {
+  if (!candidate) {
+    throw new TypeError("Expecting  a Typed Array or a C pointer");
+  }
+  let ptr;
+  if ("isNull" in candidate) {
+    if (candidate.isNull()) {
+      throw new TypeError("Expecting a non-null pointer");
+    }
+    ptr = Type.uint8_t.out_ptr.cast(candidate);
+    if (bytes == null) {
+      throw new TypeError("C pointer missing bytes indication.");
+    }
+  } else if (isTypedArray(candidate)) {
+    // Typed Array
+    ptr = Type.uint8_t.out_ptr.implementation(candidate.buffer);
+    if (bytes == null) {
+      bytes = candidate.byteLength;
+    } else if (candidate.byteLength < bytes) {
+      throw new TypeError("Buffer is too short. I need at least " +
+                         bytes +
+                         " bytes but I have only " +
+                         candidate.byteLength +
+                          "bytes");
+    }
+  } else {
+    throw new TypeError("Expecting  a Typed Array or a C pointer");
+  }
+  return {ptr: ptr, bytes: bytes};
+};
+exports.normalizeToPointer = normalizeToPointer;
 
 ///////////////////// OS interactions
 
 /**
  * An OS error.
  *
  * This class is provided mostly for type-matching. If you need more
  * details about an error, you should use the platform-specific error
--- a/toolkit/components/osfile/modules/osfile_shared_front.jsm
+++ b/toolkit/components/osfile/modules/osfile_shared_front.jsm
@@ -77,17 +77,17 @@ AbstractFile.prototype = {
    * - {number} bytes The number of |bytes| to write from the buffer. If
    * unspecified, this is |buffer.byteLength|. Note that |bytes| is required
    * if |buffer| is a C pointer.
    *
    * @return {number} The number of bytes actually read, which may be
    * less than |bytes| if the file did not contain that many bytes left.
    */
   readTo: function readTo(buffer, options = {}) {
-    let {ptr, bytes} = AbstractFile.normalizeToPointer(buffer, options.bytes);
+    let {ptr, bytes} = SharedAll.normalizeToPointer(buffer, options.bytes);
     let pos = 0;
     while (pos < bytes) {
       let chunkSize = this._read(ptr, bytes - pos, options);
       if (chunkSize == 0) {
         break;
       }
       pos += chunkSize;
       ptr = SharedAll.offsetBy(ptr, chunkSize);
@@ -111,17 +111,17 @@ AbstractFile.prototype = {
    * unspecified, this is |buffer.byteLength|. Note that |bytes| is required
    * if |buffer| is a C pointer.
    *
    * @return {number} The number of bytes actually written.
    */
   write: function write(buffer, options = {}) {
 
     let {ptr, bytes} =
-      AbstractFile.normalizeToPointer(buffer, options.bytes || undefined);
+      SharedAll.normalizeToPointer(buffer, options.bytes || undefined);
 
     let pos = 0;
     while (pos < bytes) {
       let chunkSize = this._write(ptr, bytes - pos, options);
       pos += chunkSize;
       ptr = SharedAll.offsetBy(ptr, chunkSize);
     }
     return pos;
@@ -182,61 +182,16 @@ AbstractFile.openUnique = function openU
         // keep trying ...
       }
     }
     throw OS.File.Error.exists("could not find an unused file name.");
   }
 };
 
 /**
- * Utility function used to normalize a Typed Array or C
- * pointer into a uint8_t C pointer.
- *
- * Future versions might extend this to other data structures.
- *
- * @param {Typed array | C pointer} candidate The buffer. If
- * a C pointer, it must be non-null.
- * @param {number} bytes The number of bytes that |candidate| should contain.
- * Used for sanity checking if the size of |candidate| can be determined.
- *
- * @return {ptr:{C pointer}, bytes:number} A C pointer of type uint8_t,
- * corresponding to the start of |candidate|.
- */
-AbstractFile.normalizeToPointer = function normalizeToPointer(candidate, bytes) {
-  if (!candidate) {
-    throw new TypeError("Expecting  a Typed Array or a C pointer");
-  }
-  let ptr;
-  if ("isNull" in candidate) {
-    if (candidate.isNull()) {
-      throw new TypeError("Expecting a non-null pointer");
-    }
-    ptr = SharedAll.Type.uint8_t.out_ptr.cast(candidate);
-    if (bytes == null) {
-      throw new TypeError("C pointer missing bytes indication.");
-    }
-  } else if (SharedAll.isTypedArray(candidate)) {
-    // Typed Array
-    ptr = SharedAll.Type.uint8_t.out_ptr.implementation(candidate.buffer);
-    if (bytes == null) {
-      bytes = candidate.byteLength;
-    } else if (candidate.byteLength < bytes) {
-      throw new TypeError("Buffer is too short. I need at least " +
-                         bytes +
-                         " bytes but I have only " +
-                         candidate.byteLength +
-                          "bytes");
-    }
-  } else {
-    throw new TypeError("Expecting  a Typed Array or a C pointer");
-  }
-  return {ptr: ptr, bytes: bytes};
-};
-
-/**
  * Code shared by iterators.
  */
 AbstractFile.AbstractIterator = function AbstractIterator() {
 };
 AbstractFile.AbstractIterator.prototype = {
   /**
    * Allow iterating with |for|
    */