OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_UTILS_H_ | 5 #ifndef V8_UTILS_H_ |
6 #define V8_UTILS_H_ | 6 #define V8_UTILS_H_ |
7 | 7 |
8 #include <limits.h> | 8 #include <limits.h> |
9 #include <stdlib.h> | 9 #include <stdlib.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 1579 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1590 #if defined(V8_TARGET_LITTLE_ENDIAN) | 1590 #if defined(V8_TARGET_LITTLE_ENDIAN) |
1591 WriteUnalignedValue<V>(p, value); | 1591 WriteUnalignedValue<V>(p, value); |
1592 #elif defined(V8_TARGET_BIG_ENDIAN) | 1592 #elif defined(V8_TARGET_BIG_ENDIAN) |
1593 byte* src = reinterpret_cast<byte*>(&value); | 1593 byte* src = reinterpret_cast<byte*>(&value); |
1594 byte* dst = reinterpret_cast<byte*>(p); | 1594 byte* dst = reinterpret_cast<byte*>(p); |
1595 for (size_t i = 0; i < sizeof(V); i++) { | 1595 for (size_t i = 0; i < sizeof(V); i++) { |
1596 dst[i] = src[sizeof(V) - i - 1]; | 1596 dst[i] = src[sizeof(V) - i - 1]; |
1597 } | 1597 } |
1598 #endif // V8_TARGET_LITTLE_ENDIAN | 1598 #endif // V8_TARGET_LITTLE_ENDIAN |
1599 } | 1599 } |
| 1600 |
| 1601 // Represents a linked list that threads through the nodes in the linked list. |
| 1602 // Entries in the list are pointers to nodes. The nodes need to have a T** |
| 1603 // next() method that returns the location where the next value is stored. |
| 1604 template <typename T> |
| 1605 class ThreadedList final { |
| 1606 public: |
| 1607 ThreadedList() : head_(nullptr), tail_(&head_) {} |
| 1608 void Add(T* v) { |
| 1609 DCHECK_NULL(*tail_); |
| 1610 DCHECK_NULL(*v->next()); |
| 1611 *tail_ = v; |
| 1612 tail_ = v->next(); |
| 1613 } |
| 1614 |
| 1615 void Clear() { |
| 1616 head_ = nullptr; |
| 1617 tail_ = &head_; |
| 1618 } |
| 1619 |
| 1620 class Iterator final { |
| 1621 public: |
| 1622 Iterator& operator++() { |
| 1623 entry_ = (*entry_)->next(); |
| 1624 return *this; |
| 1625 } |
| 1626 bool operator!=(const Iterator& other) { return entry_ != other.entry_; } |
| 1627 T* operator*() { return *entry_; } |
| 1628 Iterator& operator=(T* entry) { |
| 1629 T* next = *(*entry_)->next(); |
| 1630 *entry->next() = next; |
| 1631 *entry_ = entry; |
| 1632 return *this; |
| 1633 } |
| 1634 |
| 1635 private: |
| 1636 explicit Iterator(T** entry) : entry_(entry) {} |
| 1637 |
| 1638 T** entry_; |
| 1639 |
| 1640 friend class ThreadedList; |
| 1641 }; |
| 1642 |
| 1643 Iterator begin() { return Iterator(&head_); } |
| 1644 Iterator end() { return Iterator(tail_); } |
| 1645 |
| 1646 void Rewind(Iterator reset_point) { |
| 1647 tail_ = reset_point.entry_; |
| 1648 *tail_ = nullptr; |
| 1649 } |
| 1650 |
| 1651 bool is_empty() const { return head_ == nullptr; } |
| 1652 |
| 1653 // Slow. For testing purposes. |
| 1654 int LengthForTest() { |
| 1655 int result = 0; |
| 1656 for (Iterator t = begin(); t != end(); ++t) ++result; |
| 1657 return result; |
| 1658 } |
| 1659 T* AtForTest(int i) { |
| 1660 Iterator t = begin(); |
| 1661 while (i-- > 0) ++t; |
| 1662 return *t; |
| 1663 } |
| 1664 |
| 1665 private: |
| 1666 T* head_; |
| 1667 T** tail_; |
| 1668 DISALLOW_COPY_AND_ASSIGN(ThreadedList); |
| 1669 }; |
| 1670 |
1600 } // namespace internal | 1671 } // namespace internal |
1601 } // namespace v8 | 1672 } // namespace v8 |
1602 | 1673 |
1603 #endif // V8_UTILS_H_ | 1674 #endif // V8_UTILS_H_ |
OLD | NEW |