Skip to content

Instantly share code, notes, and snippets.

@ry
Created August 21, 2010 00:53
Show Gist options
  • Save ry/541543 to your computer and use it in GitHub Desktop.
Save ry/541543 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 (length) {
this.length = length;
if (length > POOLSIZE) {
// Big buffer, just alloc one.
this.parent = new Buffer(length);
this.offset = 0;
} else {
// Small buffer.
if (!pool || pool.length - pool.used < length) allocPool();
this.parent = pool;
this.offset = pool.used;
pool.used += length;
}
// HERE HERE HERE
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++
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