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 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | // General helper functions |
| 39 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 40 | #define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0) |
| 41 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 42 | // Returns true iff x is a power of 2 (or zero). Cannot be used with the |
| 43 | // maximally negative value of the type T (the -1 overflows). |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | template <typename T> |
| 45 | static inline bool IsPowerOf2(T x) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 46 | return IS_POWER_OF_TWO(x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 50 | // X must be a power of 2. Returns the number of trailing zeros. |
| 51 | template <typename T> |
| 52 | static inline int WhichPowerOf2(T x) { |
| 53 | ASSERT(IsPowerOf2(x)); |
| 54 | ASSERT(x != 0); |
| 55 | if (x < 0) return 31; |
| 56 | int bits = 0; |
| 57 | #ifdef DEBUG |
| 58 | int original_x = x; |
| 59 | #endif |
| 60 | if (x >= 0x10000) { |
| 61 | bits += 16; |
| 62 | x >>= 16; |
| 63 | } |
| 64 | if (x >= 0x100) { |
| 65 | bits += 8; |
| 66 | x >>= 8; |
| 67 | } |
| 68 | if (x >= 0x10) { |
| 69 | bits += 4; |
| 70 | x >>= 4; |
| 71 | } |
| 72 | switch (x) { |
| 73 | default: UNREACHABLE(); |
| 74 | case 8: bits++; // Fall through. |
| 75 | case 4: bits++; // Fall through. |
| 76 | case 2: bits++; // Fall through. |
| 77 | case 1: break; |
| 78 | } |
| 79 | ASSERT_EQ(1 << bits, original_x); |
| 80 | return bits; |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 85 | // The C++ standard leaves the semantics of '>>' undefined for |
| 86 | // negative signed operands. Most implementations do the right thing, |
| 87 | // though. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 88 | static inline int ArithmeticShiftRight(int x, int s) { |
| 89 | return x >> s; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | // 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] | 94 | // This allows conversion of Addresses and integral types into |
| 95 | // 0-relative int offsets. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 96 | template <typename T> |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 97 | static inline intptr_t OffsetFrom(T x) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | return x - static_cast<T>(0); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | // 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] | 103 | // This allows conversion of 0-relative int offsets into Addresses and |
| 104 | // integral types. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 105 | template <typename T> |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 106 | static inline T AddressFrom(intptr_t x) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 107 | return static_cast<T>(static_cast<T>(0) + x); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | // Return the largest multiple of m which is <= x. |
| 112 | template <typename T> |
| 113 | static inline T RoundDown(T x, int m) { |
| 114 | ASSERT(IsPowerOf2(m)); |
| 115 | return AddressFrom<T>(OffsetFrom(x) & -m); |
| 116 | } |
| 117 | |
| 118 | |
| 119 | // Return the smallest multiple of m which is >= x. |
| 120 | template <typename T> |
| 121 | static inline T RoundUp(T x, int m) { |
| 122 | return RoundDown(x + m - 1, m); |
| 123 | } |
| 124 | |
| 125 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 126 | template <typename T> |
| 127 | static int Compare(const T& a, const T& b) { |
| 128 | if (a == b) |
| 129 | return 0; |
| 130 | else if (a < b) |
| 131 | return -1; |
| 132 | else |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | |
| 137 | template <typename T> |
| 138 | static int PointerValueCompare(const T* a, const T* b) { |
| 139 | return Compare<T>(*a, *b); |
| 140 | } |
| 141 | |
| 142 | |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 143 | // Returns the smallest power of two which is >= x. If you pass in a |
| 144 | // number that is already a power of two, it is returned as is. |
| 145 | uint32_t RoundUpToPowerOf2(uint32_t x); |
| 146 | |
| 147 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 148 | template <typename T> |
| 149 | static inline bool IsAligned(T value, T alignment) { |
| 150 | ASSERT(IsPowerOf2(alignment)); |
| 151 | return (value & (alignment - 1)) == 0; |
| 152 | } |
| 153 | |
| 154 | |
| 155 | // Returns true if (addr + offset) is aligned. |
ager@chromium.org | ab99eea | 2009-08-25 07:05:41 +0000 | [diff] [blame] | 156 | static inline bool IsAddressAligned(Address addr, |
| 157 | intptr_t alignment, |
| 158 | int offset) { |
| 159 | intptr_t offs = OffsetFrom(addr + offset); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 160 | return IsAligned(offs, alignment); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | // Returns the maximum of the two parameters. |
| 165 | template <typename T> |
| 166 | static T Max(T a, T b) { |
| 167 | return a < b ? b : a; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | // Returns the minimum of the two parameters. |
| 172 | template <typename T> |
| 173 | static T Min(T a, T b) { |
| 174 | return a < b ? a : b; |
| 175 | } |
| 176 | |
| 177 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 178 | inline int StrLength(const char* string) { |
| 179 | size_t length = strlen(string); |
| 180 | ASSERT(length == static_cast<size_t>(static_cast<int>(length))); |
| 181 | return static_cast<int>(length); |
| 182 | } |
| 183 | |
| 184 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 185 | // ---------------------------------------------------------------------------- |
mads.s.ager@gmail.com | 769cc96 | 2008-08-06 10:02:49 +0000 | [diff] [blame] | 186 | // BitField is a help template for encoding and decode bitfield with |
| 187 | // unsigned content. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 188 | template<class T, int shift, int size> |
| 189 | class BitField { |
| 190 | public: |
| 191 | // Tells whether the provided value fits into the bit field. |
| 192 | static bool is_valid(T value) { |
| 193 | return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0; |
| 194 | } |
| 195 | |
| 196 | // Returns a uint32_t mask of bit field. |
| 197 | static uint32_t mask() { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 198 | // To use all bits of a uint32 in a bitfield without compiler warnings we |
| 199 | // have to compute 2^32 without using a shift count of 32. |
| 200 | return ((1U << shift) << size) - (1U << shift); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Returns a uint32_t with the bit field value encoded. |
| 204 | static uint32_t encode(T value) { |
| 205 | ASSERT(is_valid(value)); |
| 206 | return static_cast<uint32_t>(value) << shift; |
| 207 | } |
| 208 | |
| 209 | // Extracts the bit field from the value. |
| 210 | static T decode(uint32_t value) { |
fschneider@chromium.org | b95b98b | 2010-02-23 10:34:29 +0000 | [diff] [blame] | 211 | return static_cast<T>((value & mask()) >> shift); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 212 | } |
| 213 | }; |
| 214 | |
| 215 | |
| 216 | // ---------------------------------------------------------------------------- |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 217 | // Hash function. |
| 218 | |
| 219 | uint32_t ComputeIntegerHash(uint32_t key); |
| 220 | |
| 221 | |
| 222 | // ---------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 223 | // I/O support. |
| 224 | |
| 225 | // Our version of printf(). Avoids compilation errors that we get |
| 226 | // with standard printf when attempting to print pointers, etc. |
| 227 | // (the errors are due to the extra compilation flags, which we |
| 228 | // want elsewhere). |
| 229 | void PrintF(const char* format, ...); |
| 230 | |
| 231 | // Our version of fflush. |
| 232 | void Flush(); |
| 233 | |
| 234 | |
| 235 | // Read a line of characters after printing the prompt to stdout. The resulting |
| 236 | // char* needs to be disposed off with DeleteArray by the caller. |
| 237 | char* ReadLine(const char* prompt); |
| 238 | |
| 239 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 240 | // Read and return the raw bytes in a file. the size of the buffer is returned |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 241 | // in size. |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 242 | // The returned buffer must be freed by the caller. |
| 243 | byte* ReadBytes(const char* filename, int* size, bool verbose = true); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | |
| 245 | |
| 246 | // Write size chars from str to the file given by filename. |
| 247 | // The file is overwritten. Returns the number of chars written. |
| 248 | int WriteChars(const char* filename, |
| 249 | const char* str, |
| 250 | int size, |
| 251 | bool verbose = true); |
| 252 | |
| 253 | |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 254 | // Write size bytes to the file given by filename. |
| 255 | // The file is overwritten. Returns the number of bytes written. |
| 256 | int WriteBytes(const char* filename, |
| 257 | const byte* bytes, |
| 258 | int size, |
| 259 | bool verbose = true); |
| 260 | |
| 261 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 262 | // Write the C code |
| 263 | // const char* <varname> = "<str>"; |
| 264 | // const int <varname>_len = <len>; |
| 265 | // to the file given by filename. Only the first len chars are written. |
| 266 | int WriteAsCFile(const char* filename, const char* varname, |
| 267 | const char* str, int size, bool verbose = true); |
| 268 | |
| 269 | |
| 270 | // ---------------------------------------------------------------------------- |
| 271 | // Miscellaneous |
| 272 | |
| 273 | // A static resource holds a static instance that can be reserved in |
| 274 | // a local scope using an instance of Access. Attempts to re-reserve |
| 275 | // the instance will cause an error. |
| 276 | template <typename T> |
| 277 | class StaticResource { |
| 278 | public: |
| 279 | StaticResource() : is_reserved_(false) {} |
| 280 | |
| 281 | private: |
| 282 | template <typename S> friend class Access; |
| 283 | T instance_; |
| 284 | bool is_reserved_; |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | // Locally scoped access to a static resource. |
| 289 | template <typename T> |
| 290 | class Access { |
| 291 | public: |
| 292 | explicit Access(StaticResource<T>* resource) |
| 293 | : resource_(resource) |
| 294 | , instance_(&resource->instance_) { |
| 295 | ASSERT(!resource->is_reserved_); |
| 296 | resource->is_reserved_ = true; |
| 297 | } |
| 298 | |
| 299 | ~Access() { |
| 300 | resource_->is_reserved_ = false; |
| 301 | resource_ = NULL; |
| 302 | instance_ = NULL; |
| 303 | } |
| 304 | |
| 305 | T* value() { return instance_; } |
| 306 | T* operator -> () { return instance_; } |
| 307 | |
| 308 | private: |
| 309 | StaticResource<T>* resource_; |
| 310 | T* instance_; |
| 311 | }; |
| 312 | |
| 313 | |
| 314 | template <typename T> |
| 315 | class Vector { |
| 316 | public: |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 317 | Vector() : start_(NULL), length_(0) {} |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 318 | Vector(T* data, int length) : start_(data), length_(length) { |
| 319 | ASSERT(length == 0 || (length > 0 && data != NULL)); |
| 320 | } |
| 321 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 322 | static Vector<T> New(int length) { |
| 323 | return Vector<T>(NewArray<T>(length), length); |
| 324 | } |
| 325 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 326 | // Returns a vector using the same backing storage as this one, |
| 327 | // spanning from and including 'from', to but not including 'to'. |
| 328 | Vector<T> SubVector(int from, int to) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 329 | ASSERT(to <= length_); |
| 330 | ASSERT(from < to); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 331 | ASSERT(0 <= from); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 332 | return Vector<T>(start() + from, to - from); |
| 333 | } |
| 334 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 335 | // Returns the length of the vector. |
| 336 | int length() const { return length_; } |
| 337 | |
| 338 | // Returns whether or not the vector is empty. |
| 339 | bool is_empty() const { return length_ == 0; } |
| 340 | |
| 341 | // Returns the pointer to the start of the data in the vector. |
| 342 | T* start() const { return start_; } |
| 343 | |
| 344 | // Access individual vector elements - checks bounds in debug mode. |
| 345 | T& operator[](int index) const { |
| 346 | ASSERT(0 <= index && index < length_); |
| 347 | return start_[index]; |
| 348 | } |
| 349 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 350 | T& first() { return start_[0]; } |
| 351 | |
| 352 | T& last() { return start_[length_ - 1]; } |
| 353 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 354 | // Returns a clone of this vector with a new backing store. |
| 355 | Vector<T> Clone() const { |
| 356 | T* result = NewArray<T>(length_); |
| 357 | for (int i = 0; i < length_; i++) result[i] = start_[i]; |
| 358 | return Vector<T>(result, length_); |
| 359 | } |
| 360 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 361 | void Sort(int (*cmp)(const T*, const T*)) { |
| 362 | typedef int (*RawComparer)(const void*, const void*); |
| 363 | qsort(start(), |
| 364 | length(), |
| 365 | sizeof(T), |
| 366 | reinterpret_cast<RawComparer>(cmp)); |
| 367 | } |
| 368 | |
| 369 | void Sort() { |
| 370 | Sort(PointerValueCompare<T>); |
| 371 | } |
| 372 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 373 | void Truncate(int length) { |
| 374 | ASSERT(length <= length_); |
| 375 | length_ = length; |
| 376 | } |
| 377 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 378 | // Releases the array underlying this vector. Once disposed the |
| 379 | // vector is empty. |
| 380 | void Dispose() { |
| 381 | DeleteArray(start_); |
| 382 | start_ = NULL; |
| 383 | length_ = 0; |
| 384 | } |
| 385 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 386 | inline Vector<T> operator+(int offset) { |
| 387 | ASSERT(offset < length_); |
| 388 | return Vector<T>(start_ + offset, length_ - offset); |
| 389 | } |
| 390 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 391 | // Factory method for creating empty vectors. |
| 392 | static Vector<T> empty() { return Vector<T>(NULL, 0); } |
| 393 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame^] | 394 | template<typename S> |
| 395 | static Vector<T> cast(Vector<S> input) { |
| 396 | return Vector<T>(reinterpret_cast<T*>(input.start()), |
| 397 | input.length() * sizeof(S) / sizeof(T)); |
| 398 | } |
| 399 | |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 400 | protected: |
| 401 | void set_start(T* start) { start_ = start; } |
| 402 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 403 | private: |
| 404 | T* start_; |
| 405 | int length_; |
| 406 | }; |
| 407 | |
| 408 | |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 409 | // A temporary assignment sets a (non-local) variable to a value on |
| 410 | // construction and resets it the value on destruction. |
| 411 | template <typename T> |
| 412 | class TempAssign { |
| 413 | public: |
| 414 | TempAssign(T* var, T value): var_(var), old_value_(*var) { |
| 415 | *var = value; |
| 416 | } |
| 417 | |
| 418 | ~TempAssign() { *var_ = old_value_; } |
| 419 | |
| 420 | private: |
| 421 | T* var_; |
| 422 | T old_value_; |
| 423 | }; |
| 424 | |
| 425 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 426 | template <typename T, int kSize> |
| 427 | class EmbeddedVector : public Vector<T> { |
| 428 | public: |
| 429 | EmbeddedVector() : Vector<T>(buffer_, kSize) { } |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 430 | |
| 431 | // When copying, make underlying Vector to reference our buffer. |
| 432 | EmbeddedVector(const EmbeddedVector& rhs) |
| 433 | : Vector<T>(rhs) { |
| 434 | memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
| 435 | set_start(buffer_); |
| 436 | } |
| 437 | |
| 438 | EmbeddedVector& operator=(const EmbeddedVector& rhs) { |
| 439 | if (this == &rhs) return *this; |
| 440 | Vector<T>::operator=(rhs); |
| 441 | memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 442 | this->set_start(buffer_); |
sgjesse@chromium.org | 755c5b1 | 2009-05-29 11:04:38 +0000 | [diff] [blame] | 443 | return *this; |
| 444 | } |
| 445 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 446 | private: |
| 447 | T buffer_[kSize]; |
| 448 | }; |
| 449 | |
| 450 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 451 | template <typename T> |
| 452 | class ScopedVector : public Vector<T> { |
| 453 | public: |
| 454 | explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { } |
| 455 | ~ScopedVector() { |
| 456 | DeleteArray(this->start()); |
| 457 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 458 | |
| 459 | private: |
| 460 | DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 464 | inline Vector<const char> CStrVector(const char* data) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 465 | return Vector<const char>(data, StrLength(data)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | inline Vector<char> MutableCStrVector(char* data) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 469 | return Vector<char>(data, StrLength(data)); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 470 | } |
| 471 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 472 | inline Vector<char> MutableCStrVector(char* data, int max) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 473 | int length = StrLength(data); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 474 | return Vector<char>(data, (length < max) ? length : max); |
| 475 | } |
| 476 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 477 | template <typename T> |
| 478 | inline Vector< Handle<Object> > HandleVector(v8::internal::Handle<T>* elms, |
| 479 | int length) { |
| 480 | return Vector< Handle<Object> >( |
| 481 | reinterpret_cast<v8::internal::Handle<Object>*>(elms), length); |
| 482 | } |
| 483 | |
| 484 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 485 | /* |
| 486 | * A class that collects values into a backing store. |
| 487 | * Specialized versions of the class can allow access to the backing store |
| 488 | * in different ways. |
| 489 | * There is no guarantee that the backing store is contiguous (and, as a |
| 490 | * consequence, no guarantees that consecutively added elements are adjacent |
| 491 | * in memory). The collector may move elements unless it has guaranteed not |
| 492 | * to. |
| 493 | */ |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 494 | 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] | 495 | class Collector { |
| 496 | public: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 497 | explicit Collector(int initial_capacity = kMinCapacity) |
| 498 | : index_(0), size_(0) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 499 | if (initial_capacity < kMinCapacity) { |
| 500 | initial_capacity = kMinCapacity; |
| 501 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 502 | current_chunk_ = Vector<T>::New(initial_capacity); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | virtual ~Collector() { |
| 506 | // Free backing store (in reverse allocation order). |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 507 | current_chunk_.Dispose(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 508 | for (int i = chunks_.length() - 1; i >= 0; i--) { |
| 509 | chunks_.at(i).Dispose(); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | // Add a single element. |
| 514 | inline void Add(T value) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 515 | if (index_ >= current_chunk_.length()) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 516 | Grow(1); |
| 517 | } |
| 518 | current_chunk_[index_] = value; |
| 519 | index_++; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 520 | size_++; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Add a block of contiguous elements and return a Vector backed by the |
| 524 | // memory area. |
| 525 | // A basic Collector will keep this vector valid as long as the Collector |
| 526 | // is alive. |
| 527 | inline Vector<T> AddBlock(int size, T initial_value) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 528 | ASSERT(size > 0); |
| 529 | if (size > current_chunk_.length() - index_) { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 530 | Grow(size); |
| 531 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 532 | T* position = current_chunk_.start() + index_; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 533 | index_ += size; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 534 | size_ += size; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 535 | for (int i = 0; i < size; i++) { |
| 536 | position[i] = initial_value; |
| 537 | } |
| 538 | return Vector<T>(position, size); |
| 539 | } |
| 540 | |
| 541 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 542 | // Write the contents of the collector into the provided vector. |
| 543 | void WriteTo(Vector<T> destination) { |
| 544 | ASSERT(size_ <= destination.length()); |
| 545 | int position = 0; |
| 546 | for (int i = 0; i < chunks_.length(); i++) { |
| 547 | Vector<T> chunk = chunks_.at(i); |
| 548 | for (int j = 0; j < chunk.length(); j++) { |
| 549 | destination[position] = chunk[j]; |
| 550 | position++; |
| 551 | } |
| 552 | } |
| 553 | for (int i = 0; i < index_; i++) { |
| 554 | destination[position] = current_chunk_[i]; |
| 555 | position++; |
| 556 | } |
| 557 | } |
| 558 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 559 | // Allocate a single contiguous vector, copy all the collected |
| 560 | // elements to the vector, and return it. |
| 561 | // The caller is responsible for freeing the memory of the returned |
| 562 | // vector (e.g., using Vector::Dispose). |
| 563 | Vector<T> ToVector() { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 564 | Vector<T> new_store = Vector<T>::New(size_); |
| 565 | WriteTo(new_store); |
| 566 | return new_store; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // Resets the collector to be empty. |
| 570 | virtual void Reset() { |
| 571 | for (int i = chunks_.length() - 1; i >= 0; i--) { |
| 572 | chunks_.at(i).Dispose(); |
| 573 | } |
| 574 | chunks_.Rewind(0); |
| 575 | index_ = 0; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 576 | size_ = 0; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 577 | } |
| 578 | |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 579 | // Total number of elements added to collector so far. |
| 580 | inline int size() { return size_; } |
| 581 | |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 582 | protected: |
| 583 | static const int kMinCapacity = 16; |
| 584 | List<Vector<T> > chunks_; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 585 | Vector<T> current_chunk_; // Block of memory currently being written into. |
| 586 | int index_; // Current index in current chunk. |
| 587 | int size_; // Total number of elements in collector. |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 588 | |
| 589 | // Creates a new current chunk, and stores the old chunk in the chunks_ list. |
| 590 | void Grow(int min_capacity) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 591 | ASSERT(growth_factor > 1); |
| 592 | int growth = current_chunk_.length() * (growth_factor - 1); |
| 593 | if (growth > max_growth) { |
| 594 | growth = max_growth; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 595 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 596 | int new_capacity = current_chunk_.length() + growth; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 597 | if (new_capacity < min_capacity) { |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 598 | new_capacity = min_capacity + growth; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 599 | } |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 600 | Vector<T> new_chunk = Vector<T>::New(new_capacity); |
| 601 | int new_index = PrepareGrow(new_chunk); |
| 602 | if (index_ > 0) { |
| 603 | chunks_.Add(current_chunk_.SubVector(0, index_)); |
| 604 | } else { |
| 605 | // Can happen if the call to PrepareGrow moves everything into |
| 606 | // the new chunk. |
| 607 | current_chunk_.Dispose(); |
| 608 | } |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 609 | current_chunk_ = new_chunk; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 610 | index_ = new_index; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 611 | ASSERT(index_ + min_capacity <= current_chunk_.length()); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | // Before replacing the current chunk, give a subclass the option to move |
| 615 | // some of the current data into the new chunk. The function may update |
| 616 | // the current index_ value to represent data no longer in the current chunk. |
| 617 | // Returns the initial index of the new chunk (after copied data). |
| 618 | virtual int PrepareGrow(Vector<T> new_chunk) { |
| 619 | return 0; |
| 620 | } |
| 621 | }; |
| 622 | |
| 623 | |
| 624 | /* |
| 625 | * A collector that allows sequences of values to be guaranteed to |
| 626 | * stay consecutive. |
| 627 | * If the backing store grows while a sequence is active, the current |
| 628 | * sequence might be moved, but after the sequence is ended, it will |
| 629 | * not move again. |
| 630 | * NOTICE: Blocks allocated using Collector::AddBlock(int) can move |
| 631 | * as well, if inside an active sequence where another element is added. |
| 632 | */ |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 633 | template <typename T, int growth_factor = 2, int max_growth = 1 * MB> |
| 634 | class SequenceCollector : public Collector<T, growth_factor, max_growth> { |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 635 | public: |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 636 | explicit SequenceCollector(int initial_capacity) |
| 637 | : Collector<T, growth_factor, max_growth>(initial_capacity), |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 638 | sequence_start_(kNoSequence) { } |
| 639 | |
| 640 | virtual ~SequenceCollector() {} |
| 641 | |
| 642 | void StartSequence() { |
| 643 | ASSERT(sequence_start_ == kNoSequence); |
| 644 | sequence_start_ = this->index_; |
| 645 | } |
| 646 | |
| 647 | Vector<T> EndSequence() { |
| 648 | ASSERT(sequence_start_ != kNoSequence); |
| 649 | int sequence_start = sequence_start_; |
| 650 | sequence_start_ = kNoSequence; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 651 | if (sequence_start == this->index_) return Vector<T>(); |
| 652 | return this->current_chunk_.SubVector(sequence_start, this->index_); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | // Drops the currently added sequence, and all collected elements in it. |
| 656 | void DropSequence() { |
| 657 | ASSERT(sequence_start_ != kNoSequence); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 658 | int sequence_length = this->index_ - sequence_start_; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 659 | this->index_ = sequence_start_; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 660 | this->size_ -= sequence_length; |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 661 | sequence_start_ = kNoSequence; |
| 662 | } |
| 663 | |
| 664 | virtual void Reset() { |
| 665 | sequence_start_ = kNoSequence; |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 666 | this->Collector<T, growth_factor, max_growth>::Reset(); |
ricow@chromium.org | 65fae84 | 2010-08-25 15:26:24 +0000 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | private: |
| 670 | static const int kNoSequence = -1; |
| 671 | int sequence_start_; |
| 672 | |
| 673 | // Move the currently active sequence to the new chunk. |
| 674 | virtual int PrepareGrow(Vector<T> new_chunk) { |
| 675 | if (sequence_start_ != kNoSequence) { |
| 676 | int sequence_length = this->index_ - sequence_start_; |
| 677 | // The new chunk is always larger than the current chunk, so there |
| 678 | // is room for the copy. |
| 679 | ASSERT(sequence_length < new_chunk.length()); |
| 680 | for (int i = 0; i < sequence_length; i++) { |
| 681 | new_chunk[i] = this->current_chunk_[sequence_start_ + i]; |
| 682 | } |
| 683 | this->index_ = sequence_start_; |
| 684 | sequence_start_ = 0; |
| 685 | return sequence_length; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
| 689 | }; |
| 690 | |
| 691 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 692 | // Simple support to read a file into a 0-terminated C-string. |
| 693 | // The returned buffer must be freed by the caller. |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 694 | // On return, *exits tells whether the file existed. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 695 | Vector<const char> ReadFile(const char* filename, |
| 696 | bool* exists, |
| 697 | bool verbose = true); |
| 698 | |
| 699 | |
| 700 | // Simple wrapper that allows an ExternalString to refer to a |
| 701 | // Vector<const char>. Doesn't assume ownership of the data. |
| 702 | class AsciiStringAdapter: public v8::String::ExternalAsciiStringResource { |
| 703 | public: |
| 704 | explicit AsciiStringAdapter(Vector<const char> data) : data_(data) {} |
| 705 | |
| 706 | virtual const char* data() const { return data_.start(); } |
| 707 | |
| 708 | virtual size_t length() const { return data_.length(); } |
| 709 | |
| 710 | private: |
| 711 | Vector<const char> data_; |
| 712 | }; |
| 713 | |
| 714 | |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 715 | // Helper class for building result strings in a character buffer. The |
| 716 | // purpose of the class is to use safe operations that checks the |
| 717 | // buffer bounds on all operations in debug mode. |
| 718 | class StringBuilder { |
| 719 | public: |
| 720 | // Create a string builder with a buffer of the given size. The |
| 721 | // buffer is allocated through NewArray<char> and must be |
| 722 | // deallocated by the caller of Finalize(). |
| 723 | explicit StringBuilder(int size); |
| 724 | |
| 725 | StringBuilder(char* buffer, int size) |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 726 | : buffer_(buffer, size), position_(0) { } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 727 | |
| 728 | ~StringBuilder() { if (!is_finalized()) Finalize(); } |
| 729 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 730 | int size() const { return buffer_.length(); } |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 731 | |
| 732 | // Get the current position in the builder. |
| 733 | int position() const { |
| 734 | ASSERT(!is_finalized()); |
| 735 | return position_; |
| 736 | } |
| 737 | |
| 738 | // Reset the position. |
| 739 | void Reset() { position_ = 0; } |
| 740 | |
| 741 | // Add a single character to the builder. It is not allowed to add |
| 742 | // 0-characters; use the Finalize() method to terminate the string |
| 743 | // instead. |
| 744 | void AddCharacter(char c) { |
| 745 | ASSERT(c != '\0'); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 746 | ASSERT(!is_finalized() && position_ < buffer_.length()); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 747 | buffer_[position_++] = c; |
| 748 | } |
| 749 | |
| 750 | // Add an entire string to the builder. Uses strlen() internally to |
| 751 | // compute the length of the input string. |
| 752 | void AddString(const char* s); |
| 753 | |
| 754 | // Add the first 'n' characters of the given string 's' to the |
| 755 | // builder. The input string must have enough characters. |
| 756 | void AddSubstring(const char* s, int n); |
| 757 | |
| 758 | // Add formatted contents to the builder just like printf(). |
| 759 | void AddFormatted(const char* format, ...); |
| 760 | |
| 761 | // Add character padding to the builder. If count is non-positive, |
| 762 | // nothing is added to the builder. |
| 763 | void AddPadding(char c, int count); |
| 764 | |
| 765 | // Finalize the string by 0-terminating it and returning the buffer. |
| 766 | char* Finalize(); |
| 767 | |
| 768 | private: |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 769 | Vector<char> buffer_; |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 770 | int position_; |
| 771 | |
| 772 | bool is_finalized() const { return position_ < 0; } |
mads.s.ager | 31e7138 | 2008-08-13 09:32:07 +0000 | [diff] [blame] | 773 | |
| 774 | DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
kasper.lund | 7276f14 | 2008-07-30 08:49:36 +0000 | [diff] [blame] | 775 | }; |
| 776 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 777 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 778 | // Custom memcpy implementation for platforms where the standard version |
| 779 | // may not be good enough. |
| 780 | // TODO(lrn): Check whether some IA32 platforms should be excluded. |
| 781 | #if defined(V8_TARGET_ARCH_IA32) |
| 782 | |
| 783 | // TODO(lrn): Extend to other platforms as needed. |
| 784 | |
| 785 | typedef void (*MemCopyFunction)(void* dest, const void* src, size_t size); |
| 786 | |
| 787 | // Implemented in codegen-<arch>.cc. |
| 788 | MemCopyFunction CreateMemCopyFunction(); |
| 789 | |
| 790 | // Copy memory area to disjoint memory area. |
| 791 | static inline void MemCopy(void* dest, const void* src, size_t size) { |
| 792 | static MemCopyFunction memcopy = CreateMemCopyFunction(); |
| 793 | (*memcopy)(dest, src, size); |
| 794 | #ifdef DEBUG |
| 795 | CHECK_EQ(0, memcmp(dest, src, size)); |
| 796 | #endif |
| 797 | } |
| 798 | |
| 799 | |
| 800 | // Limit below which the extra overhead of the MemCopy function is likely |
| 801 | // to outweigh the benefits of faster copying. |
| 802 | // TODO(lrn): Try to find a more precise value. |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 803 | static const int kMinComplexMemCopy = 64; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 804 | |
| 805 | #else // V8_TARGET_ARCH_IA32 |
| 806 | |
| 807 | static inline void MemCopy(void* dest, const void* src, size_t size) { |
| 808 | memcpy(dest, src, size); |
| 809 | } |
| 810 | |
| 811 | static const int kMinComplexMemCopy = 256; |
| 812 | |
| 813 | #endif // V8_TARGET_ARCH_IA32 |
| 814 | |
| 815 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 816 | // Copy from ASCII/16bit chars to ASCII/16bit chars. |
| 817 | template <typename sourcechar, typename sinkchar> |
| 818 | static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { |
| 819 | sinkchar* limit = dest + chars; |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 820 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 821 | if (sizeof(*dest) == sizeof(*src)) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 822 | if (chars >= static_cast<int>(kMinComplexMemCopy / sizeof(*dest))) { |
| 823 | MemCopy(dest, src, chars * sizeof(*dest)); |
| 824 | return; |
| 825 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 826 | // Number of characters in a uintptr_t. |
| 827 | static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 828 | while (dest <= limit - kStepSize) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 829 | *reinterpret_cast<uintptr_t*>(dest) = |
| 830 | *reinterpret_cast<const uintptr_t*>(src); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 831 | dest += kStepSize; |
| 832 | src += kStepSize; |
| 833 | } |
| 834 | } |
| 835 | #endif |
| 836 | while (dest < limit) { |
| 837 | *dest++ = static_cast<sinkchar>(*src++); |
| 838 | } |
| 839 | } |
| 840 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 841 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 842 | // Compare ASCII/16bit chars to ASCII/16bit chars. |
| 843 | template <typename lchar, typename rchar> |
| 844 | static inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) { |
| 845 | const lchar* limit = lhs + chars; |
| 846 | #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 847 | if (sizeof(*lhs) == sizeof(*rhs)) { |
| 848 | // Number of characters in a uintptr_t. |
| 849 | static const int kStepSize = sizeof(uintptr_t) / sizeof(*lhs); // NOLINT |
| 850 | while (lhs <= limit - kStepSize) { |
| 851 | if (*reinterpret_cast<const uintptr_t*>(lhs) != |
| 852 | *reinterpret_cast<const uintptr_t*>(rhs)) { |
| 853 | break; |
| 854 | } |
| 855 | lhs += kStepSize; |
| 856 | rhs += kStepSize; |
| 857 | } |
| 858 | } |
| 859 | #endif |
| 860 | while (lhs < limit) { |
| 861 | int r = static_cast<int>(*lhs) - static_cast<int>(*rhs); |
| 862 | if (r != 0) return r; |
| 863 | ++lhs; |
| 864 | ++rhs; |
| 865 | } |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | |
| 870 | template <typename T> |
| 871 | static inline void MemsetPointer(T** dest, T* value, int counter) { |
| 872 | #if defined(V8_HOST_ARCH_IA32) |
| 873 | #define STOS "stosl" |
| 874 | #elif defined(V8_HOST_ARCH_X64) |
| 875 | #define STOS "stosq" |
| 876 | #endif |
| 877 | |
| 878 | #if defined(__GNUC__) && defined(STOS) |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 879 | asm volatile( |
| 880 | "cld;" |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 881 | "rep ; " STOS |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 882 | : "+&c" (counter), "+&D" (dest) |
| 883 | : "a" (value) |
| 884 | : "memory", "cc"); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 885 | #else |
| 886 | for (int i = 0; i < counter; i++) { |
| 887 | dest[i] = value; |
| 888 | } |
| 889 | #endif |
| 890 | |
| 891 | #undef STOS |
| 892 | } |
| 893 | |
| 894 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 895 | // Copies data from |src| to |dst|. The data spans MUST not overlap. |
| 896 | inline void CopyWords(Object** dst, Object** src, int num_words) { |
| 897 | ASSERT(Min(dst, src) + num_words <= Max(dst, src)); |
| 898 | ASSERT(num_words > 0); |
| 899 | |
| 900 | // Use block copying memcpy if the segment we're copying is |
| 901 | // enough to justify the extra call/setup overhead. |
| 902 | static const int kBlockCopyLimit = 16; |
| 903 | |
| 904 | if (num_words >= kBlockCopyLimit) { |
| 905 | memcpy(dst, src, num_words * kPointerSize); |
| 906 | } else { |
| 907 | int remaining = num_words; |
| 908 | do { |
| 909 | remaining--; |
| 910 | *dst++ = *src++; |
| 911 | } while (remaining > 0); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 916 | // Calculate 10^exponent. |
| 917 | int TenToThe(int exponent); |
| 918 | |
vegorov@chromium.org | f837290 | 2010-03-15 10:26:20 +0000 | [diff] [blame] | 919 | |
| 920 | // The type-based aliasing rule allows the compiler to assume that pointers of |
| 921 | // different types (for some definition of different) never alias each other. |
| 922 | // Thus the following code does not work: |
| 923 | // |
| 924 | // float f = foo(); |
| 925 | // int fbits = *(int*)(&f); |
| 926 | // |
| 927 | // The compiler 'knows' that the int pointer can't refer to f since the types |
| 928 | // don't match, so the compiler may cache f in a register, leaving random data |
| 929 | // in fbits. Using C++ style casts makes no difference, however a pointer to |
| 930 | // char data is assumed to alias any other pointer. This is the 'memcpy |
| 931 | // exception'. |
| 932 | // |
| 933 | // Bit_cast uses the memcpy exception to move the bits from a variable of one |
| 934 | // type of a variable of another type. Of course the end result is likely to |
| 935 | // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005) |
| 936 | // will completely optimize BitCast away. |
| 937 | // |
| 938 | // There is an additional use for BitCast. |
| 939 | // Recent gccs will warn when they see casts that may result in breakage due to |
| 940 | // the type-based aliasing rule. If you have checked that there is no breakage |
| 941 | // you can use BitCast to cast one pointer type to another. This confuses gcc |
| 942 | // enough that it can no longer see that you have cast one pointer type to |
| 943 | // another thus avoiding the warning. |
| 944 | template <class Dest, class Source> |
| 945 | inline Dest BitCast(const Source& source) { |
| 946 | // Compile time assertion: sizeof(Dest) == sizeof(Source) |
| 947 | // A compile error here means your Dest and Source have different sizes. |
| 948 | typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1]; |
| 949 | |
| 950 | Dest dest; |
| 951 | memcpy(&dest, &source, sizeof(dest)); |
| 952 | return dest; |
| 953 | } |
| 954 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 955 | template <class Dest, class Source> |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 956 | inline Dest BitCast(Source* source) { |
| 957 | return BitCast<Dest>(reinterpret_cast<uintptr_t>(source)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 958 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 959 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 960 | } } // namespace v8::internal |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 961 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 962 | #endif // V8_UTILS_H_ |