diff --git a/include/chaiscript/dispatchkit/boxed_value.hpp b/include/chaiscript/dispatchkit/boxed_value.hpp index 62c38a6..4a538ec 100644 --- a/include/chaiscript/dispatchkit/boxed_value.hpp +++ b/include/chaiscript/dispatchkit/boxed_value.hpp @@ -116,6 +116,11 @@ namespace chaiscript */ struct Object_Cache { + Object_Cache() + : m_cullcount(0) + { + } + boost::shared_ptr get(Boxed_Value::Void_Type) { return boost::shared_ptr (new Data( @@ -219,6 +224,12 @@ namespace chaiscript */ void cull() { + ++m_cullcount; + if (m_cullcount % 10 != 0) + { + return; + } + std::map::iterator itr = m_ptrs.begin(); while (itr != m_ptrs.end()) @@ -235,6 +246,7 @@ namespace chaiscript } std::map m_ptrs; + int m_cullcount; }; public: @@ -472,7 +484,7 @@ namespace chaiscript template<> struct Cast_Helper { - typedef Boxed_Value Result_Type; + typedef const Boxed_Value & Result_Type; static Result_Type cast(const Boxed_Value &ob) { @@ -486,7 +498,7 @@ namespace chaiscript template<> struct Cast_Helper { - typedef Boxed_Value Result_Type; + typedef const Boxed_Value & Result_Type; static Result_Type cast(const Boxed_Value &ob) { diff --git a/include/chaiscript/dispatchkit/dispatchkit.hpp b/include/chaiscript/dispatchkit/dispatchkit.hpp index edbd7ac..3b21b9b 100644 --- a/include/chaiscript/dispatchkit/dispatchkit.hpp +++ b/include/chaiscript/dispatchkit/dispatchkit.hpp @@ -133,12 +133,13 @@ namespace chaiscript public: typedef std::map Type_Name_Map; typedef std::map Scope; - typedef std::deque Stack; + typedef boost::shared_ptr > Stack; Dispatch_Engine() - : m_place_holder(boost::shared_ptr(new Placeholder_Object())) + : m_scopes(new Stack::element_type()), + m_place_holder(boost::shared_ptr(new Placeholder_Object())) { - m_scopes.push_back(Scope()); + m_scopes->push_back(Scope()); } /** @@ -163,12 +164,12 @@ namespace chaiscript */ void add(const Boxed_Value &obj, const std::string &name) { - for (int i = m_scopes.size()-1; i >= 0; --i) + for (int i = m_scopes->size()-1; i >= 0; --i) { - std::map::const_iterator itr = m_scopes[i].find(name); - if (itr != m_scopes[i].end()) + std::map::const_iterator itr = (*m_scopes)[i].find(name); + if (itr != (*m_scopes)[i].end()) { - m_scopes[i][name] = Boxed_Value(obj); + (*m_scopes)[i][name] = Boxed_Value(obj); return; } } @@ -181,7 +182,7 @@ namespace chaiscript */ void add_object(const std::string &name, const Boxed_Value &obj) { - m_scopes.back()[name] = Boxed_Value(obj); + m_scopes->back()[name] = Boxed_Value(obj); } /** @@ -189,7 +190,7 @@ namespace chaiscript */ void new_scope() { - m_scopes.push_back(Scope()); + m_scopes->push_back(Scope()); } /** @@ -197,9 +198,9 @@ namespace chaiscript */ void pop_scope() { - if (m_scopes.size() > 1) + if (m_scopes->size() > 1) { - m_scopes.pop_back(); + m_scopes->pop_back(); } else { throw std::range_error("Unable to pop global stack"); } @@ -218,10 +219,16 @@ namespace chaiscript * \returns the old stack * \param[in] s The new stack */ - Stack set_stack(Stack s) + Stack set_stack(const Stack &s) { - std::swap(s, m_scopes); - return s; + Stack old = m_scopes; + m_scopes = s; + return old; + } + + Stack new_stack() + { + return Stack(new Stack::element_type()); } /** @@ -236,10 +243,10 @@ namespace chaiscript return m_place_holder; } - for (int i = m_scopes.size()-1; i >= 0; --i) + for (int i = m_scopes->size()-1; i >= 0; --i) { - std::map::const_iterator itr = m_scopes[i].find(name); - if (itr != m_scopes[i].end()) + std::map::const_iterator itr = (*m_scopes)[i].find(name); + if (itr != (*m_scopes)[i].end()) { return itr->second; } @@ -377,7 +384,7 @@ namespace chaiscript return true; } - std::deque m_scopes; + Stack m_scopes; std::multimap m_functions; Type_Name_Map m_types; diff --git a/include/chaiscript/language/chaiscript_eval.hpp b/include/chaiscript/language/chaiscript_eval.hpp index 2ef56cc..4d3f981 100644 --- a/include/chaiscript/language/chaiscript_eval.hpp +++ b/include/chaiscript/language/chaiscript_eval.hpp @@ -405,10 +405,10 @@ namespace chaiscript Boxed_Value retval; Param_List_Builder plb; Dispatch_Engine::Stack prev_stack = ss.get_stack(); - Dispatch_Engine::Stack new_stack; + Dispatch_Engine::Stack new_stack = ss.new_stack(); unsigned int i; - new_stack.push_back(Dispatch_Engine::Scope()); + new_stack->push_back(Dispatch_Engine::Scope()); if ((node->children.size() > 1) && (node->children[1]->identifier == Token_Type::Arg_List)) { for (i = 0; i < node->children[1]->children.size(); ++i) { @@ -452,10 +452,10 @@ namespace chaiscript Boxed_Value retval; std::vector > fn; Dispatch_Engine::Stack prev_stack = ss.get_stack(); - Dispatch_Engine::Stack new_stack; + Dispatch_Engine::Stack new_stack = ss.new_stack(); unsigned int i, j; - new_stack.push_back(Dispatch_Engine::Scope()); + new_stack->push_back(Dispatch_Engine::Scope()); //todo: Please extract a single way of doing function calls between this and eval_fun_call