Permalink
Browse files

Make IStreams collectable.

  • Loading branch information...
1 parent 1849081 commit 65dd5eff758665d1706cfebbc7b840c658d9e0a5 @stormbrew committed Oct 13, 2011
Showing with 106 additions and 78 deletions.
  1. +45 −0 ext/callable_context.cpp
  2. +43 −0 ext/callable_context.hpp
  3. +0 −38 ext/context.cpp
  4. +1 −32 ext/context.hpp
  5. +7 −0 ext/istream.cpp
  6. +10 −8 ext/istream.hpp
View
@@ -0,0 +1,45 @@
+#include "callable_context.hpp"
+#include "environment.hpp"
+
+#include <time.h>
+
+namespace Channel9
+{
+ std::vector<CallableContext*> CallableContext::contexts;
+ bool CallableContext::sweep_flag = false;
+
+ void CallableContext::scan()
+ {
+ m_sweep = !sweep_flag;
+ }
+
+ void CallableContext::sweep()
+ {
+ size_t total = 0, swept = 0;
+ time_t start = 0;
+ TRACE_DO(TRACE_GC, TRACE_INFO) {
+ total = contexts.size();
+ start = time(NULL);
+ }
+ context_set fresh_contexts;
+ fresh_contexts.reserve(contexts.size());
+
+ sweep_flag = !sweep_flag;
+ context_set::iterator it = contexts.begin();
+ while (it != contexts.end())
+ {
+ CallableContext *obj = *it++;
+ if (obj->m_sweep != sweep_flag)
+ {
+ TRACE_DO(TRACE_GC, TRACE_INFO) ++swept;
+ TRACE_PRINTF(TRACE_GC, TRACE_DEBUG, "Deleting CallableContext %p\n", obj);
+ delete obj;
+ } else {
+ fresh_contexts.push_back(obj);
+ }
+ }
+ swap(contexts, fresh_contexts);
+ TRACE_PRINTF(TRACE_GC, TRACE_INFO, "Deleted %i out of %i CallableContexts, leaving %i. Took %is, %fs/obj\n",
+ int(swept), int(total), int(total - swept), int(time(NULL)-start), double(time(NULL)-start)/total);
+ }
+}
View
@@ -0,0 +1,43 @@
+#pragma once
+
+#include "channel9.hpp"
+#include "environment.hpp"
+
+#include <assert.h>
+#include <stdio.h>
+#include <vector>
+
+namespace Channel9
+{
+ class CallableContext
+ {
+ private:
+ typedef std::vector<CallableContext*> context_set;
+ static context_set contexts;
+ static bool sweep_flag;
+
+ bool m_sweep;
+
+ public:
+ CallableContext() : m_sweep(sweep_flag)
+ { contexts.push_back(this); }
+
+ static void sweep();
+
+ virtual void send(Environment *env, const Value &val, const Value &ret) = 0;
+ virtual void scan(); // Make SURE to call down to this.
+ virtual ~CallableContext() {};
+ };
+
+ inline void gc_scan(CallableContext *ctx)
+ {
+ ctx->scan();
+ }
+
+ // Specialize GCRef's scanner to only do a gc_scan of it.
+ template <>
+ inline void GCRef<CallableContext*>::scan()
+ {
+ gc_scan(m_val);
+ }
+}
View
@@ -5,44 +5,6 @@
namespace Channel9
{
- std::vector<CallableContext*> CallableContext::contexts;
- bool CallableContext::sweep_flag = false;
-
- void CallableContext::scan()
- {
- m_sweep = !sweep_flag;
- }
-
- void CallableContext::sweep()
- {
- size_t total = 0, swept = 0;
- time_t start = 0;
- TRACE_DO(TRACE_GC, TRACE_INFO) {
- total = contexts.size();
- start = time(NULL);
- }
- context_set fresh_contexts;
- fresh_contexts.reserve(contexts.size());
-
- sweep_flag = !sweep_flag;
- context_set::iterator it = contexts.begin();
- while (it != contexts.end())
- {
- CallableContext *obj = *it++;
- if (obj->m_sweep != sweep_flag)
- {
- TRACE_DO(TRACE_GC, TRACE_INFO) ++swept;
- TRACE_PRINTF(TRACE_GC, TRACE_DEBUG, "Deleting CallableContext %p\n", obj);
- delete obj;
- } else {
- fresh_contexts.push_back(obj);
- }
- }
- swap(contexts, fresh_contexts);
- TRACE_PRINTF(TRACE_GC, TRACE_INFO, "Deleted %i out of %i CallableContexts, leaving %i. Took %is, %fs/obj\n",
- int(swept), int(total), int(total - swept), int(time(NULL)-start), double(time(NULL)-start)/total);
- }
-
void RunningContext::jump(const std::string &label)
{
m_pos = &*m_instructions->begin() + m_instructions->label_pos(label);
View
@@ -2,6 +2,7 @@
#include "channel9.hpp"
#include "environment.hpp"
+#include "callable_context.hpp"
#include "istream.hpp"
#include "memory_pool.hpp"
#include "variable_frame.hpp"
@@ -12,38 +13,6 @@
namespace Channel9
{
- class CallableContext
- {
- private:
- typedef std::vector<CallableContext*> context_set;
- static context_set contexts;
- static bool sweep_flag;
-
- bool m_sweep;
-
- public:
- CallableContext() : m_sweep(sweep_flag)
- { contexts.push_back(this); }
-
- static void sweep();
-
- virtual void send(Environment *env, const Value &val, const Value &ret) = 0;
- virtual void scan(); // Make SURE to call down to this.
- virtual ~CallableContext() {};
- };
-
- inline void gc_scan(CallableContext *ctx)
- {
- ctx->scan();
- }
-
- // Specialize GCRef's scanner to only do a gc_scan of it.
- template <>
- inline void GCRef<CallableContext*>::scan()
- {
- gc_scan(m_val);
- }
-
struct RunnableContext
{
IStream *m_instructions;
View
@@ -1,4 +1,5 @@
#include "istream.hpp"
+#include "context.hpp"
#include "message.hpp"
#include <assert.h>
@@ -234,5 +235,11 @@ namespace Channel9
{
return m_frames.size();
}
+
+ void IStream::send(Environment *env, const Value &val, const Value &ret)
+ {
+ channel_send(env, ret, Nil, Nil);
+ }
+
}
View
@@ -6,6 +6,7 @@
#include "channel9.hpp"
#include "instruction.hpp"
+#include "callable_context.hpp"
#include "value.hpp"
namespace Channel9
@@ -34,7 +35,7 @@ namespace Channel9
}
};
- class IStream
+ class IStream : public CallableContext
{
private:
std::map<std::string, size_t> m_labels;
@@ -97,16 +98,17 @@ namespace Channel9
iterator end() { return m_instructions.end(); }
const_iterator begin() const { return m_instructions.begin(); }
const_iterator end() const { return m_instructions.end(); }
- };
- inline void gc_scan(IStream *stream)
- {
- for (IStream::iterator it = stream->begin(); it != stream->end(); it++)
+ void scan()
{
- gc_scan(&*it);
+ for (IStream::iterator it = begin(); it != end(); it++)
+ {
+ gc_scan(&*it);
+ }
+ CallableContext::scan();
}
- }
-
+ void send(Environment *env, const Value &val, const Value &ret);
+ };
// Specialize GCRef's scanner to only do a gc_scan of it.
template <>
inline void GCRef<IStream*>::scan()

0 comments on commit 65dd5ef

Please sign in to comment.