Path: blob/master/runtime/gc_structs/FlattenedContiguousArrayIterator.hpp
5985 views
/*******************************************************************************1* Copyright (c) 1991, 2021 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*******************************************************************************/2122/**23* @file24* @ingroup GC_Structs25*/2627#if !defined(FLATTENEDCONTIGUOUSARRAYITERATOR_HPP_)28#define FLATTENEDCONTIGUOUSARRAYITERATOR_HPP_2930#include "j9.h"31#include "j9cfg.h"3233#include "ArrayObjectModel.hpp"34#include "GCExtensionsBase.hpp"35#include "ObjectIteratorState.hpp"36#include "SlotObject.hpp"37#include "MixedObjectIterator.hpp"3839class GC_FlattenedContiguousArrayIterator40{41private:42J9IndexableObject *_arrayPtr; /**< pointer to the array object being iterated */43GC_MixedObjectIterator _mixedObjectIterator; /**< Object iterator which iterates over field of each element */44uintptr_t _elementStride; /**< Size of each element, including padding */45fj9object_t *_basePtr; /**< pointer to the first array slot, where element 0 is */46fj9object_t *_scanPtr; /**< pointer to the current array element's first slot */47fj9object_t *_endPtr; /**< pointer to the array slot where the iteration will terminate */48J9Class *_elementClass; /**< Pointer to class of the elements in the flattened array */49protected:50OMR_VM *_omrVM;51public:52MMINLINE GC_SlotObject *nextSlot()53{54/* If no more object slots to scan, returns NULL */55GC_SlotObject *result = NULL;56if (_scanPtr < _endPtr) {57result = _mixedObjectIterator.nextSlot();58if (NULL == result) {59_scanPtr = (fj9object_t *)(_elementStride + (uintptr_t)_scanPtr);60if (_scanPtr < _endPtr) {61/* mixedObjectIterator reached end of element. Move the iterator to the beginning of the next element */62_mixedObjectIterator.initialize(_omrVM, _elementClass, _scanPtr);63result = _mixedObjectIterator.nextSlot();64}65}66}67return result;68}6970MMINLINE void initialize(J9Object *objectPtr)71{72MM_GCExtensionsBase *extensions = MM_GCExtensionsBase::getExtensions(_omrVM);73GC_ArrayObjectModel *arrayObjectModel = &(extensions->indexableObjectModel);74J9Class *clazzPtr = J9GC_J9OBJECT_CLAZZ(objectPtr, extensions);7576_elementStride = J9ARRAYCLASS_GET_STRIDE(clazzPtr);77_basePtr = (fomrobject_t *)arrayObjectModel->getDataPointerForContiguous(_arrayPtr);78_scanPtr = _basePtr;79_endPtr = (fomrobject_t *)((uintptr_t)_basePtr + (arrayObjectModel->getSizeInElements(_arrayPtr) * _elementStride));80_elementClass = ((J9ArrayClass *) clazzPtr)->componentType;8182if (_scanPtr < _endPtr) {83_mixedObjectIterator.initialize(_omrVM, _elementClass, _scanPtr);84}85}8687/**88* Gets the current slot's array index.89* @return slot number (or zero based array index) of the last call to nextSlot.90*/91MMINLINE UDATA getIndex()92{93return ((uintptr_t)_scanPtr - (uintptr_t)_basePtr) / _elementStride;94}9596/**97* Sets the current slot's array index98* @param[in] index index to set scan index to99*/100MMINLINE void setIndex(UDATA index) {101_scanPtr = (fj9object_t *)((uintptr_t)_basePtr + (index * _elementStride));102}103104/**105* Restores the iterator state into this class106* @param[in] objectIteratorState partially scanned object iterator state107*/108MMINLINE void restore(GC_ObjectIteratorState *objectIteratorState)109{110_scanPtr = objectIteratorState->_scanPtr;111_endPtr = objectIteratorState->_endPtr;112}113114/**115* Saves the partially scanned state of this class116* @param[in] objectIteratorState where to store the state117*/118MMINLINE void save(GC_ObjectIteratorState *objectIteratorState)119{120objectIteratorState->_scanPtr = _scanPtr;121objectIteratorState->_endPtr = _endPtr;122}123124MMINLINE J9Object *getObject()125{126return (J9Object *)_arrayPtr;127}128129GC_FlattenedContiguousArrayIterator(OMR_VM *omrVM, J9Object *objectPtr)130: _arrayPtr((J9IndexableObject *)objectPtr)131, _mixedObjectIterator(omrVM)132, _elementStride(0)133, _basePtr(NULL)134, _scanPtr(NULL)135, _endPtr(NULL)136, _elementClass(NULL)137, _omrVM(omrVM)138{139initialize(objectPtr);140}141};142143#endif /* FLATTENEDCONTIGUOUSARRAYITERATOR_HPP_ */144145146