⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classloader.c

📁 已经移植好的java虚拟机
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * @(#)classloader.c	1.37 02/09/27 * * Copyright 1995-1999 by Sun Microsystems, Inc., * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A. * All rights reserved. * * This software is the confidential and proprietary information * of Sun Microsystems, Inc. ("Confidential Information").  You * shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement * you entered into with Sun. * Use is subject to license terms. *//*========================================================================= * SYSTEM:    Verifier * SUBSYSTEM: class loader. * FILE:      classloader.c * OVERVIEW:  Routines for loading and resolving class definitions. *            These routines should not be depending upon the interpreter *            or the garbage collector. * AUTHOR:    Sheng Liang, Sun Microsystems, Inc. *            Modifications for JAR support and comments, *            Tasneem Sayeed, Sun Microsystems *=======================================================================*//*========================================================================= * Include files *=======================================================================*/#include <string.h>#include <stdio.h>#include <sys/types.h>#include <stddef.h>#include <fcntl.h>#include <sys/stat.h>#include <stdlib.h>#include <setjmp.h>#include <jar.h>#include <oobj.h>#include <path.h>#include <tree.h>#include <signature.h>#include <convert_md.h>#include <sys_api.h>#ifdef UNIX#include <unistd.h>#endif/*========================================================================= * Globals and extern declarations *=======================================================================*/char *stat_source(ClassClass *cb, struct stat *s, char *pathbuf, int maxlen);extern ClassClass *allocClassClass();extern bool_t JARfile;extern char * zipFileName;extern zip_t * getZipEntry(char *zipFile, int len);extern bool_t findJARDirectories(zip_t *entry, struct stat *statbuf);extern JAR_DataStreamPtr loadJARfile(zip_t *entry, const char* filename);/*========================================================================= * FUNCTION:      AddBinClass * OVERVIEW:      Used by createInternalClass1() and createFakeArrayClass() *                to add a class in the class table. * INTERFACE: *   parameters:  ClassClass: cb * *   returns:     nothing *=======================================================================*/voidAddBinClass(ClassClass * cb){    register int left, right, middle, result, i;    char *name = cbName(cb);    struct Hjava_lang_ClassLoader *loader = cbLoader(cb);    BINCLASS_LOCK();    left = 0;    right = nbinclasses - 1;    result = 1;    while (left <= right) {        ClassClass *cb1;        middle = (left+right)/2;        cb1 = binclasses[middle];        result = strcmp(name, cbName(cb1));        if (result == 0) {            if (loader < cbLoader(cb1)) {                result = -1;            } else if (loader > cbLoader(cb1)) {                result = 1;            } else {                result = 0;            }        }        if (result < 0) {            right = middle-1;        } else if (result > 0) {            left = middle+1;        } else {            break;        }    }    if (result != 0) {        if (nbinclasses >= sizebinclasses) {            if (binclasses == 0)                binclasses = (ClassClass **)                sysMalloc(sizeof(ClassClass *) * (sizebinclasses = 50));            else                binclasses = (ClassClass **)                sysRealloc(binclasses, sizeof(ClassClass *)                    * (sizebinclasses = nbinclasses * 2));        }        if (binclasses == 0)            goto unlock;        right++;        for (i = nbinclasses; i > right; i--) {            binclasses[i] = binclasses[i-1];        }        binclasses[right] = cb;        nbinclasses++;    }unlock:    BINCLASS_UNLOCK();}/*========================================================================= * FUNCTION:      DelBinClass * OVERVIEW:      Intended for allowing deletion of classes from the class *                table. * * INTERFACE: *   parameters:  ClassClass: cb * *   returns:     nothing *=======================================================================*/voidDelBinClass(ClassClass * cb){    register int i, j;    BINCLASS_LOCK();    for (i = nbinclasses; --i >= 0; )        if (binclasses[i] == cb) {            nbinclasses--;            for (j = i; j < nbinclasses; j++) {                binclasses[j] = binclasses[j+1];            }            break;        }    BINCLASS_UNLOCK();}/*========================================================================= * FUNCTION:      MakeClassSticky * OVERVIEW:      Used to lock certain system classes into memory during *                initialization. * * INTERFACE: *   parameters:  ClassClass: cb * *   returns:     nothing *=======================================================================*/voidMakeClassSticky(ClassClass *cb){    /* monitorEnter(obj_monitor(cb));   */    CCSet(cb, Sticky);    /* monitorExit(obj_monitor(cb));    */}/*========================================================================= * FUNCTION:      LoadClassFromFile * OVERVIEW:      Loads a .class file normally from disk or a null if it fails. *                When the interpreter requests for a file, it is actually *                looking for a classblock structure to be created, and the *                only way it can get one of those is by loading a compiled *                class. *                OpenCode() tries to open a .class file first. If it fails *                to open the file, it returns a non-zero status. If it *                returns a valid file descriptor, this usually means that *                this is a valid .class file. *                It then invokes createInternalClass() to actually create the *                internal representation of the class. * * INTERFACE: *   parameters:  char*: file name *                char*: directory *                char*: class name * *   returns:     nothing *=======================================================================*/static ClassClass *LoadClassFromFile(char *fn, char *dir, char *class_name){    extern int OpenCode(char *, char *, char *, struct stat*);    struct stat st;    ClassClass *cb = 0;    int codefd = -1;    unsigned char *external_class;    char *detail;    codefd = OpenCode(fn, NULL, dir, &st);    if (codefd < 0)     /* open failed */        return 0;    /* Snarf the file into memory. */    external_class = (unsigned char *)sysMalloc(st.st_size);    if (external_class == 0)        goto failed;    if (sysRead(codefd, external_class, st.st_size) != st.st_size)        goto failed;    sysClose(codefd);    codefd = -1;    /* Create the internal class */    cb = allocClassClass();    if (cb == NULL ||        !createInternalClass(external_class, external_class + st.st_size,                 cb, NULL, class_name, &detail)) {        sysFree(external_class);        goto failed;    }    sysFree(external_class);    if (verbose)        jio_fprintf(stderr, "[Loaded %s]\n", fn);    return cb;failed:    if (codefd >= 0)        sysClose(codefd);    if (cb != 0)        FreeClass(cb);    return 0;}/*========================================================================= * FUNCTION:      LoadClassFromZip * TYPE:          load class from a JAR or Zip file * OVERVIEW:      Called by LoadClassLocally for loading classes from a Zip. * *                This function loads a .class file normally from a Zip or *                JAR file. *                It returns the class when it succeeds or NULL if it fails. * * INTERFACE: *   parameters:  zip_t: zip file entry *                char*: class file name to search for loading *   returns:     Pointer to ClassClass when it succeeds, or NULL if it fails. *=======================================================================*/static ClassClass *LoadClassFromZip(zip_t *zipEntry, char *class_name){    ClassClass *cb = 0;    JAR_DataStreamPtr jdstream = NULL;    unsigned char *external_class;    int data_length;    char *detail;    jdstream = loadJARfile (zipEntry, class_name);    if (jdstream == NULL)        goto failed;    external_class = jdstream->data;    data_length = jdstream->dataLen;    /* Create the internal class */    cb = allocClassClass();    if (cb == NULL ||        !createInternalClass(external_class, external_class + data_length,                 cb, NULL, class_name, &detail)) {        goto failed;    }    if (jdstream != NULL) {        freeBytes(jdstream);    }    if (verbose) {        jio_fprintf(stderr, "[Loaded %s] from [%s]\n", class_name,                                                       zipEntry->name);    }    return cb;failed:    if (cb != 0)        FreeClass(cb);    if (jdstream != NULL) {        freeBytes(jdstream);    }    return 0;}/*========================================================================= * FUNCTION:      LoadClassLocally * OVERVIEW:      Find a class file that is somewhere local, and not from a *                class loader. *                It still needs to be searched using the classpath. * * INTERFACE: *   parameters:  char* : class file name *   returns:     Pointer to ClassClass when it succeeds, or NULL if it fails. *=======================================================================*/ClassClass *LoadClassLocally(char *name){    ClassClass *cb = 0;    cpe_t **cpp;    if (name[0] == DIR_SEPARATOR || name[0] == SIGNATURE_ARRAY)        return 0;    for (cpp = sysGetClassPath(); cpp && *cpp != 0; cpp++) {        cpe_t *cpe = *cpp;        char *path;        if (cpe->type == CPE_DIR) {            path = (char *)sysMalloc(strlen(cpe->u.dir)                + sizeof(LOCAL_DIR_SEPARATOR)                + strlen(name)                + strlen(JAVAOBJEXT)                + 2);  /* 2 is for the . and the \0 */            if (sprintf(path,                 "%s%c%s." JAVAOBJEXT, cpe->u.dir,                 LOCAL_DIR_SEPARATOR, name) == -1) {                sysFree(path);                return 0;            }            if ((cb = LoadClassFromFile(sysNativePath(path),                                        cpe->u.dir, name))) {                sysFree(path);                return cb;            }        } else if (cpe->type == CPE_ZIP) {            if (JAR_DEBUG && verbose)                jio_fprintf(stderr, "Loading classes from a ZIP file... \n");            if ((cb = LoadClassFromZip(cpe->u.zip, name))) {                return cb;            }        }    }    return cb;}/*========================================================================= * FUNCTION:      stat_source * OVERVIEW:      Unused by the Verifier, but it is too late to remove it now. * * INTERFACE: *   parameters:  ClassClass *: cb *                struct stat*: s *                char*: pathbuf *                int: maxlen *   returns:     char *, or NULL if it fails. *=======================================================================*/char *stat_source(ClassClass *cb, struct stat *s, char *pathbuf, int maxlen){#define NAMEBUFLEN 255    char nm[NAMEBUFLEN];    char *p, *q, *lp;    cpe_t **cpp;    /* don't bother searching if absolute */    /* REMIND: only here for compatibility */    if (sysIsAbsolute(cbSourceName(cb))) {        if (sysStat(cbSourceName(cb), s) == 0) {            if (jio_snprintf(pathbuf, maxlen, "%s", cbSourceName(cb)) == -1) {                return 0;            }            return pathbuf;        } else {            return 0;        }    }    /* parse the package name */    p = cbName(cb);    if (strlen(p) > NAMEBUFLEN - 1) {        return 0;    }    for (q = lp = nm ; *p ; p++) {        if (*p == DIR_SEPARATOR) {            *q++ = LOCAL_DIR_SEPARATOR;            lp = q;        } else {            *q++ = *p;        }    }    /* append the source file name */    p = cbSourceName(cb);    if (strlen(p) + (lp - nm) > NAMEBUFLEN - 1) {        return 0;    }    for (; *p ; p++) {        *lp++ = (*p == DIR_SEPARATOR ? LOCAL_DIR_SEPARATOR : *p);    }    *lp = '\0';    /* search the class path */    for (cpp = sysGetClassPath() ; cpp && *cpp != 0 ; cpp++) {        cpe_t *cpe = *cpp;        if (cpe->type == CPE_DIR) {            if (jio_snprintf(pathbuf, maxlen, "%s%c%s",                             cpe->u.dir, LOCAL_DIR_SEPARATOR, nm) == -1) {                return 0;            }            if (sysStat(pathbuf, s) == 0) {                return pathbuf;            }        }    }    return 0;}/*========================================================================= * Globals and externs for Internal Class representation *=======================================================================*/struct CICmallocs {    struct CICmallocs *next;    void * alignment_padding;/* Whatever follows will be 8-byte aligned, if the structure itself is   8-byte aligned. */};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -