mirror of https://github.com/lukechilds/node.git
Browse Source
PR-URL: https://github.com/nodejs/node/pull/11406 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>v6
James M Snell
8 years ago
1 changed files with 20 additions and 19 deletions
@ -1,24 +1,25 @@ |
|||||
'use strict'; |
'use strict'; |
||||
|
|
||||
// This is a free list to avoid creating so many of the same object.
|
class FreeList { |
||||
exports.FreeList = function(name, max, constructor) { |
constructor(name, max, ctor) { |
||||
this.name = name; |
this.name = name; |
||||
this.constructor = constructor; |
this.ctor = ctor; |
||||
this.max = max; |
this.max = max; |
||||
this.list = []; |
this.list = []; |
||||
}; |
} |
||||
|
|
||||
|
|
||||
exports.FreeList.prototype.alloc = function() { |
alloc() { |
||||
return this.list.length ? this.list.pop() : |
return this.list.length ? this.list.pop() : |
||||
this.constructor.apply(this, arguments); |
this.ctor.apply(this, arguments); |
||||
}; |
} |
||||
|
|
||||
|
|
||||
exports.FreeList.prototype.free = function(obj) { |
free(obj) { |
||||
if (this.list.length < this.max) { |
if (this.list.length < this.max) { |
||||
this.list.push(obj); |
this.list.push(obj); |
||||
return true; |
return true; |
||||
} |
} |
||||
return false; |
return false; |
||||
}; |
} |
||||
|
} |
||||
|
|
||||
|
module.exports = {FreeList}; |
||||
|
Loading…
Reference in new issue