ART虚拟机 _ JNI静态注册和动态注册,一线互联网架构师Android框架体系架构

return version != JNI_VERSION_1_2 && version != JNI_VERSION_1_4 && version != JNI_VERSION_1_6;
}

JNI_Onload函数是我们进行动态注册的宝地,在其中调用RegisterNatives即可进行注册。具体的方法和细节留到"动态注册"章节阐述。

虚拟机中的native方法

通常而言,一个Java方法在虚拟机中可以找到两种执行方式,一种是解释执行,另一种是机器码执行。解释执行时,解释器会去寻找字节码的入口地址。而机器码执行时,虚拟机会去寻找机器指令的入口地址。考虑到每个Java方法在虚拟机中都由ArtMethod对象表示,字节码的入口信息(间接)存在其data_字段中,而机器码入口信息则存在entry_point_from_quick_compiled_code_字段中。如下所示。

// Must be the last fields in the method.
struct PtrSizedFields {
// Depending on the method type, the data is
// - native method: pointer to the JNI function registered to this method
// or a function to resolve the JNI function,
// - resolution method: pointer to a function to resolve the method and
// the JNI function for @CriticalNative.
// - conflict method: ImtConflictTable,
// - abstract/interface method: the single-implementation if any,
// - proxy method: the original interface method or constructor,
// - other methods: during AOT the code item offset, at runtime a pointer
// to the code item.
void* data_;

// Method dispatch from quick compiled code invokes this pointer which may cause bridging into
// the interpreter.
void* entry_point_from_quick_compiled_code_;
} ptr_sized_fields_;

不过对于native方法而言,它在Java世界中只有定义没有实现,因此不会有字节码信息。虽然不需要存储字节码的入口地址,但native方法在调用过程中却会多出一步。 JNI跳板函数.png

  • 首先进入一个跳板函数,其中会处理Java参数到Native参数的转换以及线程状态切换等过程。
  • 在跳板函数内部调用Native世界中实现的JNI函数。

这样一来,不用存储字节码入口信息的data_字段就可以用来存储JNI函数的入口地址了。而entry_point_from_quick_compiled_code_中存储的就是跳板函数的入口地址。具体可参考ART视角 | 为什么调用Native方法可以进入C++世界

静态注册

当我们不在J

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值