前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js引擎v8源码解析之list(基于0.1.5)

js引擎v8源码解析之list(基于0.1.5)

作者头像
theanarkh
发布2019-07-30 18:28:57
6540
发布2019-07-30 18:28:57
举报
文章被收录于专栏:原创分享原创分享
代码语言:javascript
复制
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_LIST_H_
#define V8_LIST_H_

namespace v8 { namespace internal {


// ----------------------------------------------------------------------------
// The list is a template for very light-weight lists. We are not
// using the STL because we want full control over space and speed of
// the code. This implementation is based on code by Robert Griesemer
// and Rob Pike.
//
// The list is parameterized by the type of its elements (T) and by an
// allocation policy (P). The policy is used for allocating lists in
// the C free store or the zone; see zone.h.

// Forward defined as
// template <typename T, class P = FreeStoreAllocationPolicy> class List;
template <typename T, class P>
class List {
 public:
  // 构造函数,申请一块内存
  INLINE(explicit List(int capacity)) { Initialize(capacity); }
  // 析构函数,是否内存
  INLINE(~List()) { DeleteData(data_); }
  // 分配内存,P是内存管理的类,如FreeStoreAllocationPolicy,见allocation.h
  INLINE(void* operator new(size_t size)) { return P::New(size); }
  // 释放内存
  INLINE(void operator delete(void* p, size_t)) { return P::Delete(p); }
  // 重载[]运算符,返回对应索引的元素
  inline T& operator[](int i) const  {
    ASSERT(0 <= i && i < length_);
    return data_[i];
  }
  // 取某个索引对应的元素
  inline T& at(int i) const  { return this->operator[](i); }
  // 取最后一个元素
  INLINE(const T& last() const)  {
    ASSERT(!is_empty());
    return this->at(length_ - 1);
  }
  // 判空
  INLINE(bool is_empty() const) { return length_ == 0; }
  // 容量
  INLINE(int length() const) { return length_; }
  // 转成Vector,见utils.h
  Vector<T> ToVector() { return Vector<T>(data_, length_); }

  // Adds a copy of the given 'element' to the end of the list,
  // expanding the list if necessary.
  // 追加元素
  T& Add(const T& element);

  // Added 'count' elements with the value 'value' and returns a
  // vector that allows access to the elements.  The vector is valid
  // until the next change is made to this list.
  Vector<T> AddBlock(const T& value, int count);

  // Removes the i'th element without deleting it even if T is a
  // pointer type; moves all elements above i "down". Returns the
  // removed element.
  T Remove(int i);

  // Removes the last element without deleting it even if T is a
  // pointer type. Returns the removed element.
  INLINE(T RemoveLast()) { return Remove(length_ - 1); }

  // Clears the list by setting the length to zero. Even if T is a
  // pointer type, clearing the list doesn't delete the entries.
  INLINE(void Clear());

  // Drops all but the first 'pos' elements from the list.
  INLINE(void Rewind(int pos));

  // Iterate through all list entries, starting at index 0.
  void Iterate(void (*callback)(T* x));

  // Sort all list entries (using QuickSort)
  void Sort(int (*cmp)(const T* x, const T* y));

  INLINE(void Initialize(int capacity));

 private:
  // 指向数组的首地址
  T* data_;
  // 最多可以存储多少个元素
  int capacity_;
  // 已经存储了多少个元素
  int length_;

  INLINE(T* NewData(int n))  { return static_cast<T*>(P::New(n * sizeof(T))); }
  INLINE(void DeleteData(T* data))  { P::Delete(data); }

  DISALLOW_EVIL_CONSTRUCTORS(List);
};


} }  // namespace v8::internal

#endif  // V8_LIST_H_

list-inl

代码语言:javascript
复制

#ifndef V8_LIST_INL_H_
#define V8_LIST_INL_H_

#include "list.h"

namespace v8 { namespace internal {


template<typename T, class P>
T& List<T, P>::Add(const T& element) {
  // 没有足够的空间了,重新申请一块新的更大的内存
  if (length_ >= capacity_) {
    // Grow the list capacity by 50%, but make sure to let it grow
    // even when the capacity is zero (possible initial case).
    // 多扩展50% + 1的空间,加一是兼容capacity_为0的情况
    int new_capacity = 1 + capacity_ + (capacity_ >> 1);
    // 申请一块内存
    T* new_data = NewData(new_capacity);
    // 把原来的复制过去
    memcpy(new_data, data_, capacity_ * sizeof(T));
    // 删除原来的
    DeleteData(data_);
    // 更新属性
    data_ = new_data;
    capacity_ = new_capacity;
  }
  // 赋值
  return data_[length_++] = element;
}


template<typename T, class P>
// 新增count个element元素到list,转成Vector返回
Vector<T> List<T, P>::AddBlock(const T& element, int count) {
  int start = length_;
  for (int i = 0; i < count; i++)
    Add(element);
  return Vector<T>(&data_[start], count);
}


template<typename T, class P>
// 删除某个元素,后续的元素补位
T List<T, P>::Remove(int i) {
  // 取出i对应的元素
  T element = at(i);
  // 长度减一
  length_--;
  // 如果删除的不是最后一个元素,则i后面的元素要往前补位
  while (i < length_) {
    data_[i] = data_[i + 1];
    i++;
  }
  return element;
}


template<typename T, class P>
// 清空list,释放内存
void List<T, P>::Clear() {
  DeleteData(data_);
  Initialize(0);
}

// 改变list的长度 
template<typename T, class P>
void List<T, P>::Rewind(int pos) {
  length_ = pos;
}

// 迭代list
template<typename T, class P>
void List<T, P>::Iterate(void (*callback)(T* x)) {
  for (int i = 0; i < length_; i++) callback(&data_[i]);
}

// 排序
template<typename T, class P>
void List<T, P>::Sort(int (*cmp)(const T* x, const T* y)) {
  qsort(data_,
        length_,
        sizeof(T),
        reinterpret_cast<int (*)(const void*, const void*)>(cmp));
#ifdef DEBUG
  for (int i = 1; i < length_; i++)
    ASSERT(cmp(&data_[i - 1], &data_[i]) <= 0);
#endif
}


template<typename T, class P>
void List<T, P>::Initialize(int capacity) {
  ASSERT(capacity >= 0);
  // 分配一块内存
  data_ = (capacity > 0) ? NewData(capacity) : NULL;
  // 容量
  capacity_ = capacity;
  // 已分配元素个数
  length_ = 0;
}


} }  // namespace v8::internal

#endif  // V8_LIST_INL_H_
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 编程杂技 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档