Heap类中有一个Object* root_[],这个数组是在Heap::CreateHeapObjects方法中初始化的,root_数组非常重要,其中包含了各种类的Map对象,和其他非常重要的全局对象。对于root_数组的存取,Heap一以贯之的使用了宏定义的方式
1.root_数组的索引
enum RootListIndex {
#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
#undef ROOT_INDEX_DECLARATION
#define STRING_INDEX_DECLARATION(name, str) k##name##RootIndex,
INTERNALIZED_STRING_LIST(STRING_INDEX_DECLARATION)
#undef STRING_DECLARATION
// Utility type maps
#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
STRUCT_LIST(DECLARE_STRUCT_MAP)
#undef DECLARE_STRUCT_MAP
kStringTableRootIndex,
kStrongRootListLength = kStringTableRootIndex,
kRootListLength
};
#define ROOT_INDEX_DECLARATION(type, name, camel_name) k##camel_name##RootIndex,
STRONG_ROOT_LIST(ROOT_INDEX_DECLARATION)
#undef ROOT_INDEX_DECLARATION
#define STRING_INDEX_DECLARATION(name, str) k##name##RootIndex,
INTERNALIZED_STRING_LIST(STRING_INDEX_DECLARATION)
#undef STRING_DECLARATION
// Utility type maps
#define DECLARE_STRUCT_MAP(NAME, Name, name) k##Name##MapRootIndex,
STRUCT_LIST(DECLARE_STRUCT_MAP)
#undef DECLARE_STRUCT_MAP
kStringTableRootIndex,
kStrongRootListLength = kStringTableRootIndex,
kRootListLength
};
这里定义了设置获取root_数组所需要的index,它是一个枚举类型
2.root_数组中对象的获取
从上面index的定义,我们可以看到Index被分为三种,分别由STRONG_ROOT_LIST,INTERNALIZED_STRING_LIST和STRUCT_LIST定义的。
我们看如下的宏定义:
#define ROOT_ACCESSOR(type, name, camel_name) \
type* name() { \
return type::cast(roots_[k##camel_name##RootIndex]); \
} \
type* raw_unchecked_##name() { \
return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
}
type* name() { \
return type::cast(roots_[k##camel_name##RootIndex]); \
} \
type* raw_unchecked_##name() { \
return reinterpret_cast<type*>(roots_[k##camel_name##RootIndex]); \
}
ROOT_LIST(ROOT_ACCESSOR)
#define STRUCT_MAP_ACCESSOR(NAME, Name, name) \
Map* name##_map() { \
return Map::cast(roots_[k##Name##MapRootIndex]); \
}
Map* name##_map() { \
return Map::cast(roots_[k##Name##MapRootIndex]); \
}
STRUCT_LIST(STRUCT_MAP_ACCESSOR)
#define STRING_ACCESSOR(name, str) String* name() { \
return String::cast(roots_[k##name##RootIndex]); \
}
return String::cast(roots_[k##name##RootIndex]); \
}
INTERNALIZED_STRING_LIST(STRING_ACCESSOR)
这三个宏定义了getter函数,用于根据类型和名称获取root中相应的对象,分别对应上面的三种不同类型的index。
这三个宏定义了getter函数,用于根据类型和名称获取root中相应的对象,分别对应上面的三种不同类型的index。
3.root_数组中对象的设置
#define ROOT_ACCESSOR(type, name, camel_name) \
inline void set_##name(type* value) { \
/* The deserializer makes use of the fact that these common roots are */ \
/* never in new space and never on a page that is being compacted. */ \
ASSERT(k##camel_name##RootIndex >= kOldSpaceRoots || !InNewSpace(value)); \
roots_[k##camel_name##RootIndex] = value; \
}
ROOT_LIST(ROOT_ACCESSOR)
inline void set_##name(type* value) { \
/* The deserializer makes use of the fact that these common roots are */ \
/* never in new space and never on a page that is being compacted. */ \
ASSERT(k##camel_name##RootIndex >= kOldSpaceRoots || !InNewSpace(value)); \
roots_[k##camel_name##RootIndex] = value; \
}
ROOT_LIST(ROOT_ACCESSOR)
这个宏显然定义了setter函数,用于设置root_数组中的一些索引的内容。奇怪的是,这里少了上面的两个宏STRUCT_LIST,INTERNALIZED_STRING_LIST,这说明,由这两个宏定义index的root_数组对象是不可写的,相应的,由ROOT_LIST定义index的root_数组对象是可写的。
4.列表宏
这三个宏的定义如下:
heap.h中定义如下两个宏
#define STRONG_ROOT_LIST(V) \
V(Map, byte_array_map, ByteArrayMap) \
V(Map, free_space_map, FreeSpaceMap) \
V(Map, one_pointer_filler_map, OnePointerFillerMap) \
V(Map, two_pointer_filler_map, TwoPointerFillerMap) \
/* Cluster the most popular ones in a few cache lines here at the top. */ \
V(Smi, store_buffer_top, StoreBufferTop) \
V(Oddball, undefined_value, UndefinedValue) \
V(Oddball, the_hole_value, TheHoleValue) \
V(Oddball, null_value, NullValue) \
V(Oddball, true_value, TrueValue) \
V(Oddball, false_value, FalseValue) \
V(Map, global_property_cell_map, GlobalPropertyCellMap) \
V(Map, shared_function_info_map, SharedFunctionInfoMap) \
V(Map, meta_map, MetaMap) \
V(Map, heap_number_map, HeapNumberMap) \
V(Map, native_context_map, NativeContextMap) \
V(Map, fixed_array_map, FixedArrayMap) \
V(M
V(Map, byte_array_map, ByteArrayMap) \
V(Map, free_space_map, FreeSpaceMap) \
V(Map, one_pointer_filler_map, OnePointerFillerMap) \
V(Map, two_pointer_filler_map, TwoPointerFillerMap) \
/* Cluster the most popular ones in a few cache lines here at the top. */ \
V(Smi, store_buffer_top, StoreBufferTop) \
V(Oddball, undefined_value, UndefinedValue) \
V(Oddball, the_hole_value, TheHoleValue) \
V(Oddball, null_value, NullValue) \
V(Oddball, true_value, TrueValue) \
V(Oddball, false_value, FalseValue) \
V(Map, global_property_cell_map, GlobalPropertyCellMap) \
V(Map, shared_function_info_map, SharedFunctionInfoMap) \
V(Map, meta_map, MetaMap) \
V(Map, heap_number_map, HeapNumberMap) \
V(Map, native_context_map, NativeContextMap) \
V(Map, fixed_array_map, FixedArrayMap) \
V(M