hprof_util.c
来自「一个小公司要求给写的很简单的任务管理系统。」· C语言 代码 · 共 1,736 行 · 第 1/4 页
C
1,736 行
/* * @(#)hprof_util.c 1.57 06/01/28 * * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. *//* General utility functions. *//* * Wrappers over JVM, JNI, and JVMTI functions are placed here. * * All memory allocation and deallocation goes through jvmtiAllocate() * and jvmtiDeallocate(). * */#include "hprof.h"/* Macro to get JNI function pointer. */#define JNI_FUNC_PTR(env,f) (*((*(env))->f))/* Macro to get JVM function pointer. */#define JVM_FUNC_PTR(env,f) (*((*(env))->f))/* Macro to get JVMTI function pointer. */#define JVMTI_FUNC_PTR(env,f) (*((*(env))->f))/* ------------------------------------------------------------------- *//* JVM functions */JNIEnv *getEnv(void){ JNIEnv *env; jint res; res = JVM_FUNC_PTR(gdata->jvm,GetEnv) (gdata->jvm, (void **)&env, JNI_VERSION_1_2); if (res != JNI_OK) { char buf[256]; (void)md_snprintf(buf, sizeof(buf), "Unable to access JNI Version 1.2 (0x%x)," " is your JDK a 5.0 or newer version?" " JNIEnv's GetEnv() returned %d", JNI_VERSION_1_2, res); buf[sizeof(buf)-1] = 0; HPROF_ERROR(JNI_FALSE, buf); error_exit_process(1); /* Kill entire process, no core dump */ } return env;}/* ------------------------------------------------------------------- *//* Memory Allocation */void *jvmtiAllocate(int size){ jvmtiError error; unsigned char *ptr; HPROF_ASSERT(size>=0); ptr = NULL; if ( size == 0 ) { return ptr; } error = JVMTI_FUNC_PTR(gdata->jvmti,Allocate) (gdata->jvmti, (jlong)size, &ptr); if ( error != JVMTI_ERROR_NONE || ptr == NULL ) { HPROF_JVMTI_ERROR(error, "Cannot allocate jvmti memory"); } return (void*)ptr;}voidjvmtiDeallocate(void *ptr){ if ( ptr != NULL ) { jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,Deallocate) (gdata->jvmti, (unsigned char*)ptr); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot deallocate jvmti memory"); } }}#ifdef DEBUGvoid *hprof_debug_malloc(int size, char *file, int line){ void *ptr; HPROF_ASSERT(size>0); rawMonitorEnter(gdata->debug_malloc_lock); { ptr = debug_malloc(size, file, line); } rawMonitorExit(gdata->debug_malloc_lock); if ( ptr == NULL ) { HPROF_ERROR(JNI_TRUE, "Cannot allocate malloc memory"); } return ptr;}voidhprof_debug_free(void *ptr, char *file, int line){ HPROF_ASSERT(ptr!=NULL); rawMonitorEnter(gdata->debug_malloc_lock); { (void)debug_free(ptr, file, line); } rawMonitorExit(gdata->debug_malloc_lock);}#endifvoid *hprof_malloc(int size){ void *ptr; HPROF_ASSERT(size>0); ptr = malloc(size); if ( ptr == NULL ) { HPROF_ERROR(JNI_TRUE, "Cannot allocate malloc memory"); } return ptr;}voidhprof_free(void *ptr){ HPROF_ASSERT(ptr!=NULL); (void)free(ptr);}/* ------------------------------------------------------------------- *//* JVMTI Version functions */jintjvmtiVersion(void){ if (gdata->cachedJvmtiVersion == 0) { jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,GetVersionNumber) (gdata->jvmti, &(gdata->cachedJvmtiVersion)); if (error != JVMTI_ERROR_NONE) { HPROF_JVMTI_ERROR(error, "Cannot get jvmti version number"); } } return gdata->cachedJvmtiVersion;}static jintjvmtiMajorVersion(void){ return (jvmtiVersion() & JVMTI_VERSION_MASK_MAJOR) >> JVMTI_VERSION_SHIFT_MAJOR;}static jintjvmtiMinorVersion(void){ return (jvmtiVersion() & JVMTI_VERSION_MASK_MINOR) >> JVMTI_VERSION_SHIFT_MINOR;}static jintjvmtiMicroVersion(void){ return (jvmtiVersion() & JVMTI_VERSION_MASK_MICRO) >> JVMTI_VERSION_SHIFT_MICRO;}/* Logic to determine JVMTI version compatibility */static jbooleancompatible_versions(jint major_runtime, jint minor_runtime, jint major_compiletime, jint minor_compiletime){ /* Runtime major version must match. */ if ( major_runtime != major_compiletime ) { return JNI_FALSE; } /* Runtime minor version must be >= the version compiled with. */ if ( minor_runtime < minor_compiletime ) { return JNI_FALSE; } /* Assumed compatible */ return JNI_TRUE;}/* ------------------------------------------------------------------- *//* JVMTI Raw Monitor support functions */jrawMonitorIDcreateRawMonitor(const char *str){ jvmtiError error; jrawMonitorID m; m = NULL; error = JVMTI_FUNC_PTR(gdata->jvmti,CreateRawMonitor) (gdata->jvmti, str, &m); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot create raw monitor"); } return m;}voidrawMonitorEnter(jrawMonitorID m){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorEnter) (gdata->jvmti, m); if ( error == JVMTI_ERROR_WRONG_PHASE ) { /* Treat this as ok, after agent shutdown CALLBACK code may call this */ error = JVMTI_ERROR_NONE; } if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot enter with raw monitor"); }}voidrawMonitorWait(jrawMonitorID m, jlong pause_time){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorWait) (gdata->jvmti, m, pause_time); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot wait with raw monitor"); }}voidrawMonitorNotifyAll(jrawMonitorID m){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorNotifyAll) (gdata->jvmti, m); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot notify all with raw monitor"); }}voidrawMonitorExit(jrawMonitorID m){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,RawMonitorExit) (gdata->jvmti, m); if ( error == JVMTI_ERROR_WRONG_PHASE ) { /* Treat this as ok, after agent shutdown CALLBACK code may call this */ error = JVMTI_ERROR_NONE; } if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot exit with raw monitor"); }}voiddestroyRawMonitor(jrawMonitorID m){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,DestroyRawMonitor) (gdata->jvmti, m); if ( error == JVMTI_ERROR_WRONG_PHASE ) { /* Treat this as ok */ error = JVMTI_ERROR_NONE; } if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot destroy raw monitor"); }}/* ------------------------------------------------------------------- *//* JVMTI Event enabling/disabilin */voidsetEventNotificationMode(jvmtiEventMode mode, jvmtiEvent event, jthread thread){ jvmtiError error; error = JVMTI_FUNC_PTR(gdata->jvmti,SetEventNotificationMode) (gdata->jvmti, mode, event, thread); if ( error != JVMTI_ERROR_NONE ) { HPROF_JVMTI_ERROR(error, "Cannot set event notification"); }}/* ---------------------------------------------------------------------- *//* JNI Support Functions */jobjectexceptionOccurred(JNIEnv *env){ return JNI_FUNC_PTR(env,ExceptionOccurred)(env);}voidexceptionDescribe(JNIEnv *env){ JNI_FUNC_PTR(env,ExceptionDescribe)(env);}voidexceptionClear(JNIEnv *env){ JNI_FUNC_PTR(env,ExceptionClear)(env);}jobjectnewGlobalReference(JNIEnv *env, jobject object){ jobject gref; HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); gref = JNI_FUNC_PTR(env,NewGlobalRef)(env, object); HPROF_ASSERT(gref!=NULL); return gref;}jobjectnewWeakGlobalReference(JNIEnv *env, jobject object){ jobject gref; HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); gref = JNI_FUNC_PTR(env,NewWeakGlobalRef)(env, object); HPROF_ASSERT(gref!=NULL); return gref;}voiddeleteGlobalReference(JNIEnv *env, jobject object){ HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); JNI_FUNC_PTR(env,DeleteGlobalRef)(env, object);}jobjectnewLocalReference(JNIEnv *env, jobject object){ jobject lref; HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); lref = JNI_FUNC_PTR(env,NewLocalRef)(env, object); /* Possible for a non-null weak reference to return a NULL localref */ return lref;}voiddeleteLocalReference(JNIEnv *env, jobject object){ HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); JNI_FUNC_PTR(env,DeleteLocalRef)(env, object);}voiddeleteWeakGlobalReference(JNIEnv *env, jobject object){ HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); JNI_FUNC_PTR(env,DeleteWeakGlobalRef)(env, object);}jclassgetObjectClass(JNIEnv *env, jobject object)/* WARNING: Must be called inside WITH_LOCAL_REFS */{ jclass clazz; HPROF_ASSERT(env!=NULL); HPROF_ASSERT(object!=NULL); clazz = JNI_FUNC_PTR(env,GetObjectClass)(env, object); HPROF_ASSERT(clazz!=NULL); return clazz;}jclassgetSuperclass(JNIEnv *env, jclass klass)/* WARNING: Must be called inside WITH_LOCAL_REFS */{ jclass super_klass; HPROF_ASSERT(env!=NULL); HPROF_ASSERT(klass!=NULL);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?