/*found a match, try to load it */

clazz =loadClassFromDex(pDvmDex, pClassDef, loader);

这行代码是已经发现合适的类,并且找到相应的Dex文件时,就可以调用函数loadClassFromDex来加载类到内存,并准备好可以运行的状态。


if(dvmCheckException(self)) {

/*class was found but had issues */

dvmReleaseTrackedAlloc((Object*)clazz, NULL);

gotobail;

}

这段代码是当从Dex文件时加载类出错时返回。



/*

* Lock the class while welink it so other threads must wait for us

* to finish. Set the"initThreadId" so we can identify recursive

* invocation.

*/

dvmLockObject(self,(Object*)clazz);

clazz->initThreadId= self->threadId;

这段代码是锁住别的线程访问,然后设置这个类的初始化线程的ID,以便防止递归调用时可以判断出来。



/*

* Add to hash table solookups succeed.

*

* [Are circularreferences possible when linking a class?]

*/

下面这段代码是添加新加载的类到hash表里,以便后面查找快速。


assert(clazz->classLoader== loader);

if(!dvmAddClassToHash(clazz)) {

这行代码是调用dvmAddClassToHash函数来添加新加载的类clazzhash也就是保存在gDvm.loadedClasses。但是添加到hash表里,也不是每次成功的,因为加载类是多线程进行的,可能同一个类在不同的线程里加载了,另一个线程加载就不成功,因此dvmAddClassToHash函数会返回插入不成功,这时就需要进行下面的处理了。


/*

* Another thread musthave loaded the class after we

* started but beforewe finished. Discard what we've

* done and leave somehints for the GC.

*

* (Yes, thishappens.)

*/

//LOGW("WOW:somebody loaded %s simultaneously\n", descriptor);

clazz->initThreadId= 0;

dvmUnlockObject(self,(Object*)clazz);

这段代码是发现已经有别的线程加载同一个类,就设置这个类初始化的线程为空,然后释放这个线程锁。


/*Let the GC free the class.

*/

assert(clazz->obj.clazz== gDvm.unlinkedJavaLangClass);

dvmReleaseTrackedAlloc((Object*)clazz, NULL);

这段代码释放分配类占用的内存。


/*Grab the winning class.

*/

clazz =dvmLookupClass(descriptor, loader, true);

assert(clazz != NULL);

gotogot_class;

这段代码是从已经加载的类引用一份就可以返回给调用函数了。


}

更多相关文章

  1. 使用android SharedPreference判断程序是否第一次运行
  2. Android代码混淆proguard技…
  3. 一段漂亮的Fragment hide,show相关的代码
  4. android 周报 - 2019/10/6-Issue #382
  5. Android Studio 快捷键
  6. 【Android性能优化】Android图片加载方案--Bitmap的内存管理和优
  7. 自定义ThreadPoolExecutor
  8. Android(安卓)webView 中loadData方法加载 带中文时出现乱码
  9. 【Android】说做就做:偶的头像华丽的漂浮在map上。。。

随机推荐

  1. Android(安卓)硬件加速的优缺点
  2. Android(安卓)Drawable 系列——ClipDraw
  3. android tools:
  4. LinearLayout(线性布局)
  5. android源码导入
  6. Android(安卓)UI测量、布局、绘制过程探
  7. android中的xml处理
  8. 2010.12.28(3)——— android alertDialo
  9. android bundle 对象 序列化
  10. Xamarin开发Android时Visual Studio 2012