Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #ifndef VM_GC_CALLBACKS_H_ | |
| 6 #define VM_GC_CALLBACKS_H_ | |
| 7 | |
| 8 #include "include/dart_api.h" | |
| 9 #include "platform/utils.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
| 13 // A container for garbage collection callback function pointers. | |
| 14 // Pointers to the callback methods are stored within linked list | |
| 15 // nodes managed by the container. | |
| 16 template<typename T> | |
| 17 class GcCallbacks { | |
| 18 public: | |
| 19 GcCallbacks() : head_(NULL) { | |
| 20 } | |
| 21 | |
| 22 ~GcCallbacks() { | |
| 23 while (head_ != NULL) { | |
| 24 Link* prev = head_; | |
| 25 head_ = head_->next_; | |
| 26 delete prev; | |
| 27 } | |
| 28 } | |
| 29 | |
| 30 // Adds a new callback to the list. The new callback must not | |
| 31 // already be present in the list. | |
| 32 void Add(T callback) { | |
| 33 ASSERT(callback != NULL); | |
| 34 Link* link = new Link(callback, head_); | |
| 35 head_ = link; | |
| 36 } | |
| 37 | |
| 38 // Removes a callback from the list. The callback must be present | |
| 39 // in the list. | |
| 40 void Remove(T callback) { | |
| 41 ASSERT(callback != NULL); | |
| 42 if (head_ == NULL) return; | |
| 43 Link* prev = head_; | |
| 44 Link* curr = head_->next_; | |
| 45 if (prev->callback_ == callback) { | |
| 46 head_ = curr; | |
| 47 delete prev; | |
| 48 return; | |
| 49 } | |
| 50 while (curr != NULL) { | |
| 51 if (curr->callback_ == callback) { | |
| 52 prev->next_ = curr->next_; | |
| 53 delete curr; | |
| 54 return; | |
| 55 } | |
| 56 prev = curr; | |
| 57 curr = curr->next_; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 // Iterates through all of the callbacks in the list and invokes | |
| 62 // their callback methods. | |
| 63 void Invoke() const { | |
| 64 for (Link* curr = head_; curr != NULL; curr = curr->next_) { | |
| 65 (*curr->callback_)(); | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 // Returns the number of callbacks stored in the list. | |
| 70 intptr_t Count() const { | |
| 71 intptr_t sum = 0; | |
| 72 for (Link* curr = head_; curr != NULL; curr = curr->next_) { | |
| 73 ++sum; | |
| 74 } | |
| 75 return sum; | |
| 76 } | |
| 77 | |
| 78 // Returns true if the specified callback has is present in the | |
|
turnidge
2012/03/07 17:28:37
Extra word "has is"
cshapiro
2012/03/07 19:37:08
Thanks, done.
| |
| 79 // container. | |
| 80 bool Contains(T callback) const { | |
| 81 for (Link* curr = head_; curr != NULL; curr = curr->next_) { | |
| 82 if (curr->callback_ == callback) { | |
| 83 return true; | |
| 84 } | |
| 85 } | |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 // A linked-list element. | |
| 91 struct Link { | |
| 92 Link(T callback, Link* next) : callback_(callback), next_(next) { | |
| 93 } | |
| 94 T callback_; | |
| 95 Link* next_; | |
| 96 }; | |
| 97 | |
| 98 Link* head_; | |
| 99 }; | |
| 100 | |
| 101 | |
| 102 // A container for storing garbage collection prologue methods. | |
| 103 class GcPrologueCallbacks : public GcCallbacks<Dart_GcPrologueCallback> {}; | |
| 104 | |
| 105 | |
| 106 // A container for storing garbage collection epilogue methods. | |
| 107 class GcEpilogueCallbacks : public GcCallbacks<Dart_GcEpilogueCallback> {}; | |
| 108 | |
| 109 }; // namespace dart | |
| 110 | |
| 111 #endif // VM_GC_CALLBACKS_H_ | |
| OLD | NEW |