OLD | NEW |
1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dartino project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 | 4 |
5 #include <stdarg.h> | 5 #include <stdarg.h> |
6 #include <stdlib.h> | 6 #include <stdlib.h> |
7 | 7 |
8 #include "src/shared/utils.h" | 8 #include "src/shared/utils.h" |
9 | 9 |
10 #include "src/shared/platform.h" | 10 #include "src/shared/platform.h" |
11 | 11 |
12 namespace fletch { | 12 namespace dartino { |
13 | 13 |
14 Atomic<bool> Print::standard_output_enabled_(true); | 14 Atomic<bool> Print::standard_output_enabled_(true); |
15 | 15 |
16 #ifdef FLETCH_ENABLE_PRINT_INTERCEPTORS | 16 #ifdef DARTINO_ENABLE_PRINT_INTERCEPTORS |
17 Mutex* Print::mutex_ = Platform::CreateMutex(); | 17 Mutex* Print::mutex_ = Platform::CreateMutex(); |
18 PrintInterceptor* Print::interceptor_ = NULL; | 18 PrintInterceptor* Print::interceptor_ = NULL; |
19 #endif | 19 #endif |
20 | 20 |
21 void Print::Out(const char* format, ...) { | 21 void Print::Out(const char* format, ...) { |
22 #ifdef FLETCH_ENABLE_PRINT_INTERCEPTORS | 22 #ifdef DARTINO_ENABLE_PRINT_INTERCEPTORS |
23 va_list args; | 23 va_list args; |
24 va_start(args, format); | 24 va_start(args, format); |
25 int size = vsnprintf(NULL, 0, format, args); | 25 int size = vsnprintf(NULL, 0, format, args); |
26 va_end(args); | 26 va_end(args); |
27 char* message = reinterpret_cast<char*>(malloc(size + 1)); | 27 char* message = reinterpret_cast<char*>(malloc(size + 1)); |
28 va_start(args, format); | 28 va_start(args, format); |
29 int printed = vsnprintf(message, size + 1, format, args); | 29 int printed = vsnprintf(message, size + 1, format, args); |
30 ASSERT(printed == size); | 30 ASSERT(printed == size); |
31 va_end(args); | 31 va_end(args); |
32 if (standard_output_enabled_) { | 32 if (standard_output_enabled_) { |
33 fputs(message, stdout); | 33 fputs(message, stdout); |
34 fflush(stdout); | 34 fflush(stdout); |
35 } | 35 } |
36 ScopedLock scope(mutex_); | 36 ScopedLock scope(mutex_); |
37 for (PrintInterceptor* interceptor = interceptor_; interceptor != NULL; | 37 for (PrintInterceptor* interceptor = interceptor_; interceptor != NULL; |
38 interceptor = interceptor->next_) { | 38 interceptor = interceptor->next_) { |
39 interceptor->Out(message); | 39 interceptor->Out(message); |
40 } | 40 } |
41 free(message); | 41 free(message); |
42 #else | 42 #else |
43 if (standard_output_enabled_) { | 43 if (standard_output_enabled_) { |
44 va_list args; | 44 va_list args; |
45 va_start(args, format); | 45 va_start(args, format); |
46 vfprintf(stdout, format, args); | 46 vfprintf(stdout, format, args); |
47 va_end(args); | 47 va_end(args); |
48 } | 48 } |
49 #endif // FLETCH_ENABLE_PRINT_INTERCEPTORS | 49 #endif // DARTINO_ENABLE_PRINT_INTERCEPTORS |
50 } | 50 } |
51 | 51 |
52 void Print::Error(const char* format, ...) { | 52 void Print::Error(const char* format, ...) { |
53 #ifdef FLETCH_ENABLE_PRINT_INTERCEPTORS | 53 #ifdef DARTINO_ENABLE_PRINT_INTERCEPTORS |
54 va_list args; | 54 va_list args; |
55 va_start(args, format); | 55 va_start(args, format); |
56 int size = vsnprintf(NULL, 0, format, args); | 56 int size = vsnprintf(NULL, 0, format, args); |
57 va_end(args); | 57 va_end(args); |
58 char* message = reinterpret_cast<char*>(malloc(size + 1)); | 58 char* message = reinterpret_cast<char*>(malloc(size + 1)); |
59 va_start(args, format); | 59 va_start(args, format); |
60 int printed = vsnprintf(message, size + 1, format, args); | 60 int printed = vsnprintf(message, size + 1, format, args); |
61 ASSERT(printed == size); | 61 ASSERT(printed == size); |
62 va_end(args); | 62 va_end(args); |
63 if (standard_output_enabled_) { | 63 if (standard_output_enabled_) { |
64 fputs(message, stderr); | 64 fputs(message, stderr); |
65 fflush(stderr); | 65 fflush(stderr); |
66 } | 66 } |
67 ScopedLock scope(mutex_); | 67 ScopedLock scope(mutex_); |
68 for (PrintInterceptor* interceptor = interceptor_; interceptor != NULL; | 68 for (PrintInterceptor* interceptor = interceptor_; interceptor != NULL; |
69 interceptor = interceptor->next_) { | 69 interceptor = interceptor->next_) { |
70 interceptor->Error(message); | 70 interceptor->Error(message); |
71 } | 71 } |
72 free(message); | 72 free(message); |
73 #else | 73 #else |
74 if (standard_output_enabled_) { | 74 if (standard_output_enabled_) { |
75 va_list args; | 75 va_list args; |
76 va_start(args, format); | 76 va_start(args, format); |
77 vfprintf(stderr, format, args); | 77 vfprintf(stderr, format, args); |
78 va_end(args); | 78 va_end(args); |
79 } | 79 } |
80 #endif // FLETCH_ENABLE_PRINT_INTERCEPTORS | 80 #endif // DARTINO_ENABLE_PRINT_INTERCEPTORS |
81 } | 81 } |
82 | 82 |
83 void Print::RegisterPrintInterceptor(PrintInterceptor* interceptor) { | 83 void Print::RegisterPrintInterceptor(PrintInterceptor* interceptor) { |
84 #ifdef FLETCH_ENABLE_PRINT_INTERCEPTORS | 84 #ifdef DARTINO_ENABLE_PRINT_INTERCEPTORS |
85 ScopedLock scope(mutex_); | 85 ScopedLock scope(mutex_); |
86 ASSERT(!interceptor->next_); | 86 ASSERT(!interceptor->next_); |
87 interceptor->next_ = interceptor_; | 87 interceptor->next_ = interceptor_; |
88 interceptor_ = interceptor; | 88 interceptor_ = interceptor; |
89 #else | 89 #else |
90 UNIMPLEMENTED(); | 90 UNIMPLEMENTED(); |
91 #endif // FLETCH_ENABLE_PRINT_INTERCEPTORS | 91 #endif // DARTINO_ENABLE_PRINT_INTERCEPTORS |
92 } | 92 } |
93 | 93 |
94 void Print::UnregisterPrintInterceptor(PrintInterceptor* interceptor) { | 94 void Print::UnregisterPrintInterceptor(PrintInterceptor* interceptor) { |
95 #ifdef FLETCH_ENABLE_PRINT_INTERCEPTORS | 95 #ifdef DARTINO_ENABLE_PRINT_INTERCEPTORS |
96 ScopedLock scope(mutex_); | 96 ScopedLock scope(mutex_); |
97 if (interceptor == interceptor_) { | 97 if (interceptor == interceptor_) { |
98 interceptor_ = interceptor->next_; | 98 interceptor_ = interceptor->next_; |
99 } else { | 99 } else { |
100 PrintInterceptor* prev = interceptor_; | 100 PrintInterceptor* prev = interceptor_; |
101 while (prev != NULL && prev->next_ != interceptor) { | 101 while (prev != NULL && prev->next_ != interceptor) { |
102 prev = prev->next_; | 102 prev = prev->next_; |
103 } | 103 } |
104 if (prev != NULL) { | 104 if (prev != NULL) { |
105 prev->next_ = interceptor->next_; | 105 prev->next_ = interceptor->next_; |
106 } | 106 } |
107 } | 107 } |
108 interceptor->next_ = NULL; | 108 interceptor->next_ = NULL; |
109 #else | 109 #else |
110 UNIMPLEMENTED(); | 110 UNIMPLEMENTED(); |
111 #endif // FLETCH_ENABLE_PRINT_INTERCEPTORS | 111 #endif // DARTINO_ENABLE_PRINT_INTERCEPTORS |
112 } | 112 } |
113 | 113 |
114 void Print::UnregisterPrintInterceptors() { | 114 void Print::UnregisterPrintInterceptors() { |
115 #ifdef FLETCH_ENABLE_PRINT_INTERCEPTORS | 115 #ifdef DARTINO_ENABLE_PRINT_INTERCEPTORS |
116 ScopedLock scope(mutex_); | 116 ScopedLock scope(mutex_); |
117 delete interceptor_; | 117 delete interceptor_; |
118 interceptor_ = NULL; | 118 interceptor_ = NULL; |
119 #endif // FLETCH_ENABLE_PRINT_INTERCEPTORS | 119 #endif // DARTINO_ENABLE_PRINT_INTERCEPTORS |
120 } | 120 } |
121 | 121 |
122 uint32 Utils::StringHash(const uint8* data, int length, int char_width) { | 122 uint32 Utils::StringHash(const uint8* data, int length, int char_width) { |
123 // This implementation is based on the public domain MurmurHash | 123 // This implementation is based on the public domain MurmurHash |
124 // version 2.0. The constants M and R have been determined work | 124 // version 2.0. The constants M and R have been determined work |
125 // well experimentally. | 125 // well experimentally. |
126 const uint32 M = 0x5bd1e995; | 126 const uint32 M = 0x5bd1e995; |
127 const int R = 24; | 127 const int R = 24; |
128 int remaining = length; | 128 int remaining = length; |
129 uint32 hash = length; | 129 uint32 hash = length; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 163 } |
164 | 164 |
165 // Do a few final mixes of the hash to ensure the last few bytes are | 165 // Do a few final mixes of the hash to ensure the last few bytes are |
166 // well-incorporated. | 166 // well-incorporated. |
167 hash ^= hash >> 13; | 167 hash ^= hash >> 13; |
168 hash *= M; | 168 hash *= M; |
169 hash ^= hash >> 15; | 169 hash ^= hash >> 15; |
170 return hash; | 170 return hash; |
171 } | 171 } |
172 | 172 |
173 } // namespace fletch | 173 } // namespace dartino |
OLD | NEW |