Path: blob/trunk/third_party/closure/goog/async/freelist.js
2868 views
// Copyright 2015 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Simple freelist.16*17* An anterative to goog.structs.SimplePool, it imposes the requirement that the18* objects in the list contain a "next" property that can be used to maintain19* the pool.20*/2122goog.provide('goog.async.FreeList');232425/**26* @template ITEM27*/28goog.async.FreeList = goog.defineClass(null, {29/**30* @param {function():ITEM} create31* @param {function(ITEM):void} reset32* @param {number} limit33*/34constructor: function(create, reset, limit) {35/** @private @const {number} */36this.limit_ = limit;37/** @private @const {function()} */38this.create_ = create;39/** @private @const {function(ITEM):void} */40this.reset_ = reset;4142/** @private {number} */43this.occupants_ = 0;44/** @private {ITEM} */45this.head_ = null;46},4748/**49* @return {ITEM}50*/51get: function() {52var item;53if (this.occupants_ > 0) {54this.occupants_--;55item = this.head_;56this.head_ = item.next;57item.next = null;58} else {59item = this.create_();60}61return item;62},6364/**65* @param {ITEM} item An item available for possible future reuse.66*/67put: function(item) {68this.reset_(item);69if (this.occupants_ < this.limit_) {70this.occupants_++;71item.next = this.head_;72this.head_ = item;73}74},7576/**77* Visible for testing.78* @package79* @return {number}80*/81occupants: function() { return this.occupants_; }82});838485