2015-02-14 20:53:34 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-10-01 17:11:25 +02:00
|
|
|
const is_reused_symbol = Symbol('isReused');
|
|
|
|
|
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() {
|
2018-10-01 17:11:25 +02:00
|
|
|
let item;
|
|
|
|
if (this.list.length > 0) {
|
|
|
|
item = this.list.pop();
|
|
|
|
item[is_reused_symbol] = true;
|
|
|
|
} else {
|
|
|
|
item = this.ctor.apply(this, arguments);
|
|
|
|
item[is_reused_symbol] = false;
|
|
|
|
}
|
|
|
|
return item;
|
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
|
|
|
}
|
|
|
|
|
2018-10-01 17:11:25 +02:00
|
|
|
module.exports = {
|
|
|
|
FreeList,
|
|
|
|
symbols: {
|
|
|
|
is_reused_symbol
|
|
|
|
}
|
|
|
|
};
|