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

Side by Side Diff: src/utils.h

Issue 113992: fix embedded vector copy constructor and assignment. (Closed)
Patch Set: Created 11 years, 6 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
« no previous file with comments | « no previous file | test/cctest/test-utils.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 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 } 372 }
373 373
374 inline Vector<T> operator+(int offset) { 374 inline Vector<T> operator+(int offset) {
375 ASSERT(offset < length_); 375 ASSERT(offset < length_);
376 return Vector<T>(start_ + offset, length_ - offset); 376 return Vector<T>(start_ + offset, length_ - offset);
377 } 377 }
378 378
379 // Factory method for creating empty vectors. 379 // Factory method for creating empty vectors.
380 static Vector<T> empty() { return Vector<T>(NULL, 0); } 380 static Vector<T> empty() { return Vector<T>(NULL, 0); }
381 381
382 protected:
383 void set_start(T* start) { start_ = start; }
384
382 private: 385 private:
383 T* start_; 386 T* start_;
384 int length_; 387 int length_;
385 }; 388 };
386 389
387 390
388 // A temporary assignment sets a (non-local) variable to a value on 391 // A temporary assignment sets a (non-local) variable to a value on
389 // construction and resets it the value on destruction. 392 // construction and resets it the value on destruction.
390 template <typename T> 393 template <typename T>
391 class TempAssign { 394 class TempAssign {
392 public: 395 public:
393 TempAssign(T* var, T value): var_(var), old_value_(*var) { 396 TempAssign(T* var, T value): var_(var), old_value_(*var) {
394 *var = value; 397 *var = value;
395 } 398 }
396 399
397 ~TempAssign() { *var_ = old_value_; } 400 ~TempAssign() { *var_ = old_value_; }
398 401
399 private: 402 private:
400 T* var_; 403 T* var_;
401 T old_value_; 404 T old_value_;
402 }; 405 };
403 406
404 407
405 template <typename T, int kSize> 408 template <typename T, int kSize>
406 class EmbeddedVector : public Vector<T> { 409 class EmbeddedVector : public Vector<T> {
407 public: 410 public:
408 EmbeddedVector() : Vector<T>(buffer_, kSize) { } 411 EmbeddedVector() : Vector<T>(buffer_, kSize) { }
412
413 // When copying, make underlying Vector to reference our buffer.
414 EmbeddedVector(const EmbeddedVector& rhs)
415 : Vector<T>(rhs) {
416 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
417 set_start(buffer_);
418 }
419
420 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
421 if (this == &rhs) return *this;
422 Vector<T>::operator=(rhs);
423 memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
424 set_start(buffer_);
425 return *this;
426 }
427
409 private: 428 private:
410 T buffer_[kSize]; 429 T buffer_[kSize];
411 }; 430 };
412 431
413 432
414 template <typename T> 433 template <typename T>
415 class ScopedVector : public Vector<T> { 434 class ScopedVector : public Vector<T> {
416 public: 435 public:
417 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { } 436 explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
418 ~ScopedVector() { 437 ~ScopedVector() {
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 #endif 565 #endif
547 while (dest < limit) { 566 while (dest < limit) {
548 *dest++ = static_cast<sinkchar>(*src++); 567 *dest++ = static_cast<sinkchar>(*src++);
549 } 568 }
550 } 569 }
551 570
552 571
553 } } // namespace v8::internal 572 } } // namespace v8::internal
554 573
555 #endif // V8_UTILS_H_ 574 #endif // V8_UTILS_H_
OLDNEW
« no previous file with comments | « no previous file | test/cctest/test-utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698