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 | #include "vm/code_observers.h" |
6 | |
7 | #include "vm/os.h" |
8 | #include "vm/os_thread.h" |
9 | |
10 | namespace dart { |
11 | |
12 | #ifndef PRODUCT |
13 | |
14 | Mutex* CodeObservers::mutex_ = NULL; |
15 | intptr_t CodeObservers::observers_length_ = 0; |
16 | CodeObserver** CodeObservers::observers_ = NULL; |
17 | |
18 | class ExternalCodeObserverAdapter : public CodeObserver { |
19 | public: |
20 | explicit ExternalCodeObserverAdapter(Dart_CodeObserver delegate) |
21 | : delegate_(delegate) {} |
22 | |
23 | virtual bool IsActive() const { return true; } |
24 | |
25 | virtual void (const char* name, |
26 | uword base, |
27 | uword prologue_offset, |
28 | uword size, |
29 | bool optimized, |
30 | const CodeComments* ) { |
31 | return delegate_.on_new_code(&delegate_, name, base, size); |
32 | } |
33 | |
34 | private: |
35 | Dart_CodeObserver delegate_; |
36 | }; |
37 | |
38 | void CodeObservers::RegisterExternal(Dart_CodeObserver observer) { |
39 | Register(new ExternalCodeObserverAdapter(observer)); |
40 | } |
41 | |
42 | void CodeObservers::Register(CodeObserver* observer) { |
43 | observers_length_++; |
44 | observers_ = reinterpret_cast<CodeObserver**>( |
45 | realloc(observers_, sizeof(observer) * observers_length_)); |
46 | if (observers_ == NULL) { |
47 | FATAL("failed to grow code observers array" ); |
48 | } |
49 | observers_[observers_length_ - 1] = observer; |
50 | } |
51 | |
52 | void CodeObservers::(const char* name, |
53 | uword base, |
54 | uword prologue_offset, |
55 | uword size, |
56 | bool optimized, |
57 | const CodeComments* ) { |
58 | ASSERT(!AreActive() || (strlen(name) != 0)); |
59 | for (intptr_t i = 0; i < observers_length_; i++) { |
60 | if (observers_[i]->IsActive()) { |
61 | observers_[i]->Notify(name, base, prologue_offset, size, optimized, |
62 | comments); |
63 | } |
64 | } |
65 | } |
66 | |
67 | bool CodeObservers::AreActive() { |
68 | for (intptr_t i = 0; i < observers_length_; i++) { |
69 | if (observers_[i]->IsActive()) return true; |
70 | } |
71 | return false; |
72 | } |
73 | |
74 | void CodeObservers::Cleanup() { |
75 | for (intptr_t i = 0; i < observers_length_; i++) { |
76 | delete observers_[i]; |
77 | } |
78 | free(observers_); |
79 | observers_length_ = 0; |
80 | observers_ = NULL; |
81 | } |
82 | |
83 | void CodeObservers::Init() { |
84 | if (mutex_ == NULL) { |
85 | mutex_ = new Mutex(); |
86 | } |
87 | ASSERT(mutex_ != NULL); |
88 | OS::RegisterCodeObservers(); |
89 | } |
90 | |
91 | #endif // !PRODUCT |
92 | |
93 | } // namespace dart |
94 | |