Skip to content

Instantly share code, notes, and snippets.

@tim-smart
Forked from ry/fast_buffer2.js
Created August 21, 2010 01:17
Show Gist options
  • Save tim-smart/541565 to your computer and use it in GitHub Desktop.
Save tim-smart/541565 to your computer and use it in GitHub Desktop.
var POOLSIZE = 8*1024;
var pool;
function allocPool () {
pool = new Buffer(POOLSIZE);
pool.used = 0;
}
function FastBuffer (subject, encoding, legacy, slice_legacy) {
var length, type;
// Are we slicing?
if (typeof legacy === 'number') {
this.parent = subject;
this.length = encoding;
this.offset = legacy;
legacy = slice_legacy;
} else {
// Find the length
switch (type = typeof subject) {
case 'number':
length = subject;
break;
case 'string':
case 'object': // Assume object is an array
length = subject.length;
break;
default:
throw new Error("First argument need to be an number, array or string.");
}
this.length = length;
if (length > POOLSIZE) {
// Big buffer, just alloc one.
this.parent = new Buffer(subject, encoding);
this.offset = 0;
} else {
// Small buffer.
if (!pool || pool.length - pool.used < length) allocPool();
this.parent = pool;
this.offset = pool.used;
pool.used += length;
// Do we need to write stuff?
if (type !== 'number') {
// Assume object is an array
if (type === 'object') {
for (var i = 0; i < length; i++) {
this.parent[i + this.offset] = subject[i];
}
} else {
// We are a string
this.write(subject, 0, encoding);
}
}
}
}
// Make sure the api is equivilent to old buffers, unless user doesn't
// want overhead
if (legacy !== false) {
Buffer.makeFastBuffer(this.parent, this, this.offset, this.length);
}
}
exports.FastBuffer = FastBuffer;
FastBuffer.prototype.get = function (i) {
if (i < 0 || i >= this.length) throw new Error("oob");
return this.parent[this.offset + i];
};
FastBuffer.prototype.set = function (i, v) {
if (i < 0 || i >= this.length) throw new Error("oob");
return this.parent[this.offset + i] = v;
};
// TODO define slice, toString, write, etc.
// slice should not use c++
// write(string, offset = 0, encoding = 'uft8')
FastBuffer.prototype.write = function write (string, offset, encoding) {
var max_length;
offset || (offset = 0);
encoding || (encoding = 'uft8');
// Make sure we are not going to overflow
max_length = this.length - offset;
if (string.length > max_length) {
string = string.slice(0, max_length);
}
return this.parent.write(string, this.offset + offset, encoding);
}
// toString(encoding, start=0, end=buffer.length)
FastBuffer.prototype.toString = function toSting (encoding, start, end) {
encoding || (encoding = 'utf8');
start || (start = 0);
end || (end = this.length);
// Make sure we aren't oob
if (end > this.length) {
end = this.length;
}
return this.parent.toString(encoding, start + this.offset, end + this.offset);
};
// byteLength
FastBuffer.prototype.byteLength = Buffer.prototype.byteLength;
// copy(targetBuffer, targetStart, sourceStart, sourceEnd=buffer.length)
FastBuffer.prototype.copy = function copy (target_buffer, target_start, start, end) {
start || (start = 0);
end || (end = this.length);
// Are we oob?
if (end > this.length) {
end = this.length;
}
return this.parent.copy(target_buffer, target_start, start + this.offset, end + this.offset);
};
// slice(start, end)
FastBuffer.prototype.slice = function slice (start, end, legacy) {
if (end > this.length) {
throw new Error("oob");
}
if (start > end) {
throw new Error("oob");
}
return new FastBuffer(this.parent, end - start, start + this.offset, legacy);
};
Handle<Value> Buffer::MakeFastBuffer(const Arguments &args) {
HandleScope scope;
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
Local<Object> fast_buffer = args[1]->ToObject();;
uint32_t offset = args[2]->Uint32Value();
uint32_t length = args[3]->Uint32Value();
fast_buffer->SetIndexedPropertiesToPixelData((uint8_t*)buffer->data() + offset,
length);
return Undefined();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment