summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJames M Snell <jasnell@gmail.com>2016-03-21 12:38:08 -0700
committerJames M Snell <jasnell@gmail.com>2016-04-15 10:36:01 -0700
commit627524973a22c584fdd06c951fbe82364927a1ed (patch)
treeabd6cf987b5cd834bedeb3fd43c2e9b71880d920 /lib
parentb488b19eaf2b2e7a3ca5eccd2445e245847a5f76 (diff)
downloadandroid-node-v8-627524973a22c584fdd06c951fbe82364927a1ed.tar.gz
android-node-v8-627524973a22c584fdd06c951fbe82364927a1ed.tar.bz2
android-node-v8-627524973a22c584fdd06c951fbe82364927a1ed.zip
buffer: add Buffer.allocUnsafeSlow(size)
Aligns the functionality of SlowBuffer with the new Buffer constructor API. Next step is to docs-only deprecate SlowBuffer. Replace the internal uses of SlowBuffer with `Buffer.allocUnsafeSlow(size)` PR-URL: https://github.com/nodejs/node/pull/5833 Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/buffer.js29
-rw-r--r--lib/fs.js5
2 files changed, 27 insertions, 7 deletions
diff --git a/lib/buffer.js b/lib/buffer.js
index f672dae558..1d0963bb50 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -133,13 +133,23 @@ Buffer.from = function(value, encodingOrOffset, length) {
Object.setPrototypeOf(Buffer.prototype, Uint8Array.prototype);
Object.setPrototypeOf(Buffer, Uint8Array);
+function assertSize(size) {
+ if (typeof size !== 'number') {
+ const err = new TypeError('"size" argument must be a number');
+ // The following hides the 'assertSize' method from the
+ // callstack. This is done simply to hide the internal
+ // details of the implementation from bleeding out to users.
+ Error.captureStackTrace(err, assertSize);
+ throw err;
+ }
+}
+
/**
* Creates a new filled Buffer instance.
* alloc(size[, fill[, encoding]])
**/
Buffer.alloc = function(size, fill, encoding) {
- if (typeof size !== 'number')
- throw new TypeError('"size" argument must be a number');
+ assertSize(size);
if (size <= 0)
return createBuffer(size);
if (fill !== undefined) {
@@ -161,11 +171,22 @@ Buffer.alloc = function(size, fill, encoding) {
* instance. If `--zero-fill-buffers` is set, will zero-fill the buffer.
**/
Buffer.allocUnsafe = function(size) {
- if (typeof size !== 'number')
- throw new TypeError('"size" argument must be a number');
+ assertSize(size);
return allocate(size);
};
+/**
+ * Equivalent to SlowBuffer(num), by default creates a non-zero-filled
+ * Buffer instance that is not allocated off the pre-initialized pool.
+ * If `--zero-fill-buffers` is set, will zero-fill the buffer.
+ **/
+Buffer.allocUnsafeSlow = function(size) {
+ assertSize(size);
+ if (size > 0)
+ flags[kNoZeroFill] = 1;
+ return createBuffer(size);
+};
+
// If --zero-fill-buffers command line argument is set, a zero-filled
// buffer is returned.
function SlowBuffer(length) {
diff --git a/lib/fs.js b/lib/fs.js
index 3047e5ad98..e441746366 100644
--- a/lib/fs.js
+++ b/lib/fs.js
@@ -3,7 +3,6 @@
'use strict';
-const SlowBuffer = require('buffer').SlowBuffer;
const util = require('util');
const pathModule = require('path');
@@ -321,7 +320,7 @@ ReadFileContext.prototype.read = function() {
var length;
if (this.size === 0) {
- buffer = this.buffer = SlowBuffer(kReadFileBufferLength);
+ buffer = this.buffer = Buffer.allocUnsafeSlow(kReadFileBufferLength);
offset = 0;
length = kReadFileBufferLength;
} else {
@@ -389,7 +388,7 @@ function readFileAfterStat(err, st) {
return context.close(err);
}
- context.buffer = SlowBuffer(size);
+ context.buffer = Buffer.allocUnsafeSlow(size);
context.read();
}