Moving the http.js, net.js FreeList to being standalone.
[platform/upstream/nodejs.git] / lib / freelist.js
1 // This is a free list to avoid creating so many of the same object.
2 exports.FreeList = function(name, max, constructor) {
3   this.name = name;
4   this.constructor = constructor;
5   this.max = max;
6   this.list = [];
7 }
8
9
10 exports.FreeList.prototype.alloc = function () {
11   //debug("alloc " + this.name + " " + this.list.length);
12   return this.list.length ? this.list.shift()
13                           : this.constructor.apply(this, arguments);
14 };
15
16
17 exports.FreeList.prototype.free = function (obj) {
18   //debug("free " + this.name + " " + this.list.length);
19   if (this.list.length < this.max) {
20     this.list.push(obj);
21   }
22 };