about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJon Morton <jonanin@gmail.com>2012-04-02 22:18:01 -0500
committerBrian Anderson <banderson@mozilla.com>2012-04-03 16:02:38 -0700
commit632a4c9326561de2bd9d9ded19a82f22678a0cf4 (patch)
treee5c5c509b2679cab824819c1d6340e44986d950d
parent704ca046a13977175975cb20a77ea2fbac15d77a (diff)
downloadrust-632a4c9326561de2bd9d9ded19a82f22678a0cf4.tar.gz
rust-632a4c9326561de2bd9d9ded19a82f22678a0cf4.zip
Refactor includes structure, getting rid of rust_internal.h
Many changes to code structure are included:
- removed TIME_SLICE_IN_MS
- removed sychronized_indexed_list
- removed region_owned
- kernel_owned move to kernel.h, task_owned moved to task.h
- global configs moved to rust_globals.h
- changed #pragma once to standard guard in rust_upcall.h
- got rid of memory.h
-rw-r--r--src/rt/arch/i386/context.cpp8
-rw-r--r--src/rt/arch/x86_64/context.cpp8
-rw-r--r--src/rt/boxed_region.cpp6
-rw-r--r--src/rt/circular_buffer.cpp4
-rw-r--r--src/rt/circular_buffer.h3
-rw-r--r--src/rt/memory.h34
-rw-r--r--src/rt/memory_region.cpp4
-rw-r--r--src/rt/memory_region.h2
-rw-r--r--src/rt/rust.cpp5
-rw-r--r--src/rt/rust.h41
-rw-r--r--src/rt/rust_box_annihilator.cpp4
-rw-r--r--src/rt/rust_builtin.cpp2
-rw-r--r--src/rt/rust_cc.cpp14
-rw-r--r--src/rt/rust_debug.cpp4
-rw-r--r--src/rt/rust_env.cpp2
-rw-r--r--src/rt/rust_env.h8
-rw-r--r--src/rt/rust_globals.h37
-rw-r--r--src/rt/rust_internal.h183
-rw-r--r--src/rt/rust_kernel.cpp5
-rw-r--r--src/rt/rust_kernel.h21
-rw-r--r--src/rt/rust_log.cpp7
-rw-r--r--src/rt/rust_log.h2
-rw-r--r--src/rt/rust_port.cpp4
-rw-r--r--src/rt/rust_port.h3
-rw-r--r--src/rt/rust_port_selector.cpp2
-rw-r--r--src/rt/rust_port_selector.h3
-rw-r--r--src/rt/rust_refcount.h31
-rw-r--r--src/rt/rust_run_program.cpp3
-rw-r--r--src/rt/rust_sched_driver.cpp4
-rw-r--r--src/rt/rust_sched_launcher.cpp1
-rw-r--r--src/rt/rust_sched_launcher.h3
-rw-r--r--src/rt/rust_sched_loop.cpp6
-rw-r--r--src/rt/rust_sched_loop.h8
-rw-r--r--src/rt/rust_sched_reaper.cpp2
-rw-r--r--src/rt/rust_scheduler.cpp3
-rw-r--r--src/rt/rust_scheduler.h4
-rw-r--r--src/rt/rust_shape.cpp8
-rw-r--r--src/rt/rust_shape.h3
-rw-r--r--src/rt/rust_stack.cpp2
-rw-r--r--src/rt/rust_stack.h3
-rw-r--r--src/rt/rust_task.cpp9
-rw-r--r--src/rt/rust_task.h40
-rw-r--r--src/rt/rust_type.h72
-rw-r--r--src/rt/rust_upcall.cpp4
-rw-r--r--src/rt/rust_upcall.h5
-rw-r--r--src/rt/rust_util.h3
-rw-r--r--src/rt/rust_uv.cpp4
-rw-r--r--src/rt/sync/lock_and_signal.cpp5
-rw-r--r--src/rt/sync/lock_and_signal.h2
-rw-r--r--src/rt/sync/lock_free_queue.h1
-rw-r--r--src/rt/sync/rust_thread.cpp2
-rw-r--r--src/rt/sync/rust_thread.h2
-rw-r--r--src/rt/util/array_list.h3
-rw-r--r--src/rt/util/synchronized_indexed_list.h74
54 files changed, 311 insertions, 412 deletions
diff --git a/src/rt/arch/i386/context.cpp b/src/rt/arch/i386/context.cpp
index e65420dc0e3..14dbb3e0db7 100644
--- a/src/rt/arch/i386/context.cpp
+++ b/src/rt/arch/i386/context.cpp
@@ -1,10 +1,6 @@
-#include "context.h"
-
-#include "../../rust.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
+#include "context.h"
+#include "../../rust_globals.h"
 
 extern "C" uint32_t CDECL swap_registers(registers_t *oregs,
                                          registers_t *regs)
diff --git a/src/rt/arch/x86_64/context.cpp b/src/rt/arch/x86_64/context.cpp
index 46a606c6c6e..aefb1fef2c2 100644
--- a/src/rt/arch/x86_64/context.cpp
+++ b/src/rt/arch/x86_64/context.cpp
@@ -1,10 +1,6 @@
-#include "context.h"
-
-#include "../../rust.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <assert.h>
+#include "context.h"
+#include "../../rust_globals.h"
 
 extern "C" void CDECL swap_registers(registers_t *oregs,
                                      registers_t *regs)
diff --git a/src/rt/boxed_region.cpp b/src/rt/boxed_region.cpp
index 937069bc38c..5eec01730f4 100644
--- a/src/rt/boxed_region.cpp
+++ b/src/rt/boxed_region.cpp
@@ -1,6 +1,8 @@
-#include <assert.h>
+
+
 #include "boxed_region.h"
-#include "rust_internal.h"
+#include "rust_globals.h"
+#include "rust_task.h"
 
 // #define DUMP_BOXED_REGION
 
diff --git a/src/rt/circular_buffer.cpp b/src/rt/circular_buffer.cpp
index 6753eb20f2e..93d7d612f0b 100644
--- a/src/rt/circular_buffer.cpp
+++ b/src/rt/circular_buffer.cpp
@@ -2,7 +2,9 @@
  * A simple resizable circular buffer.
  */
 
