From 79be539892e5a85bbce6e24f92337dec0580efe2 Mon Sep 17 00:00:00 2001 From: "jochen@chromium.org" Date: Fri, 25 Apr 2014 08:37:18 +0000 Subject: [PATCH] Break cyclic reference between utils and platform. Platform includes utils.h for the definition of Vector<>, so utils.h can't include platform.h and has to use memcpy instead of OS::MemCopy. We split out Vector<> into its own header to break this cycle dependency. BUG=none R=mstarzinger@chromium.org LOG=n Review URL: https://codereview.chromium.org/251753002 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@20962 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/libplatform/default-platform.cc | 7 +- src/libplatform/default-platform.h | 2 +- src/platform.h | 2 +- src/utils.h | 161 +---------------------- src/vector.h | 194 ++++++++++++++++++++++++++++ tools/gyp/v8.gyp | 1 + 6 files changed, 208 insertions(+), 159 deletions(-) create mode 100644 src/vector.h diff --git a/src/libplatform/default-platform.cc b/src/libplatform/default-platform.cc index 1e21ca4e8..decfbeabc 100644 --- a/src/libplatform/default-platform.cc +++ b/src/libplatform/default-platform.cc @@ -27,6 +27,7 @@ #include "default-platform.h" +#include #include // TODO(jochen): We should have our own version of checks.h. @@ -39,6 +40,9 @@ namespace v8 { namespace internal { +const int DefaultPlatform::kMaxThreadPoolSize = 4; + + DefaultPlatform::DefaultPlatform() : initialized_(false), thread_pool_size_(0) {} @@ -60,7 +64,8 @@ void DefaultPlatform::SetThreadPoolSize(int thread_pool_size) { ASSERT(thread_pool_size >= 0); if (thread_pool_size < 1) thread_pool_size = CPU::NumberOfProcessorsOnline(); - thread_pool_size_ = Max(Min(thread_pool_size, kMaxThreadPoolSize), 1); + thread_pool_size_ = + std::max(std::min(thread_pool_size, kMaxThreadPoolSize), 1); } diff --git a/src/libplatform/default-platform.h b/src/libplatform/default-platform.h index f509a3562..96840685d 100644 --- a/src/libplatform/default-platform.h +++ b/src/libplatform/default-platform.h @@ -58,7 +58,7 @@ class DefaultPlatform : public Platform { Task *task) V8_OVERRIDE; private: - static const int kMaxThreadPoolSize = 4; + static const int kMaxThreadPoolSize; Mutex lock_; bool initialized_; diff --git a/src/platform.h b/src/platform.h index d087d2397..2b6f2865b 100644 --- a/src/platform.h +++ b/src/platform.h @@ -48,7 +48,7 @@ #include "platform/mutex.h" #include "platform/semaphore.h" -#include "utils.h" +#include "vector.h" #include "v8globals.h" #ifdef __sun diff --git a/src/utils.h b/src/utils.h index 845cb7555..f7800a84e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -31,11 +31,12 @@ #include #include #include -#include #include "allocation.h" #include "checks.h" #include "globals.h" +#include "platform.h" +#include "vector.h" namespace v8 { namespace internal { @@ -249,13 +250,6 @@ T NegAbs(T a) { } -inline int StrLength(const char* string) { - size_t length = strlen(string); - ASSERT(length == static_cast(static_cast(length))); - return static_cast(length); -} - - // TODO(svenpanne) Clean up the whole power-of-2 mess. inline int32_t WhichPowerOf2Abs(int32_t x) { return (x == kMinInt) ? 31 : WhichPowerOf2(Abs(x)); @@ -394,110 +388,6 @@ class Access { }; -template -class Vector { - public: - Vector() : start_(NULL), length_(0) {} - Vector(T* data, int length) : start_(data), length_(length) { - ASSERT(length == 0 || (length > 0 && data != NULL)); - } - - static Vector New(int length) { - return Vector(NewArray(length), length); - } - - // Returns a vector using the same backing storage as this one, - // spanning from and including 'from', to but not including 'to'. - Vector SubVector(int from, int to) { - SLOW_ASSERT(to <= length_); - SLOW_ASSERT(from < to); - ASSERT(0 <= from); - return Vector(start() + from, to - from); - } - - // Returns the length of the vector. - int length() const { return length_; } - - // Returns whether or not the vector is empty. - bool is_empty() const { return length_ == 0; } - - // Returns the pointer to the start of the data in the vector. - T* start() const { return start_; } - - // Access individual vector elements - checks bounds in debug mode. - T& operator[](int index) const { - ASSERT(0 <= index && index < length_); - return start_[index]; - } - - const T& at(int index) const { return operator[](index); } - - T& first() { return start_[0]; } - - T& last() { return start_[length_ - 1]; } - - // Returns a clone of this vector with a new backing store. - Vector Clone() const { - T* result = NewArray(length_); - for (int i = 0; i < length_; i++) result[i] = start_[i]; - return Vector(result, length_); - } - - void Sort(int (*cmp)(const T*, const T*)) { - std::sort(start(), start() + length(), RawComparer(cmp)); - } - - void Sort() { - std::sort(start(), start() + length()); - } - - void Truncate(int length) { - ASSERT(length <= length_); - length_ = length; - } - - // Releases the array underlying this vector. Once disposed the - // vector is empty. - void Dispose() { - DeleteArray(start_); - start_ = NULL; - length_ = 0; - } - - inline Vector operator+(int offset) { - ASSERT(offset < length_); - return Vector(start_ + offset, length_ - offset); - } - - // Factory method for creating empty vectors. - static Vector empty() { return Vector(NULL, 0); } - - template - static Vector cast(Vector input) { - return Vector(reinterpret_cast(input.start()), - input.length() * sizeof(S) / sizeof(T)); - } - - protected: - void set_start(T* start) { start_ = start; } - - private: - T* start_; - int length_; - - class RawComparer { - public: - explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {} - bool operator()(const T& a, const T& b) { - return cmp_(&a, &b) < 0; - } - - private: - int (*cmp_)(const T*, const T*); - }; -}; - - // A pointer that can only be set once and doesn't allow NULL values. template class SetOncePointer { @@ -535,16 +425,14 @@ class EmbeddedVector : public Vector { // When copying, make underlying Vector to reference our buffer. EmbeddedVector(const EmbeddedVector& rhs) : Vector(rhs) { - // TODO(jkummerow): Refactor #includes and use OS::MemCopy() instead. - memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); + OS::MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize); set_start(buffer_); } EmbeddedVector& operator=(const EmbeddedVector& rhs) { if (this == &rhs) return *this; Vector::operator=(rhs); - // TODO(jkummerow): Refactor #includes and use OS::MemCopy() instead. - memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize); + OS::MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize); this->set_start(buffer_); return *this; } @@ -554,44 +442,6 @@ class EmbeddedVector : public Vector { }; -template -class ScopedVector : public Vector { - public: - explicit ScopedVector(int length) : Vector(NewArray(length), length) { } - ~ScopedVector() { - DeleteArray(this->start()); - } - - private: - DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); -}; - -#define STATIC_ASCII_VECTOR(x) \ - v8::internal::Vector(reinterpret_cast(x), \ - ARRAY_SIZE(x)-1) - -inline Vector CStrVector(const char* data) { - return Vector(data, StrLength(data)); -} - -inline Vector OneByteVector(const char* data, int length) { - return Vector(reinterpret_cast(data), length); -} - -inline Vector OneByteVector(const char* data) { - return OneByteVector(data, StrLength(data)); -} - -inline Vector MutableCStrVector(char* data) { - return Vector(data, StrLength(data)); -} - -inline Vector MutableCStrVector(char* data, int max) { - int length = StrLength(data); - return Vector(data, (length < max) ? length : max); -} - - /* * A class that collects values into a backing store. * Specialized versions of the class can allow access to the backing store @@ -919,8 +769,7 @@ struct BitCastHelper { INLINE(static Dest cast(const Source& source)) { Dest dest; - // TODO(jkummerow): Refactor #includes and use OS::MemCopy() instead. - memcpy(&dest, &source, sizeof(dest)); + OS::MemCopy(&dest, &source, sizeof(dest)); return dest; } }; diff --git a/src/vector.h b/src/vector.h new file mode 100644 index 000000000..22e6a810a --- /dev/null +++ b/src/vector.h @@ -0,0 +1,194 @@ +// Copyright 2014 the V8 project authors. All rights reserved. +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following +// disclaimer in the documentation and/or other materials provided +// with the distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#ifndef V8_VECTOR_H_ +#define V8_VECTOR_H_ + +#include +#include + +#include "allocation.h" +#include "checks.h" +#include "globals.h" + +namespace v8 { +namespace internal { + + +template +class Vector { + public: + Vector() : start_(NULL), length_(0) {} + Vector(T* data, int length) : start_(data), length_(length) { + ASSERT(length == 0 || (length > 0 && data != NULL)); + } + + static Vector New(int length) { + return Vector(NewArray(length), length); + } + + // Returns a vector using the same backing storage as this one, + // spanning from and including 'from', to but not including 'to'. + Vector SubVector(int from, int to) { + SLOW_ASSERT(to <= length_); + SLOW_ASSERT(from < to); + ASSERT(0 <= from); + return Vector(start() + from, to - from); + } + + // Returns the length of the vector. + int length() const { return length_; } + + // Returns whether or not the vector is empty. + bool is_empty() const { return length_ == 0; } + + // Returns the pointer to the start of the data in the vector. + T* start() const { return start_; } + + // Access individual vector elements - checks bounds in debug mode. + T& operator[](int index) const { + ASSERT(0 <= index && index < length_); + return start_[index]; + } + + const T& at(int index) const { return operator[](index); } + + T& first() { return start_[0]; } + + T& last() { return start_[length_ - 1]; } + + // Returns a clone of this vector with a new backing store. + Vector Clone() const { + T* result = NewArray(length_); + for (int i = 0; i < length_; i++) result[i] = start_[i]; + return Vector(result, length_); + } + + void Sort(int (*cmp)(const T*, const T*)) { + std::sort(start(), start() + length(), RawComparer(cmp)); + } + + void Sort() { + std::sort(start(), start() + length()); + } + + void Truncate(int length) { + ASSERT(length <= length_); + length_ = length; + } + + // Releases the array underlying this vector. Once disposed the + // vector is empty. + void Dispose() { + DeleteArray(start_); + start_ = NULL; + length_ = 0; + } + + inline Vector operator+(int offset) { + ASSERT(offset < length_); + return Vector(start_ + offset, length_ - offset); + } + + // Factory method for creating empty vectors. + static Vector empty() { return Vector(NULL, 0); } + + template + static Vector cast(Vector input) { + return Vector(reinterpret_cast(input.start()), + input.length() * sizeof(S) / sizeof(T)); + } + + protected: + void set_start(T* start) { start_ = start; } + + private: + T* start_; + int length_; + + class RawComparer { + public: + explicit RawComparer(int (*cmp)(const T*, const T*)) : cmp_(cmp) {} + bool operator()(const T& a, const T& b) { + return cmp_(&a, &b) < 0; + } + + private: + int (*cmp_)(const T*, const T*); + }; +}; + + +template +class ScopedVector : public Vector { + public: + explicit ScopedVector(int length) : Vector(NewArray(length), length) { } + ~ScopedVector() { + DeleteArray(this->start()); + } + + private: + DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector); +}; + + +inline int StrLength(const char* string) { + size_t length = strlen(string); + ASSERT(length == static_cast(static_cast(length))); + return static_cast(length); +} + + +#define STATIC_ASCII_VECTOR(x) \ + v8::internal::Vector(reinterpret_cast(x), \ + ARRAY_SIZE(x)-1) + +inline Vector CStrVector(const char* data) { + return Vector(data, StrLength(data)); +} + +inline Vector OneByteVector(const char* data, int length) { + return Vector(reinterpret_cast(data), length); +} + +inline Vector OneByteVector(const char* data) { + return OneByteVector(data, StrLength(data)); +} + +inline Vector MutableCStrVector(char* data) { + return Vector(data, StrLength(data)); +} + +inline Vector MutableCStrVector(char* data, int max) { + int length = StrLength(data); + return Vector(data, (length < max) ? length : max); +} + + +} } // namespace v8::internal + +#endif // V8_VECTOR_H_ diff --git a/tools/gyp/v8.gyp b/tools/gyp/v8.gyp index 5785336a2..50f5a4815 100644 --- a/tools/gyp/v8.gyp +++ b/tools/gyp/v8.gyp @@ -605,6 +605,7 @@ '../../src/v8utils.h', '../../src/variables.cc', '../../src/variables.h', + '../../src/vector.h', '../../src/version.cc', '../../src/version.h', '../../src/vm-state-inl.h', -- 2.34.1