From a36bd7fbafcd41e91f14c884f7b462c4800bfb8f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 17 Sep 2010 20:52:27 -0700 Subject: [PATCH] vm: don't keep more than 10 unused contexts around to prevent address space wastage on low-budget operating systems like OpenBSD --- vm/contexts.cpp | 11 +++++++++-- vm/master.hpp | 1 + vm/vm.hpp | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/vm/contexts.cpp b/vm/contexts.cpp index 0ddd51e1be..3d3008c2ab 100644 --- a/vm/contexts.cpp +++ b/vm/contexts.cpp @@ -111,8 +111,8 @@ void factor_vm::init_contexts(cell datastack_size_, cell retainstack_size_, cell void factor_vm::delete_contexts() { assert(!ctx); - std::vector::const_iterator iter = unused_contexts.begin(); - std::vector::const_iterator end = unused_contexts.end(); + std::list::const_iterator iter = unused_contexts.begin(); + std::list::const_iterator end = unused_contexts.end(); while(iter != end) { delete *iter; @@ -159,6 +159,13 @@ void factor_vm::delete_context(context *old_context) { unused_contexts.push_back(old_context); active_contexts.erase(old_context); + + while(unused_contexts.size() > 10) + { + context *stale_context = unused_contexts.front(); + unused_contexts.pop_front(); + delete stale_context; + } } VM_C_API void delete_context(factor_vm *parent, context *old_context) diff --git a/vm/master.hpp b/vm/master.hpp index d2b9a5f709..d4cd70f867 100755 --- a/vm/master.hpp +++ b/vm/master.hpp @@ -24,6 +24,7 @@ /* C++ headers */ #include +#include #include #include #include diff --git a/vm/vm.hpp b/vm/vm.hpp index 2cbedbcc7b..d9c7186c4e 100755 --- a/vm/vm.hpp +++ b/vm/vm.hpp @@ -35,7 +35,7 @@ struct factor_vm int callback_id; /* Pooling unused contexts to make context allocation cheaper */ - std::vector unused_contexts; + std::list unused_contexts; /* Active contexts, for tracing by the GC */ std::set active_contexts;