Move unref of input Buffer to C++ #138

This commit is contained in:
Lovell Fuller 2014-12-14 10:31:25 +00:00
parent 47241db789
commit f026a835fd
2 changed files with 7 additions and 7 deletions

View File

@ -18,6 +18,7 @@ var Sharp = function(input) {
stream.Duplex.call(this); stream.Duplex.call(this);
this.options = { this.options = {
// input options // input options
bufferIn: null,
streamIn: false, streamIn: false,
sequentialRead: false, sequentialRead: false,
// ICC profiles // ICC profiles
@ -95,16 +96,16 @@ Sharp.prototype._write = function(chunk, encoding, callback) {
/*jslint unused: false */ /*jslint unused: false */
if (this.options.streamIn) { if (this.options.streamIn) {
if (typeof chunk === 'object' && chunk instanceof Buffer) { if (typeof chunk === 'object' && chunk instanceof Buffer) {
if (typeof this.options.bufferIn === 'undefined') { if (this.options.bufferIn instanceof Buffer) {
// Create new Buffer
this.options.bufferIn = new Buffer(chunk.length);
chunk.copy(this.options.bufferIn);
} else {
// Append to existing Buffer // Append to existing Buffer
this.options.bufferIn = Buffer.concat( this.options.bufferIn = Buffer.concat(
[this.options.bufferIn, chunk], [this.options.bufferIn, chunk],
this.options.bufferIn.length + chunk.length this.options.bufferIn.length + chunk.length
); );
} else {
// Create new Buffer
this.options.bufferIn = new Buffer(chunk.length);
chunk.copy(this.options.bufferIn);
} }
callback(); callback();
} else { } else {
@ -506,8 +507,6 @@ Sharp.prototype._sharp = function(callback) {
}); });
} }
} }
// A copy is taken of the Buffer so let V8 garbage collect ours
this.options.bufferIn = null;
}; };
/* /*

View File

@ -841,6 +841,7 @@ NAN_METHOD(resize) {
baton->bufferInLength = node::Buffer::Length(buffer); baton->bufferInLength = node::Buffer::Length(buffer);
baton->bufferIn = g_malloc(baton->bufferInLength); baton->bufferIn = g_malloc(baton->bufferInLength);
memcpy(baton->bufferIn, node::Buffer::Data(buffer), baton->bufferInLength); memcpy(baton->bufferIn, node::Buffer::Data(buffer), baton->bufferInLength);
options->Set(NanNew<String>("bufferIn"), NanNull());
} }
// ICC profile to use when input CMYK image has no embedded profile // ICC profile to use when input CMYK image has no embedded profile
baton->iccProfilePath = *String::Utf8Value(options->Get(NanNew<String>("iccProfilePath"))->ToString()); baton->iccProfilePath = *String::Utf8Value(options->Get(NanNew<String>("iccProfilePath"))->ToString());