Do not allocate native objects on the managed heap.
Descriptors for the managed heap were being allocated in the managed
heap using mspace-malloc. With this change we use an ordinary malloc
to allocate those objects instead.
diff --git a/vm/alloc/HeapSource.c b/vm/alloc/HeapSource.c
index 197fb6c..666e88d 100644
--- a/vm/alloc/HeapSource.c
+++ b/vm/alloc/HeapSource.c
@@ -255,7 +255,7 @@
*
* These aren't exact, and should not be treated as such.
*/
-static void countAllocation(Heap *heap, const void *ptr, bool isObj)
+static void countAllocation(Heap *heap, const void *ptr)
{
HeapSource *hs;
@@ -263,11 +263,9 @@
heap->bytesAllocated += mspace_usable_size(heap->msp, ptr) +
HEAP_SOURCE_CHUNK_OVERHEAD;
- if (isObj) {
- heap->objectsAllocated++;
- hs = gDvm.gcHeap->heapSource;
- dvmHeapBitmapSetObjectBit(&hs->liveBits, ptr);
- }
+ heap->objectsAllocated++;
+ hs = gDvm.gcHeap->heapSource;
+ dvmHeapBitmapSetObjectBit(&hs->liveBits, ptr);
assert(heap->bytesAllocated < mspace_footprint(heap->msp));
}
@@ -520,18 +518,17 @@
goto fail;
}
- /* Allocate a descriptor from the heap we just created.
- */
- gcHeap = (GcHeap *)mspace_malloc(msp, sizeof(*gcHeap));
+ gcHeap = (GcHeap *)malloc(sizeof(*gcHeap));
if (gcHeap == NULL) {
LOGE_HEAP("Can't allocate heap descriptor\n");
goto fail;
}
memset(gcHeap, 0, sizeof(*gcHeap));
- hs = (HeapSource *)mspace_malloc(msp, sizeof(*hs));
+ hs = (HeapSource *)malloc(sizeof(*hs));
if (hs == NULL) {
LOGE_HEAP("Can't allocate heap source\n");
+ free(gcHeap);
goto fail;
}
memset(hs, 0, sizeof(*hs));
@@ -569,9 +566,6 @@
gcHeap->markContext.bitmap = &hs->markBits;
gcHeap->heapSource = hs;
- countAllocation(hs2heap(hs), gcHeap, false);
- countAllocation(hs2heap(hs), hs, false);
-
gHs = hs;
return gcHeap;
@@ -636,7 +630,9 @@
dvmHeapBitmapDelete(&hs->markBits);
freeMarkStack(&(*gcHeap)->markContext.stack);
munmap(hs->heapBase, hs->heapLength);
+ free(hs);
gHs = NULL;
+ free(*gcHeap);
*gcHeap = NULL;
}
}
@@ -816,7 +812,7 @@
if (ptr == NULL) {
return NULL;
}
- countAllocation(heap, ptr, true);
+ countAllocation(heap, ptr);
/*
* Check to see if a concurrent GC should be initiated.
*/