Skip to content
This repository
Browse code

Move capi::Handles into separate files

  • Loading branch information...
commit 1f6af7cd1fdc4a4aa0d9db598c0399d671420d64 1 parent 0f2e708
Dirkjan Bussink authored May 19, 2012
103  vm/capi/handle.cpp
... ...
@@ -1,8 +1,10 @@
1 1
 #include "builtin/nativemethod.hpp"
2 2
 #include "objectmemory.hpp"
3 3
 #include "gc/baker.hpp"
  4
+#include "util/allocator.hpp"
4 5
 #include "capi/capi.hpp"
5 6
 #include "capi/handle.hpp"
  7
+#include "capi/handles.hpp"
6 8
 #include "capi/18/include/ruby.h"
7 9
 
8 10
 namespace rubinius {
@@ -57,107 +59,6 @@ namespace rubinius {
57 59
       std::cerr << "  object:     " << object_ << std::endl;
58 60
     }
59 61
 
60  
-    Handles::~Handles() {
61  
-      delete allocator_;
62  
-    }
63  
-
64  
-
65  
-    Handle* Handles::allocate(STATE, Object* obj) {
66  
-      bool needs_gc = false;
67  
-      Handle* handle = allocator_->allocate(&needs_gc);
68  
-      handle->set_object(obj);
69  
-      handle->validate();
70  
-      if(needs_gc) {
71  
-        state->memory()->collect_mature_now = true;
72  
-      }
73  
-      return handle;
74  
-    }
75  
-
76  
-    void Handles::deallocate_handles(std::list<Handle*>* cached, int mark, BakerGC* young) {
77  
-
78  
-      std::vector<bool> chunk_marks(allocator_->chunks_.size(), false);
79  
-
80  
-      size_t i = 0;
81  
-
82  
-      for(std::vector<Handle*>::iterator it = allocator_->chunks_.begin();
83  
-          it != allocator_->chunks_.end(); ++it) {
84  
-        Handle* chunk = *it;
85  
-
86  
-        for(size_t j = 0; j < allocator_->cChunkSize; j++) {
87  
-          Handle* handle = &chunk[j];
88  
-
89  
-          Object* obj = handle->object();
90  
-
91  
-          if(!handle->in_use_p()) {
92  
-            continue;
93  
-          }
94  
-
95  
-          // Strong references will already have been updated.
96  
-          if(!handle->weak_p()) {
97  
-            chunk_marks[i] = true;
98  
-            continue;
99  
-          }
100  
-
101  
-          if(young) {
102  
-            if(obj->young_object_p()) {
103  
-
104  
-              // A weakref pointing to a valid young object
105  
-              //
106  
-              // TODO this only works because we run prune_handles right after
107  
-              // a collection. In this state, valid objects are only in current.
108  
-              if(young->in_current_p(obj)) {
109  
-                chunk_marks[i] = true;
110  
-              // A weakref pointing to a forwarded young object
111  
-              } else if(obj->forwarded_p()) {
112  
-                handle->set_object(obj->forward());
113  
-                chunk_marks[i] = true;
114  
-              // A weakref pointing to a dead young object
115  
-              } else {
116  
-                handle->clear();
117  
-              }
118  
-            } else {
119  
-              // Not a young object, so won't be GC'd so mark
120  
-              // chunk as still active
121  
-              chunk_marks[i] = true;
122  
-            }
123  
-
124  
-          // A weakref pointing to a dead mature object
125  
-          } else if(!obj->marked_p(mark)) {
126  
-            handle->clear();
127  
-          } else {
128  
-            chunk_marks[i] = true;
129  
-          }
130  
-        }
131  
-        ++i;
132  
-      }
133  
-
134  
-      // Cleanup cached handles
135  
-      for(std::list<Handle*>::iterator i = cached->begin(); i != cached->end();) {
136  
-        Handle* handle = *i;
137  
-        if(handle->in_use_p()) {
138  
-          ++i;
139  
-        } else {
140  
-          i = cached->erase(i);
141  
-        }
142  
-      }
143  
-
144  
-      i = 0;
145  
-      for(std::vector<Handle*>::iterator it = allocator_->chunks_.begin();
146  
-          it != allocator_->chunks_.end();) {
147  
-        // No header was marked, so it's completely empty. Free it.
148  
-        if(!chunk_marks[i]) {
149  
-          Handle* chunk = *it;
150  
-          delete[] chunk;
151  
-          it = allocator_->chunks_.erase(it);
152  
-        } else {
153  
-          ++it;
154  
-        }
155  
-        ++i;
156  
-      }
157  
-
158  
-      allocator_->rebuild_freelist();
159  
-    }
160  
-
161 62
     HandleSet::HandleSet()
162 63
       : slow_(0)
163 64
     {
33  vm/capi/handle.hpp
@@ -5,10 +5,8 @@
5 5
 #include "gc/root.hpp"
6 6
 
7 7
 #include "capi/value.hpp"
8  
-#include "util/allocator.hpp"
9 8
 
10 9
 #include <tr1/unordered_set>
11  
-#include <vector>
12 10
 
13 11
 #ifndef RIO
14 12
 #define RIO rb_io_t
@@ -23,7 +21,6 @@ struct RFile;
23 21
 
24 22
 namespace rubinius {
25 23
   class NativeMethodEnvironment;
26  
-  class BakerGC;
27 24
 
28 25
   namespace capi {
29 26
 
@@ -202,36 +199,6 @@ namespace rubinius {
202 199
       bool rio_close();
203 200
     };
204 201
 
205  
-    class Handles {
206  
-
207  
-    private:
208  
-      Allocator<Handle>* allocator_;
209  
-
210  
-    public:
211  
-
212  
-      Handles()
213  
-        : allocator_(new Allocator<Handle>())
214  
-      {}
215  
-
216  
-      ~Handles();
217  
-
218  
-      Handle* allocate(STATE, Object* obj);
219  
-
220  
-      void deallocate_handles(std::list<Handle*>* cached, int mark, BakerGC* young);
221  
-
222  
-      void flush_all(NativeMethodEnvironment* env);
223  
-
224  
-      Allocator<Handle>* allocator() const {
225  
-        return allocator_;
226  
-      }
227  
-
228  
-      int size() const {
229  
-        return allocator_->in_use_;
230  
-      }
231  
-
232  
-    };
233  
-
234  
-
235 202
     typedef std::tr1::unordered_set<Handle*> SlowHandleSet;
236 203
 
237 204
     class HandleSet {
106  vm/capi/handles.cpp
... ...
@@ -0,0 +1,106 @@
  1
+#include "builtin/nativemethod.hpp"
  2
+#include "objectmemory.hpp"
  3
+#include "gc/baker.hpp"
  4
+#include "capi/capi.hpp"
  5
+#include "capi/handles.hpp"
  6
+
  7
+namespace rubinius {
  8
+  namespace capi {
  9
+
  10
+    Handle* Handles::allocate(STATE, Object* obj) {
  11
+      bool needs_gc = false;
  12
+      Handle* handle = allocator_->allocate(&needs_gc);
  13
+      handle->set_object(obj);
  14
+      handle->validate();
  15
+      if(needs_gc) {
  16
+        state->memory()->collect_mature_now = true;
  17
+      }
  18
+      return handle;
  19
+    }
  20
+
  21
+    void Handles::deallocate_handles(std::list<Handle*>* cached, int mark, BakerGC* young) {
  22
+
  23
+      std::vector<bool> chunk_marks(allocator_->chunks_.size(), false);
  24
+
  25
+      size_t i = 0;
  26
+
  27
+      for(std::vector<Handle*>::iterator it = allocator_->chunks_.begin();
  28
+          it != allocator_->chunks_.end(); ++it) {
  29
+        Handle* chunk = *it;
  30
+
  31
+        for(size_t j = 0; j < allocator_->cChunkSize; j++) {
  32
+          Handle* handle = &chunk[j];
  33
+
  34
+          Object* obj = handle->object();
  35
+
  36
+          if(!handle->in_use_p()) {
  37
+            continue;
  38
+          }
  39
+
  40
+          // Strong references will already have been updated.
  41
+          if(!handle->weak_p()) {
  42
+            chunk_marks[i] = true;
  43
+            continue;
  44
+          }
  45
+
  46
+          if(young) {
  47
+            if(obj->young_object_p()) {
  48
+
  49
+              // A weakref pointing to a valid young object
  50
+              //
  51
+              // TODO this only works because we run prune_handles right after
  52
+              // a collection. In this state, valid objects are only in current.
  53
+              if(young->in_current_p(obj)) {
  54
+                chunk_marks[i] = true;
  55
+              // A weakref pointing to a forwarded young object
  56
+              } else if(obj->forwarded_p()) {
  57
+                handle->set_object(obj->forward());
  58
+                chunk_marks[i] = true;
  59
+              // A weakref pointing to a dead young object
  60
+              } else {
  61
+                handle->clear();
  62
+              }
  63
+            } else {
  64
+              // Not a young object, so won't be GC'd so mark
  65
+              // chunk as still active
  66
+              chunk_marks[i] = true;
  67
+            }
  68
+
  69
+          // A weakref pointing to a dead mature object
  70
+          } else if(!obj->marked_p(mark)) {
  71
+            handle->clear();
  72
+          } else {
  73
+            chunk_marks[i] = true;
  74
+          }
  75
+        }
  76
+        ++i;
  77
+      }
  78
+
  79
+      // Cleanup cached handles
  80
+      for(std::list<Handle*>::iterator i = cached->begin(); i != cached->end();) {
  81
+        Handle* handle = *i;
  82
+        if(handle->in_use_p()) {
  83
+          ++i;
  84
+        } else {
  85
+          i = cached->erase(i);
  86
+        }
  87
+      }
  88
+
  89
+      i = 0;
  90
+      for(std::vector<Handle*>::iterator it = allocator_->chunks_.begin();
  91
+          it != allocator_->chunks_.end();) {
  92
+        // No header was marked, so it's completely empty. Free it.
  93
+        if(!chunk_marks[i]) {
  94
+          Handle* chunk = *it;
  95
+          delete[] chunk;
  96
+          it = allocator_->chunks_.erase(it);
  97
+        } else {
  98
+          ++it;
  99
+        }
  100
+        ++i;
  101
+      }
  102
+
  103
+      allocator_->rebuild_freelist();
  104
+    }
  105
+  }
  106
+}
49  vm/capi/handles.hpp
... ...
@@ -0,0 +1,49 @@
  1
+#ifndef RBX_CAPI_HANDLES_HPP
  2
+#define RBX_CAPI_HANDLES_HPP
  3
+
  4
+#include "vm.hpp"
  5
+#include "gc/root.hpp"
  6
+#include "util/allocator.hpp"
  7
+#include "capi/handle.hpp"
  8
+
  9
+#include <vector>
  10
+
  11
+namespace rubinius {
  12
+  class BakerGC;
  13
+
  14
+  namespace capi {
  15
+
  16
+    class Handles {
  17
+
  18
+    private:
  19
+      Allocator<Handle>* allocator_;
  20
+
  21
+    public:
  22
+
  23
+      Handles()
  24
+        : allocator_(new Allocator<Handle>())
  25
+      {}
  26
+
  27
+      ~Handles() {
  28
+        delete allocator_;
  29
+      }
  30
+
  31
+      Handle* allocate(STATE, Object* obj);
  32
+
  33
+      void deallocate_handles(std::list<Handle*>* cached, int mark, BakerGC* young);
  34
+
  35
+      void flush_all(NativeMethodEnvironment* env);
  36
+
  37
+      Allocator<Handle>* allocator() const {
  38
+        return allocator_;
  39
+      }
  40
+
  41
+      int size() const {
  42
+        return allocator_->in_use_;
  43
+      }
  44
+
  45
+    };
  46
+  }
  47
+}
  48
+
  49
+#endif
2  vm/gc/baker.cpp
@@ -13,7 +13,7 @@
13 13
 
14 14
 #include "gc/gc.hpp"
15 15
 
16  
-#include "capi/handle.hpp"
  16
+#include "capi/handles.hpp"
17 17
 #include "capi/tag.hpp"
18 18
 
19 19
 #ifdef ENABLE_LLVM
2  vm/gc/immix.cpp
@@ -3,7 +3,7 @@
3 3
 
4 4
 #include "instruments/stats.hpp"
5 5
 
6  
-#include "capi/handle.hpp"
  6
+#include "capi/handles.hpp"
7 7
 #include "capi/tag.hpp"
8 8
 #include "object_watch.hpp"
9 9
 
2  vm/gc/walker.cpp
... ...
@@ -1,7 +1,7 @@
1 1
 #include "gc/walker.hpp"
2 2
 #include "objectmemory.hpp"
3 3
 
4  
-#include "capi/handle.hpp"
  4
+#include "capi/handles.hpp"
5 5
 
6 6
 namespace rubinius {
7 7
   ObjectWalker::~ObjectWalker() {
2  vm/objectmemory.cpp
@@ -28,7 +28,7 @@
28 28
 #include "builtin/array.hpp"
29 29
 #include "builtin/thread.hpp"
30 30
 
31  
-#include "capi/handle.hpp"
  31
+#include "capi/handles.hpp"
32 32
 #include "configuration.hpp"
33 33
 
34 34
 #include "global_cache.hpp"
2  vm/shared_state.cpp
@@ -7,7 +7,7 @@
7 7
 #include "instruments/tooling.hpp"
8 8
 #include "instruments/timing.hpp"
9 9
 #include "global_cache.hpp"
10  
-#include "capi/handle.hpp"
  10
+#include "capi/handles.hpp"
11 11
 
12 12
 #include "util/thread.hpp"
13 13
 #include "inline_cache.hpp"
1  vm/test/test_objectmemory.hpp
@@ -6,6 +6,7 @@
6 6
 #include "gc/root.hpp"
7 7
 #include "gc/baker.hpp"
8 8
 #include "gc/marksweep.hpp"
  9
+#include "capi/handles.hpp"
9 10
 
10 11
 #include "object_utils.hpp"
11 12
 

0 notes on commit 1f6af7c

Please sign in to comment.
Something went wrong with that request. Please try again.