Refinements to array and class object size computation.

* Rename dvmArrayObjectLength to dvmArrayObjectSize.  In the context
  of arrays length may refer to the length of the array data and not
  the size of the array object instance.

* Add a new method dvmClassObjectSize which computes the total size of
  a class object including its static fields.

Change-Id: I693ec8e66dfa5df35c9f35474c80411ea917c899
diff --git a/vm/Sync.c b/vm/Sync.c
index 8a1d92e..cc0b8e8 100644
--- a/vm/Sync.c
+++ b/vm/Sync.c
@@ -1315,7 +1315,7 @@
 {
     Thread *self, *thread;
     volatile u4 *lw;
-    size_t length;
+    size_t size;
     u4 lock, owner, hashState;
 
     if (obj == NULL) {
@@ -1341,12 +1341,12 @@
          * aligned word following the instance data.
          */
         if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
-            length = dvmArrayObjectLength((ArrayObject *)obj);
-            length = (length + 3) & ~3;
+            size = dvmArrayObjectSize((ArrayObject *)obj);
+            size = (size + 3) & ~3;
         } else {
-            length = obj->clazz->objectSize;
+            size = obj->clazz->objectSize;
         }
-        return *(u4 *)(((char *)obj) + length);
+        return *(u4 *)(((char *)obj) + size);
     } else if (hashState == LW_HASH_STATE_UNHASHED) {
         /*
          * The object has never been hashed.  Change the hash state to
diff --git a/vm/alloc/Alloc.c b/vm/alloc/Alloc.c
index 97b7a95..e46f9c2 100644
--- a/vm/alloc/Alloc.c
+++ b/vm/alloc/Alloc.c
@@ -211,8 +211,11 @@
     else
         flags = ALLOC_DEFAULT;
 
-//TODO: use clazz->objectSize for non-arrays
-    size = dvmObjectSizeInHeap(obj);
+    if (IS_CLASS_FLAG_SET(obj->clazz, CLASS_ISARRAY)) {
+        size = dvmArrayObjectSize((ArrayObject *)obj);
+    } else {
+        size = obj->clazz->objectSize;
+    }
 
     copy = dvmMalloc(size, flags);
     if (copy == NULL)
diff --git a/vm/oo/Array.c b/vm/oo/Array.c
index c76ea53..c1f5c3a 100644
--- a/vm/oo/Array.c
+++ b/vm/oo/Array.c
@@ -786,13 +786,14 @@
     return 0;  /* Quiet the compiler. */
 }
 
-size_t dvmArrayObjectLength(const ArrayObject *array)
+size_t dvmArrayObjectSize(const ArrayObject *array)
 {
-    size_t length;
+    size_t size;
 
-    length = offsetof(ArrayObject, contents);
-    length += array->length * arrayElementWidth(array);
-    return length;
+    assert(array != NULL);
+    size = offsetof(ArrayObject, contents);
+    size += array->length * arrayElementWidth(array);
+    return size;
 }
 
 /*
diff --git a/vm/oo/Array.h b/vm/oo/Array.h
index 17bdb22..36a847e 100644
--- a/vm/oo/Array.h
+++ b/vm/oo/Array.h
@@ -152,6 +152,9 @@
 bool dvmUnboxObjectArray(ArrayObject* dstArray, const ArrayObject* srcArray,
     ClassObject* dstElemClass);
 
-size_t dvmArrayObjectLength(const ArrayObject *array);
+/*
+ * Returns the size of the given array object in bytes.
+ */
+size_t dvmArrayObjectSize(const ArrayObject *array);
 
 #endif /*_DALVIK_OO_ARRAY*/
diff --git a/vm/oo/Class.c b/vm/oo/Class.c
index bbb8a35..b7211ec 100644
--- a/vm/oo/Class.c
+++ b/vm/oo/Class.c
@@ -4952,3 +4952,13 @@
 
     return dvmCompareDescriptorAndMethodProto(descriptor, method);
 }
+
+size_t dvmClassObjectSize(const ClassObject *clazz)
+{
+    size_t size;
+
+    assert(clazz != NULL);
+    size = offsetof(ClassObject, sfields);
+    size += sizeof(StaticField) * clazz->sfieldCount;
+    return size;
+}
diff --git a/vm/oo/Class.h b/vm/oo/Class.h
index 59e0da4..34fbff1 100644
--- a/vm/oo/Class.h
+++ b/vm/oo/Class.h
@@ -273,4 +273,9 @@
 int dvmCompareNameDescriptorAndMethod(const char* name,
     const char* descriptor, const Method* method);
 
+/*
+ * Returns the size of the given class object in bytes.
+ */
+size_t dvmClassObjectSize(const ClassObject *clazz);
+
 #endif /*_DALVIK_OO_CLASS*/