Debian Codegraph @

closure-library/closure/goog/structs/simplepool.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Datastructure: Pool.
17 *
18 *
19 * A generic class for handling pools of objects that is more efficient than
20 * goog.structs.Pool because it doesn't maintain a list of objects that are in
21 * use. See constructor comment.
22 */
23
24
25 goog.provide('goog.structs.SimplePool');
26
27 goog.require('goog.Disposable');
28
29
30
31 /**
32 * A generic pool class. Simpler and more efficient than goog.structs.Pool
33 * because it doesn't maintain a list of objects that are in use. This class
34 * has constant overhead and doesn't create any additional objects as part of
35 * the pool management after construction time.
36 *
37 * IMPORTANT: If the objects being pooled are arrays or maps that can have
38 * unlimited number of properties, they need to be cleaned before being
39 * returned to the pool.
40 *
41 * Also note that {@see goog.object.clean} actually allocates an array to clean
42 * the object passed to it, so simply using this function would defy the
43 * purpose of using the pool.
44 *
45 * @param {number} initialCount Initial number of objects to populate the free
46 * pool at construction time.
47 * @param {number} maxCount Maximum number of objects to keep in the free pool.
48 * @constructor
49 * @extends {goog.Disposable}
50 * @template T
51 */
52 goog.structs.SimplePool = function(initialCount, maxCount) {
53 goog.Disposable.call(this);
54
55 /**
56 * Function for overriding createObject. The avoids a common case requiring
57 * subclassing this class.
58 * @private {Function}
59 */
60 this.createObjectFn_ = null;
61
62 /**
63 * Function for overriding disposeObject. The avoids a common case requiring
64 * subclassing this class.
65 * @private {Function}
66 */
67 this.disposeObjectFn_ = null;
68
69 /**
70 * Maximum number of objects allowed
71 * @private {number}
72 */
73 this.maxCount_ = maxCount;
74
75 /**
76 * Queue used to store objects that are currently in the pool and available
77 * to be used.
78 * @private {Array<T>}
79 */
80 this.freeQueue_ = [];
81
82 this.createInitial_(initialCount);
83 };
84 goog.inherits(goog.structs.SimplePool, goog.Disposable);
85
86
87 /**
88 * Sets the {@code createObject} function which is used for creating a new
89 * object in the pool.
90 * @param {Function} createObjectFn Create object function which returns the
91 * newly created object.
92 */
93 goog.structs.SimplePool.prototype.setCreateObjectFn = function(createObjectFn) {
94 this.createObjectFn_ = createObjectFn;
95 };
96
97
98 /**
99 * Sets the {@code disposeObject} function which is used for disposing of an
100 * object in the pool.
101 * @param {Function} disposeObjectFn Dispose object function which takes the
102 * object to dispose as a parameter.
103 */
104 goog.structs.SimplePool.prototype.setDisposeObjectFn = function(
105 disposeObjectFn) {
106 this.disposeObjectFn_ = disposeObjectFn;
107 };
108
109
110 /**
111 * Gets an unused object from the the pool, if there is one available,
112 * otherwise creates a new one.
113 * @return {T} An object from the pool or a new one if necessary.
114 */
115 goog.structs.SimplePool.prototype.getObject = function() {
116 if (this.freeQueue_.length) {
117 return this.freeQueue_.pop();
118 }
119 return this.createObject();
120 };
121
122
123 /**
124 * Returns an object to the pool so that it can be reused. If the pool is
125 * already full, the object is disposed instead.
126 * @param {T} obj The object to release.
127 */
128 goog.structs.SimplePool.prototype.releaseObject = function(obj) {
129 if (this.freeQueue_.length < this.maxCount_) {
130 this.freeQueue_.push(obj);
131 } else {
132 this.disposeObject(obj);
133 }
134 };
135
136
137 /**
138 * Populates the pool with initialCount objects.
139 * @param {number} initialCount The number of objects to add to the pool.
140 * @private
141 */
142 goog.structs.SimplePool.prototype.createInitial_ = function(initialCount) {
143 if (initialCount > this.maxCount_) {
144 throw new Error(
145 '[goog.structs.SimplePool] Initial cannot be greater than max');
146 }
147 for (var i = 0; i < initialCount; i++) {
148 this.freeQueue_.push(this.createObject());
149 }
150 };
151
152
153 /**
154 * Should be overridden by sub-classes to return an instance of the object type
155 * that is expected in the pool.
156 * @return {T} The created object.
157 */
158 goog.structs.SimplePool.prototype.createObject = function() {
159 if (this.createObjectFn_) {
160 return this.createObjectFn_();
161 } else {
162 return {};
163 }
164 };
165
166
167 /**
168 * Should be overrideen to dispose of an object. Default implementation is to
169 * remove all of the object's members, which should render it useless. Calls the
170 * object's dispose method, if available.
171 * @param {T} obj The object to dispose.
172 */
173 goog.structs.SimplePool.prototype.disposeObject = function(obj) {
174 if (this.disposeObjectFn_) {
175 this.disposeObjectFn_(obj);
176 } else if (goog.isObject(obj)) {
177 if (goog.isFunction(obj.dispose)) {
178 obj.dispose();
179 } else {
180 for (var i in obj) {
181 delete obj[i];
182 }
183 }
184 }
185 };
186
187
188 /**
189 * Disposes of the pool and all objects currently held in the pool.
190 * @override
191 * @protected
192 */
193 goog.structs.SimplePool.prototype.disposeInternal = function() {
194 goog.structs.SimplePool.superClass_.disposeInternal.call(this);
195 // Call disposeObject on each object held by the pool.
196 var freeQueue = this.freeQueue_;
197 while (freeQueue.length) {
198 this.disposeObject(freeQueue.pop());
199 }
200 delete this.freeQueue_;
201 };