mirror of
https://github.com/nodejs/node.git
synced 2024-12-01 16:10:02 +01:00
2db758c562
Internal modules can be used to share private code between public modules without risk to expose private APIs to the user. PR-URL: https://github.com/iojs/io.js/pull/848 Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
25 lines
555 B
JavaScript
25 lines
555 B
JavaScript
'use strict';
|
|
|
|
// This is a free list to avoid creating so many of the same object.
|
|
exports.FreeList = function(name, max, constructor) {
|
|
this.name = name;
|
|
this.constructor = constructor;
|
|
this.max = max;
|
|
this.list = [];
|
|
};
|
|
|
|
|
|
exports.FreeList.prototype.alloc = function() {
|
|
return this.list.length ? this.list.shift() :
|
|
this.constructor.apply(this, arguments);
|
|
};
|
|
|
|
|
|
exports.FreeList.prototype.free = function(obj) {
|
|
if (this.list.length < this.max) {
|
|
this.list.push(obj);
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|