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

Unified Diff: src/utils.h

Issue 254823002: Revert 20962 "Break cyclic reference between utils and platform." (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/platform.h ('k') | src/vector.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/utils.h
diff --git a/src/utils.h b/src/utils.h
index f7800a84e3d8da1f3b81bd33ffd51f238a9fafcd..845cb7555b2e56b9bfa0b0faa2e7f751d42623fc 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -31,12 +31,11 @@
#include <limits.h>
#include <stdlib.h>
#include <string.h>
+#include <algorithm>
#include "allocation.h"
#include "checks.h"
#include "globals.h"
-#include "platform.h"
-#include "vector.h"
namespace v8 {
namespace internal {
@@ -250,6 +249,13 @@ T NegAbs(T a) {
}
+inline int StrLength(const char* string) {
+ size_t length = strlen(string);
+ ASSERT(length == static_cast<size_t>(static_cast<int>(length)));
+ return static_cast<int>(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));
@@ -388,6 +394,110 @@ class Access {
};
+template <typename T>
+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<T> New(int length) {
+ return Vector<T>(NewArray<T>(length), length);
+ }
+
+ // Returns a vector using the same backing storage as this one,
+ // spanning from and including 'from', to but not including 'to'.
+ Vector<T> SubVector(int from, int to) {
+ SLOW_ASSERT(to <= length_);
+ SLOW_ASSERT(from < to);
+ ASSERT(0 <= from);
+ return Vector<T>(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<T> Clone() const {
+ T* result = NewArray<T>(length_);
+ for (int i = 0; i < length_; i++) result[i] = start_[i];
+ return Vector<T>(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<T> operator+(int offset) {
+ ASSERT(offset < length_);
+ return Vector<T>(start_ + offset, length_ - offset);
+ }
+
+ // Factory method for creating empty vectors.
+ static Vector<T> empty() { return Vector<T>(NULL, 0); }
+
+ template<typename S>
+ static Vector<T> cast(Vector<S> input) {
+ return Vector<T>(reinterpret_cast<T*>(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<typename T>
class SetOncePointer {
@@ -425,14 +535,16 @@ class EmbeddedVector : public Vector<T> {
// When copying, make underlying Vector to reference our buffer.
EmbeddedVector(const EmbeddedVector& rhs)
: Vector<T>(rhs) {
- OS::MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize);
+ // TODO(jkummerow): Refactor #includes and use OS::MemCopy() instead.
+ memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
set_start(buffer_);
}
EmbeddedVector& operator=(const EmbeddedVector& rhs) {
if (this == &rhs) return *this;
Vector<T>::operator=(rhs);
- OS::MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize);
+ // TODO(jkummerow): Refactor #includes and use OS::MemCopy() instead.
+ memcpy(buffer_, rhs.buffer_, sizeof(T) * kSize);
this->set_start(buffer_);
return *this;
}
@@ -442,6 +554,44 @@ class EmbeddedVector : public Vector<T> {
};
+template <typename T>
+class ScopedVector : public Vector<T> {
+ public:
+ explicit ScopedVector(int length) : Vector<T>(NewArray<T>(length), length) { }
+ ~ScopedVector() {
+ DeleteArray(this->start());
+ }
+
+ private:
+ DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedVector);
+};
+
+#define STATIC_ASCII_VECTOR(x) \
+ v8::internal::Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(x), \
+ ARRAY_SIZE(x)-1)
+
+inline Vector<const char> CStrVector(const char* data) {
+ return Vector<const char>(data, StrLength(data));
+}
+
+inline Vector<const uint8_t> OneByteVector(const char* data, int length) {
+ return Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), length);
+}
+
+inline Vector<const uint8_t> OneByteVector(const char* data) {
+ return OneByteVector(data, StrLength(data));
+}
+
+inline Vector<char> MutableCStrVector(char* data) {
+ return Vector<char>(data, StrLength(data));
+}
+
+inline Vector<char> MutableCStrVector(char* data, int max) {
+ int length = StrLength(data);
+ return Vector<char>(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
@@ -769,7 +919,8 @@ struct BitCastHelper {
INLINE(static Dest cast(const Source& source)) {
Dest dest;
- OS::MemCopy(&dest, &source, sizeof(dest));
+ // TODO(jkummerow): Refactor #includes and use OS::MemCopy() instead.
+ memcpy(&dest, &source, sizeof(dest));
return dest;
}
};
« no previous file with comments | « src/platform.h ('k') | src/vector.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698