Index: vm/vmcore/include/object_handles.h =================================================================== --- vm/vmcore/include/object_handles.h (revision 601284) +++ vm/vmcore/include/object_handles.h (working copy) @@ -160,6 +160,8 @@ VMEXPORT // temporary solution for interpreter unplug ObjectHandle oh_allocate_local_handle(); +ObjectHandle oh_allocate_local_handle_from_jni(); + ObjectHandle oh_convert_to_local_handle(ManagedObject* pointer); ObjectHandle oh_copy_to_local_handle(ObjectHandle oh); Index: vm/vmcore/src/jni/jni_array.cpp =================================================================== --- vm/vmcore/src/jni/jni_array.cpp (revision 601284) +++ vm/vmcore/src/jni/jni_array.cpp (working copy) @@ -126,7 +126,11 @@ REFS_RUNTIME_SWITCH_ENDIF } - ObjectHandle new_handle = oh_allocate_local_handle(); + ObjectHandle new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } new_handle->object = (ManagedObject*)vector; tmn_suspend_enable(); //---------------------------------^ @@ -160,7 +164,11 @@ ManagedObject *val = get_raw_reference_pointer((ManagedObject **)addr); ObjectHandle new_handle = NULL; if (val != NULL) { - new_handle = oh_allocate_local_handle(); + new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } new_handle->object = val; } @@ -250,7 +258,11 @@ clss->instance_allocated(sz); #endif //VM_STATS - ObjectHandle h = oh_allocate_local_handle(); + ObjectHandle h = oh_allocate_local_handle_from_jni(); + if (h == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } h->object = (ManagedObject *)array; tmn_suspend_enable(); //---------------------------------^ Index: vm/vmcore/src/jni/jni.cpp =================================================================== --- vm/vmcore/src/jni/jni.cpp (revision 601284) +++ vm/vmcore/src/jni/jni.cpp (working copy) @@ -872,15 +872,14 @@ Global_Env * vm_env = jni_get_vm_env(jni_env); if (exn_raised() || ref == NULL) return NULL; - jobject new_ref = oh_copy_to_local_handle(ref); + tmn_suspend_disable(); + jobject new_ref = oh_allocate_local_handle_from_jni(); - if (NULL == new_ref) { - exn_raise_object(vm_env->java_lang_OutOfMemoryError); - return NULL; + if (NULL != new_ref) { + new_ref->object = ref->object; + TRACE2("jni", "NewLocalRef class = " << jobject_to_struct_Class(new_ref)); } - - TRACE2("jni", "NewLocalRef class = " << jobject_to_struct_Class(new_ref)); - + tmn_suspend_enable(); return new_ref; } //NewLocalRef @@ -980,7 +979,11 @@ tmn_suspend_enable(); //---------------------------------^ return NULL; } - ObjectHandle new_handle = oh_allocate_local_handle(); + ObjectHandle new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } new_handle->object = (ManagedObject *)new_obj; tmn_suspend_enable(); //---------------------------------^ @@ -1044,7 +1047,11 @@ tmn_suspend_disable(); //---------------------------------v - ObjectHandle new_handle = oh_allocate_local_handle(); + ObjectHandle new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } ManagedObject *jlo = h->object; assert(jlo); assert(jlo->vt()); @@ -1084,7 +1091,11 @@ tmn_suspend_disable(); //---------------------------------v - ObjectHandle new_handle = oh_allocate_local_handle(); + ObjectHandle new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } ManagedObject *jlo = h->object; assert(jlo); assert(jlo->vt()); Index: vm/vmcore/src/jni/jni_utils.cpp =================================================================== --- vm/vmcore/src/jni/jni_utils.cpp (revision 601284) +++ vm/vmcore/src/jni/jni_utils.cpp (working copy) @@ -64,8 +64,10 @@ hythread_suspend_enable(); return NULL; } - ObjectHandle res = oh_allocate_local_handle(); - res->object = obj; + ObjectHandle res = oh_allocate_local_handle_from_jni(); + if (res) { + res->object = obj; + } hythread_suspend_enable(); return (jobject)res; } @@ -756,13 +758,17 @@ jobject create_default_instance(Class* clss) { hythread_suspend_disable(); - jobject h = oh_allocate_local_handle(); ManagedObject *new_obj = class_alloc_new_object_and_run_default_constructor(clss); if (new_obj == NULL) { hythread_suspend_enable(); assert(exn_raised()); return NULL; } + jobject h = oh_allocate_local_handle_from_jni(); + if (h == NULL) { + hythread_suspend_enable(); + return NULL; + } h->object = new_obj; hythread_suspend_enable(); return h; Index: vm/vmcore/src/jni/jni_field.cpp =================================================================== --- vm/vmcore/src/jni/jni_field.cpp (revision 601284) +++ vm/vmcore/src/jni/jni_field.cpp (working copy) @@ -126,7 +126,11 @@ ManagedObject *val = get_raw_reference_pointer(field_addr); ObjectHandle new_handle = NULL; if (val != NULL) { - new_handle = oh_allocate_local_handle(); + new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle == NULL) { + tmn_suspend_enable(); //---------------------------------^ + return NULL; + } new_handle->object = val; } @@ -777,8 +781,10 @@ // compress static fields. ManagedObject *val = get_raw_reference_pointer(field_addr); if (val != NULL) { - new_handle = oh_allocate_local_handle(); - new_handle->object = val; + new_handle = oh_allocate_local_handle_from_jni(); + if (new_handle != NULL) { + new_handle->object = val; + } } else { new_handle = NULL; } Index: vm/vmcore/src/object/object_handles.cpp =================================================================== --- vm/vmcore/src/object/object_handles.cpp (revision 601284) +++ vm/vmcore/src/object/object_handles.cpp (working copy) @@ -25,6 +25,7 @@ #include #include "environment.h" +#include "exceptions.h" #include "open/gc.h" #include "lil.h" #include "lock_manager.h" @@ -278,6 +279,11 @@ unsigned capacity = 10; unsigned size = sizeof(ObjectHandlesNew)+sizeof(ManagedObject*)*(capacity-1); ObjectHandlesNew* n = (ObjectHandlesNew*)STD_MALLOC(size); + + if (n == NULL) { + return NULL; + } + assert(n); memset(n, 0, size); #ifdef _IPF_ @@ -291,14 +297,22 @@ return n; } -ObjectHandle oh_allocate_handle(ObjectHandles** hs) +static ObjectHandle oh_allocate_handle(ObjectHandles** hs) { // the function should be called only from suspend disabled mode // as it is not gc safe. assert(!hythread_is_suspend_enabled()); ObjectHandlesNew* cur = (ObjectHandlesNew*)*hs; - if (!cur || cur->size>=cur->capacity) - cur = oh_add_new_handles((ObjectHandlesNew**)hs); + + if (!cur || cur->size>=cur->capacity) { + ObjectHandlesNew* new_handle_block = oh_add_new_handles((ObjectHandlesNew**)hs); + + if (new_handle_block == NULL) { + return NULL; + } + assert(new_handle_block); + cur = new_handle_block; + } ObjectHandle h = (ObjectHandle)&cur->refs[cur->size]; cur->size++; h->object = NULL; @@ -346,7 +360,9 @@ ObjectHandle NativeObjectHandles::allocate() { - return oh_allocate_handle(&handles); + ObjectHandle res = oh_allocate_handle(&handles); + assert(res); + return res; } void NativeObjectHandles::enumerate() @@ -359,8 +375,7 @@ ////////////////////////////////////////////////////////////////////////// // Local Handles -VMEXPORT // temporary solution for interpreter unplug -ObjectHandle oh_allocate_local_handle() +static ObjectHandle oh_allocate_local_handle_internal() { assert(!hythread_is_suspend_enabled()); @@ -391,6 +406,23 @@ return res; } +VMEXPORT // temporary solution for interpreter unplug +ObjectHandle oh_allocate_local_handle() { + ObjectHandle res = oh_allocate_local_handle_internal(); + assert(res); + return res; +} + +ObjectHandle oh_allocate_local_handle_from_jni() { + ObjectHandle res = oh_allocate_local_handle_internal(); + + if (res == NULL) { + exn_raise_object(VM_Global_State::loader_env->java_lang_OutOfMemoryError); + } + return res; +} + + ObjectHandle oh_convert_to_local_handle(ManagedObject* pointer) { assert(!hythread_is_suspend_enabled()); assert(pointer);