diff options
| author | Trevor Norris <trev.norris@gmail.com> | 2013-05-15 11:01:33 -0700 |
|---|---|---|
| committer | Trevor Norris <trev.norris@gmail.com> | 2013-06-18 15:39:32 -0700 |
| commit | fb40da822fa8bb6da588af1989aaf32ef01d0a17 (patch) | |
| tree | e039eceebaff60372da7b89d3e4846898e691be1 /lib/buffer.js | |
| parent | 456942a920fe313ebe0b0da366d26ef400ec177e (diff) | |
| download | node-fb40da822fa8bb6da588af1989aaf32ef01d0a17.tar.gz | |
buffer: expose class methods alloc and dispose
Expose the ability for users to allocate and manually dispose data on
any object. These are user-safe versions of internal smalloc functions.
Diffstat (limited to 'lib/buffer.js')
| -rw-r--r-- | lib/buffer.js | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/buffer.js b/lib/buffer.js index e0ce4c545..a21fb79f2 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -176,6 +176,26 @@ Buffer.concat = function(list, length) { }; +Buffer.alloc = function(n, obj) { + n = ~~n; + if (n < 0) n = 0; + + if (n > kMaxLength) + throw new RangeError('n > kMaxLength'); + if (Array.isArray(obj)) + throw new TypeError('Arrays are not supported'); + + return alloc(obj != null ? obj : {}, n); +}; + + +Buffer.dispose = function(obj) { + if (obj instanceof Buffer) + throw new TypeError('obj cannot be a Buffer'); + smalloc.dispose(obj); +}; + + // toString(encoding, start=0, end=buffer.length) Buffer.prototype.toString = function(encoding, start, end) { encoding = !!encoding ? (encoding + '').toLowerCase() : 'utf8'; |
