Chromium Code Reviews| Index: runtime/vm/dart_api_state.h |
| diff --git a/runtime/vm/dart_api_state.h b/runtime/vm/dart_api_state.h |
| index f039db0a2ec002ee97e07cf72c473d561fa1e482..a48f1bdee1635ee406da06fee0799552e9d9edac 100644 |
| --- a/runtime/vm/dart_api_state.h |
| +++ b/runtime/vm/dart_api_state.h |
| @@ -401,6 +401,103 @@ class WeakReference { |
| }; |
| +// A container for garbage collection callback function pointers. |
| +// Pointers to the callback methods are stored within linked list |
| +// nodes managed by the container. |
|
turnidge
2012/03/07 00:25:16
I dislike how much code ends up in this header fil
cshapiro
2012/03/07 03:06:10
Yeah, moved to its own file.
|
| +template<typename T> |
| +class GcCallbacks { |
| + public: |
| + GcCallbacks() : head_(NULL) { |
| + } |
| + |
| + ~GcCallbacks() { |
| + while (head_ != NULL) { |
| + Link* prev = head_; |
| + head_ = head_->next_; |
| + delete prev; |
| + } |
| + } |
| + |
| + // Adds a new callback to the list. The new callback must not |
| + // already be present in the list. |
| + void Add(T callback) { |
| + ASSERT(callback != NULL); |
| + Link* link = new Link(callback, head_); |
| + head_ = link; |
| + } |
| + |
| + // Removes a callback from the list. The callback must be present |
| + // in the list. |
| + void Remove(T callback) { |
| + ASSERT(callback != NULL); |
| + if (head_ == NULL) return; |
| + Link* prev = head_; |
| + Link* curr = head_->next_; |
| + if (prev->callback_ == callback) { |
| + head_ = curr; |
| + delete prev; |
| + return; |
| + } |
| + while (curr != NULL) { |
| + if (curr->callback_ == callback) { |
| + prev->next_ = curr->next_; |
| + delete curr; |
| + return; |
| + } |
| + prev = curr; |
| + curr = curr->next_; |
| + } |
| + } |
| + |
| + // Iterates through all of the callbacks in the list and invokes |
| + // their callback methods. |
| + void Invoke() const { |
| + for (Link* curr = head_; curr != NULL; curr = curr->next_) { |
| + (*curr->callback_)(); |
| + } |
| + } |
| + |
| + // Returns the number of callbacks stored in the list. |
| + intptr_t Count() const { |
| + intptr_t sum = 0; |
| + for (Link* curr = head_; curr != NULL; curr = curr->next_) { |
| + ++sum; |
| + } |
| + return sum; |
| + } |
| + |
| + // Returns true if the specified callback has is present in the |
|
turnidge
2012/03/07 00:25:16
Extra word "has"
|
| + // container. |
| + bool Contains(T callback) const { |
| + for (Link* curr = head_; curr != NULL; curr = curr->next_) { |
| + if (curr->callback_ == callback) { |
| + return true; |
| + } |
| + } |
| + return false; |
| + } |
| + |
| + private: |
| + // A linked-list element. |
| + struct Link { |
| + Link(T callback, Link* next) : callback_(callback), next_(next) { |
| + } |
| + T callback_; |
| + Link* next_; |
| + }; |
| + |
| + Link* head_; |
| +}; |
| + |
| + |
| +// A container for storing garbage collection prologue methods. |
| +class GcPrologueCallbacks : public GcCallbacks<Dart_GcPrologueCallback> {}; |
| + |
| + |
| +// A container for storing garbage collection epilogue methods. |
| +class GcEpilogueCallbacks : public GcCallbacks<Dart_GcEpilogueCallback> {}; |
| + |
| + |
| // Structure used for the implementation of local scopes used in dart_api. |
| // These local scopes manage handles and memory allocated in the scope. |
| class ApiLocalScope { |
| @@ -458,14 +555,25 @@ class ApiState { |
| // Accessors. |
| ApiLocalScope* top_scope() const { return top_scope_; } |
| void set_top_scope(ApiLocalScope* value) { top_scope_ = value; } |
| + |
| PersistentHandles& persistent_handles() { return persistent_handles_; } |
| WeakPersistentHandles& weak_persistent_handles() { |
| return weak_persistent_handles_; |
| } |
| + |
| WeakReference* delayed_weak_references() { return delayed_weak_references_; } |
| void set_delayed_weak_references(WeakReference* reference) { |
| delayed_weak_references_ = reference; |
| } |
| + |
| + GcPrologueCallbacks& gc_prologue_callbacks() { |
| + return gc_prologue_callbacks_; |
| + }; |
| + |
| + GcEpilogueCallbacks& gc_epilogue_callbacks() { |
| + return gc_epilogue_callbacks_; |
| + }; |
| + |
| void UnwindScopes(uword sp) { |
| while (top_scope_ != NULL && top_scope_->stack_marker() < sp) { |
| ApiLocalScope* scope = top_scope_; |
| @@ -573,6 +681,10 @@ class ApiState { |
| ApiLocalScope* top_scope_; |
| WeakReference* delayed_weak_references_; |
| + // |
|
turnidge
2012/03/07 00:25:16
<empty comment>
|
| + GcPrologueCallbacks gc_prologue_callbacks_; |
| + GcEpilogueCallbacks gc_epilogue_callbacks_; |
| + |
| // Persistent handles to important objects. |
| PersistentHandle* null_; |
| PersistentHandle* true_; |