指针转换
今天在看hotspot代码时,有处没能看明白,于是请教青老师,给出了解答是指针转换
的语法
share/vm/memory/defNewGeneration.cpp DefNewGeneration::collect
share/vm/memory/genOopClosures.inline.hpp FastScanClosure::do_oop_work
share/vm/memory/defNewGeneration.cpp DefNewGeneration::copy_to_survivor_space
share/vm/oops/oop.inline.hpp forward_to::forward_to
markOop.hpp
src/share/vm/oops/markOop.hpp
// Prepare address of oop for placement into mark
inline static markOop encode_pointer_as_mark(void* p) { return markOop(p)->set_marked(); }
就是上面markOop(p) 这个写法当时没看懂,他不是什么方法调用,也不是什么构造函数,而是指针转换。
青老师给我写了一小段demo如下:
#include<iostream>
using namespace std;
struct A {
int a;
int b;
};
typedef A* APtr;
int main(){
A a;
a.a=10;
a.b=20;
void *q = (void*)&a;
APtr p = APtr(q); // 指针转换
cout<<p->a<<","<<p->b<<endl;
}