annotate src/share/instrument/Utilities.c @ 3188:5de001f5f8b4

6957378: JMX memory leak Reviewed-by: emcmanus, kevinw
author coffeys
date Fri, 05 Nov 2010 17:15:44 +0000
parents b6d6877c1155
children
rev   line source
duke@0 1 /*
ohair@2486 2 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
ohair@2486 7 * published by the Free Software Foundation. Oracle designates this
duke@0 8 * particular file as subject to the "Classpath" exception as provided
ohair@2486 9 * by Oracle in the LICENSE file that accompanied this code.
duke@0 10 *
duke@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 15 * accompanied this code).
duke@0 16 *
duke@0 17 * You should have received a copy of the GNU General Public License version
duke@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 20 *
ohair@2486 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@2486 22 * or visit www.oracle.com if you need additional information or have any
ohair@2486 23 * questions.
duke@0 24 */
duke@0 25
duke@0 26 /*
duke@0 27 * Copyright 2003 Wily Technology, Inc.
duke@0 28 */
duke@0 29
duke@0 30 #include <stdlib.h>
duke@0 31 #include <stdio.h>
duke@0 32
duke@0 33 #include "JPLISAssert.h"
duke@0 34 #include "Utilities.h"
duke@0 35 #include "JavaExceptions.h"
duke@0 36
duke@0 37 /*
duke@0 38 * This module provides various simple JNI and JVMTI utility functionality.
duke@0 39 */
duke@0 40
duke@0 41 void *
duke@0 42 allocate(jvmtiEnv * jvmtienv, size_t bytecount) {
duke@0 43 void * resultBuffer = NULL;
duke@0 44 jvmtiError error = JVMTI_ERROR_NONE;
duke@0 45
duke@0 46 error = (*jvmtienv)->Allocate(jvmtienv,
duke@0 47 bytecount,
duke@0 48 (unsigned char**) &resultBuffer);
dcubed@142 49 /* may be called from any phase */
duke@0 50 jplis_assert(error == JVMTI_ERROR_NONE);
duke@0 51 if ( error != JVMTI_ERROR_NONE ) {
duke@0 52 resultBuffer = NULL;
duke@0 53 }
duke@0 54 return resultBuffer;
duke@0 55 }
duke@0 56
duke@0 57 /**
duke@0 58 * Convenience method that deallocates memory.
duke@0 59 * Throws assert on error.
duke@0 60 * JVMTI Deallocate can only fail due to internal error, that is, this
duke@0 61 * agent has done something wrong or JVMTI has done something wrong. These
duke@0 62 * errors aren't interesting to a JPLIS agent and so are not returned.
duke@0 63 */
duke@0 64 void
duke@0 65 deallocate(jvmtiEnv * jvmtienv, void * buffer) {
duke@0 66 jvmtiError error = JVMTI_ERROR_NONE;
duke@0 67
duke@0 68 error = (*jvmtienv)->Deallocate(jvmtienv,
duke@0 69 (unsigned char*)buffer);
dcubed@142 70 /* may be called from any phase */
duke@0 71 jplis_assert_msg(error == JVMTI_ERROR_NONE, "Can't deallocate memory");
duke@0 72 return;
duke@0 73 }
duke@0 74
duke@0 75 /**
duke@0 76 * Returns whether the passed exception is an instance of the given classname
duke@0 77 * Clears any JNI exceptions before returning
duke@0 78 */
duke@0 79 jboolean
duke@0 80 isInstanceofClassName( JNIEnv * jnienv,
duke@0 81 jobject instance,
duke@0 82 const char * className) {
duke@0 83 jboolean isInstanceof = JNI_FALSE;
duke@0 84 jboolean errorOutstanding = JNI_FALSE;
duke@0 85 jclass classHandle = NULL;
duke@0 86
duke@0 87 jplis_assert(isSafeForJNICalls(jnienv));
duke@0 88
duke@0 89 /* get an instance of unchecked exception for instanceof comparison */
duke@0 90 classHandle = (*jnienv)->FindClass(jnienv, className);
duke@0 91 errorOutstanding = checkForAndClearThrowable(jnienv);
duke@0 92 jplis_assert(!errorOutstanding);
duke@0 93
duke@0 94 if (!errorOutstanding) {
duke@0 95 isInstanceof = (*jnienv)->IsInstanceOf(jnienv, instance, classHandle);
duke@0 96 errorOutstanding = checkForAndClearThrowable(jnienv);
duke@0 97 jplis_assert(!errorOutstanding);
duke@0 98 }
duke@0 99
duke@0 100 jplis_assert(isSafeForJNICalls(jnienv));
duke@0 101 return isInstanceof;
duke@0 102 }
duke@0 103
duke@0 104 /* We don't come back from this
duke@0 105 */
duke@0 106 void
duke@0 107 abortJVM( JNIEnv * jnienv,
duke@0 108 const char * message) {
duke@0 109 (*jnienv)->FatalError(jnienv, message);
duke@0 110 }