blob: 62b8726b802ae30f8d0c64f9343d7849d3f98e9e [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2008 the V8 project authors. All rights reserved.
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
31#include <stdlib.h>
Steve Block6ded16b2010-05-10 14:33:55 +010032#include <string.h>
Steve Blocka7e24c12009-10-30 11:49:00 +000033
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080034#include "globals.h"
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080035#include "checks.h"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -080036#include "allocation.h"
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -080037
Steve Blocka7e24c12009-10-30 11:49:00 +000038namespace v8 {
39namespace internal {
40
41// ----------------------------------------------------------------------------
42// General helper functions
43
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010044#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
45
Steve Block3ce2e202009-11-05 08:53:23 +000046// Returns true iff x is a power of 2 (or zero). Cannot be used with the
47// maximally negative value of the type T (the -1 overflows).
Steve Blocka7e24c12009-10-30 11:49:00 +000048template <typename T>
49static inline bool IsPowerOf2(T x) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010050 return IS_POWER_OF_TWO(x);
Steve Blocka7e24c12009-10-30 11:49:00 +000051}
52
53
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010054// X must be a power of 2. Returns the number of trailing zeros.
55template <typename T>
56static inline int WhichPowerOf2(T x) {
57 ASSERT(IsPowerOf2(x));
58 ASSERT(x != 0);
59 if (x < 0) return 31;
60 int bits = 0;
61#ifdef DEBUG
62 int original_x = x;
63#endif
64 if (x >= 0x10000) {
65 bits += 16;
66 x >>= 16;
67 }
68 if (x >= 0x100) {
69 bits += 8;
70 x >>= 8;
71 }
72 if (x >= 0x10) {
73 bits += 4;
74 x >>= 4;
75 }
76 switch (x) {
77 default: UNREACHABLE();
78 case 8: bits++; // Fall through.
79 case 4: bits++; // Fall through.
80 case 2: bits++; // Fall through.
81 case 1: break;
82 }
83 ASSERT_EQ(1 << bits, original_x);
84 return bits;
85 return 0;
86}
87
88
Steve Blocka7e24c12009-10-30 11:49:00 +000089// The C++ standard leaves the semantics of '>>' undefined for
90// negative signed operands. Most implementations do the right thing,
91// though.
92static inline int ArithmeticShiftRight(int x, int s) {
93 return x >> s;
94}
95
96
97// Compute the 0-relative offset of some absolute value x of type T.
98// This allows conversion of Addresses and integral types into
99// 0-relative int offsets.
100template <typename T>
101static inline intptr_t OffsetFrom(T x) {
102 return x - static_cast<T>(0);
103}
104
105
106// Compute the absolute value of type T for some 0-relative offset x.
107// This allows conversion of 0-relative int offsets into Addresses and
108// integral types.
109template <typename T>
110static inline T AddressFrom(intptr_t x) {
Steve Blockd0582a62009-12-15 09:54:21 +0000111 return static_cast<T>(static_cast<T>(0) + x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000112}
113
114
115// Return the largest multiple of m which is <= x.
116template <typename T>
117static inline T RoundDown(T x, int m) {
118 ASSERT(IsPowerOf2(m));
119 return AddressFrom<T>(OffsetFrom(x) & -m);
120}
121
122
123// Return the smallest multiple of m which is >= x.
124template <typename T>
125static inline T RoundUp(T x, int m) {
126 return RoundDown(x + m - 1, m);
127}
128
129
130template <typename T>
131static int Compare(const T& a, const T& b) {
132 if (a == b)
133 return 0;
134 else if (a < b)
135 return -1;
136 else
137 return 1;
138}
139
140
141template <typename T>
142static int PointerValueCompare(const T* a, const T* b) {
143 return Compare<T>(*a, *b);
144}
145
146
147// Returns the smallest power of two which is >= x. If you pass in a
148// number that is already a power of two, it is returned as is.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800149// Implementation is from "Hacker's Delight" by Henry S. Warren, Jr.,
150// figure 3-3, page 48, where the function is called clp2.
151static inline uint32_t RoundUpToPowerOf2(uint32_t x) {
152 ASSERT(x <= 0x80000000u);
153 x = x - 1;
154 x = x | (x >> 1);
155 x = x | (x >> 2);
156 x = x | (x >> 4);
157 x = x | (x >> 8);
158 x = x | (x >> 16);
159 return x + 1;
160}
161
Steve Blocka7e24c12009-10-30 11:49:00 +0000162
163
164template <typename T>
165static inline bool IsAligned(T value, T alignment) {
166 ASSERT(IsPowerOf2(alignment));
167 return (value & (alignment - 1)) == 0;
168}
169
170
171// Returns true if (addr + offset) is aligned.
172static inline bool IsAddressAligned(Address addr,
173 intptr_t alignment,
174 int offset) {
175 intptr_t offs = OffsetFrom(addr + offset);
176 return IsAligned(offs, alignment);
177}
178
179
180// Returns the maximum of the two parameters.
181template <typename T>
182static T Max(T a, T b) {
183 return a < b ? b : a;
184}
185
186
187// Returns the minimum of the two parameters.
188template <typename T>
189static T Min(T a, T b) {
190 return a < b ? a : b;
191}
192
193
Steve Blockd0582a62009-12-15 09:54:21 +0000194inline int StrLength(const char* string) {
195 size_t length = strlen(string);
196 ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
197 return static_cast<int>(length);
198}
199
200
Steve Blocka7e24c12009-10-30 11:49:00 +0000201// ----------------------------------------------------------------------------
202// BitField is a help template for encoding and decode bitfield with
203// unsigned content.
204template<class T, int shift, int size>
205class BitField {
206 public:
207 // Tells whether the provided value fits into the bit field.
208 static bool is_valid(T value) {
209 return (static_cast<uint32_t>(value) & ~((1U << (size)) - 1)) == 0;
210 }
211
212 // Returns a uint32_t mask of bit field.
213 static uint32_t mask() {
Andrei Popescu402d9372010-02-26 13:31:12 +0000214 // To use all bits of a uint32 in a bitfield without compiler warnings we
215 // have to compute 2^32 without using a shift count of 32.
216 return ((1U << shift) << size) - (1U << shift);
Steve Blocka7e24c12009-10-30 11:49:00 +0000217 }
218
219 // Returns a uint32_t with the bit field value encoded.
220 static uint32_t encode(T value) {
221 ASSERT(is_valid(value));
222 return static_cast<uint32_t>(value) << shift;
223 }
224
225 // Extracts the bit field from the value.
226 static T decode(uint32_t value) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000227 return static_cast<T>((value & mask()) >> shift);
Steve Blocka7e24c12009-10-30 11:49:00 +0000228 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100229
230 // Value for the field with all bits set.
231 static T max() {
232 return decode(mask());
233 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000234};
235
236
237// ----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000238// Hash function.
239
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800240// Thomas Wang, Integer Hash Functions.
241// http://www.concentric.net/~Ttwang/tech/inthash.htm
242static inline uint32_t ComputeIntegerHash(uint32_t key) {
243 uint32_t hash = key;
244 hash = ~hash + (hash << 15); // hash = (hash << 15) - hash - 1;
245 hash = hash ^ (hash >> 12);
246 hash = hash + (hash << 2);
247 hash = hash ^ (hash >> 4);
248 hash = hash * 2057; // hash = (hash + (hash << 3)) + (hash << 11);
249 hash = hash ^ (hash >> 16);
250 return hash;
251}
Steve Blocka7e24c12009-10-30 11:49:00 +0000252
253
254// ----------------------------------------------------------------------------
255// Miscellaneous
256
257// A static resource holds a static instance that can be reserved in
258// a local scope using an instance of Access. Attempts to re-reserve
259// the instance will cause an error.
260template <typename T>
261class StaticResource {
262 public:
263 StaticResource() : is_reserved_(false) {}
264
265 private:
266 template <typename S> friend class Access;
267 T instance_;
268 bool is_reserved_;
269};
270
271
272// Locally scoped access to a static resource.
273template <typename T>
274class Access {
275 public:
276 explicit Access(StaticResource<T>* resource)
277 : resource_(resource)
278 , instance_(&resource->instance_) {
279 ASSERT(!resource->is_reserved_);
280 resource->is_reserved_ = true;
281 }
282
283 ~Access() {
284 resource_->is_reserved_ = false;
285 resource_ = NULL;
286 instance_ = NULL;
287 }
288
289 T* value() { return instance_; }
290 T* operator -> () { return instance_; }
291
292 private:
293 StaticResource<T>* resource_;
294 T* instance_;
295};
296
297
298template <typename T>
299class Vector {
300 public:
301 Vector() : start_(NULL), length_(0) {}
302 Vector(T* data, int length) : start_(data), length_(length) {
303 ASSERT(length == 0 || (length > 0 && data != NULL));
304 }
305
306 static Vector<T> New(int length) {
307 return Vector<T>(NewArray<T>(length), length);
308 }
309
310 // Returns a vector using the same backing storage as this one,
311 // spanning from and including 'from', to but not including 'to'.
312 Vector<T> SubVector(int from, int to) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000313 ASSERT(to <= length_);
314 ASSERT(from < to);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100315 ASSERT(0 <= from);
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 return Vector<T>(start() + from, to - from);
317 }
318
319 // Returns the length of the vector.
320 int length() const { return length_; }
321
322 // Returns whether or not the vector is empty.
323 bool is_empty() const { return length_ == 0; }
324
325 // Returns the pointer to the start of the data in the vector.
326 T* start() const { return start_; }
327
328 // Access individual vector elements - checks bounds in debug mode.
329 T& operator[](int index) const {
330 ASSERT(0 <= index && index < length_);
331 return start_[index];
332 }
333
Ben Murdochb0fe1622011-05-05 13:52:32 +0100334 const T& at(int index) const { return operator[](index); }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800335
Steve Blocka7e24c12009-10-30 11:49:00 +0000336 T& first() { return start_[0]; }
337
338 T& last() { return start_[length_ - 1]; }
339
340 // Returns a clone of this vector with a new backing store.
341 Vector<T> Clone() const {
342 T* result = NewArray<T>(length_);
343 for (int i = 0; i < length_; i++) result[i] = start_[i];
344 return Vector<T>(result, length_);
345 }
346
347 void Sort(int (*cmp)(const T*, const T*)) {
348 typedef int (*RawComparer)(const void*, const void*);
349 qsort(start(),
350 length(),
351 sizeof(T),
352 reinterpret_cast<RawComparer>(cmp));
353 }
354
355 void Sort() {
356 Sort(PointerValueCompare<T>);
357 }
358
359 void Truncate(int length) {
360 ASSERT(length <= length_);
361 length_ = length;
362 }
363
364 // Releases the array underlying this vector. Once disposed the
365 // vector is empty.
366 void Dispose() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 DeleteArray(start_);
368 start_ = NULL;
369 length_ = 0;
370 }
371
372 inline Vector<T> operator+(int offset) {
373 ASSERT(offset < length_);
374 return Vector<T>(start_ + offset, length_ - offset);
375 }
376
377 // Factory method for creating empty vectors.
378 static Vector<T> empty() { return Vector<T>(NULL, 0); }
379
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100380 template<typename S>
381 static Vector<T> cast(Vector<S> input) {
382 return Vector<T>(reinterpret_cast<T*>(input.start()),
383 input.length() * sizeof(S) / sizeof(T));
384 }
385
Steve Blocka7e24c12009-10-30 11:49:00 +0000386 protected:
387 void set_start(T* start) { start_ = start; }
388
389 private:
390 T* start_;
391 int length_;
392};
393
394
Ben Murdochb0fe1622011-05-05 13:52:32 +0100395// A pointer that can only be set once and doesn't allow NULL values.
396template<typename T>
397class SetOncePointer {
398 public:
399 SetOncePointer() : pointer_(NULL) { }
400
401 bool is_set() const { return pointer_ != NULL; }
402
403 T* get() const {
404 ASSERT(pointer_ != NULL);
405 return pointer_;
406 }
407
408 void set(T* value) {
409 ASSERT(pointer_ == NULL && value != NULL);
410 pointer_ = value;
411 }
412
413 private:
414 T* pointer_;
415};
416
417
Steve Blocka7e24c12009-10-30 11:49:00 +0000418template <typename T, int kSize>
419class EmbeddedVector : public Vector<T> {
420 public:
421 EmbeddedVector() : Vector<T>(buffer_, kSize) { }
422
Ben Murdochb0fe1622011-05-05 13:52:32 +0100423 explicit EmbeddedVector(T initial_value) : Vector<T>(buffer_, kSize) {
424 for (int i = 0; i < kSize; ++i) {
425 buffer_[i] = initial_value;
426 }
427 }
428
Steve Blocka7e24c12009-10-30 11:49:00 +0000429 // When copying, make underlying Vector to reference our buffer.
430 EmbeddedVector(const EmbeddedVector& rhs)
431 : Vector<T>(rhs) {
432 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
433 set_start(buffer_);
434 }
435
436 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
437 if (this == &rhs) return *this;
438 Vector<T>::operator=(rhs);
439 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
Steve Block6ded16b2010-05-10 14:33:55 +0100440 this->set_start(buffer_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000441 return *this;
442 }
443
444 private:
445 T buffer_[kSize];
446};
447
448
449template <typename T>
450class ScopedVector : public Vector<T> {
451 public:
452 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
453 ~ScopedVector() {
454 DeleteArray(this->start());
455 }
Kristian Monsen25f61362010-05-21 11:50:48 +0100456
457 private:
458 DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
Steve Blocka7e24c12009-10-30 11:49:00 +0000459};
460
461
462inline Vector<const char> CStrVector(const char* data) {
Steve Blockd0582a62009-12-15 09:54:21 +0000463 return Vector<const char>(data, StrLength(data));
Steve Blocka7e24c12009-10-30 11:49:00 +0000464}
465
466inline Vector<char> MutableCStrVector(char* data) {
Steve Blockd0582a62009-12-15 09:54:21 +0000467 return Vector<char>(data, StrLength(data));
Steve Blocka7e24c12009-10-30 11:49:00 +0000468}
469
470inline Vector<char> MutableCStrVector(char* data, int max) {
Steve Blockd0582a62009-12-15 09:54:21 +0000471 int length = StrLength(data);
Steve Blocka7e24c12009-10-30 11:49:00 +0000472 return Vector<char>(data, (length < max) ? length : max);
473}
474
Steve Blocka7e24c12009-10-30 11:49:00 +0000475
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100476/*
477 * A class that collects values into a backing store.
478 * Specialized versions of the class can allow access to the backing store
479 * in different ways.
480 * There is no guarantee that the backing store is contiguous (and, as a
481 * consequence, no guarantees that consecutively added elements are adjacent
482 * in memory). The collector may move elements unless it has guaranteed not
483 * to.
484 */
485template <typename T, int growth_factor = 2, int max_growth = 1 * MB>
486class Collector {
487 public:
488 explicit Collector(int initial_capacity = kMinCapacity)
489 : index_(0), size_(0) {
490 if (initial_capacity < kMinCapacity) {
491 initial_capacity = kMinCapacity;
492 }
493 current_chunk_ = Vector<T>::New(initial_capacity);
494 }
495
496 virtual ~Collector() {
497 // Free backing store (in reverse allocation order).
498 current_chunk_.Dispose();
499 for (int i = chunks_.length() - 1; i >= 0; i--) {
500 chunks_.at(i).Dispose();
501 }
502 }
503
504 // Add a single element.
505 inline void Add(T value) {
506 if (index_ >= current_chunk_.length()) {
507 Grow(1);
508 }
509 current_chunk_[index_] = value;
510 index_++;
511 size_++;
512 }
513
514 // Add a block of contiguous elements and return a Vector backed by the
515 // memory area.
516 // A basic Collector will keep this vector valid as long as the Collector
517 // is alive.
518 inline Vector<T> AddBlock(int size, T initial_value) {
519 ASSERT(size > 0);
520 if (size > current_chunk_.length() - index_) {
521 Grow(size);
522 }
523 T* position = current_chunk_.start() + index_;
524 index_ += size;
525 size_ += size;
526 for (int i = 0; i < size; i++) {
527 position[i] = initial_value;
528 }
529 return Vector<T>(position, size);
530 }
531
532
533 // Write the contents of the collector into the provided vector.
534 void WriteTo(Vector<T> destination) {
535 ASSERT(size_ <= destination.length());
536 int position = 0;
537 for (int i = 0; i < chunks_.length(); i++) {
538 Vector<T> chunk = chunks_.at(i);
539 for (int j = 0; j < chunk.length(); j++) {
540 destination[position] = chunk[j];
541 position++;
542 }
543 }
544 for (int i = 0; i < index_; i++) {
545 destination[position] = current_chunk_[i];
546 position++;
547 }
548 }
549
550 // Allocate a single contiguous vector, copy all the collected
551 // elements to the vector, and return it.
552 // The caller is responsible for freeing the memory of the returned
553 // vector (e.g., using Vector::Dispose).
554 Vector<T> ToVector() {
555 Vector<T> new_store = Vector<T>::New(size_);
556 WriteTo(new_store);
557 return new_store;
558 }
559
560 // Resets the collector to be empty.
561 virtual void Reset() {
562 for (int i = chunks_.length() - 1; i >= 0; i--) {
563 chunks_.at(i).Dispose();
564 }
565 chunks_.Rewind(0);
566 index_ = 0;
567 size_ = 0;
568 }
569
570 // Total number of elements added to collector so far.
571 inline int size() { return size_; }
572
573 protected:
574 static const int kMinCapacity = 16;
575 List<Vector<T> > chunks_;
576 Vector<T> current_chunk_; // Block of memory currently being written into.
577 int index_; // Current index in current chunk.
578 int size_; // Total number of elements in collector.
579
580 // Creates a new current chunk, and stores the old chunk in the chunks_ list.
581 void Grow(int min_capacity) {
582 ASSERT(growth_factor > 1);
583 int growth = current_chunk_.length() * (growth_factor - 1);
584 if (growth > max_growth) {
585 growth = max_growth;
586 }
587 int new_capacity = current_chunk_.length() + growth;
588 if (new_capacity < min_capacity) {
589 new_capacity = min_capacity + growth;
590 }
591 Vector<T> new_chunk = Vector<T>::New(new_capacity);
592 int new_index = PrepareGrow(new_chunk);
593 if (index_ > 0) {
594 chunks_.Add(current_chunk_.SubVector(0, index_));
595 } else {
596 // Can happen if the call to PrepareGrow moves everything into
597 // the new chunk.
598 current_chunk_.Dispose();
599 }
600 current_chunk_ = new_chunk;
601 index_ = new_index;
602 ASSERT(index_ + min_capacity <= current_chunk_.length());
603 }
604
605 // Before replacing the current chunk, give a subclass the option to move
606 // some of the current data into the new chunk. The function may update
607 // the current index_ value to represent data no longer in the current chunk.
608 // Returns the initial index of the new chunk (after copied data).
609 virtual int PrepareGrow(Vector<T> new_chunk) {
610 return 0;
611 }
612};
613
614
615/*
616 * A collector that allows sequences of values to be guaranteed to
617 * stay consecutive.
618 * If the backing store grows while a sequence is active, the current
619 * sequence might be moved, but after the sequence is ended, it will
620 * not move again.
621 * NOTICE: Blocks allocated using Collector::AddBlock(int) can move
622 * as well, if inside an active sequence where another element is added.
623 */
624template <typename T, int growth_factor = 2, int max_growth = 1 * MB>
625class SequenceCollector : public Collector<T, growth_factor, max_growth> {
626 public:
627 explicit SequenceCollector(int initial_capacity)
628 : Collector<T, growth_factor, max_growth>(initial_capacity),
629 sequence_start_(kNoSequence) { }
630
631 virtual ~SequenceCollector() {}
632
633 void StartSequence() {
634 ASSERT(sequence_start_ == kNoSequence);
635 sequence_start_ = this->index_;
636 }
637
638 Vector<T> EndSequence() {
639 ASSERT(sequence_start_ != kNoSequence);
640 int sequence_start = sequence_start_;
641 sequence_start_ = kNoSequence;
642 if (sequence_start == this->index_) return Vector<T>();
643 return this->current_chunk_.SubVector(sequence_start, this->index_);
644 }
645
646 // Drops the currently added sequence, and all collected elements in it.
647 void DropSequence() {
648 ASSERT(sequence_start_ != kNoSequence);
649 int sequence_length = this->index_ - sequence_start_;
650 this->index_ = sequence_start_;
651 this->size_ -= sequence_length;
652 sequence_start_ = kNoSequence;
653 }
654
655 virtual void Reset() {
656 sequence_start_ = kNoSequence;
657 this->Collector<T, growth_factor, max_growth>::Reset();
658 }
659
660 private:
661 static const int kNoSequence = -1;
662 int sequence_start_;
663
664 // Move the currently active sequence to the new chunk.
665 virtual int PrepareGrow(Vector<T> new_chunk) {
666 if (sequence_start_ != kNoSequence) {
667 int sequence_length = this->index_ - sequence_start_;
668 // The new chunk is always larger than the current chunk, so there
669 // is room for the copy.
670 ASSERT(sequence_length < new_chunk.length());
671 for (int i = 0; i < sequence_length; i++) {
672 new_chunk[i] = this->current_chunk_[sequence_start_ + i];
673 }
674 this->index_ = sequence_start_;
675 sequence_start_ = 0;
676 return sequence_length;
677 }
678 return 0;
679 }
680};
681
682
Steve Block6ded16b2010-05-10 14:33:55 +0100683// Compare ASCII/16bit chars to ASCII/16bit chars.
684template <typename lchar, typename rchar>
685static inline int CompareChars(const lchar* lhs, const rchar* rhs, int chars) {
686 const lchar* limit = lhs + chars;
687#ifdef V8_HOST_CAN_READ_UNALIGNED
688 if (sizeof(*lhs) == sizeof(*rhs)) {
689 // Number of characters in a uintptr_t.
690 static const int kStepSize = sizeof(uintptr_t) / sizeof(*lhs); // NOLINT
691 while (lhs <= limit - kStepSize) {
692 if (*reinterpret_cast<const uintptr_t*>(lhs) !=
693 *reinterpret_cast<const uintptr_t*>(rhs)) {
694 break;
695 }
696 lhs += kStepSize;
697 rhs += kStepSize;
698 }
699 }
700#endif
701 while (lhs < limit) {
702 int r = static_cast<int>(*lhs) - static_cast<int>(*rhs);
703 if (r != 0) return r;
704 ++lhs;
705 ++rhs;
706 }
707 return 0;
708}
709
710
Steve Blockd0582a62009-12-15 09:54:21 +0000711// Calculate 10^exponent.
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -0800712static inline int TenToThe(int exponent) {
713 ASSERT(exponent <= 9);
714 ASSERT(exponent >= 1);
715 int answer = 10;
716 for (int i = 1; i < exponent; i++) answer *= 10;
717 return answer;
718}
Steve Blockd0582a62009-12-15 09:54:21 +0000719
Steve Block6ded16b2010-05-10 14:33:55 +0100720
721// The type-based aliasing rule allows the compiler to assume that pointers of
722// different types (for some definition of different) never alias each other.
723// Thus the following code does not work:
724//
725// float f = foo();
726// int fbits = *(int*)(&f);
727//
728// The compiler 'knows' that the int pointer can't refer to f since the types
729// don't match, so the compiler may cache f in a register, leaving random data
730// in fbits. Using C++ style casts makes no difference, however a pointer to
731// char data is assumed to alias any other pointer. This is the 'memcpy
732// exception'.
733//
734// Bit_cast uses the memcpy exception to move the bits from a variable of one
735// type of a variable of another type. Of course the end result is likely to
736// be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
737// will completely optimize BitCast away.
738//
739// There is an additional use for BitCast.
740// Recent gccs will warn when they see casts that may result in breakage due to
741// the type-based aliasing rule. If you have checked that there is no breakage
742// you can use BitCast to cast one pointer type to another. This confuses gcc
743// enough that it can no longer see that you have cast one pointer type to
744// another thus avoiding the warning.
745template <class Dest, class Source>
746inline Dest BitCast(const Source& source) {
747 // Compile time assertion: sizeof(Dest) == sizeof(Source)
748 // A compile error here means your Dest and Source have different sizes.
749 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
750
751 Dest dest;
752 memcpy(&dest, &source, sizeof(dest));
753 return dest;
754}
755
Iain Merrick75681382010-08-19 15:07:18 +0100756template <class Dest, class Source>
Steve Block791712a2010-08-27 10:21:07 +0100757inline Dest BitCast(Source* source) {
758 return BitCast<Dest>(reinterpret_cast<uintptr_t>(source));
Iain Merrick75681382010-08-19 15:07:18 +0100759}
Steve Blocka7e24c12009-10-30 11:49:00 +0000760
Iain Merrick75681382010-08-19 15:07:18 +0100761} } // namespace v8::internal
Steve Block6ded16b2010-05-10 14:33:55 +0100762
Steve Blocka7e24c12009-10-30 11:49:00 +0000763#endif // V8_UTILS_H_