2015-02-14 19:53:34 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-11-22 17:04:46 +00:00
|
|
|
const {
|
|
|
|
ReflectApply,
|
|
|
|
} = primordials;
|
2018-10-01 15:11:25 +00:00
|
|
|
|
2017-02-15 21:49:51 +00:00
|
|
|
class FreeList {
|
|
|
|
constructor(name, max, ctor) {
|
|
|
|
this.name = name;
|
|
|
|
this.ctor = ctor;
|
|
|
|
this.max = max;
|
|
|
|
this.list = [];
|
|
|
|
}
|
2015-02-14 19:53:34 +00:00
|
|
|
|
2017-02-15 21:49:51 +00:00
|
|
|
alloc() {
|
2019-03-30 21:13:50 +00:00
|
|
|
return this.list.length > 0 ?
|
|
|
|
this.list.pop() :
|
2019-11-22 17:04:46 +00:00
|
|
|
ReflectApply(this.ctor, this, arguments);
|
2017-02-15 21:49:51 +00:00
|
|
|
}
|
2015-02-14 19:53:34 +00:00
|
|
|
|
2017-02-15 21:49:51 +00:00
|
|
|
free(obj) {
|
|
|
|
if (this.list.length < this.max) {
|
|
|
|
this.list.push(obj);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2015-02-14 19:53:34 +00:00
|
|
|
}
|
2017-02-15 21:49:51 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 21:13:50 +00:00
|
|
|
module.exports = FreeList;
|