Search in sources :

Example 1 with Heap

use of com.squareup.haha.perflib.Heap in project leakcanary by square.

the class HahaHelper method asString.

static String asString(Object stringObject) {
    Instance instance = (Instance) stringObject;
    List<ClassInstance.FieldValue> values = classInstanceValues(instance);
    Integer count = fieldValue(values, "count");
    Object value = fieldValue(values, "value");
    Integer offset;
    ArrayInstance charArray;
    if (isCharArray(value)) {
        charArray = (ArrayInstance) value;
        offset = 0;
        // https://android-review.googlesource.com/#/c/83611/
        if (hasField(values, "offset")) {
            offset = fieldValue(values, "offset");
        }
    } else {
        // In M preview 2, the underlying char buffer resides in the heap with ID equaling the
        // String's ID + 16.
        // https://android-review.googlesource.com/#/c/160380/2/android/src/com/android/tools/idea/
        // editors/hprof/descriptors/InstanceFieldDescriptorImpl.java
        // This workaround is only needed for M preview 2, as it has been fixed on the hprof
        // generation end by reintroducing a virtual "value" variable.
        // https://android.googlesource.com/platform/art/+/master/runtime/hprof/hprof.cc#1242
        Heap heap = instance.getHeap();
        Instance inlineInstance = heap.getInstance(instance.getId() + 16);
        if (isCharArray(inlineInstance)) {
            charArray = (ArrayInstance) inlineInstance;
            offset = 0;
        } else {
            throw new UnsupportedOperationException("Could not find char array in " + instance);
        }
    }
    checkNotNull(count, "count");
    checkNotNull(charArray, "charArray");
    checkNotNull(offset, "offset");
    if (count == 0) {
        return "";
    }
    char[] chars = charArray.asCharArray(offset, count);
    return new String(chars);
}
Also used : Instance(com.squareup.haha.perflib.Instance) ClassInstance(com.squareup.haha.perflib.ClassInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) ArrayInstance(com.squareup.haha.perflib.ArrayInstance) Heap(com.squareup.haha.perflib.Heap)

Aggregations

ArrayInstance (com.squareup.haha.perflib.ArrayInstance)1 ClassInstance (com.squareup.haha.perflib.ClassInstance)1 Heap (com.squareup.haha.perflib.Heap)1 Instance (com.squareup.haha.perflib.Instance)1