-#include "rust_internal.h"
+#include "circular_buffer.h"
+#include "rust_globals.h"
+#include "rust_kernel.h"
 
 circular_buffer::circular_buffer(rust_kernel *kernel, size_t unit_sz) :
     kernel(kernel),
diff --git a/src/rt/circular_buffer.h b/src/rt/circular_buffer.h
index 289fcf1ec4f..c54ee8a33e4 100644
--- a/src/rt/circular_buffer.h
+++ b/src/rt/circular_buffer.h
@@ -5,6 +5,9 @@
 #ifndef CIRCULAR_BUFFER_H
 #define CIRCULAR_BUFFER_H
 
+#include "rust_globals.h"
+#include "rust_kernel.h"
+
 class
 circular_buffer : public kernel_owned<circular_buffer> {
     static const size_t INITIAL_CIRCULAR_BUFFER_SIZE_IN_UNITS = 8;
diff --git a/src/rt/memory.h b/src/rt/memory.h
index ea087d14b51..16662fd7a14 100644
--- a/src/rt/memory.h
+++ b/src/rt/memory.h
@@ -2,40 +2,8 @@
 #ifndef MEMORY_H
 #define MEMORY_H
 
-// FIXME: It would be really nice to be able to get rid of this.
-inline void *operator new[](size_t size, rust_task *task, const char *tag) {
-    return task->malloc(size, tag);
-}
+#include "rust_task.h"
 
-template <typename T>
-inline void *task_owned<T>::operator new(size_t size, rust_task *task,
-                                         const char *tag) {
-    return task->malloc(size, tag);
-}
-
-template <typename T>
-inline void *task_owned<T>::operator new[](size_t size, rust_task *task,
-                                           const char *tag) {
-    return task->malloc(size, tag);
-}
-
-template <typename T>
-inline void *task_owned<T>::operator new(size_t size, rust_task &task,
-                                         const char *tag) {
-    return task.malloc(size, tag);
-}
-
-template <typename T>
-inline void *task_owned<T>::operator new[](size_t size, rust_task &task,
-                                           const char *tag) {
-    return task.malloc(size, tag);
-}
-
-template <typename T>
-inline void *kernel_owned<T>::operator new(size_t size, rust_kernel *kernel,
-                                           const char *tag) {
-    return kernel->malloc(size, tag);
-}
 
 
 #endif /* MEMORY_H */
diff --git a/src/rt/memory_region.cpp b/src/rt/memory_region.cpp
index c5c36b2b5f3..70966579991 100644
--- a/src/rt/memory_region.cpp
+++ b/src/rt/memory_region.cpp
@@ -1,5 +1,7 @@
-#include "rust_internal.h"
+
+#include "sync/sync.h"
 #include "memory_region.h"
+#include "rust_env.h"
 
 #if RUSTRT_TRACK_ALLOCATIONS >= 3
 #include <execinfo.h>
diff --git a/src/rt/memory_region.h b/src/rt/memory_region.h
index 3cec23641b1..145020e3cca 100644
--- a/src/rt/memory_region.h
+++ b/src/rt/memory_region.h
@@ -9,7 +9,9 @@
 #ifndef MEMORY_REGION_H
 #define MEMORY_REGION_H
 
+#include "rust_globals.h"
 #include "sync/lock_and_signal.h"
+#include "util/array_list.h"
 
 // There are three levels of debugging:
 //
diff --git a/src/rt/rust.cpp b/src/rt/rust.cpp
index 695132979be..4f9cd3bf7fd 100644
--- a/src/rt/rust.cpp
+++ b/src/rt/rust.cpp
@@ -1,7 +1,8 @@
-#include "rust_internal.h"
+
+#include "rust_globals.h"
+#include "rust_kernel.h"
 #include "rust_util.h"
 #include "rust_scheduler.h"
-#include <cstdio>
 
 struct
 command_line_args : public kernel_owned<command_line_args>
diff --git a/src/rt/rust.h b/src/rt/rust.h
deleted file mode 100644
index fdf95040df8..00000000000
--- a/src/rt/rust.h
+++ /dev/null
@@ -1,41 +0,0 @@
-#ifndef RUST_H
-#define RUST_H
-
-/*
- * Include this file after you've defined the ISO C9x stdint
- * types (size_t, uint8_t, uintptr_t, etc.)
- */
-
-#ifdef __i386__
-// 'cdecl' ABI only means anything on i386
-#ifdef __WIN32__
-#ifndef CDECL
-#define CDECL __cdecl
-#endif
-#ifndef FASTCALL
-#define FASTCALL __fastcall
-#endif
-#else
-#define CDECL __attribute__((cdecl))
-#define FASTCALL __attribute__((fastcall))
-#endif
-#else
-#define CDECL
-#define FASTCALL
-#endif
-
-/* Controls whether claims are turned into checks */
-/* Variable name must be kept consistent with trans.rs */
-extern "C" int check_claims;
-
-/*
- * Local Variables:
- * fill-column: 78;
- * indent-tabs-mode: nil
- * c-basic-offset: 4
- * buffer-file-coding-system: utf-8-unix
- * compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
- * End:
- */
-
-#endif /* RUST_H */
diff --git a/src/rt/rust_box_annihilator.cpp b/src/rt/rust_box_annihilator.cpp
index b5abfde0274..21f7b9d2dc3 100644
--- a/src/rt/rust_box_annihilator.cpp
+++ b/src/rt/rust_box_annihilator.cpp
@@ -1,4 +1,6 @@
-#include "rust_internal.h"
+
+#include "rust_globals.h"
+#include "rust_task.h"
 #include "rust_shape.h"
 
 class annihilator : public shape::data<annihilator,shape::ptr> {
diff --git a/src/rt/rust_builtin.cpp b/src/rt/rust_builtin.cpp
index 08707d7f38d..79e1e880f57 100644
--- a/src/rt/rust_builtin.cpp
+++ b/src/rt/rust_builtin.cpp
@@ -1,12 +1,12 @@
 /* Native builtins. */
 
-#include "rust_internal.h"
 #include "rust_sched_loop.h"
 #include "rust_task.h"
 #include "rust_util.h"
 #include "rust_scheduler.h"
 #include "sync/timer.h"
 #include "rust_abi.h"
+#include "rust_port.h"
 
 #ifdef __APPLE__
 #include <crt_externs.h>
diff --git a/src/rt/rust_cc.cpp b/src/rt/rust_cc.cpp
index b0a1804d096..41cc1684290 100644
--- a/src/rt/rust_cc.cpp
+++ b/src/rt/rust_cc.cpp
@@ -1,19 +1,17 @@
 // Rust cycle collector. Temporary, but will probably stick around for some
 // time until LLVM's GC infrastructure is more mature.
 
-#include "rust_debug.h"
-#include "rust_internal.h"
-#include "rust_shape.h"
-#include "rust_task.h"
-#include <cassert>
-#include <cstdio>
-#include <cstdlib>
 #include <map>
 #include <set>
 #include <vector>
-#include <stdint.h>
 #include <ios>
 
+#include "rust_globals.h"
+#include "rust_cc.h"
+#include "rust_debug.h"
+#include "rust_shape.h"
+#include "rust_task.h"
+
 // The number of allocations Rust code performs before performing cycle
 // collection.
 #define RUST_CC_FREQUENCY   5000
diff --git a/src/rt/rust_debug.cpp b/src/rt/rust_debug.cpp
index 9591d9ade07..47aaec00087 100644
--- a/src/rt/rust_debug.cpp
+++ b/src/rt/rust_debug.cpp
@@ -1,13 +1,13 @@
 // Routines useful when debugging the Rust runtime.
 
+#include "rust_globals.h"
 #include "rust_abi.h"
 #include "rust_debug.h"
-#include "rust_internal.h"
+#include "rust_task.h"
 
 #include <iostream>
 #include <string>
 #include <sstream>
-#include <stdint.h>
 
 namespace {
 
diff --git a/src/rt/rust_env.cpp b/src/rt/rust_env.cpp
index 9237e8b593c..a54dc27c71f 100644
--- a/src/rt/rust_env.cpp
+++ b/src/rt/rust_env.cpp
@@ -3,7 +3,7 @@
 // that might come from the environment is loaded here, once, during
 // init.
 
-#include "rust_internal.h"
+#include "rust_env.h"
 
 // The environment variables that the runtime knows about
 #define RUST_THREADS "RUST_THREADS"
diff --git a/src/rt/rust_env.h b/src/rt/rust_env.h
index adf3dc8a7d4..007ac9b1e0b 100644
--- a/src/rt/rust_env.h
+++ b/src/rt/rust_env.h
@@ -1,3 +1,9 @@
+
+#ifndef RUST_ENV_H
+#define RUST_ENV_H
+
+#include "rust_globals.h"
+
 struct rust_env {
     size_t num_sched_threads;
     size_t min_stack_size;
@@ -11,3 +17,5 @@ struct rust_env {
 
 rust_env* load_env();
 void free_env(rust_env *rust_env);
+
+#endif
diff --git a/src/rt/rust_globals.h b/src/rt/rust_globals.h
index dcd1ec85575..54b80ee7908 100644
--- a/src/rt/rust_globals.h
+++ b/src/rt/rust_globals.h
@@ -25,11 +25,10 @@
 #include <string.h>
 #include <fcntl.h>
 #include <math.h>
+#include <assert.h>
 
-#include "rust.h"
 #include "rand.h"
 #include "uthash.h"
-#include "rust_env.h"
 
 #if defined(__WIN32__)
 extern "C" {
@@ -52,6 +51,28 @@ extern "C" {
 #error "Platform not supported."
 #endif
 
+#ifdef __i386__
+// 'cdecl' ABI only means anything on i386
+#ifdef __WIN32__
+#ifndef CDECL
+#define CDECL __cdecl
+#endif
+#ifndef FASTCALL
+#define FASTCALL __fastcall
+#endif
+#else
+#define CDECL __attribute__((cdecl))
+#define FASTCALL __attribute__((fastcall))
+#endif
+#else
+#define CDECL
+#define FASTCALL
+#endif
+
+/* Controls whether claims are turned into checks */
+/* Variable name must be kept consistent with trans.rs */
+extern "C" int check_claims;
+
 #define CHECKED(call)                                               \
     {                                                               \
     int res = (call);                                               \
@@ -64,4 +85,16 @@ extern "C" {
         }                                                           \
     }
 
+#define PTR "0x%" PRIxPTR
+
+// This accounts for logging buffers.
+static size_t const BUF_BYTES = 2048;
+
+// The error status to use when the process fails
+#define PROC_FAIL_CODE 101
+
+// A cond(ition) is something we can block on. This can be a channel
+// (writing), a port (reading) or a task (waiting).
+struct rust_cond { };
+
 #endif /* RUST_GLOBALS_H */
diff --git a/src/rt/rust_internal.h b/src/rt/rust_internal.h
deleted file mode 100644
index bcf88cfcaf1..00000000000
--- a/src/rt/rust_internal.h
+++ /dev/null
@@ -1,183 +0,0 @@
-#ifndef RUST_INTERNAL_H
-#define RUST_INTERNAL_H
-
-#include "rust_globals.h"
-#include "util/array_list.h"
-#include "util/indexed_list.h"
-#include "util/synchronized_indexed_list.h"
-#include "util/hash_map.h"
-#include "sync/sync.h"
-#include "sync/lock_and_signal.h"
-#include "sync/lock_free_queue.h"
-
-struct rust_sched_loop;
-struct rust_task;
-class rust_log;
-class rust_port;
-class rust_kernel;
-
-struct stk_seg;
-struct type_desc;
-struct frame_glue_fns;
-
-typedef intptr_t rust_sched_id;
-typedef intptr_t rust_task_id;
-typedef intptr_t rust_port_id;
-
-#define PTR "0x%" PRIxPTR
-
-// This drives our preemption scheme.
-
-static size_t const TIME_SLICE_IN_MS = 10;
-
-// This accounts for logging buffers.
-
-static size_t const BUF_BYTES = 2048;
-
-// The error status to use when the process fails
-#define PROC_FAIL_CODE 101
-
-// Every reference counted object should use this macro and initialize
-// ref_count.
-
-#define RUST_REFCOUNTED(T) \
-  RUST_REFCOUNTED_WITH_DTOR(T, delete (T*)this)
-#define RUST_REFCOUNTED_WITH_DTOR(T, dtor) \
-  intptr_t ref_count;      \
-  void ref() { ++ref_count; } \
-  void deref() { if (--ref_count == 0) { dtor; } }
-
-#define RUST_ATOMIC_REFCOUNT()                                             \
-private:                                                                   \
-   intptr_t ref_count;                                                     \
-public:                                                                    \
-   void ref() {                                                            \
-       intptr_t old = sync::increment(ref_count);                          \
-       assert(old > 0);                                                    \
-   }                                                                       \
-   void deref() { if(0 == sync::decrement(ref_count)) { delete_this(); } } \
-   intptr_t get_ref_count() { return sync::read(ref_count); }
-
-template <typename T> struct task_owned {
-    inline void *operator new(size_t size, rust_task *task, const char *tag);
-
-    inline void *operator new[](size_t size, rust_task *task,
-                                const char *tag);
-
-    inline void *operator new(size_t size, rust_task &task, const char *tag);
-
-    inline void *operator new[](size_t size, rust_task &task,
-                                const char *tag);
-
-    void operator delete(void *ptr) {
-        ((T *)ptr)->task->free(ptr);
-    }
-};
-
-template <typename T> struct kernel_owned {
-    inline void *operator new(size_t size, rust_kernel *kernel,
-                              const char *tag);
-
-    void operator delete(void *ptr) {
-        ((T *)ptr)->kernel->free(ptr);
-    }
-};
-
-template <typename T> struct region_owned {
-    void operator delete(void *ptr) {
-        ((T *)ptr)->region->free(ptr);
-    }
-};
-
-// A cond(ition) is something we can block on. This can be a channel
-// (writing), a port (reading) or a task (waiting).
-
-struct rust_cond { };
-
-#include "memory_region.h"
-#include "rust_log.h"
-#include "rust_kernel.h"
-#include "rust_sched_loop.h"
-
-typedef void CDECL (glue_fn)(void *, void *,
-                             const type_desc **, void *);
-
-struct rust_shape_tables {
-    uint8_t *tags;
-    uint8_t *resources;
-};
-
-typedef unsigned long ref_cnt_t;
-
-// Corresponds to the boxed data in the @ region.  The body follows the
-// header; you can obtain a ptr via box_body() below.
-struct rust_opaque_box {
-    ref_cnt_t ref_count;
-    type_desc *td;
-    rust_opaque_box *prev;
-    rust_opaque_box *next;
-};
-
-// The type of functions that we spawn, which fall into two categories:
-// - the main function: has a NULL environment, but uses the void* arg
-// - unique closures of type fn~(): have a non-NULL environment, but
-//   no arguments (and hence the final void*) is harmless
-typedef void (*CDECL spawn_fn)(void*, rust_opaque_box*, void *);
-
-// corresponds to the layout of a fn(), fn@(), fn~() etc
-struct fn_env_pair {
-    spawn_fn f;
-    rust_opaque_box *env;
-};
-
-static inline void *box_body(rust_opaque_box *box) {
-    // Here we take advantage of the fact that the size of a box in 32
-    // (resp. 64) bit is 16 (resp. 32) bytes, and thus always 16-byte aligned.
-    // If this were to change, we would have to update the method
-    // rustc::middle::trans::base::opaque_box_body() as well.
-    return (void*)(box + 1);
-}
-
-struct type_desc {
-    // First part of type_desc is known to compiler.
-    // first_param = &descs[1] if dynamic, null if static.
-    const type_desc **first_param;
-    size_t size;
-    size_t align;
-    glue_fn *take_glue;
-    glue_fn *drop_glue;
-    glue_fn *free_glue;
-    void *UNUSED;
-    glue_fn *sever_glue;    // For GC.
-    glue_fn *mark_glue;     // For GC.
-    uintptr_t unused2;
-    void *UNUSED_2;
-    const uint8_t *shape;
-    const rust_shape_tables *shape_tables;
-    uintptr_t n_params;
-    uintptr_t n_obj_params;
-
-    // Residual fields past here are known only to runtime.
-    UT_hash_handle hh;
-    size_t n_descs;
-    const type_desc *descs[];
-};
-
-extern "C" type_desc *rust_clone_type_desc(type_desc*);
-
-#include "circular_buffer.h"
-#include "rust_task.h"
-#include "rust_port.h"
-#include "memory.h"
-
-//
-// Local Variables:
-// mode: C++
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// End:
-//
-
-#endif
diff --git a/src/rt/rust_kernel.cpp b/src/rt/rust_kernel.cpp
index 172926f5f8d..259870bcd69 100644
--- a/src/rt/rust_kernel.cpp
+++ b/src/rt/rust_kernel.cpp
@@ -1,4 +1,7 @@
-#include "rust_internal.h"
+
+#include "rust_kernel.h"
+#include "rust_globals.h"
+#include "rust_port.h"
 #include "rust_util.h"
 #include "rust_scheduler.h"
 
diff --git a/src/rt/rust_kernel.h b/src/rt/rust_kernel.h
index 7e3640f0631..3683773a7fb 100644
--- a/src/rt/rust_kernel.h
+++ b/src/rt/rust_kernel.h
@@ -7,9 +7,15 @@
 #include "memory_region.h"
 #include "rust_log.h"
 #include "rust_sched_reaper.h"
+#include "util/hash_map.h"
 
 struct rust_task_thread;
 class rust_scheduler;
+class rust_port;
+
+typedef intptr_t rust_sched_id;
+typedef intptr_t rust_task_id;
+typedef intptr_t rust_port_id;
 
 typedef std::map<rust_sched_id, rust_scheduler*> sched_map;
 
@@ -81,4 +87,19 @@ public:
     void set_exit_status(int code);
 };
 
+template <typename T> struct kernel_owned {
+    inline void *operator new(size_t size, rust_kernel *kernel,
+                              const char *tag);
+
+    void operator delete(void *ptr) {
+        ((T *)ptr)->kernel->free(ptr);
+    }
+};
+
+template <typename T>
+inline void *kernel_owned<T>::operator new(size_t size, rust_kernel *kernel,
+                                           const char *tag) {
+    return kernel->malloc(size, tag);
+}
+
 #endif /* RUST_KERNEL_H */
diff --git a/src/rt/rust_log.cpp b/src/rt/rust_log.cpp
index ce48c37062d..df1ae6fd47f 100644
--- a/src/rt/rust_log.cpp
+++ b/src/rt/rust_log.cpp
@@ -2,12 +2,11 @@
  * Logging infrastructure that aims to support multi-threading
  */
 
-#include "rust_internal.h"
+
+#include "rust_log.h"
 #include "util/array_list.h"
-#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
 #include "rust_util.h"
+#include "rust_task.h"
 
 /**
  * Synchronizes access to the underlying logging mechanism.
diff --git a/src/rt/rust_log.h b/src/rt/rust_log.h
index 33fb9eef38b..423f66fa7cd 100644
--- a/src/rt/rust_log.h
+++ b/src/rt/rust_log.h
@@ -2,6 +2,8 @@
 #ifndef RUST_LOG_H
 #define RUST_LOG_H
 
+#include "rust_globals.h"
+
 const uint32_t log_err = 0;
 const uint32_t log_warn = 1;
 const uint32_t log_info = 2;
diff --git a/src/rt/rust_port.cpp b/src/rt/rust_port.cpp
index 3d876585962..f1a40508aca 100644
--- a/src/rt/rust_port.cpp
+++ b/src/rt/rust_port.cpp
@@ -1,6 +1,6 @@
-#include "rust_internal.h"
-#include "rust_port.h"
 
+#include "rust_port.h"
+#include "rust_task.h"
 
 rust_port::rust_port(rust_task *task, size_t unit_sz)
     : ref_count(1), kernel(task->kernel), task(task),
diff --git a/src/rt/rust_port.h b/src/rt/rust_port.h
index 454b1dcce45..ae160de217f 100644
--- a/src/rt/rust_port.h
+++ b/src/rt/rust_port.h
@@ -1,7 +1,8 @@
 #ifndef RUST_PORT_H
 #define RUST_PORT_H
 
-#include "rust_internal.h"
+#include "rust_globals.h"
+#include "circular_buffer.h"
 
 class port_detach_cond : public rust_cond { };
 
diff --git a/src/rt/rust_port_selector.cpp b/src/rt/rust_port_selector.cpp
index 51018bf95a9..7b3b45788f7 100644
--- a/src/rt/rust_port_selector.cpp
+++ b/src/rt/rust_port_selector.cpp
@@ -1,5 +1,7 @@
+
 #include "rust_port.h"
 #include "rust_port_selector.h"
+#include "rust_task.h"
 
 rust_port_selector::rust_port_selector()
     : ports(NULL), n_ports(0) {
diff --git a/src/rt/rust_port_selector.h b/src/rt/rust_port_selector.h
index 3bfc454aaf8..8dbf0c40329 100644
--- a/src/rt/rust_port_selector.h
+++ b/src/rt/rust_port_selector.h
@@ -1,9 +1,8 @@
 #ifndef RUST_PORT_SELECTOR_H
 #define RUST_PORT_SELECTOR_H
 
-#include "rust_internal.h"
+#include "rust_globals.h"
 
-struct rust_task;
 class rust_port;
 
 class rust_port_selector : public rust_cond {
diff --git a/src/rt/rust_refcount.h b/src/rt/rust_refcount.h
new file mode 100644
index 00000000000..0e70af31aff
--- /dev/null
+++ b/src/rt/rust_refcount.h
@@ -0,0 +1,31 @@
+
+#ifndef RUST_REFCOUNT_H
+#define RUST_REFCOUNT_H
+
+#include "sync/sync.h"
+
+// Refcounting defines
+typedef unsigned long ref_cnt_t;
+
+#define RUST_REFCOUNTED(T) \
+  RUST_REFCOUNTED_WITH_DTOR(T, delete (T*)this)
+
+#define RUST_REFCOUNTED_WITH_DTOR(T, dtor)      \
+  intptr_t ref_count;      \
+  void ref() { ++ref_count; } \
+  void deref() { if (--ref_count == 0) { dtor; } }
+
+#define RUST_ATOMIC_REFCOUNT()                                             \
+private:                                                                   \
+   intptr_t ref_count;                                                     \
+public:                                                                    \
+   void ref() {                                                            \
+       intptr_t old = sync::increment(ref_count);                          \
+       assert(old > 0);                                                    \
+   }                                                                       \
+   void deref() { if(0 == sync::decrement(ref_count)) { delete_this(); } } \
+   intptr_t get_ref_count() { return sync::read(ref_count); }
+
+
+
+#endif
diff --git a/src/rt/rust_run_program.cpp b/src/rt/rust_run_program.cpp
index 8390c8e4723..8d7396aab93 100644
--- a/src/rt/rust_run_program.cpp
+++ b/src/rt/rust_run_program.cpp
@@ -1,4 +1,5 @@
-#include "rust_internal.h"
+
+#include "rust_kernel.h"
 
 #ifdef __APPLE__
 #include <crt_externs.h>
diff --git a/src/rt/rust_sched_driver.cpp b/src/rt/rust_sched_driver.cpp
index dd9ca9e7f10..0da9d580dc3 100644
--- a/src/rt/rust_sched_driver.cpp
+++ b/src/rt/rust_sched_driver.cpp
@@ -1,5 +1,5 @@
-#include <assert.h>
-#include "rust_internal.h"
+
+#include "rust_globals.h"
 #include "rust_sched_driver.h"
 #include "rust_sched_loop.h"
 
diff --git a/src/rt/rust_sched_launcher.cpp b/src/rt/rust_sched_launcher.cpp
index 7111cbb804b..3406835dfbb 100644
--- a/src/rt/rust_sched_launcher.cpp
+++ b/src/rt/rust_sched_launcher.cpp
@@ -1,3 +1,4 @@
+
 #include "rust_sched_launcher.h"
 #include "rust_scheduler.h"
 
diff --git a/src/rt/rust_sched_launcher.h b/src/rt/rust_sched_launcher.h
index e5b230fce4d..523a199401e 100644
--- a/src/rt/rust_sched_launcher.h
+++ b/src/rt/rust_sched_launcher.h
@@ -1,9 +1,10 @@
 #ifndef RUST_SCHED_LAUNCHER_H
 #define RUST_SCHED_LAUNCHER_H
 
-#include "rust_internal.h"
 #include "sync/rust_thread.h"
 #include "rust_sched_driver.h"
+#include "rust_kernel.h"
+#include "rust_sched_loop.h"
 
 class rust_sched_launcher : public kernel_owned<rust_sched_launcher> {
 public:
diff --git a/src/rt/rust_sched_loop.cpp b/src/rt/rust_sched_loop.cpp
index 1076aa8af3f..e8942f37733 100644
--- a/src/rt/rust_sched_loop.cpp
+++ b/src/rt/rust_sched_loop.cpp
@@ -1,9 +1,5 @@
 
-#include <stdarg.h>
-#include <cassert>
-#include <pthread.h>
-#include <vector>
-#include "rust_internal.h"
+#include "rust_sched_loop.h"
 #include "rust_util.h"
 #include "rust_scheduler.h"
 
diff --git a/src/rt/rust_sched_loop.h b/src/rt/rust_sched_loop.h
index 9c059ac6230..7f60f7e449d 100644
--- a/src/rt/rust_sched_loop.h
+++ b/src/rt/rust_sched_loop.h
@@ -1,10 +1,12 @@
 #ifndef RUST_SCHED_LOOP_H
 #define RUST_SCHED_LOOP_H
 
-#include "rust_internal.h"
+#include "rust_globals.h"
+#include "rust_log.h"
 #include "rust_stack.h"
 #include "rust_signal.h"
 #include "context.h"
+#include "util/indexed_list.h"
 
 enum rust_task_state {
     task_state_newborn,
@@ -23,6 +25,8 @@ enum rust_sched_loop_state {
     sched_loop_state_exit
 };
 
+class rust_kernel;
+class rust_scheduler;
 struct rust_task;
 
 typedef indexed_list<rust_task> rust_task_list;
@@ -176,6 +180,8 @@ rust_sched_loop::return_c_stack(stk_seg *stack) {
     }
 }
 
+// this is needed to appease the circular dependency gods
+#include "rust_task.h"
 
 //
 // Local Variables:
diff --git a/src/rt/rust_sched_reaper.cpp b/src/rt/rust_sched_reaper.cpp
index f2897859f23..4d5237ac54e 100644
--- a/src/rt/rust_sched_reaper.cpp
+++ b/src/rt/rust_sched_reaper.cpp
@@ -1,4 +1,4 @@
-#include "rust_internal.h"
+
 #include "rust_kernel.h"
 #include "rust_sched_reaper.h"
 
diff --git a/src/rt/rust_scheduler.cpp b/src/rt/rust_scheduler.cpp
index 582289a3182..acf5dfcbe88 100644
--- a/src/rt/rust_scheduler.cpp
+++ b/src/rt/rust_scheduler.cpp
@@ -1,4 +1,7 @@
+
 #include "rust_scheduler.h"
+#include "rust_task.h"
+#include "rust_globals.h"
 #include "rust_util.h"
 #include "rust_sched_launcher.h"
 
diff --git a/src/rt/rust_scheduler.h b/src/rt/rust_scheduler.h
index cd98992b917..84f3df195a4 100644
--- a/src/rt/rust_scheduler.h
+++ b/src/rt/rust_scheduler.h
@@ -1,7 +1,9 @@
 #ifndef RUST_SCHEDULER_H
 #define RUST_SCHEDULER_H
 
-#include "rust_internal.h"
+#include "rust_globals.h"
+#include "util/array_list.h"
+#include "rust_kernel.h"
 
 class rust_sched_launcher;
 
diff --git a/src/rt/rust_shape.cpp b/src/rt/rust_shape.cpp
index 472a691a6fa..9598744aea3 100644
--- a/src/rt/rust_shape.cpp
+++ b/src/rt/rust_shape.cpp
@@ -1,16 +1,14 @@
 // Functions that interpret the shape of a type to perform various low-level
 // actions, such as copying, freeing, comparing, and so on.
 
+
 #include <algorithm>
 #include <iomanip>
 #include <iostream>
 #include <sstream>
 #include <utility>
-#include <cassert>
-#include <cctype>
-#include <cstdio>
-#include <cstdlib>
-#include "rust_internal.h"
+
+#include "rust_task.h"
 #include "rust_shape.h"
 
 namespace shape {
diff --git a/src/rt/rust_shape.h b/src/rt/rust_shape.h
index ad3841b5443..a181a881457 100644
--- a/src/rt/rust_shape.h
+++ b/src/rt/rust_shape.h
@@ -9,7 +9,8 @@
 #undef min
 
 #include <iostream>
-#include "rust_internal.h"
+
+#include "rust_globals.h"
 #include "rust_util.h"
 
 // ISAAC pollutes our namespace.
diff --git a/src/rt/rust_stack.cpp b/src/rt/rust_stack.cpp
index adcf7e4b4ba..5b5cda74828 100644
--- a/src/rt/rust_stack.cpp
+++ b/src/rt/rust_stack.cpp
@@ -1,5 +1,5 @@
-#include "rust_internal.h"
 
+#include "rust_stack.h"
 #include "vg/valgrind.h"
 #include "vg/memcheck.h"
 
diff --git a/src/rt/rust_stack.h b/src/rt/rust_stack.h
index ecd591cbe36..4d88868d558 100644
--- a/src/rt/rust_stack.h
+++ b/src/rt/rust_stack.h
@@ -1,8 +1,11 @@
 #ifndef RUST_STACK_H
 #define RUST_STACK_H
 
+#include "rust_globals.h"
 #include "memory_region.h"
 
+struct rust_task;
+
 struct stk_seg {
     stk_seg *prev;
     stk_seg *next;
diff --git a/src/rt/rust_task.cpp b/src/rt/rust_task.cpp
index f56ea868b30..e1bc74e53df 100644
--- a/src/rt/rust_task.cpp
+++ b/src/rt/rust_task.cpp
@@ -1,16 +1,15 @@
 
-#include "rust_internal.h"
-#include "rust_cc.h"
-
 #ifndef __WIN32__
 #include <execinfo.h>
 #endif
 #include <iostream>
-#include <cassert>
-#include <cstring>
 #include <algorithm>
 
+#include "rust_task.h"
+#include "rust_cc.h"
 #include "rust_upcall.h"
+#include "rust_env.h"
+#include "rust_port.h"
 
 // Tasks
 rust_task::rust_task(rust_sched_loop *sched_loop, rust_task_state state,
diff --git a/src/rt/rust_task.h b/src/rt/rust_task.h
index f670704293a..2a6ed8fdd44 100644
--- a/src/rt/rust_task.h
+++ b/src/rt/rust_task.h
@@ -1,6 +1,3 @@
-/*
- *
- */
 
 #ifndef RUST_TASK_H
 #define RUST_TASK_H
@@ -8,14 +5,15 @@
 #include <map>
 
 #include "util/array_list.h"
-
 #include "context.h"
 #include "rust_debug.h"
-#include "rust_internal.h"
 #include "rust_kernel.h"
 #include "boxed_region.h"
 #include "rust_stack.h"
 #include "rust_port_selector.h"
+#include "rust_type.h"
+#include "rust_sched_loop.h"
+#include "memory.h"
 
 // The amount of extra space at the end of each stack segment, available
 // to the rt, compiler and dynamic linker for running small functions
@@ -272,6 +270,38 @@ public:
     void cleanup_after_turn();
 };
 
+// FIXME: It would be really nice to be able to get rid of this.
+inline void *operator new[](size_t size, rust_task *task, const char *tag) {
+    return task->malloc(size, tag);
+}
+
+
+template <typename T> struct task_owned {
+    inline void *operator new(size_t size, rust_task *task,
+                                             const char *tag) {
+        return task->malloc(size, tag);
+    }
+
+    inline void *operator new[](size_t size, rust_task *task,
+                                               const char *tag) {
+        return task->malloc(size, tag);
+    }
+
+    inline void *operator new(size_t size, rust_task &task,
+                                             const char *tag) {
+        return task.malloc(size, tag);
+    }
+
+    inline void *operator new[](size_t size, rust_task &task,
+                                               const char *tag) {
+        return task.malloc(size, tag);
+    }
+
+    void operator delete(void *ptr) {
+        ((T *)ptr)->task->free(ptr);
+    }
+};
+
 // This stuff is on the stack-switching fast path
 
 // Get a rough approximation of the current stack pointer
diff --git a/src/rt/rust_type.h b/src/rt/rust_type.h
new file mode 100644
index 00000000000..399832d553c
--- /dev/null
+++ b/src/rt/rust_type.h
@@ -0,0 +1,72 @@
+
+#ifndef RUST_TYPE_H
+#define RUST_TYPE_H
+
+#include "rust_refcount.h"
+
+// The type of functions that we spawn, which fall into two categories:
+// - the main function: has a NULL environment, but uses the void* arg
+// - unique closures of type fn~(): have a non-NULL environment, but
+//   no arguments (and hence the final void*) is harmless
+typedef void (*CDECL spawn_fn)(void*, rust_opaque_box*, void *);
+
+struct type_desc;
+
+typedef void CDECL (glue_fn)(void *, void *, const type_desc **, void *);
+
+struct rust_shape_tables {
+    uint8_t *tags;
+    uint8_t *resources;
+};
+
+// Corresponds to the boxed data in the @ region.  The body follows the
+// header; you can obtain a ptr via box_body() below.
+struct rust_opaque_box {
+    ref_cnt_t ref_count;
+    type_desc *td;
+    rust_opaque_box *prev;
+    rust_opaque_box *next;
+};
+
+// corresponds to the layout of a fn(), fn@(), fn~() etc
+struct fn_env_pair {
+    spawn_fn f;
+    rust_opaque_box *env;
+};
+
+static inline void *box_body(rust_opaque_box *box) {
+    // Here we take advantage of the fact that the size of a box in 32
+    // (resp. 64) bit is 16 (resp. 32) bytes, and thus always 16-byte aligned.
+    // If this were to change, we would have to update the method
+    // rustc::middle::trans::base::opaque_box_body() as well.
+    return (void*)(box + 1);
+}
+
+struct type_desc {
+    // First part of type_desc is known to compiler.
+    // first_param = &descs[1] if dynamic, null if static.
+    const type_desc **first_param;
+    size_t size;
+    size_t align;
+    glue_fn *take_glue;
+    glue_fn *drop_glue;
+    glue_fn *free_glue;
+    void *UNUSED;
+    glue_fn *sever_glue;    // For GC.
+    glue_fn *mark_glue;     // For GC.
+    uintptr_t unused2;
+    void *UNUSED_2;
+    const uint8_t *shape;
+    const rust_shape_tables *shape_tables;
+    uintptr_t n_params;
+    uintptr_t n_obj_params;
+
+    // Residual fields past here are known only to runtime.
+    UT_hash_handle hh;
+    size_t n_descs;
+    const type_desc *descs[];
+};
+
+extern "C" type_desc *rust_clone_type_desc(type_desc*);
+
+#endif
diff --git a/src/rt/rust_upcall.cpp b/src/rt/rust_upcall.cpp
index cf40e1500de..a257c031cc0 100644
--- a/src/rt/rust_upcall.cpp
+++ b/src/rt/rust_upcall.cpp
@@ -6,15 +6,15 @@
   switch to the C stack.
  */
 
+#include "rust_globals.h"
+#include "rust_task.h"
 #include "rust_cc.h"
-#include "rust_internal.h"
 #include "rust_sched_loop.h"
 #include "rust_unwind.h"
 #include "rust_upcall.h"
 #include "rust_util.h"
 #include <stdint.h>
 
-
 #ifdef __GNUC__
 #define LOG_UPCALL_ENTRY(task)                            \
     LOG(task, upcall,                                     \
diff --git a/src/rt/rust_upcall.h b/src/rt/rust_upcall.h
index 344730e2171..0030ef19b36 100644
--- a/src/rt/rust_upcall.h
+++ b/src/rt/rust_upcall.h
@@ -1,6 +1,9 @@
-#pragma once
+
+#ifndef RUST_UPCALL_H
+#define RUST_UPCALL_H
 
 // Upcalls used from C code on occasion:
 
 extern "C" CDECL void upcall_shared_free(void* ptr);
 
+#endif
diff --git a/src/rt/rust_util.h b/src/rt/rust_util.h
index 0af16978390..9928a05c057 100644
--- a/src/rt/rust_util.h
+++ b/src/rt/rust_util.h
@@ -1,8 +1,9 @@
 #ifndef RUST_UTIL_H
 #define RUST_UTIL_H
 
-#include "rust_task.h"
 #include <limits.h>
+#include "rust_task.h"
+#include "rust_env.h"
 
 // Inline fn used regularly elsewhere.
 
diff --git a/src/rt/rust_uv.cpp b/src/rt/rust_uv.cpp
index a926ed8b288..7e7486fca62 100644
--- a/src/rt/rust_uv.cpp
+++ b/src/rt/rust_uv.cpp
@@ -1,4 +1,6 @@
-#include "rust_internal.h"
+
+#include "rust_globals.h"
+#include "rust_task.h"
 #include "uv.h"
 
 // crust fn pointers
diff --git a/src/rt/sync/lock_and_signal.cpp b/src/rt/sync/lock_and_signal.cpp
index 7500a9c3a19..ccc762c2cc9 100644
--- a/src/rt/sync/lock_and_signal.cpp
+++ b/src/rt/sync/lock_and_signal.cpp
@@ -1,5 +1,6 @@
-#include <assert.h>
+
 #include "../rust_globals.h"
+#include "lock_and_signal.h"
 
 /*
  * A "lock-and-signal" pair. These are necessarily coupled on pthreads
@@ -8,8 +9,6 @@
  * if you're using a pthreads cvar+mutex pair.
  */
 
-#include "lock_and_signal.h"
-
 // FIXME: This is not a portable way of specifying an invalid pthread_t
 #define INVALID_THREAD 0
 
diff --git a/src/rt/sync/lock_and_signal.h b/src/rt/sync/lock_and_signal.h
index fae9b1c24ea..927b0c5b0ff 100644
--- a/src/rt/sync/lock_and_signal.h
+++ b/src/rt/sync/lock_and_signal.h
@@ -2,6 +2,8 @@
 #ifndef LOCK_AND_SIGNAL_H
 #define LOCK_AND_SIGNAL_H
 
+#include "rust_globals.h"
+
 #ifndef RUST_NDEBUG
 #define DEBUG_LOCKS
 #endif
diff --git a/src/rt/sync/lock_free_queue.h b/src/rt/sync/lock_free_queue.h
index 377dd93e34e..3ee15d5d3a6 100644
--- a/src/rt/sync/lock_free_queue.h
+++ b/src/rt/sync/lock_free_queue.h
@@ -42,6 +42,7 @@
  */
 
 #include <assert.h>
+
 template <class T>
 class lock_free_queue {
 
diff --git a/src/rt/sync/rust_thread.cpp b/src/rt/sync/rust_thread.cpp
index 6937f4f7811..5d533acde3d 100644
--- a/src/rt/sync/rust_thread.cpp
+++ b/src/rt/sync/rust_thread.cpp
@@ -1,4 +1,4 @@
-#include "rust_globals.h"
+
 #include "rust_thread.h"
 
 const size_t default_stack_sz = 1024*1024;
diff --git a/src/rt/sync/rust_thread.h b/src/rt/sync/rust_thread.h
index 1a483b658c0..56cf85d4660 100644
--- a/src/rt/sync/rust_thread.h
+++ b/src/rt/sync/rust_thread.h
@@ -1,6 +1,8 @@
 #ifndef RUST_THREAD_H
 #define RUST_THREAD_H
 
+#include "rust_globals.h"
+
 /**
  * Thread utility class. Derive and implement your own run() method.
  */
diff --git a/src/rt/util/array_list.h b/src/rt/util/array_list.h
index 495594cf7e6..6321611c81c 100644
--- a/src/rt/util/array_list.h
+++ b/src/rt/util/array_list.h
@@ -2,6 +2,9 @@
 #ifndef ARRAY_LIST_H
 #define ARRAY_LIST_H
 
+#include <inttypes.h>
+#include <stddef.h>
+
 /**
  * A simple, resizable array list.
  */
diff --git a/src/rt/util/synchronized_indexed_list.h b/src/rt/util/synchronized_indexed_list.h
deleted file mode 100644
index f7c451ee5b1..00000000000
--- a/src/rt/util/synchronized_indexed_list.h
+++ /dev/null
@@ -1,74 +0,0 @@
-#ifndef SYNCHRONIZED_INDEXED_LIST_H
-#define SYNCHRONIZED_INDEXED_LIST_H
-
-#include "indexed_list.h"
-#include "../sync/lock_and_signal.h"
-
-template<typename T> class synchronized_indexed_list :
-    public indexed_list<T> {
-    lock_and_signal _lock;
-
-public:
-    synchronized_indexed_list() {
-    }
-
-    int32_t append(T *value) {
-        int32_t index = 0;
-        _lock.lock();
-        index = indexed_list<T>::append(value);
-        _lock.unlock();
-        return index;
-    }
-
-    bool pop(T **value) {
-        _lock.lock();
-        bool result = indexed_list<T>::pop(value);
-        _lock.unlock();
-        return result;
-    }
-
-    size_t length() {
-       size_t length = 0;
-       _lock.lock();
-       length = indexed_list<T>::length();
-       _lock.unlock();
-       return length;
-    }
-
-    bool is_empty() {
-        bool empty = false;
-        _lock.lock();
-        empty = indexed_list<T>::is_empty();
-        _lock.unlock();
-        return empty;
-    }
-
-    int32_t remove(T* value) {
-        size_t index = 0;
-        _lock.lock();
-        index = indexed_list<T>::remove(value);
-        _lock.unlock();
-        return index;
-    }
-
-    T *operator[](size_t index) {
-        T *value = NULL;
-        _lock.lock();
-        value = indexed_list<T>::operator[](index);
-        _lock.unlock();
-        return value;
-    }
-};
-
-//
-// Local Variables:
-// mode: C++
-// fill-column: 78;
-// indent-tabs-mode: nil
-// c-basic-offset: 4
-// buffer-file-coding-system: utf-8-unix
-// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
-// End:
-//
-
-#endif /* SYNCHRONIZED_INDEXED_LIST_H */