summaryrefslogtreecommitdiff
path: root/lib/freelist.js
diff options
context:
space:
mode:
authorMicheil Smith <micheil@brandedcode.com>2010-04-13 02:27:32 +1000
committerRyan Dahl <ry@tinyclouds.org>2010-04-12 16:57:45 -0700
commit57ea07ac912d88fb947a95ffd502c23f2d60f8b9 (patch)
tree94c91e315c4358ce07416851c223956d44ed56d4 /lib/freelist.js
parentb7947e45c092df8b032f3b689a020ed972ef485a (diff)
downloadnode-new-57ea07ac912d88fb947a95ffd502c23f2d60f8b9.tar.gz
Moving the http.js, net.js FreeList to being standalone.
Diffstat (limited to 'lib/freelist.js')
-rw-r--r--lib/freelist.js22
1 files changed, 22 insertions, 0 deletions
diff --git a/lib/freelist.js b/lib/freelist.js
new file mode 100644
index 0000000000..a09fb4b21e
--- /dev/null
+++ b/lib/freelist.js
@@ -0,0 +1,22 @@
+// 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 () {
+ //debug("alloc " + this.name + " " + this.list.length);
+ return this.list.length ? this.list.shift()
+ : this.constructor.apply(this, arguments);
+};
+
+
+exports.FreeList.prototype.free = function (obj) {
+ //debug("free " + this.name + " " + this.list.length);
+ if (this.list.length < this.max) {
+ this.list.push(obj);
+ }
+};