ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_UTILS_H_ |
| 29 | #define V8_UTILS_H_ |
| 30 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 31 | #include <stdlib.h> |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 32 | #include <string.h> |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 33 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame^] | 34 | #include "checks.h" |
| 35 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 36 | namespace v8 { |
| 37 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 38 | |
| 39 | // ---------------------------------------------------------------------------- |
| 40 | // General helper functions |
| 41 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 42 | #define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0) |
| 43 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 44 | // Returns true iff x is a power of 2 (or zero). Cannot be used with the |
| 45 | // maximally negative value of the type T (the -1 overflows). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 46 | template <typename T> |
| 47 | static inline bool IsPowerOf2(T x) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 48 | return IS_POWER_OF_TWO(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 52 | // X must be a power of 2. Returns the number of trailing zeros. |
| 53 | template <typename T> |
| 54 | static inline int WhichPowerOf2(T x) { |
| 55 | ASSERT(IsPowerOf2(x)); |
| 56 | ASSERT(x != 0); |
| 57 | if (x < 0) return 31; |
| 58 | int bits = 0; |
| 59 | #ifdef DEBUG |
| 60 | int original_x = x; |
| 61 | #endif |
| 62 | if (x >= 0x10000) { |
| 63 | bits += 16; |
| 64 | x >>= 16; |
| 65 | } |
| 66 | if (x >= 0x100) { |
| 67 | bits += 8; |
| 68 | x >>= 8; |
| 69 | } |
| 70 | if (x >= 0x10) { |
| 71 | bits += 4; |
| 72 | x >>= 4; |
| 73 | } |
| 74 | switch (x) { |
| 75 | default: UNREACHABLE(); |
| 76 | case 8: bits++; // Fall through. |
| 77 | case 4: bits++; // Fall through. |
| 78 | case 2: bits++; // Fall through. |
| 79 | case 1: break; |
| 80 | } |
| 81 | ASSERT_EQ(1 << bits, original_x); |
| 82 | return bits; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 87 | // The C++ standard leaves the semantics of '>>' undefined for |
| 88 | // negative signed operands. Most implementations do the right thing, |
| 89 | // though. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 90 | static inline int ArithmeticShiftRight(int x, int s) { |
| 91 | return x >> s; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | // Compute the 0-relative offset of some absolute value x of type T. |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 96 | // This allows conversion of Addresses and integral types into |
| 97 | // 0-relative int offsets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | template <typename T> |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 99 | static inline intptr_t OffsetFrom(T x) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 100 | return x - static_cast<T>(0); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | // Compute the absolute value of type T for some 0-relative offset x. |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 105 | // This allows conversion of 0-relative int offsets into Addresses and |
| 106 | // integral types. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | template <typename T> |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 108 | static inline T AddressFrom(intptr_t x) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 109 | return static_cast<T>(static_cast<T>(0) + x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
| 113 | // Return the largest multiple of m which is <= x. |
| 114 | template <typename T> |
| 115 | static inline T RoundDown(T x, int m) { |
| 116 | ASSERT(IsPowerOf2(m)); |
| 117 | return AddressFrom<T>(OffsetFrom(x) & -m); |
| 118 | } |
| 119 | |
| 120 | |
| 121 | // Return the smallest multiple of m which is >= x. |
| 122 | template <typename T> |
| 123 | static inline T RoundUp(T x, int m) { |
| 124 | return RoundDown(x + m - 1, m); |
| 125 | } |
| 126 | |
| 127 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 128 | template <typename T> |
| 129 | static int Compare(const T& a, const T& b) { |
| 130 | if (a == b) |
| 131 | return 0; |
| 132 | else if (a < b) |
| 133 | return -1; |
| 134 | else |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | template <typename T> |
| 140 | static int PointerValueCompare(const T* a, const T* b) { |
| 141 | return Compare<T>(*a, *b); |
| 142 | } |
| 143 | |
| 144 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 145 | // Returns the smallest power of two which is >= x. If you pass in a |
| 146 | // number that is already a power of two, it is returned as is. |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame^] | 147 | // Implementation is from "Hacker's Delight" by Henry S. Warren, Jr., |
| 148 | // figure 3-3, page 48, where the function is called clp2. |
| 149 | static inline uint32_t RoundUpToPowerOf2(uint32_t x) { |
| 150 | ASSERT(x <= 0x80000000u); |
| 151 | x = x - 1; |
| 152 | x = x | (x >> 1); |
| 153 | x = x | (x >> 2); |
| 154 | x = x | (x >> 4); |
| 155 | x = x | (x >> 8); |
| 156 | x = x | (x >> 16); |
| 157 | return x + 1; |
| 158 | } |
| 159 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 160 | |
| 161 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 162 | template <typename T> |
| 163 | static inline bool IsAligned(T value, T alignment) { |
| 164 | ASSERT(IsPowerOf2(alignment)); |
| 165 | return (value & (alignment - 1)) == 0; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | // Returns true if (addr + offset) is aligned. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 170 | static inline bool IsAddressAligned(Address addr, |
| 171 | intptr_t alignment, |
| 172 | int offset) { |
| 173 | intptr_t offs = OffsetFrom(addr + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 174 | return IsAligned(offs, alignment); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | // Returns the maximum of the two parameters. |
| 179 | template <typename T> |
| 180 | static T Max(T a, T b) { |
| 181 | return a < b ? b : a; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | // Returns the minimum of the two parameters. |
| 186 | template <typename T> |
| 187 | static T Min(T a, T b) { |
| 188 | return a < b ? a : b; |
| 189 | } |
| 190 | |
| 191 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 192 | inline int StrLength(const char* string) { |
| 193 | size_t length = strlen(string); |
| 194 | ASSERT(length == static_cast<size_t>(static_cast<int>(length))); |
| 195 | return static_cast<int>(length); |
| 196 | } |
| 197 | |
| 198 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 199 | // ---------------------------------------------------------------------------- |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 200 | // BitField is a help template for encoding and decode bitfield with |
| 201 | // unsigned content. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 202 | template<class T, int shift, int size> |
| 203 | class BitField { |
| 204 | public: |
| 205 | // Tells whether the provided value fits into the bit field. |
| 206 | static bool is_valid(T value) { |
| 207 | return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0; |
| 208 | } |
| 209 | |
| 210 | // Returns a uint32_t mask of bit field. |
| 211 | static uint32_t mask() { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 212 | // To use all bits of a uint32 in a bitfield without compiler warnings we |
| 213 | // have to compute 2^32 without using a shift count of 32. |
| 214 | return ((1U << shift) << size) - (1U << shift); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Returns a uint32_t with the bit field value encoded. |
| 218 | static uint32_t encode(T value) { |
| 219 | ASSERT(is_valid(value)); |
| 220 | return static_cast<uint32_t>(value) << shift; |
| 221 | } |
| 222 | |
| 223 | // Extracts the bit field from the value. |
| 224 | static T decode(uint32_t value) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 225 | return static_cast<T>((value & mask()) >> shift); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 226 | } |
| 227 | }; |
| 228 | |
| 229 | |
| 230 | // ---------------------------------------------------------------------------- |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 231 | // Hash function. |
| 232 | |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame^] | 233 | // Thomas Wang, Integer Hash Functions. |
| 234 | // http://www.concentric.net/~Ttwang/tech/inthash.htm |
| 235 | static inline uint32_t ComputeIntegerHash(uint32_t key) { |
| 236 | uint32_t hash = key; |
| 237 | hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1; |
| 238 | hash = hash ^ (hash >> 12); |
| 239 | hash = hash + (hash << 2); |
| 240 | hash = hash ^ (hash >> 4); |
| 241 | hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11); |
| 242 | hash = hash ^ (hash >> 16); |
| 243 | return hash; |
| 244 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 245 | |
| 246 | |
| 247 | // ---------------------------------------------------------------------------- |
| 248 | // Miscellaneous |
| 249 | |
| 250 | // A static resource holds a static instance that can be reserved in |
| 251 | // a local scope using an instance of Access. Attempts to re-reserve |
| 252 | // the instance will cause an error. |
| 253 | template <typename T> |
| 254 | class StaticResource { |
| 255 | public: |
| 256 | StaticResource() : is_reserved_(false) {} |
| 257 | |
| 258 | private: |
| 259 | template <typename S> friend class Access; |
| 260 | T instance_; |
| 261 | bool is_reserved_; |
| 262 | }; |
| 263 | |
| 264 | |
| 265 | // Locally scoped access to a static resource. |
| 266 | template <typename T> |
| 267 | class Access { |
| 268 | public: |
| 269 | explicit Access(StaticResource<T>* resource) |
| 270 | : resource_(resource) |
| 271 | , instance_(&resource->instance_) { |
| 272 | ASSERT(!resource->is_reserved_); |
| 273 | resource->is_reserved_ = true; |
| 274 | } |
| 275 | |
| 276 | ~Access() { |
| 277 | resource_->is_reserved_ = false; |
| 278 | resource_ = NULL; |
| 279 | instance_ = NULL; |
| 280 | } |
| 281 | |
| 282 | T* value() { return instance_; } |
| 283 | T* operator -> () { return instance_; } |
| 284 | |
| 285 | private: |
| 286 | StaticResource<T>* resource_; |
| 287 | T* instance_; |
| 288 | }; |
| 289 | |
| 290 | |
| 291 | template <typename T> |
| 292 | class Vector { |
| 293 | public: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 294 | Vector() : start_(NULL), length_(0) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 295 | Vector(T* data, int length) : start_(data), length_(length) { |
| 296 | ASSERT(length == 0 || (length > 0 && data != NULL)); |
| 297 | } |
| 298 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 299 | static Vector<T> New(int length) { |
| 300 | return Vector<T>(NewArray<T>(length), length); |
| 301 | } |
| 302 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 303 | // Returns a vector using the same backing storage as this one, |
| 304 | // spanning from and including 'from', to but not including 'to'. |
| 305 | Vector<T> SubVector(int from, int to) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 306 | ASSERT(to <= length_); |
| 307 | ASSERT(from < to); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 308 | ASSERT(0 <= from); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 309 | return Vector<T>(start() + from, to - from); |
| 310 | } |
| 311 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 312 | // Returns the length of the vector. |
| 313 | int length() const { return length_; } |
| 314 | |
| 315 | // Returns whether or not the vector is empty. |
| 316 | bool is_empty() const { return length_ == 0; } |
| 317 | |
| 318 | // Returns the pointer to the start of the data in the vector. |
| 319 | T* start() const { return start_; } |
| 320 | |
| 321 | // Access individual vector elements - checks bounds in debug mode. |
| 322 | T& operator[](int index) const { |
| 323 | ASSERT(0 <= index && index < length_); |
| 324 | return start_[index]; |
| 325 | } |
| 326 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 327 | T& first() { return start_[0]; } |
| 328 | |
| 329 | T& last() { return start_[length_ - 1]; } |
| 330 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 331 | // Returns a clone of this vector with a new backing store. |
| 332 | Vector<T> Clone() const { |
| 333 | T* result = NewArray<T>(length_); |
| 334 | for (int i = 0; i < length_; i++) result[i] = start_[i]; |
| 335 | return Vector<T>(result, length_); |
| 336 | } |
| 337 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 338 | void Sort(int (*cmp)(const T*, const T*)) { |
| 339 | typedef int (*RawComparer)(const void*, const void*); |
| 340 | qsort(start(), |
| 341 | length(), |
| 342 | sizeof(T), |
| 343 | reinterpret_cast<RawComparer>(cmp)); |
| 344 | } |
| 345 | |
| 346 | void Sort() { |
| 347 | Sort(PointerValueCompare<T>); |
| 348 | } |
| 349 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 350 | void Truncate(int length) { |
| 351 | ASSERT(length <= length_); |
| 352 | length_ = length; |
| 353 | } |
| 354 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 355 | // Releases the array underlying this vector. Once disposed the |
| 356 | // vector is empty. |
| 357 | void Dispose() { |
| 358 | DeleteArray(start_); |
| 359 | start_ = NULL; |
| 360 | length_ = 0; |
| 361 | } |
| 362 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 363 | inline Vector<T> operator+(int offset) { |
| 364 | ASSERT(offset < length_); |
| 365 | return Vector<T>(start_ + offset, length_ - offset); |
| 366 | } |
| 367 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 368 | // Factory method for creating empty vectors. |
| 369 | static Vector<T> empty() { return Vector<T>(NULL, 0); } |
| 370 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 371 | template<typename S> |
| 372 | static Vector<T> cast(Vector<S> input) { |
| 373 | return Vector<T>(reinterpret_cast<T*>(input.start()), |
| 374 | input.length() * sizeof(S) / sizeof(T)); |
| 375 | } |
| 376 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 377 | protected: |
| 378 | void set_start(T* start) { start_ = start; } |
| 379 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 380 | private: |
| 381 | T* start_; |
| 382 | int length_; |
| 383 | }; |
| 384 | |
| 385 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 386 | template <typename T, int kSize> |
| 387 | class EmbeddedVector : public Vector<T> { |
| 388 | public: |
| 389 | EmbeddedVector() : Vector<T>(buffer_, kSize) { } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 390 | |
| 391 | // When copying, make underlying Vector to reference our buffer. |
| 392 | EmbeddedVector(const EmbeddedVector& rhs) |
| 393 | : Vector<T>(rhs) { |
| 394 | memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
| 395 | set_start(buffer_); |
| 396 | } |
| 397 | |
| 398 | EmbeddedVector& operator=(const EmbeddedVector& rhs) { |
| 399 | if (this == &rhs) return *this; |
| 400 | Vector<T>::operator=(rhs); |
| 401 | memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 402 | this->set_start(buffer_); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 403 | return *this; |
| 404 | } |
| 405 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 406 | private: |
| 407 | T buffer_[kSize]; |
| 408 | }; |
| 409 | |
| 410 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 411 | template <typename T> |
| 412 | class ScopedVector : public Vector<T> { |
| 413 | public: |
| 414 | explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { } |
| 415 | ~ScopedVector() { |
| 416 | DeleteArray(this->start()); |
| 417 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 418 | |
| 419 | private: |
| 420 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 421 | }; |
| 422 | |
| 423 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 424 | inline Vector<const char> CStrVector(const char* data) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 425 | return Vector<const char>(data, StrLength(data)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | inline Vector<char> MutableCStrVector(char* data) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 429 | return Vector<char>(data, StrLength(data)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 430 | } |
| 431 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 432 | inline Vector<char> MutableCStrVector(char* data, int max) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 433 | int length = StrLength(data); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 434 | return Vector<char>(data, (length < max) ? length : max); |
| 435 | } |
| 436 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 437 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 438 | /* |
| 439 | * A class that collects values into a backing store. |
| 440 | * Specialized versions of the class can allow access to the backing store |
| 441 | * in different ways. |
| 442 | * There is no guarantee that the backing store is contiguous (and, as a |
| 443 | * consequence, no guarantees that consecutively added elements are adjacent |
| 444 | * in memory). The collector may move elements unless it has guaranteed not |
| 445 | * to. |
| 446 | */ |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 447 | template <typename T, int growth_factor = 2, int max_growth = 1 * MB> |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 448 | class Collector { |
| 449 | public: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 450 | explicit Collector(int initial_capacity = kMinCapacity) |
| 451 | : index_(0), size_(0) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 452 | if (initial_capacity < kMinCapacity) { |
| 453 | initial_capacity = kMinCapacity; |
| 454 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 455 | current_chunk_ = Vector<T>::New(initial_capacity); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | virtual ~Collector() { |
| 459 | // Free backing store (in reverse allocation order). |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 460 | current_chunk_.Dispose(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 461 | for (int i = chunks_.length() - 1; i >= 0; i--) { |
| 462 | chunks_.at(i).Dispose(); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // Add a single element. |
| 467 | inline void Add(T value) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 468 | if (index_ >= current_chunk_.length()) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 469 | Grow(1); |
| 470 | } |
| 471 | current_chunk_[index_] = value; |
| 472 | index_++; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 473 | size_++; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // Add a block of contiguous elements and return a Vector backed by the |
| 477 | // memory area. |
| 478 | // A basic Collector will keep this vector valid as long as the Collector |
| 479 | // is alive. |
| 480 | inline Vector<T> AddBlock(int size, T initial_value) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 481 | ASSERT(size > 0); |
| 482 | if (size > current_chunk_.length() - index_) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 483 | Grow(size); |
| 484 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 485 | T* position = current_chunk_.start() + index_; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 486 | index_ += size; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 487 | size_ += size; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 488 | for (int i = 0; i < size; i++) { |
| 489 | position[i] = initial_value; |
| 490 | } |
| 491 | return Vector<T>(position, size); |
| 492 | } |
| 493 | |
| 494 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 495 | // Write the contents of the collector into the provided vector. |
| 496 | void WriteTo(Vector<T> destination) { |
| 497 | ASSERT(size_ <= destination.length()); |
| 498 | int position = 0; |
| 499 | for (int i = 0; i < chunks_.length(); i++) { |
| 500 | Vector<T> chunk = chunks_.at(i); |
| 501 | for (int j = 0; j < chunk.length(); j++) { |
| 502 | destination[position] = chunk[j]; |
| 503 | position++; |
| 504 | } |
| 505 | } |
| 506 | for (int i = 0; i < index_; i++) { |
| 507 | destination[position] = current_chunk_[i]; |
| 508 | position++; |
| 509 | } |
| 510 | } |
| 511 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 512 | // Allocate a single contiguous vector, copy all the collected |
| 513 | // elements to the vector, and return it. |
| 514 | // The caller is responsible for freeing the memory of the returned |
| 515 | // vector (e.g., using Vector::Dispose). |
| 516 | Vector<T> ToVector() { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 517 | Vector<T> new_store = Vector<T>::New(size_); |
| 518 | WriteTo(new_store); |
| 519 | return new_store; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // Resets the collector to be empty. |
| 523 | virtual void Reset() { |
| 524 | for (int i = chunks_.length() - 1; i >= 0; i--) { |
| 525 | chunks_.at(i).Dispose(); |
| 526 | } |
| 527 | chunks_.Rewind(0); |
| 528 | index_ = 0; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 529 | size_ = 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 530 | } |
| 531 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 532 | // Total number of elements added to collector so far. |
| 533 | inline int size() { return size_; } |
| 534 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 535 | protected: |
| 536 | static const int kMinCapacity = 16; |
| 537 | List<Vector<T> > chunks_; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 538 | Vector<T> current_chunk_; // Block of memory currently being written into. |
| 539 | int index_; // Current index in current chunk. |
| 540 | int size_; // Total number of elements in collector. |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 541 | |
| 542 | // Creates a new current chunk, and stores the old chunk in the chunks_ list. |
| 543 | void Grow(int min_capacity) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 544 | ASSERT(growth_factor > 1); |
| 545 | int growth = current_chunk_.length() * (growth_factor - 1); |
| 546 | if (growth > max_growth) { |
| 547 | growth = max_growth; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 548 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 549 | int new_capacity = current_chunk_.length() + growth; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 550 | if (new_capacity < min_capacity) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 551 | new_capacity = min_capacity + growth; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 552 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 553 | Vector<T> new_chunk = Vector<T>::New(new_capacity); |
| 554 | int new_index = PrepareGrow(new_chunk); |
| 555 | if (index_ > 0) { |
| 556 | chunks_.Add(current_chunk_.SubVector(0, index_)); |
| 557 | } else { |
| 558 | // Can happen if the call to PrepareGrow moves everything into |
| 559 | // the new chunk. |
| 560 | current_chunk_.Dispose(); |
| 561 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 562 | current_chunk_ = new_chunk; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 563 | index_ = new_index; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 564 | ASSERT(index_ + min_capacity <= current_chunk_.length()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | // Before replacing the current chunk, give a subclass the option to move |
| 568 | // some of the current data into the new chunk. The function may update |
| 569 | // the current index_ value to represent data no longer in the current chunk. |
| 570 | // Returns the initial index of the new chunk (after copied data). |
| 571 | virtual int PrepareGrow(Vector<T> new_chunk) { |
| 572 | return 0; |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | |
| 577 | /* |
| 578 | * A collector that allows sequences of values to be guaranteed to |
| 579 | * stay consecutive. |
| 580 | * If the backing store grows while a sequence is active, the current |
| 581 | * sequence might be moved, but after the sequence is ended, it will |
| 582 | * not move again. |
| 583 | * NOTICE: Blocks allocated using Collector::AddBlock(int) can move |
| 584 | * as well, if inside an active sequence where another element is added. |
| 585 | */ |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 586 | template <typename T, int growth_factor = 2, int max_growth = 1 * MB> |
| 587 | class SequenceCollector : public Collector<T, growth_factor, max_growth> { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 588 | public: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 589 | explicit SequenceCollector(int initial_capacity) |
| 590 | : Collector<T, growth_factor, max_growth>(initial_capacity), |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 591 | sequence_start_(kNoSequence) { } |
| 592 | |
| 593 | virtual ~SequenceCollector() {} |
| 594 | |
| 595 | void StartSequence() { |
| 596 | ASSERT(sequence_start_ == kNoSequence); |
| 597 | sequence_start_ = this->index_; |
| 598 | } |
| 599 | |
| 600 | Vector<T> EndSequence() { |
| 601 | ASSERT(sequence_start_ != kNoSequence); |
| 602 | int sequence_start = sequence_start_; |
| 603 | sequence_start_ = kNoSequence; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 604 | if (sequence_start == this->index_) return Vector<T>(); |
| 605 | return this->current_chunk_.SubVector(sequence_start, this->index_); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | // Drops the currently added sequence, and all collected elements in it. |
| 609 | void DropSequence() { |
| 610 | ASSERT(sequence_start_ != kNoSequence); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 611 | int sequence_length = this->index_ - sequence_start_; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 612 | this->index_ = sequence_start_; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 613 | this->size_ -= sequence_length; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 614 | sequence_start_ = kNoSequence; |
| 615 | } |
| 616 | |
| 617 | virtual void Reset() { |
| 618 | sequence_start_ = kNoSequence; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 619 | this->Collector<T, growth_factor, max_growth>::Reset(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | private: |
| 623 | static const int kNoSequence = -1; |
| 624 | int sequence_start_; |
| 625 | |
| 626 | // Move the currently active sequence to the new chunk. |
| 627 | virtual int PrepareGrow(Vector<T> new_chunk) { |
| 628 | if (sequence_start_ != kNoSequence) { |
| 629 | int sequence_length = this->index_ - sequence_start_; |
| 630 | // The new chunk is always larger than the current chunk, so there |
| 631 | // is room for the copy. |
| 632 | ASSERT(sequence_length < new_chunk.length()); |
| 633 | for (int i = 0; i < sequence_length; i++) { |
| 634 | new_chunk[i] = this->current_chunk_[sequence_start_ + i]; |
| 635 | } |
| 636 | this->index_ = sequence_start_; |
| 637 | sequence_start_ = 0; |
| 638 | return sequence_length; |
| 639 | } |
| 640 | return 0; |
| 641 | } |
| 642 | }; |
| 643 | |
| 644 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 645 | // Compare ASCII/16bit chars to ASCII/16bit chars. |
| 646 | template <typename lchar, typename rchar> |
| 647 | static inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) { |
| 648 | const lchar* limit = lhs + chars; |
| 649 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 650 | if (sizeof(*lhs) == sizeof(*rhs)) { |
| 651 | // Number of characters in a uintptr_t. |
| 652 | static const int kStepSize = sizeof(uintptr_t) / sizeof(*lhs); // NOLINT |
| 653 | while (lhs <= limit - kStepSize) { |
| 654 | if (*reinterpret_cast<const uintptr_t*>(lhs) != |
| 655 | *reinterpret_cast<const uintptr_t*>(rhs)) { |
| 656 | break; |
| 657 | } |
| 658 | lhs += kStepSize; |
| 659 | rhs += kStepSize; |
| 660 | } |
| 661 | } |
| 662 | #endif |
| 663 | while (lhs < limit) { |
| 664 | int r = static_cast<int>(*lhs) - static_cast<int>(*rhs); |
| 665 | if (r != 0) return r; |
| 666 | ++lhs; |
| 667 | ++rhs; |
| 668 | } |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 673 | // Calculate 10^exponent. |
whesse@chromium.org | f0ac72d | 2010-11-08 12:47:26 +0000 | [diff] [blame^] | 674 | static inline int TenToThe(int exponent) { |
| 675 | ASSERT(exponent <= 9); |
| 676 | ASSERT(exponent >= 1); |
| 677 | int answer = 10; |
| 678 | for (int i = 1; i < exponent; i++) answer *= 10; |
| 679 | return answer; |
| 680 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 681 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 682 | |
| 683 | // The type-based aliasing rule allows the compiler to assume that pointers of |
| 684 | // different types (for some definition of different) never alias each other. |
| 685 | // Thus the following code does not work: |
| 686 | // |
| 687 | // float f = foo(); |
| 688 | // int fbits = *(int*)(&f); |
| 689 | // |
| 690 | // The compiler 'knows' that the int pointer can't refer to f since the types |
| 691 | // don't match, so the compiler may cache f in a register, leaving random data |
| 692 | // in fbits. Using C++ style casts makes no difference, however a pointer to |
| 693 | // char data is assumed to alias any other pointer. This is the 'memcpy |
| 694 | // exception'. |
| 695 | // |
| 696 | // Bit_cast uses the memcpy exception to move the bits from a variable of one |
| 697 | // type of a variable of another type. Of course the end result is likely to |
| 698 | // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005) |
| 699 | // will completely optimize BitCast away. |
| 700 | // |
| 701 | // There is an additional use for BitCast. |
| 702 | // Recent gccs will warn when they see casts that may result in breakage due to |
| 703 | // the type-based aliasing rule. If you have checked that there is no breakage |
| 704 | // you can use BitCast to cast one pointer type to another. This confuses gcc |
| 705 | // enough that it can no longer see that you have cast one pointer type to |
| 706 | // another thus avoiding the warning. |
| 707 | template <class Dest, class Source> |
| 708 | inline Dest BitCast(const Source& source) { |
| 709 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 710 | // A compile error here means your Dest and Source have different sizes. |
| 711 | typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1]; |
| 712 | |
| 713 | Dest dest; |
| 714 | memcpy(&dest, &source, sizeof(dest)); |
| 715 | return dest; |
| 716 | } |
| 717 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 718 | template <class Dest, class Source> |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 719 | inline Dest BitCast(Source* source) { |
| 720 | return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 721 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 722 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 723 | } } // namespace v8::internal |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 724 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 725 | #endif // V8_UTILS_H_ |