1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.ddmuilib.heap; 18 19 import com.android.ddmlib.NativeAllocationInfo; 20 21 import org.eclipse.jface.viewers.ILazyTreeContentProvider; 22 import org.eclipse.jface.viewers.TreeViewer; 23 import org.eclipse.jface.viewers.Viewer; 24 25 import java.util.List; 26 27 /** 28 * Content Provider for the native heap tree viewer in {@link NativeHeapPanel}. 29 * It expects a {@link NativeHeapSnapshot} as input, and provides the list of allocations 30 * in the heap dump as content to the UI. 31 */ 32 public final class NativeHeapProviderByAllocations implements ILazyTreeContentProvider { 33 private TreeViewer mViewer; 34 private boolean mDisplayZygoteMemory; 35 private NativeHeapSnapshot mNativeHeapDump; 36 NativeHeapProviderByAllocations(TreeViewer viewer, boolean displayZygotes)37 public NativeHeapProviderByAllocations(TreeViewer viewer, boolean displayZygotes) { 38 mViewer = viewer; 39 mDisplayZygoteMemory = displayZygotes; 40 } 41 dispose()42 public void dispose() { 43 } 44 inputChanged(Viewer viewer, Object oldInput, Object newInput)45 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 46 mNativeHeapDump = (NativeHeapSnapshot) newInput; 47 } 48 getParent(Object arg0)49 public Object getParent(Object arg0) { 50 return null; 51 } 52 updateChildCount(Object element, int currentChildCount)53 public void updateChildCount(Object element, int currentChildCount) { 54 int childCount = 0; 55 56 if (element == mNativeHeapDump) { // root element 57 childCount = getAllocations().size(); 58 } 59 60 mViewer.setChildCount(element, childCount); 61 } 62 updateElement(Object parent, int index)63 public void updateElement(Object parent, int index) { 64 Object item = null; 65 66 if (parent == mNativeHeapDump) { // root element 67 item = getAllocations().get(index); 68 } 69 70 mViewer.replace(parent, index, item); 71 mViewer.setChildCount(item, 0); 72 } 73 displayZygoteMemory(boolean en)74 public void displayZygoteMemory(boolean en) { 75 mDisplayZygoteMemory = en; 76 } 77 getAllocations()78 private List<NativeAllocationInfo> getAllocations() { 79 if (mDisplayZygoteMemory) { 80 return mNativeHeapDump.getAllocations(); 81 } else { 82 return mNativeHeapDump.getNonZygoteAllocations(); 83 } 84 } 85 } 86