forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject-pool.js
More file actions
34 lines (29 loc) · 838 Bytes
/
object-pool.js
File metadata and controls
34 lines (29 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
pc.extend(pc, (function () {
var AllocatePool = function (constructor, size) {
this._constructor = constructor;
this._pool = [];
this._count = 0;
this._resize(size);
};
AllocatePool.prototype = {
_resize: function (size) {
if (size > this._pool.length) {
for (var i = this._pool.length; i < size; i++) {
this._pool[i] = new this._constructor();
}
}
},
allocate: function () {
if (this._count >= this._pool.length) {
this._resize(this._pool.length*2);
}
return this._pool[this._count++];
},
freeAll: function () {
this._count = 0;
}
};
return {
AllocatePool: AllocatePool
};
}()));