📄 garbage.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: garbage.c
/*0023./ * OVERVIEW: Memory manager/garbage collector for the KVM.
/*0024./ * AUTHOR: Antero Taivalsaari, Sun Labs
/*0025./ * Edited by Doug Simon 11/1998
/*0026./ * Frank Yellin (exact collection, compaction)
/*0027./ * Compaction based on code written by Matt Seidl
/*0028./ *=======================================================================*/
/*0029*/
/*0030*//*=========================================================================
/*0031./ * For detailed explanation of the memory system, see Garbage.h
/*0032./ *=======================================================================*/
/*0033*/
/*0034*//*=========================================================================
/*0035./ * Include files
/*0036./ *=======================================================================*/
/*0037*/
/*0038*/#include <global.h>
/*0039*/
/*0040*//*=========================================================================
/*0041./ * Variables
/*0042./ *=======================================================================*/
/*0043*/
/*0044*/int TemporaryRootsLength;
/*0045*/int GlobalRootsLength;
/*0046*/
/*0047*/union cellOrPointer TemporaryRoots[MAXIMUM_TEMPORARY_ROOTS];
//\\ = 50
/*0048*/union cellOrPointer GlobalRoots[MAXIMUM_GLOBAL_ROOTS];
//\\ = 20
/*0049*/POINTERLIST CleanupRoots;
/*0050*/
/*0051*//*=========================================================================
/*0052./ * Functions
/*0053./ *=======================================================================*/
/*0054*/
/*0055*//*=========================================================================
/*0056./ * FUNCTION: garbageCollect
/*0057./ * TYPE: public garbage collection function
/*0058./ * OVERVIEW: Perform mark-and-sweep garbage collection.
/*0059./ * INTERFACE:
/*0060./ * parameters: int moreMemory: the amount by which the heap
/*0061./ * size should be grown during garbage collection
/*0062./ * (this feature is not supported in the mark-and-sweep
/*0063./ * collector).
/*0064./ * returns: <nothing>
/*0065./ *=======================================================================*/
/*0066*/
/*0067*/void
/*0068*/garbageCollect(int moreMemory)
/*0069*/{
/*0075*/ RundownAsynchronousFunctions(); //\\thread.c:959
/*0076*/
/*0095*/ MonitorCache = NULL; /* Clear any temporary monitors */
/*0096*/
/*0097*/ /* Store virtual machine registers of the currently active thread before
/*0098./ * garbage collection (must be done to enable execution stack scanning).
/*0099./ */
/*0100*/ if (CurrentThread) {
/*0101*/ storeExecutionEnvironment(CurrentThread);
/*0102*/ }
/*0103*/
/*0104*/ garbageCollectForReal(moreMemory); //\\collector.c:364
/*0105*/
/*0106*/ if (CurrentThread) {
/*0107*/ loadExecutionEnvironment(CurrentThread);
/*0108*/ }
/*0109*/
/*0130*/ RestartAsynchronousFunctions();
/*0131*/}
/*0132*/
/*0142*/void
/*0143*/makeGlobalRoot(cell** object)
/*0144*/{
/*0145*/ int index = GlobalRootsLength;
/*0146*/ if (index >= MAXIMUM_GLOBAL_ROOTS) {
/*0147*/ fatalError(KVM_MSG_GLOBAL_ROOT_OVERFLOW);
/*0148*/ }
/*0149*/ GlobalRoots[index].cellpp = object;
/*0150*/ GlobalRootsLength = index + 1;
/*0151*/}
/*0152*/
/*0153*//*=========================================================================
/*0154./ * Memory allocation operations
/*0155./ *=======================================================================*/
/*0156*/
/*0157*//*=========================================================================
/*0158./ * FUNCTION: mallocObject, callocObject
/*0159./ * TYPE: public memory allocation operation
/*0160./ * OVERVIEW: Allocate a contiguous area of n cells in the dynamic heap.
/*0161./ * INTERFACE:
/*0162./ * parameters: size: the requested object size in cells,
/*0163./ * type: garbage collection type of the object
/*0164./ * returns: pointer to newly allocated area.
/*0165./ * Gives a fatal error if space cannot be allocated.
/*0166./ *
/*0167./ * COMMENTS: callocObject zeros the space before returning it.
/*0168./ *=======================================================================*/
/*0169*/
/*0170*/cell* mallocObject(long size, GCT_ObjectType type)
/*0171*//* Remember: size is given in CELLs rather than bytes */
/*0172*/{
/*0173*/ cell *result = mallocHeapObject(size, type);
/*0174*/ if (result == NULL) {
/*0175*/ fatalVMError(KVM_MSG_OUT_OF_HEAP_MEMORY);
/*0176*/ }
/*0177*/
/*0178*/ return result;
/*0179*/}
/*0180*/
/*0181*/cell* callocObject(long size, GCT_ObjectType type)
/*0182*/{
/*0183*/ cell *result = mallocHeapObject(size, type);
/*0184*/ if (result == NULL) {
/*0185*/ fatalVMError(KVM_MSG_OUT_OF_HEAP_MEMORY);
/*0186*/ }
/*0187*/
/*0188*/ /* Initialize the area to zero */
/*0189*/ memset(result, 0, size << log2CELL);
/*0190*/
/*0191*/ return result;
/*0192*/}
/*0193*/
/*0194*//*=========================================================================
/*0195./ * FUNCTION: mallocBytes
/*0196./ * TYPE: public memory allocation operation
/*0197./ * OVERVIEW: Allocate a contiguous area of n bytes in the dynamic heap.
/*0198./ * INTERFACE:
/*0199./ * parameters: size: the requested object size in bytes
/*0200./ * returns: pointer to newly allocated area, or
/*0201./ * NIL is allocation fails.
/*0202./ * COMMENTS: mallocBytes allocates areas that are treated as
/*0203./ * as byte data only. Any possible heap references
/*0204./ * within such areas are ignored during garbage
/*0205./ * collection.
/*0206./ *
/*0207./ * The actual size of the data area is rounded
/*0208./ * up to the next four-byte aligned memory address,
/*0209./ * so the area may contain extra bytes.
/*0210./ *=======================================================================*/
/*0211*/
/*0212*/char* mallocBytes(long size)
/*0213*/{
/*0214*/ /* The size is given in bytes, so it must be */
/*0215*/ /* rounded up to next long word boundary */
/*0216*/ /* and converted to CELLs */
/*0217*/ cell* dataArea = mallocObject((size + CELL - 1) >> log2CELL,
/*0218*/ GCT_NOPOINTERS);
/*0219*/ return (char*)dataArea;
/*0220*/}
/*0221*/
/*0276*//*=========================================================================
/*0277./ * Auxiliary memory management operations (both static and dynamic heap)
/*0278./ *=======================================================================*/
/*0279*/
/*0280*//*=========================================================================
/*0281./ * FUNCTION: getObjectSize()
/*0282./ * TYPE: public helper function
/*0283./ * OVERVIEW: Returns the size of the given heap object (in CELLs).
/*0284./ * INTERFACE:
/*0285./ * parameters: an object pointer
/*0286./ * returns: the size of the object in cells (excluding the header).
/*0287./ *=======================================================================*/
/*0288*/
/*0289*/unsigned long getObjectSize(cell* object)
/*0290*/{
/*0291*/ /* Calculate the address of the object header */
/*0292*/ cell* header = object-HEADERSIZE;
/*0293*/
/*0294*/ /* Get the size of object by shifting the type */
/*0295*/ /* information away from the first header word */
/*0296*/ return ((unsigned long)*header) >> TYPEBITS;
/*0297*/}
/*0298*/
/*0299*//*=========================================================================
/*0300./ * FUNCTION: getObjectType()
/*0301./ * TYPE: public helper function
/*0302./ * OVERVIEW: Returns the garbage collection type (GCT_*)
/*0303./ * of the given heap object.
/*0304./ * INTERFACE:
/*0305./ * parameters: an object pointer
/*0306./ * returns: GC type
/*0307./ *=======================================================================*/
/*0308*/
/*0309*/GCT_ObjectType getObjectType(cell* object)
/*0310*/{
/*0311*/ /* Calculate the address of the object header */
/*0312*/ cell* header = object-HEADERSIZE;
/*0313*/
/*0314*/ /* Get the type of object by masking away the */
/*0315*/ /* size information and the flags */
/*0316*/ return TYPE(*header);
/*0317*/}
/*0318*/
/*0319*//*=========================================================================
/*0320./ * Global garbage collection operations
/*0321./ *=======================================================================*/
/*0322*/
/*0323*//*=========================================================================
/*0324./ * FUNCTION: InitializeGlobals()
/*0325./ * TYPE: public global operation
/*0326./ * OVERVIEW: Initialize all the globals variables. This ensures that
/*0327./ * they have correct values in each invocation of the VM.
/*0328./ * DON'T rely on static initializers instead. Any new
/*0329./ * globals added to the system should be initialized here.
/*0330./ * INTERFACE:
/*0331./ * parameters: <none>
/*0332./ * returns: <nothing>
/*0333./ *=======================================================================*/
/*0334*/
/*0335*/void InitializeGlobals(void)
/*0336*/{
/*0337*/}
/*0338*/
/*0339*//*=========================================================================
/*0340./ * FUNCTION: InitializeMemoryManagement()
/*0341./ * TYPE: public global operation
/*0342./ * OVERVIEW: Initialize the system memory heaps.
/*0343./ * INTERFACE:
/*0344./ * parameters: <none>
/*0345./ * returns: <nothing>
/*0346./ *=======================================================================*/
/*0347*/
//\\
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -