private JavaHeapObject getNextObjToScan()

in application/org.openjdk.jmc.joverflow/src/main/java/org/openjdk/jmc/joverflow/stats/DepthFirstHeapScaner.java [146:217]


	private JavaHeapObject getNextObjToScan(JavaHeapObject oldObj) {
		long oldObjOfsInFile = -1;
		JavaHeapObject obj = null;

		while (!fieldsOrArrayElsStack.isEmpty()) {
			JavaThing[] fieldsOrElements = fieldsOrArrayElsStack.peek();
			TwoHandIndexContainer curIdxContainer = (TwoHandIndexContainer) refChain.getCurrentIndexContainer();
			int nextIdx = curIdxContainer.incrementAndGetBase();
			while (nextIdx < fieldsOrElements.length) {
				JavaThing objThing = fieldsOrElements[nextIdx];
				// Ignore null, primitive and already visited fields
				if (objThing != null && objThing instanceof JavaHeapObject) {
					obj = (JavaHeapObject) objThing;
					if (!obj.isVisited()) {
						if (optimizeForLocality && (obj instanceof JavaLazyReadObject)) {
							oldObjOfsInFile = (oldObj instanceof JavaLazyReadObject)
									? ((JavaLazyReadObject) oldObj).getObjOfsInFile() : -1;
							if (oldObjOfsInFile != -1) {
								curIdxContainer.setBase(nextIdx - 1);
								break; // We'll attempt to look for a closer located object
							}
						}

						curIdxContainer.setBase(nextIdx);
						curIdxContainer.set(nextIdx);
						return obj;
					} else {
						obj = null;
					}
				}
				nextIdx++;
			}

			if (obj == null) { // Fields or elements of the top object exhausted
				fieldsOrArrayElsStack.pop();
				refChain.pop2(); // Pop the index holder and object
				continue;
			}

			// Look through the next few elements to see if any of them is closer to old obj
			long curObjOfsInFile = ((JavaLazyReadObject) obj).getObjOfsInFile();
			long minDistance = Math.abs(curObjOfsInFile - oldObjOfsInFile);
			int bestIdx = nextIdx;
			int nCheckedFields = 0;
			nextIdx++;
			while (nextIdx < fieldsOrElements.length && nCheckedFields < 8) {
				JavaThing objThing = fieldsOrElements[nextIdx];
				if (objThing != null && objThing instanceof JavaHeapObject) {
					obj = (JavaHeapObject) objThing;
					if (!obj.isVisited()) {
						if (!(obj instanceof JavaLazyReadObject)) {
							curIdxContainer.set(nextIdx);
							return obj;
						}
						nCheckedFields++;
						curObjOfsInFile = ((JavaLazyReadObject) obj).getObjOfsInFile();
						long distance = Math.abs(curObjOfsInFile - oldObjOfsInFile);
						if (distance < minDistance) {
							bestIdx = nextIdx;
							minDistance = distance;
						}
					}
				}
				nextIdx++;
			}

			curIdxContainer.set(bestIdx);
			return (JavaHeapObject) fieldsOrElements[bestIdx];
		}

		return obj;
	}