v8 handlescope的机制

V8引擎中的HandleScope负责管理对象的引用,不存储对象内容。它使用HandleScopeData对象,通过Isolate的block数组动态扩展内存。HandleScope形成链表,析构时关闭作用域并释放引用,而EscopeHandleScope则用于保留关键对象,提供一种临时对象管理的方式。

HandleScope

成员

HandleScope类重要的3个成员

internal::Isolate* isolate_;

intieral::Object** prev_next_;

internal::Object** prev_limit_;

HandleScope::CreateHandle
Object** HandleScope::CreateHandle(Isolate* isolate, Object* value) {
  DCHECK(AllowHandleAllocation::IsAllowed());
  HandleScopeData* data = isolate->handle_scope_data();

  Object** result = data->next;
  if (result == data->limit) result = Extend(isolate);
  // Update the current next field, set the value in the created
  // handle, and return the result.
  DCHECK(result < data->limit);
  data->next = result + 1;                                                                                                                                                                                                               

  *result = value;
  return result;
}

HandleScopeData对象,是一个struct

                                                                                                                                                                                                                                         
struct HandleScopeData final {                                                                                      
  Object** next;                                                                                                    
  Object** limit;                                                                                                   
  int level;                                                                                                        
  int sealed_level;                                                                                                 
  CanonicalHandleScope* canonical_scope;                                                                            
                                                                                                                    
  void Initialize() {                                                                                               
    next = limit = nullptr;                                                                                         
    sealed_level = level = 0;                                                                                       
    canonical_scope = nullptr;                                                                                      
  }                                                                                                                 
}; 

data的next保存者被管理的对象。

HandleScopeData对象是被internal::Isolate的handle_scope_data_对象获取的。

HandleScope是管理对象的引用,不是保存对象的内容。

结构图:
在这里插入图片描述

Isolate管理了一个Object** 的block数组。每个Block组占用(4/8)KB的大小(一个或者两个页面),这是每次不够用时的扩展一个block。当前的block可能是最后一个block或者倒数第二block。这样做的目的是为了避免过多的block没有被释放。
Isolate的HandleScopeData对象,是指向当前Block组内的当前的下个free的slot,以及当前block的limit。这样,当next == limit的时候,就会请求一个新的block。每次请求都是取block数组的最后一个block。如果最后一个block就是当前的block,那么就会分配新的block,并加入到数组中。
HandleScope是为了记录上次HandleScopeData所在的next和limit指针。这里是模拟了堆栈。HandleScope相当于保存了上个函数的堆栈指针。
当HandleScope析构的时候,会调用HandleScope::CloseScope方法,将当前的slot都丢弃掉(这样相当于删除了本地引用,GC就会认为这些对象不再被引用了),也会删除那些没有任何有效数据的block。
HandleScope会形成一个链表,直到最上层的一个scope。

EscopeHandleScope

EscopeHandleScope继承自HandleScope。

它只能Escope一个value值。它的原理是这样的:

  • 在初始化HandleScope(它的父类)数据之前,先获取一个escape_slot_,这个solt是建立在它的上层的HandleScope上的。
  • 然后他建立一个新的HandleScope
  • 最后调用Escape的时候,将需要 escape的值填充到escape_slot_内,即把这个值放在上层HandleScope上。
    其余的Local 对象都被丢弃了。

EscopeHandleScope显然是一个类似语法糖的存在。目的是为了能够调用一个C++函数,将临时分配的对象统统扔掉,只保留关键的对象。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值