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