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

📄 fakestaticmemory.c

📁 This is a java virtual machine implement in c
💻 C
字号:
/*0001*//*
/*0002./ * Copyright (c) 1998-2001 Sun Microsystems, Inc. All Rights Reserved.
/*0003./ * 
/*0004./ * This software is the confidential and proprietary information of Sun
/*0005./ * Microsystems, Inc. ("Confidential Information").  You shall not
/*0006./ * disclose such Confidential Information and shall use it only in
/*0007./ * accordance with the terms of the license agreement you entered into
/*0008./ * with Sun.
/*0009./ * 
/*0010./ * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
/*0011./ * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
/*0012./ * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
/*0013./ * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
/*0014./ * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
/*0015./ * THIS SOFTWARE OR ITS DERIVATIVES.
/*0016./ * 
/*0017./ */
/*0018*/
/*0019*//*=========================================================================
/*0020./ * SYSTEM:    KVM
/*0021./ * SUBSYSTEM: Memory management
/*0022./ * FILE:      staticMemory.c
/*0023./ * OVERVIEW:  These definitions allow KVM to emulate the special
/*0024./ *            USESTATIC mode on Unix/Solaris/Windows/Linux for
/*0025./ *            debugging purposes.  This mode (informally known 
/*0026./ *            as "simonizing") was originally developed for the 
/*0027./ *            Palm version of the Spotless/KVM system to overcome 
/*0028./ *            some Palm-specific memory limitations.
/*0029./ * 
/*0030./ *            When the USESTATIC mode is on, KVM will copy all 
/*0031./ *            the immutable runtime structures from "dynamic"
/*0032./ *            memory to "static/storage" memory (which is fast to
/*0033./ *            read but very slow to write to).  This allows the
/*0034./ *            VM to leave more heap space for Java objects.
/*0035./ * AUTHOR:    Frank Yellin (based on an implementation written
/*0036./ *            originally by Doug Simon)
/*0037./ *
/*0038./ * NOTE:      The USESTATIC mode is completely useless on the Windows
/*0039./ *            and Unix versions of the KVM other than for debugging
/*0040./ *            the memory system.  If your target device does not 
/*0041./ *            differentiate "real" RAM from "storage" RAM, then
/*0042./ *            this mode should always be turned OFF in a production
/*0043./ *            release.
/*0044./ *=======================================================================*/
/*0045*/
/*0046*//*=========================================================================
/*0047./ * Include files
/*0048./ *=======================================================================*/
/*0049*/
/*0050*/#include <global.h>
/*0051*/
/*0052*/#if USESTATIC
/*0053*/
/*0054*//*=========================================================================
/*0055./ * Definitions and variables
/*0056./ *=======================================================================*/
/*0057*/
/*0058*/static char * memoryStart = 0;
/*0059*/static int memoryOffset;
/*0060*/static int pageSize;
/*0061*/
/*0062*/#define MEMORY_SIZE 0x20000
/*0063*/#define MEMORY_SHIBBOLETH 0xCAFEBABE
/*0064*/
/*0065*//*=========================================================================
/*0066./ * Functions for emulating Simonizing on Solaris
/*0067./ *=======================================================================*/
/*0068*/
/*0069*/static void
/*0070*/initialize()
/*0071*/{
/*0072*/    memoryStart = allocateVirtualMemory_md(MEMORY_SIZE);
/*0073*/    protectVirtualMemory_md(memoryStart, MEMORY_SIZE, PVM_ReadOnly);
/*0074*/    memoryOffset = 0;
/*0075*/    pageSize = getpagesize();
/*0076*/}
/*0077*/
/*0078*//*=========================================================================
/*0079./ * FUNCTION:      modifyStaticMemory()
/*0080./ * TYPE:          public global operation
/*0081./ * OVERVIEW:      Modify a chunk of static memory.
/*0082./ * INTERFACE:
/*0083./ *   parameters:  staticMemory - a pointer to some piece of memory returned
/*0084./ *                     by mallocStaticBytes
/*0085./ *                ptr - A pointer into the interior of the object pointed at
/*0086./ *                     by staticMemory, indicating the bytes to change
/*0087./ *                newVal - The new value to place into the memory
/*0088./ *                size - The number of bytes to change.
/*0089./ *=======================================================================*/
/*0090*/
/*0091*/void
/*0092*/modifyStaticMemory(void *staticMemory, int offset, void *newVal, int size)
/*0093*/{
/*0094*/    char *start = (char*)staticMemory + offset;
/*0095*/    char* end = start + size;
/*0096*/
/*0097*/    char* pageStart = (char*)((long)start & ~(pageSize - 1));
/*0098*/    char* pageEnd = (char*)(((long)end + pageSize - 1) & ~(pageSize - 1));
/*0099*/
/*0100*/    if ((char *)staticMemory < memoryStart 
/*0101*/        || (end >= (((char *)memoryStart) + MEMORY_SIZE))) {
/*0102*/        fatalError(KVM_MSG_STATIC_MEMORY_ERROR);
/*0103*/    }
/*0104*/    if ( ((unsigned long *)staticMemory)[-1] != MEMORY_SHIBBOLETH) { 
/*0105*/        fprintf(stderr, KVM_MSG_STATIC_MEMORY_ERROR);
/*0106*/    }
/*0107*/
/*0108*/    protectVirtualMemory_md(pageStart, pageEnd - pageStart, PVM_ReadWrite); 
/*0109*/    memcpy(start, newVal, size);
/*0110*/    protectVirtualMemory_md(pageStart, pageEnd - pageStart, PVM_ReadOnly); 
/*0111*/}
/*0112*/
/*0113*/static void
/*0114*/insertShibboleth(void *staticMemory)
/*0115*/{
/*0116*/    long start = (long)staticMemory & ~(pageSize - 1);
/*0117*/    protectVirtualMemory_md((void *)start, pageSize, PVM_ReadWrite);
/*0118*/    *(unsigned long *)staticMemory = MEMORY_SHIBBOLETH;
/*0119*/    protectVirtualMemory_md((void *)start, pageSize, PVM_ReadOnly);
/*0120*/}
/*0121*/
/*0122*//*=========================================================================
/*0123./ * FUNCTION:      mallocStaticBytes
/*0124./ * TYPE:          public global operation
/*0125./ * OVERVIEW:      Allocate space from static memory
/*0126./ * INTERFACE:
/*0127./ *   parameters:  size - The size of the memory to allocate.
/*0128./ *   returns:     the start of the staticized object
/*0129./ *=======================================================================*/
/*0130*/
/*0131*/void *
/*0132*/mallocStaticBytes(int size) { 
/*0133*/    cell* newChunk, *newObject;
/*0134*/    int actualSize;
/*0135*/    if (memoryStart == 0) { 
/*0136*/        initialize();
/*0137*/    }
/*0138*/    actualSize = (size + sizeof(cell *) + sizeof(cell *) - 1)
/*0139*/        & ~(sizeof(cell *) - 1);
/*0140*/    
/*0141*/    newChunk = (cell *)(((char *)memoryStart) + memoryOffset);
/*0142*/    newObject = newChunk + 1;
/*0143*/    insertShibboleth(newChunk);
/*0144*/    memoryOffset += actualSize;
/*0145*/    if (memoryOffset > MEMORY_SIZE) { 
/*0146*/        fatalError(KVM_MSG_OUT_OF_STATIC_MEMORY);
/*0147*/    }
/*0148*/    return newObject;
/*0149*/}
/*0150*/
/*0151*//*=========================================================================
/*0152./ * FUNCTION:      FinalizeStaticMemory()
/*0153./ * TYPE:          public global operation
/*0154./ * OVERVIEW:      Deallocate all static memory chunks allocated by
/*0155./ *                mallocStaticBytes.
/*0156./ * INTERFACE:
/*0157./ *   parameters:  <none>
/*0158./ *   returns:     <nothing>
/*0159./ *=======================================================================*/
/*0160*/
/*0161*/void FinalizeStaticMemory()
/*0162*/{
/*0163*/    if (memoryStart != 0) { 
/*0164*/        freeVirtualMemory_md(memoryStart, MEMORY_SIZE);
/*0165*/    }
/*0166*/    /*** REINIT ****/
/*0167*/    memoryStart = 0;
/*0168*/}
/*0169*/
/*0170*/#endif /* USESTATIC */
/*0171*/

⌨️ 快捷键说明

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