Files
cocos4/cocos/3d/memop/pool.js
T
2018-09-04 16:02:46 +08:00

45 lines
865 B
JavaScript

export default class Pool {
constructor(fn, size) {
this._fn = fn;
this._idx = size - 1;
this._frees = new Array(size);
for (let i = 0; i < size; ++i) {
this._frees[i] = fn();
}
}
_expand(size) {
let old = this._frees;
this._frees = new Array(size);
let len = size - old.length;
for (let i = 0; i < len; ++i) {
this._frees[i] = this._fn();
}
for (let i = len, j = 0; i < size; ++i, ++j) {
this._frees[i] = old[j];
}
this._idx += len;
}
alloc() {
// create some more space (expand by 20%, minimum 1)
if (this._idx < 0) {
this._expand(Math.round(this._frees.length * 1.2) + 1);
}
let ret = this._frees[this._idx];
this._frees[this._idx] = null;
--this._idx;
return ret;
}
free(obj) {
++this._idx;
this._frees[this._idx] = obj;
}
}