Path: blob/trunk/third_party/closure/goog/async/freelist.js
4122 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Simple freelist.8*9* An anterative to goog.structs.SimplePool, it imposes the requirement that the10* objects in the list contain a "next" property that can be used to maintain11* the pool.12*/13goog.module('goog.async.FreeList');14goog.module.declareLegacyNamespace();1516/** @template ITEM */17class FreeList {18/**19* @param {function():ITEM} create20* @param {function(ITEM):void} reset21* @param {number} limit22*/23constructor(create, reset, limit) {24/** @private @const {number} */25this.limit_ = limit;26/** @private @const {function()} */27this.create_ = create;28/** @private @const {function(ITEM):void} */29this.reset_ = reset;3031/** @private {number} */32this.occupants_ = 0;33/** @private {ITEM} */34this.head_ = null;35}3637/** @return {ITEM} */38get() {39let item;40if (this.occupants_ > 0) {41this.occupants_--;42item = this.head_;43this.head_ = item.next;44item.next = null;45} else {46item = this.create_();47}48return item;49}5051/** @param {ITEM} item An item available for possible future reuse. */52put(item) {53this.reset_(item);54if (this.occupants_ < this.limit_) {55this.occupants_++;56item.next = this.head_;57this.head_ = item;58}59}6061/**62* Visible for testing.63* @return {number}64* @package65*/66occupants() {67return this.occupants_;68}69}7071exports = FreeList;727374