0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/lib/freelist.js

23 lines
622 B
JavaScript
Raw Normal View History

// This is a free list to avoid creating so many of the same object.
exports.FreeList = function(name, max, constructor) {
this.name = name;
this.constructor = constructor;
this.max = max;
this.list = [];
2010-10-07 05:05:23 +02:00
};
2010-12-02 02:43:30 +01:00
exports.FreeList.prototype.alloc = function() {
//debug("alloc " + this.name + " " + this.list.length);
2010-12-02 02:43:30 +01:00
return this.list.length ? this.list.shift() :
this.constructor.apply(this, arguments);
};
2010-12-02 02:43:30 +01:00
exports.FreeList.prototype.free = function(obj) {
//debug("free " + this.name + " " + this.list.length);
if (this.list.length < this.max) {
this.list.push(obj);
}
};