mirror of https://github.com/nodejs/node.git
27 lines
422 B
JavaScript
27 lines
422 B
JavaScript
'use strict';
|
|
|
|
class FreeList {
|
|
constructor(name, max, ctor) {
|
|
this.name = name;
|
|
this.ctor = ctor;
|
|
this.max = max;
|
|
this.list = [];
|
|
}
|
|
|
|
alloc() {
|
|
return this.list.length ?
|
|
this.list.pop() :
|
|
this.ctor.apply(this, arguments);
|
|
}
|
|
|
|
free(obj) {
|
|
if (this.list.length < this.max) {
|
|
this.list.push(obj);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = FreeList;
|