OSDN Git Service

Added dvmLinearAllocContains().
authorAndy McFadden <fadden@android.com>
Mon, 27 Jul 2009 21:44:22 +0000 (14:44 -0700)
committerAndy McFadden <fadden@android.com>
Mon, 27 Jul 2009 21:44:22 +0000 (14:44 -0700)
vm/LinearAlloc.c
vm/LinearAlloc.h
vm/oo/Class.c

index 84bb103..5863fb0 100644 (file)
@@ -688,3 +688,21 @@ static void checkAllFree(Object* classLoader)
     dvmUnlockMutex(&pHdr->lock);
 }
 
+/*
+ * Determine if [start, start+length) is contained in the in-use area of
+ * a single LinearAlloc.  The full set of linear allocators is scanned.
+ *
+ * [ Since we currently only have one region, this is pretty simple.  In
+ * the future we'll need to traverse a table of class loaders. ]
+ */
+bool dvmLinearAllocContains(void* start, size_t length)
+{
+    LinearAllocHdr* pHdr = getHeader(NULL);
+
+    if (pHdr == NULL)
+        return false;
+
+    return (char*) start >= pHdr->mapAddr &&
+           ((char*)start + length) <= (pHdr->mapAddr + pHdr->curOffset);
+}
+
index 9c1d096..c9a5c08 100644 (file)
@@ -112,4 +112,10 @@ char* dvmLinearStrdup(Object* classLoader, const char* str);
  */
 void dvmLinearAllocDump(Object* classLoader);
 
+/*
+ * Determine if [start, start+length) is contained in the in-use area of
+ * a single LinearAlloc.  The full set of linear allocators is scanned.
+ */
+bool dvmLinearAllocContains(void* start, size_t length);
+
 #endif /*_DALVIK_LINEARALLOC*/
index 330c381..23ffb0b 100644 (file)
@@ -271,6 +271,12 @@ static void linearAllocTests()
     char* str = dvmLinearStrdup(NULL, "This is a test!");
     LOGI("GOT: '%s'\n", str);
 
+    /* try to check the bounds; allocator may round allocation size up */
+    fiddle = dvmLinearAlloc(NULL, 12);
+    LOGI("Should be 1: %d\n", dvmLinearAllocContains(fiddle, 12));
+    LOGI("Should be 0: %d\n", dvmLinearAllocContains(fiddle, 13));
+    LOGI("Should be 0: %d\n", dvmLinearAllocContains(fiddle - 128*1024, 1));
+
     dvmLinearAllocDump(NULL);
     dvmLinearFree(NULL, str);
 }