OLD | NEW |
1 // Copyright 2009 The RE2 Authors. All Rights Reserved. | 1 // Copyright 2009 The RE2 Authors. All Rights Reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef RE2_UTIL_UTIL_H__ | 5 #ifndef RE2_UTIL_UTIL_H__ |
6 #define RE2_UTIL_UTIL_H__ | 6 #define RE2_UTIL_UTIL_H__ |
7 | 7 |
8 // C | 8 // C |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <string.h> | 10 #include <string.h> |
11 #include <stdint.h> | 11 #include <stdint.h> |
12 #include <stddef.h> // For size_t | 12 #include <stddef.h> // For size_t |
13 #include <assert.h> | 13 #include <assert.h> |
14 #include <stdarg.h> | 14 #include <stdarg.h> |
15 #include <time.h> // For clock_gettime, CLOCK_REALTIME | 15 #ifndef WIN32 |
16 #include <ctype.h> // For isdigit, isalpha | 16 #include <sys/time.h> |
17 | |
18 #if !defined(_WIN32) | |
19 #include <sys/time.h> // For gettimeofday | |
20 #endif | 17 #endif |
| 18 #include <time.h> |
| 19 #include <ctype.h> // For isdigit, isalpha. |
21 | 20 |
22 // C++ | 21 // C++ |
23 #include <ctime> | |
24 #include <vector> | 22 #include <vector> |
25 #include <string> | 23 #include <string> |
26 #include <algorithm> | 24 #include <algorithm> |
27 #include <iosfwd> | 25 #include <iosfwd> |
28 #include <map> | 26 #include <map> |
29 #include <stack> | 27 #include <stack> |
30 #include <ostream> | 28 #include <ostream> |
31 #include <utility> | 29 #include <utility> |
32 #include <set> | 30 #include <set> |
33 | 31 |
| 32 #include "build/build_config.h" |
| 33 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" |
| 34 |
34 // Use std names. | 35 // Use std names. |
35 using std::set; | 36 using std::set; |
36 using std::pair; | 37 using std::pair; |
37 using std::vector; | 38 using std::vector; |
38 using std::string; | 39 using std::string; |
39 using std::min; | 40 using std::min; |
40 using std::max; | 41 using std::max; |
41 using std::ostream; | 42 using std::ostream; |
42 using std::map; | 43 using std::map; |
43 using std::stack; | 44 using std::stack; |
44 using std::sort; | 45 using std::sort; |
45 using std::swap; | 46 using std::swap; |
46 using std::make_pair; | 47 using std::make_pair; |
47 | 48 |
48 #if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(_LIBCPP_ABI_VERSION) | 49 #if defined(__GNUC__) && !defined(USE_CXX0X) && !defined(_LIBCPP_ABI_VERSION) &&
!defined(OS_ANDROID) |
49 | 50 |
50 #include <tr1/unordered_set> | 51 #include <tr1/unordered_set> |
51 using std::tr1::unordered_set; | 52 using std::tr1::unordered_set; |
52 | 53 |
53 #else | 54 #else |
54 | 55 |
55 #include <unordered_set> | 56 #include <unordered_set> |
56 #if defined(_WIN32) | 57 #if defined(WIN32) || (defined(OS_ANDROID) && !defined(_LIBCPP_ABI_VERSION)) |
57 using std::tr1::unordered_set; | 58 using std::tr1::unordered_set; |
58 #else | 59 #else |
59 using std::unordered_set; | 60 using std::unordered_set; |
60 #endif | 61 #endif |
61 | 62 |
62 #endif | 63 #endif |
63 | 64 |
64 #ifdef _WIN32 | |
65 | |
66 #define snprintf _snprintf_s | |
67 #define stricmp _stricmp | |
68 #define strtof strtod /* not really correct but best we can do */ | |
69 #define strtoll _strtoi64 | |
70 #define strtoull _strtoui64 | |
71 #define vsnprintf vsnprintf_s | |
72 | |
73 #endif | |
74 | |
75 namespace re2 { | 65 namespace re2 { |
76 | 66 |
77 typedef int8_t int8; | 67 typedef int8_t int8; |
78 typedef uint8_t uint8; | 68 typedef uint8_t uint8; |
79 typedef int16_t int16; | 69 typedef int16_t int16; |
80 typedef uint16_t uint16; | 70 typedef uint16_t uint16; |
81 typedef int32_t int32; | 71 typedef int32_t int32; |
82 typedef uint32_t uint32; | 72 typedef uint32_t uint32; |
83 typedef int64_t int64; | 73 typedef int64_t int64; |
84 typedef uint64_t uint64; | 74 typedef uint64_t uint64; |
85 | 75 |
86 typedef unsigned long ulong; | 76 typedef unsigned long ulong; |
87 typedef unsigned int uint; | 77 typedef unsigned int uint; |
88 typedef unsigned short ushort; | 78 typedef unsigned short ushort; |
89 | 79 |
90 // Prevent the compiler from complaining about or optimizing away variables | |
91 // that appear unused. | |
92 #undef ATTRIBUTE_UNUSED | |
93 #if defined(__GNUC__) | |
94 #define ATTRIBUTE_UNUSED __attribute__ ((unused)) | |
95 #else | |
96 #define ATTRIBUTE_UNUSED | |
97 #endif | |
98 | |
99 // COMPILE_ASSERT causes a compile error about msg if expr is not true. | 80 // COMPILE_ASSERT causes a compile error about msg if expr is not true. |
100 #if __cplusplus >= 201103L | 81 #if __cplusplus >= 201103L |
101 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) | 82 #define COMPILE_ASSERT(expr, msg) static_assert(expr, #msg) |
102 #else | 83 #else |
103 template<bool> struct CompileAssert {}; | 84 template<bool> struct CompileAssert {}; |
104 #define COMPILE_ASSERT(expr, msg) \ | 85 #define COMPILE_ASSERT(expr, msg) \ |
105 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] ATTRIBUTE_UNUSED | 86 typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1] |
106 #endif | 87 #endif |
107 | 88 |
108 // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. | 89 // DISALLOW_EVIL_CONSTRUCTORS disallows the copy and operator= functions. |
109 // It goes in the private: declarations in a class. | 90 // It goes in the private: declarations in a class. |
110 #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ | 91 #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) \ |
111 TypeName(const TypeName&); \ | 92 TypeName(const TypeName&); \ |
112 void operator=(const TypeName&) | 93 void operator=(const TypeName&) |
113 | 94 |
114 #define arraysize(array) (int)(sizeof(array)/sizeof((array)[0])) | 95 #define arraysize(array) (sizeof(array)/sizeof((array)[0])) |
| 96 |
| 97 // Fake lock annotations. For real ones, see |
| 98 // http://code.google.com/p/data-race-test/ |
| 99 #ifndef ANNOTATE_PUBLISH_MEMORY_RANGE |
| 100 #define ANNOTATE_PUBLISH_MEMORY_RANGE(a, b) |
| 101 #define ANNOTATE_IGNORE_WRITES_BEGIN() |
| 102 #define ANNOTATE_IGNORE_WRITES_END() |
| 103 #define ANNOTATE_BENIGN_RACE(a, b) |
| 104 #define NO_THREAD_SAFETY_ANALYSIS |
| 105 #define ANNOTATE_HAPPENS_BEFORE(x) |
| 106 #define ANNOTATE_HAPPENS_AFTER(x) |
| 107 #define ANNOTATE_UNPROTECTED_READ(x) (x) |
| 108 #endif |
115 | 109 |
116 class StringPiece; | 110 class StringPiece; |
117 | 111 |
118 string CEscape(const StringPiece& src); | 112 string CEscape(const StringPiece& src); |
119 int CEscapeString(const char* src, int src_len, char* dest, int dest_len); | 113 int CEscapeString(const char* src, int src_len, char* dest, int dest_len); |
120 | 114 |
121 extern string StringPrintf(const char* format, ...); | 115 extern string StringPrintf(const char* format, ...); |
122 extern void SStringPrintf(string* dst, const char* format, ...); | 116 extern void SStringPrintf(string* dst, const char* format, ...); |
123 extern void StringAppendF(string* dst, const char* format, ...); | 117 extern void StringAppendF(string* dst, const char* format, ...); |
124 extern string PrefixSuccessor(const StringPiece& prefix); | 118 extern string PrefixSuccessor(const StringPiece& prefix); |
125 | 119 |
126 uint32 hashword(const uint32*, size_t, uint32); | 120 uint32 hashword(const uint32*, size_t, uint32); |
127 void hashword2(const uint32*, size_t, uint32*, uint32*); | 121 void hashword2(const uint32*, size_t, uint32*, uint32*); |
128 | 122 |
129 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) { | 123 static inline uint32 Hash32StringWithSeed(const char* s, int len, uint32 seed) { |
130 return hashword((uint32*)s, len/4, seed); | 124 return hashword((uint32*)s, len/4, seed); |
131 } | 125 } |
132 | 126 |
133 static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) { | 127 static inline uint64 Hash64StringWithSeed(const char* s, int len, uint32 seed) { |
134 uint32 x, y; | 128 uint32 x, y; |
135 x = seed; | 129 x = seed; |
136 y = 0; | 130 y = 0; |
137 hashword2((uint32*)s, len/4, &x, &y); | 131 hashword2((uint32*)s, len/4, &x, &y); |
138 return ((uint64)x << 32) | y; | 132 return ((uint64)x << 32) | y; |
139 } | 133 } |
140 | 134 |
141 bool RunningOnValgrind(); | 135 inline bool RunningOnValgrindOrMemorySanitizer() { |
| 136 #if defined(MEMORY_SANITIZER) |
| 137 return true; |
| 138 #else |
| 139 return RunningOnValgrind(); |
| 140 #endif |
| 141 } |
142 | 142 |
143 } // namespace re2 | 143 } // namespace re2 |
144 | 144 |
| 145 #include "util/arena.h" |
145 #include "util/logging.h" | 146 #include "util/logging.h" |
146 #include "util/mutex.h" | 147 #include "util/mutex.h" |
147 #include "util/utf.h" | 148 #include "util/utf.h" |
148 | 149 |
149 #endif // RE2_UTIL_UTIL_H__ | 150 #endif // RE2_UTIL_UTIL_H__ |
OLD | NEW |