Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: src/utils.h

Issue 1148007: Merge bleeding_edge from version 2.1.3 up to revision 4205... (Closed) Base URL: http://v8.googlecode.com/svn/branches/experimental/partial_snapshots/
Patch Set: Created 10 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« src/runtime.cc ('K') | « src/top.cc ('k') | src/v8.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 11 matching lines...) Expand all
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 #ifndef V8_UTILS_H_ 28 #ifndef V8_UTILS_H_
29 #define V8_UTILS_H_ 29 #define V8_UTILS_H_
30 30
31 #include <stdlib.h> 31 #include <stdlib.h>
32 #include <string.h>
32 33
33 namespace v8 { 34 namespace v8 {
34 namespace internal { 35 namespace internal {
35 36
36 // ---------------------------------------------------------------------------- 37 // ----------------------------------------------------------------------------
37 // General helper functions 38 // General helper functions
38 39
39 // Returns true iff x is a power of 2 (or zero). Cannot be used with the 40 // Returns true iff x is a power of 2 (or zero). Cannot be used with the
40 // maximally negative value of the type T (the -1 overflows). 41 // maximally negative value of the type T (the -1 overflows).
41 template <typename T> 42 template <typename T>
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 } 334 }
334 335
335 void Truncate(int length) { 336 void Truncate(int length) {
336 ASSERT(length <= length_); 337 ASSERT(length <= length_);
337 length_ = length; 338 length_ = length;
338 } 339 }
339 340
340 // Releases the array underlying this vector. Once disposed the 341 // Releases the array underlying this vector. Once disposed the
341 // vector is empty. 342 // vector is empty.
342 void Dispose() { 343 void Dispose() {
343 if (is_empty()) return;
344 DeleteArray(start_); 344 DeleteArray(start_);
345 start_ = NULL; 345 start_ = NULL;
346 length_ = 0; 346 length_ = 0;
347 } 347 }
348 348
349 inline Vector<T> operator+(int offset) { 349 inline Vector<T> operator+(int offset) {
350 ASSERT(offset < length_); 350 ASSERT(offset < length_);
351 return Vector<T>(start_ + offset, length_ - offset); 351 return Vector<T>(start_ + offset, length_ - offset);
352 } 352 }
353 353
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 EmbeddedVector(const EmbeddedVector& rhs) 389 EmbeddedVector(const EmbeddedVector& rhs)
390 : Vector<T>(rhs) { 390 : Vector<T>(rhs) {
391 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); 391 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
392 set_start(buffer_); 392 set_start(buffer_);
393 } 393 }
394 394
395 EmbeddedVector& operator=(const EmbeddedVector& rhs) { 395 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
396 if (this == &rhs) return *this; 396 if (this == &rhs) return *this;
397 Vector<T>::operator=(rhs); 397 Vector<T>::operator=(rhs);
398 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); 398 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
399 set_start(buffer_); 399 this->set_start(buffer_);
400 return *this; 400 return *this;
401 } 401 }
402 402
403 private: 403 private:
404 T buffer_[kSize]; 404 T buffer_[kSize];
405 }; 405 };
406 406
407 407
408 template <typename T> 408 template <typename T>
409 class ScopedVector : public Vector<T> { 409 class ScopedVector : public Vector<T> {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 } 592 }
593 #endif 593 #endif
594 594
595 #undef STOS 595 #undef STOS
596 } 596 }
597 597
598 598
599 // Calculate 10^exponent. 599 // Calculate 10^exponent.
600 int TenToThe(int exponent); 600 int TenToThe(int exponent);
601 601
602
603 // The type-based aliasing rule allows the compiler to assume that pointers of
604 // different types (for some definition of different) never alias each other.
605 // Thus the following code does not work:
606 //
607 // float f = foo();
608 // int fbits = *(int*)(&f);
609 //
610 // The compiler 'knows' that the int pointer can't refer to f since the types
611 // don't match, so the compiler may cache f in a register, leaving random data
612 // in fbits. Using C++ style casts makes no difference, however a pointer to
613 // char data is assumed to alias any other pointer. This is the 'memcpy
614 // exception'.
615 //
616 // Bit_cast uses the memcpy exception to move the bits from a variable of one
617 // type of a variable of another type. Of course the end result is likely to
618 // be implementation dependent. Most compilers (gcc-4.2 and MSVC 2005)
619 // will completely optimize BitCast away.
620 //
621 // There is an additional use for BitCast.
622 // Recent gccs will warn when they see casts that may result in breakage due to
623 // the type-based aliasing rule. If you have checked that there is no breakage
624 // you can use BitCast to cast one pointer type to another. This confuses gcc
625 // enough that it can no longer see that you have cast one pointer type to
626 // another thus avoiding the warning.
627 template <class Dest, class Source>
628 inline Dest BitCast(const Source& source) {
629 // Compile time assertion: sizeof(Dest) == sizeof(Source)
630 // A compile error here means your Dest and Source have different sizes.
631 typedef char VerifySizesAreEqual[sizeof(Dest) == sizeof(Source) ? 1 : -1];
632
633 Dest dest;
634 memcpy(&dest, &source, sizeof(dest));
635 return dest;
636 }
637
638
602 } } // namespace v8::internal 639 } } // namespace v8::internal
603 640
604 #endif // V8_UTILS_H_ 641 #endif // V8_UTILS_H_
OLDNEW
« src/runtime.cc ('K') | « src/top.cc ('k') | src/v8.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698