Path: blob/master/runtime/bcutil/ROMClassSegmentAllocationStrategy.cpp
5985 views
/*******************************************************************************1* Copyright (c) 2001, 2020 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception20*******************************************************************************/21/*22* ROMClassSegmentAllocationStrategy.cpp23*/2425#include "ROMClassSegmentAllocationStrategy.hpp"26#include "ut_j9bcu.h"2728U_8*29ROMClassSegmentAllocationStrategy::allocate(UDATA bytesRequired)30{31U_8* result = NULL;3233/* Scan existing segments for one large enough to hold the new ROM class */3435J9MemorySegment* segment = NULL;36/* always make a new segment if its an anonClass */37bool allocNewSegment = (_classLoader == _javaVM->anonClassLoader);3839if (!allocNewSegment) {40J9MemorySegmentList* classSegments = _javaVM->classMemorySegments;41#ifdef J9VM_THR_PREEMPTIVE42omrthread_monitor_enter(classSegments->segmentMutex);43#endif44segment = _classLoader->classSegments;4546while (NULL != segment) {47if ((segment->type & (MEMORY_TYPE_ROM_CLASS | MEMORY_TYPE_ALLOCATED)) == (MEMORY_TYPE_ROM_CLASS | MEMORY_TYPE_ALLOCATED)) {48UDATA romAvailable = segment->heapTop - segment->heapAlloc;4950if (romAvailable >= bytesRequired) {51result = segment->heapAlloc;52break;53}54}55segment = segment->nextSegmentInClassLoader;56}57#ifdef J9VM_THR_PREEMPTIVE58omrthread_monitor_exit(classSegments->segmentMutex);59#endif60}6162/* If no segment was found which could hold the new ROM class, allocate a new one */6364if (NULL == result) {65UDATA classAllocationIncrement = _javaVM->romClassAllocationIncrement;66if (allocNewSegment) {67classAllocationIncrement = 0;68}69segment = _javaVM->internalVMFunctions->allocateClassMemorySegment(_javaVM, bytesRequired, MEMORY_TYPE_DYNAMIC_LOADED_CLASSES, _classLoader, classAllocationIncrement);70if (segment != NULL) {71result = segment->heapAlloc;72}73}7475if ( NULL != result ) {76segment->heapAlloc += bytesRequired;77/*78* store a reference to the used segment and bytesRequested79* so that heapAlloc can later be adjusted to80* the actual amount of memory used in updateFinalROMSize()81*/82_segment = segment;83_bytesRequested = bytesRequired;84}8586return result;87}8889void90ROMClassSegmentAllocationStrategy::updateFinalROMSize(UDATA finalSize)91{92Trc_BCU_Assert_NotEquals( NULL, _segment );93/* this assumes that the appropriate lock is held from allocate() to now */94_segment->heapAlloc -= _bytesRequested;95_segment->heapAlloc += finalSize;96}979899