mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
555 B
25 lines
555 B
10 years ago
|
'use strict';
|
||
|
|
||
|
// 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 = [];
|
||
|
};
|
||
|
|
||
|
|
||
|
exports.FreeList.prototype.alloc = function() {
|
||
|
return this.list.length ? this.list.shift() :
|
||
|
this.constructor.apply(this, arguments);
|
||
|
};
|
||
|
|
||
|
|
||
|
exports.FreeList.prototype.free = function(obj) {
|
||
|
if (this.list.length < this.max) {
|
||
|
this.list.push(obj);
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
};
|