00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include "hprof.h"
00057
00058 typedef struct {
00059 jobject globalref;
00060 ObjectIndex object_index;
00061 } LoaderInfo;
00062
00063 static LoaderInfo *
00064 get_info(LoaderIndex index)
00065 {
00066 return (LoaderInfo*)table_get_info(gdata->loader_table, index);
00067 }
00068
00069 static void
00070 delete_globalref(JNIEnv *env, LoaderInfo *info)
00071 {
00072 jobject ref;
00073
00074 HPROF_ASSERT(env!=NULL);
00075 HPROF_ASSERT(info!=NULL);
00076 ref = info->globalref;
00077 info->globalref = NULL;
00078 if ( ref != NULL ) {
00079 deleteWeakGlobalReference(env, ref);
00080 }
00081 info->object_index = 0;
00082 }
00083
00084 static void
00085 cleanup_item(TableIndex index, void *key_ptr, int key_len,
00086 void *info_ptr, void *arg)
00087 {
00088 }
00089
00090 static void
00091 delete_ref_item(TableIndex index, void *key_ptr, int key_len,
00092 void *info_ptr, void *arg)
00093 {
00094 delete_globalref((JNIEnv*)arg, (LoaderInfo*)info_ptr);
00095 }
00096
00097 static void
00098 list_item(TableIndex index, void *key_ptr, int key_len,
00099 void *info_ptr, void *arg)
00100 {
00101 LoaderInfo *info;
00102
00103 HPROF_ASSERT(info_ptr!=NULL);
00104
00105 info = (LoaderInfo*)info_ptr;
00106 debug_message( "Loader 0x%08x: globalref=%p, object_index=%d\n",
00107 index, (void*)info->globalref, info->object_index);
00108 }
00109
00110 static void
00111 free_entry(JNIEnv *env, LoaderIndex index)
00112 {
00113 LoaderInfo *info;
00114
00115 info = get_info(index);
00116 delete_globalref(env, info);
00117 table_free_entry(gdata->loader_table, index);
00118 }
00119
00120 typedef struct SearchData {
00121 JNIEnv *env;
00122 jobject loader;
00123 LoaderIndex found;
00124 } SearchData;
00125
00126 static void
00127 search_item(TableIndex index, void *key_ptr, int key_len, void *info_ptr, void *arg)
00128 {
00129 LoaderInfo *info;
00130 SearchData *data;
00131
00132 HPROF_ASSERT(info_ptr!=NULL);
00133 HPROF_ASSERT(arg!=NULL);
00134 info = (LoaderInfo*)info_ptr;
00135 data = (SearchData*)arg;
00136 if ( data->loader == info->globalref ) {
00137
00138 HPROF_ASSERT(data->found==0);
00139 data->found = index;
00140 } else if ( data->env != NULL && data->loader != NULL &&
00141 info->globalref != NULL ) {
00142 jobject lref;
00143
00144 lref = newLocalReference(data->env, info->globalref);
00145 if ( lref == NULL ) {
00146
00147 free_entry(data->env, index);
00148 } else if ( isSameObject(data->env, data->loader, lref) ) {
00149 HPROF_ASSERT(data->found==0);
00150 data->found = index;
00151 }
00152 if ( lref != NULL ) {
00153 deleteLocalReference(data->env, lref);
00154 }
00155 }
00156
00157 }
00158
00159 static LoaderIndex
00160 search(JNIEnv *env, jobject loader)
00161 {
00162 SearchData data;
00163
00164 data.env = env;
00165 data.loader = loader;
00166 data.found = 0;
00167 table_walk_items(gdata->loader_table, &search_item, (void*)&data);
00168 return data.found;
00169 }
00170
00171 LoaderIndex
00172 loader_find_or_create(JNIEnv *env, jobject loader)
00173 {
00174 LoaderIndex index;
00175
00176
00177 if ( loader==NULL && gdata->system_loader != 0 ) {
00178 return gdata->system_loader;
00179 }
00180 if ( loader==NULL ) {
00181 env = NULL;
00182 }
00183 index = search(env, loader);
00184 if ( index == 0 ) {
00185 static LoaderInfo empty_info;
00186 LoaderInfo info;
00187
00188 info = empty_info;
00189 if ( loader != NULL ) {
00190 HPROF_ASSERT(env!=NULL);
00191 info.globalref = newWeakGlobalReference(env, loader);
00192 info.object_index = 0;
00193 }
00194 index = table_create_entry(gdata->loader_table, NULL, 0, (void*)&info);
00195 }
00196 HPROF_ASSERT(search(env,loader)==index);
00197
00198 if ( loader==NULL && gdata->system_loader == 0 ) {
00199 gdata->system_loader = index;
00200 }
00201 return index;
00202 }
00203
00204 void
00205 loader_init(void)
00206 {
00207 gdata->loader_table = table_initialize("Loader",
00208 16, 16, 0, (int)sizeof(LoaderInfo));
00209 }
00210
00211 void
00212 loader_list(void)
00213 {
00214 debug_message(
00215 "--------------------- Loader Table ------------------------\n");
00216 table_walk_items(gdata->loader_table, &list_item, NULL);
00217 debug_message(
00218 "----------------------------------------------------------\n");
00219 }
00220
00221 void
00222 loader_cleanup(void)
00223 {
00224 table_cleanup(gdata->loader_table, &cleanup_item, NULL);
00225 gdata->loader_table = NULL;
00226 }
00227
00228 void
00229 loader_delete_global_references(JNIEnv *env)
00230 {
00231 table_walk_items(gdata->loader_table, &delete_ref_item, (void*)env);
00232 }
00233
00234
00235 ObjectIndex
00236 loader_object_index(JNIEnv *env, LoaderIndex index)
00237 {
00238 LoaderInfo *info;
00239 ObjectIndex object_index;
00240 jobject wref;
00241
00242
00243 info = get_info(index);
00244 object_index = info->object_index;
00245 wref = info->globalref;
00246 if ( wref != NULL && object_index == 0 ) {
00247 jobject lref;
00248
00249 object_index = 0;
00250 lref = newLocalReference(env, wref);
00251 if ( lref != NULL && !isSameObject(env, lref, NULL) ) {
00252 jlong tag;
00253
00254
00255 tag = getTag(lref);
00256 if ( tag != (jlong)0 ) {
00257 object_index = tag_extract(tag);
00258 }
00259 }
00260 if ( lref != NULL ) {
00261 deleteLocalReference(env, lref);
00262 }
00263 info->object_index = object_index;
00264 }
00265 return object_index;
00266 }
00267