Convert the internal and in-line natives to C++.

Change-Id: I2ece682bc3b4d3b55ab27c60fd84a0b3243d7ca6
diff --git a/libdex/CmdUtils.h b/libdex/CmdUtils.h
index 7a85287..3f05aff 100644
--- a/libdex/CmdUtils.h
+++ b/libdex/CmdUtils.h
@@ -31,6 +31,10 @@
 #ifndef _LIBDEX_CMDUTILS
 #define _LIBDEX_CMDUTILS
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* encode the result of unzipping to a file */
 typedef enum UnzipToFileResult {
     kUTFRSuccess = 0,
@@ -70,4 +74,8 @@
 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
     const char* outFileName, bool quiet);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_LIBDEX_CMDUTILS*/
diff --git a/libdex/DexCatch.h b/libdex/DexCatch.h
index c0eec7c..7c40bc5 100644
--- a/libdex/DexCatch.h
+++ b/libdex/DexCatch.h
@@ -24,6 +24,10 @@
 #include "DexFile.h"
 #include "Leb128.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Catch handler entry, used while iterating over catch_handler_items.
  */
@@ -159,4 +163,8 @@
     }
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/libdex/DexClass.h b/libdex/DexClass.h
index 3d1e11b..6003fcf 100644
--- a/libdex/DexClass.h
+++ b/libdex/DexClass.h
@@ -24,6 +24,10 @@
 #include "DexFile.h"
 #include "Leb128.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /* expanded form of a class_data_item header */
 typedef struct DexClassDataHeader {
     u4 staticFieldsSize;
@@ -159,4 +163,8 @@
     *lastIndex = index;
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/libdex/DexDataMap.h b/libdex/DexDataMap.h
index fa556d5..654933c 100644
--- a/libdex/DexDataMap.h
+++ b/libdex/DexDataMap.h
@@ -23,6 +23,10 @@
 
 #include "DexFile.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 typedef struct DexDataMap {
     u4 count;    /* number of items currently in the map */
     u4 max;      /* maximum number of items that may be held */
@@ -70,4 +74,8 @@
     return dexDataMapVerify(map, offset, type);
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_LIBDEX_DEXDATAMAP*/
diff --git a/libdex/DexFile.h b/libdex/DexFile.h
index 45f9a91..6652286 100644
--- a/libdex/DexFile.h
+++ b/libdex/DexFile.h
@@ -36,6 +36,10 @@
 #include "vm/Common.h"      // basic type defs, e.g. u1/u2/u4/u8, and LOG
 #include "libdex/SysUtil.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * gcc-style inline management -- ensures we have a copy of all functions
  * in the library, so code that links against us will work whether or not
@@ -960,4 +964,8 @@
  */
 PrimitiveType dexGetPrimitiveTypeFromDescriptorChar(char descriptorChar);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_LIBDEX_DEXFILE*/
diff --git a/libdex/DexOpcodes.h b/libdex/DexOpcodes.h
index 07188b2..68b8498 100644
--- a/libdex/DexOpcodes.h
+++ b/libdex/DexOpcodes.h
@@ -30,6 +30,10 @@
 
 #include "DexFile.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * kMaxOpcodeValue: the highest possible raw (unpacked) opcode value
  *
@@ -1133,4 +1137,8 @@
  */
 const char* dexGetOpcodeName(Opcode op);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_LIBDEX_DEXOPCODES*/
diff --git a/libdex/DexOptData.h b/libdex/DexOptData.h
index 69eda01..562553d 100644
--- a/libdex/DexOptData.h
+++ b/libdex/DexOptData.h
@@ -24,6 +24,10 @@
 
 #include "libdex/DexFile.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Parse the optimized data tables in the given dex file.
  *
@@ -39,4 +43,8 @@
  */
 u4 dexComputeOptChecksum(const DexOptHeader* pOptHeader);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* def _LIBDEX_DEXOPTDATA */
diff --git a/libdex/DexProto.h b/libdex/DexProto.h
index 50d4bf3..a300d3f 100644
--- a/libdex/DexProto.h
+++ b/libdex/DexProto.h
@@ -23,6 +23,10 @@
 
 #include "DexFile.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Single-thread single-string cache. This structure holds a pointer to
  * a string which is semi-automatically manipulated by some of the
@@ -226,6 +230,8 @@
 const char* dexParameterIteratorNextDescriptor(
         DexParameterIterator* pIterator);
 
-
+#ifdef __cplusplus
+}
+#endif
 
 #endif /*_LIBDEX_DEXPROTO*/
diff --git a/libdex/DexUtf.h b/libdex/DexUtf.h
index a7eb28c..88d98b0 100644
--- a/libdex/DexUtf.h
+++ b/libdex/DexUtf.h
@@ -23,6 +23,10 @@
 
 #include "DexFile.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Retrieve the next UTF-16 character from a UTF-8 string.
  *
@@ -128,4 +132,8 @@
  * is for anything but "void". */
 bool dexIsFieldDescriptor(const char* s);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* def _LIBDEX_DEXUTF */
diff --git a/libdex/InstrUtils.h b/libdex/InstrUtils.h
index f09429c..88804cf 100644
--- a/libdex/InstrUtils.h
+++ b/libdex/InstrUtils.h
@@ -23,6 +23,10 @@
 #include "DexFile.h"
 #include "DexOpcodes.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Possible instruction formats associated with Dalvik opcodes.
  *
@@ -198,4 +202,8 @@
  */
 void dexDecodeInstruction(const u2* insns, DecodedInstruction* pDec);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_LIBDEX_INSTRUTILS*/
diff --git a/libdex/Leb128.h b/libdex/Leb128.h
index 41799fe..3287c16 100644
--- a/libdex/Leb128.h
+++ b/libdex/Leb128.h
@@ -23,6 +23,10 @@
 
 #include "DexFile.h"
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * Reads an unsigned LEB128 value, updating the given pointer to point
  * just past the end of the read value. This function tolerates
@@ -161,4 +165,8 @@
     return count;
 }
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif
diff --git a/libdex/ZipArchive.h b/libdex/ZipArchive.h
index bf4edf9..872ceed 100644
--- a/libdex/ZipArchive.h
+++ b/libdex/ZipArchive.h
@@ -23,6 +23,9 @@
 #include "SysUtil.h"
 #include "DexFile.h"            // need DEX_INLINE
 
+#ifdef __cplusplus
+extern "C" {
+#endif
 
 /*
  * Trivial typedef to ensure that ZipEntry is not treated as a simple
@@ -179,4 +182,8 @@
 u4 dexInitCrc32(void);
 u4 dexComputeCrc32(u4 crc, const void* buf, size_t len);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_LIBDEX_ZIPARCHIVE*/
diff --git a/libdex/sha1.h b/libdex/sha1.h
index 65cf667..0cef623 100644
--- a/libdex/sha1.h
+++ b/libdex/sha1.h
@@ -4,6 +4,10 @@
 #ifndef _DALVIK_SHA1
 #define _DALVIK_SHA1
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 typedef struct {
     unsigned long state[5];
     unsigned long count[2];
@@ -17,4 +21,8 @@
     unsigned long len);
 void SHA1Final(unsigned char digest[HASHSIZE], SHA1_CTX* context);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_DALVIK_SHA1*/
diff --git a/vm/Dvm.mk b/vm/Dvm.mk
index add2207..ca09183 100644
--- a/vm/Dvm.mk
+++ b/vm/Dvm.mk
@@ -148,35 +148,35 @@
 	jdwp/JdwpSocket.c \
 	mterp/Mterp.c.arm \
 	mterp/out/InterpC-portable.c.arm \
-	native/InternalNative.c \
-	native/dalvik_bytecode_OpcodeInfo.c \
-	native/dalvik_system_DexFile.c \
-	native/dalvik_system_VMDebug.c \
-	native/dalvik_system_VMRuntime.c \
-	native/dalvik_system_VMStack.c \
-	native/dalvik_system_Zygote.c \
-	native/java_lang_Class.c \
-	native/java_lang_Double.c \
-	native/java_lang_Float.c \
-	native/java_lang_Math.c \
-	native/java_lang_Object.c \
-	native/java_lang_Runtime.c \
-	native/java_lang_String.c \
-	native/java_lang_System.c \
-	native/java_lang_Throwable.c \
-	native/java_lang_VMClassLoader.c \
-	native/java_lang_VMThread.c \
-	native/java_lang_reflect_AccessibleObject.c \
-	native/java_lang_reflect_Array.c \
-	native/java_lang_reflect_Constructor.c \
-	native/java_lang_reflect_Field.c \
-	native/java_lang_reflect_Method.c \
-	native/java_lang_reflect_Proxy.c \
-	native/java_util_concurrent_atomic_AtomicLong.c \
-	native/org_apache_harmony_dalvik_NativeTestTarget.c \
-	native/org_apache_harmony_dalvik_ddmc_DdmServer.c \
-	native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.c \
-	native/sun_misc_Unsafe.c \
+	native/InternalNative.cpp \
+	native/dalvik_bytecode_OpcodeInfo.cpp \
+	native/dalvik_system_DexFile.cpp \
+	native/dalvik_system_VMDebug.cpp \
+	native/dalvik_system_VMRuntime.cpp \
+	native/dalvik_system_VMStack.cpp \
+	native/dalvik_system_Zygote.cpp \
+	native/java_lang_Class.cpp \
+	native/java_lang_Double.cpp \
+	native/java_lang_Float.cpp \
+	native/java_lang_Math.cpp \
+	native/java_lang_Object.cpp \
+	native/java_lang_Runtime.cpp \
+	native/java_lang_String.cpp \
+	native/java_lang_System.cpp \
+	native/java_lang_Throwable.cpp \
+	native/java_lang_VMClassLoader.cpp \
+	native/java_lang_VMThread.cpp \
+	native/java_lang_reflect_AccessibleObject.cpp \
+	native/java_lang_reflect_Array.cpp \
+	native/java_lang_reflect_Constructor.cpp \
+	native/java_lang_reflect_Field.cpp \
+	native/java_lang_reflect_Method.cpp \
+	native/java_lang_reflect_Proxy.cpp \
+	native/java_util_concurrent_atomic_AtomicLong.cpp \
+	native/org_apache_harmony_dalvik_NativeTestTarget.cpp \
+	native/org_apache_harmony_dalvik_ddmc_DdmServer.cpp \
+	native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cpp \
+	native/sun_misc_Unsafe.cpp \
 	oo/AccessCheck.c \
 	oo/Array.c \
 	oo/Class.c \
@@ -224,9 +224,9 @@
 	dalvik \
 	dalvik/vm \
 	external/zlib \
+	libcore/include \
 	$(KERNEL_HEADERS)
 
-
 ifeq ($(dvm_simulator),true)
   LOCAL_LDLIBS += -lpthread -ldl
   ifeq ($(HOST_OS),linux)
diff --git a/vm/InlineNative.h b/vm/InlineNative.h
index 4f86cef..bf08412 100644
--- a/vm/InlineNative.h
+++ b/vm/InlineNative.h
@@ -125,6 +125,75 @@
  */
 Method* dvmResolveInlineNative(int opIndex);
 
+/*
+ * The actual inline native definitions.
+ */
+bool javaLangString_charAt(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                           JValue* pResult);
+
+bool javaLangString_compareTo(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                              JValue* pResult);
+
+bool javaLangString_equals(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                           JValue* pResult);
+
+bool javaLangString_length(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                           JValue* pResult);
+
+bool javaLangString_isEmpty(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                            JValue* pResult);
+
+bool javaLangString_fastIndexOf_II(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                                   JValue* pResult);
+
+bool javaLangMath_abs_int(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                          JValue* pResult);
+
+bool javaLangMath_abs_long(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                           JValue* pResult);
+
+bool javaLangMath_abs_float(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                            JValue* pResult);
+
+bool javaLangMath_abs_double(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                             JValue* pResult);
+
+bool javaLangMath_min_int(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                          JValue* pResult);
+
+bool javaLangMath_max_int(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                          JValue* pResult);
+
+bool javaLangMath_sqrt(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                       JValue* pResult);
+
+bool javaLangMath_cos(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                      JValue* pResult);
+
+bool javaLangMath_sin(u4 arg0, u4 arg1, u4 arg2, u4 arg3,
+                      JValue* pResult);
+
+bool javaLangFloat_floatToIntBits(u4 arg0, u4 arg1, u4 arg2, u4 arg,
+                                  JValue* pResult);
+
+bool javaLangFloat_floatToRawIntBits(u4 arg0, u4 arg1, u4 arg2, u4 arg,
+                                     JValue* pResult);
+
+bool javaLangFloat_intBitsToFloat(u4 arg0, u4 arg1, u4 arg2, u4 arg,
+                                  JValue* pResult);
+
+bool javaLangDouble_doubleToLongBits(u4 arg0, u4 arg1, u4 arg2, u4 arg,
+                                     JValue* pResult);
+
+bool javaLangDouble_longBitsToDouble(u4 arg0, u4 arg1, u4 arg2, u4 arg,
+                                     JValue* pResult);
+
+bool javaLangDouble_doubleToRawLongBits(u4 arg0, u4 arg1, u4 arg2,
+                                        u4 arg, JValue* pResult);
+
+bool javaLangDouble_longBitsToDouble(u4 arg0, u4 arg1, u4 arg2, u4 arg,
+                                     JValue* pResult);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/vm/UtfString.c b/vm/UtfString.c
index 58545cd..ec35cdf 100644
--- a/vm/UtfString.c
+++ b/vm/UtfString.c
@@ -418,7 +418,7 @@
                   len1 * sizeof(u2));
 }
 
-ArrayObject* dvmCreateStringArray(char** strings, size_t count)
+ArrayObject* dvmCreateStringArray(const char** strings, size_t count)
 {
     Thread* self = dvmThreadSelf();
 
diff --git a/vm/UtfString.h b/vm/UtfString.h
index f0413a4..a1ee712 100644
--- a/vm/UtfString.h
+++ b/vm/UtfString.h
@@ -67,7 +67,7 @@
  *
  * Returns NULL and throws an exception on failure.
  */
-ArrayObject* dvmCreateStringArray(char** strings, size_t count);
+ArrayObject* dvmCreateStringArray(const char** strings, size_t count);
 
 /*
  * Create a java/lang/String from a C string.
diff --git a/vm/interp/Stack.c b/vm/interp/Stack.c
index 97fbeb5..85e54ff 100644
--- a/vm/interp/Stack.c
+++ b/vm/interp/Stack.c
@@ -971,37 +971,24 @@
 }
 
 /*
- * Create a flat array of methods that comprise the current interpreter
- * stack trace.  Pass in the current frame ptr.
- *
- * Allocates a new array and fills it with method pointers.  Break frames
- * are skipped, but reflection invocations are not.  The caller must free
- * "*pArray".
+ * Fill a flat array of methods that comprise the current interpreter
+ * stack trace.  Pass in the current frame ptr.  Break frames are
+ * skipped, but reflection invocations are not.
  *
  * The current frame will be in element 0.
- *
- * Returns "true" on success, "false" on failure (e.g. malloc failed).
  */
-bool dvmCreateStackTraceArray(const void* fp, const Method*** pArray,
-    int* pLength)
+void dvmFillStackTraceArray(const void* fp, const Method** array, size_t length)
 {
-    const Method** array;
-    int idx, depth;
-
-    depth = dvmComputeExactFrameDepth(fp);
-    array = (const Method**) malloc(depth * sizeof(Method*));
-    if (array == NULL)
-        return false;
-
-    for (idx = 0; fp != NULL; fp = SAVEAREA_FROM_FP(fp)->prevFrame) {
-        if (!dvmIsBreakFrame((u4*)fp))
-            array[idx++] = SAVEAREA_FROM_FP(fp)->method;
+    assert(fp != NULL);
+    assert(array != NULL);
+    size_t i = 0;
+    while (fp != NULL) {
+        if (!dvmIsBreakFrame((u4*)fp)) {
+            assert(i < length);
+            array[i++] = SAVEAREA_FROM_FP(fp)->method;
+        }
+        fp = SAVEAREA_FROM_FP(fp)->prevFrame;
     }
-    assert(idx == depth);
-
-    *pArray = array;
-    *pLength = depth;
-    return true;
 }
 
 /*
diff --git a/vm/interp/Stack.h b/vm/interp/Stack.h
index 403cae2..99bef1a 100644
--- a/vm/interp/Stack.h
+++ b/vm/interp/Stack.h
@@ -266,11 +266,10 @@
 ClassObject* dvmGetCaller3Class(const void* curFrame);
 
 /*
- * Allocate and fill an array of method pointers representing the current
- * stack trace (element 0 is current frame).
+ * Fill an array of method pointers representing the current stack
+ * trace (element 0 is current frame).
  */
-bool dvmCreateStackTraceArray(const void* fp, const Method*** pArray,
-    int* pLength);
+void dvmFillStackTraceArray(const void* fp, const Method** array, size_t length);
 
 /*
  * Common handling for stack overflow.
diff --git a/vm/native/InternalNative.c b/vm/native/InternalNative.cpp
similarity index 100%
rename from vm/native/InternalNative.c
rename to vm/native/InternalNative.cpp
diff --git a/vm/native/dalvik_bytecode_OpcodeInfo.c b/vm/native/dalvik_bytecode_OpcodeInfo.cpp
similarity index 95%
rename from vm/native/dalvik_bytecode_OpcodeInfo.c
rename to vm/native/dalvik_bytecode_OpcodeInfo.cpp
index cabd7b1..3861fef 100644
--- a/vm/native/dalvik_bytecode_OpcodeInfo.c
+++ b/vm/native/dalvik_bytecode_OpcodeInfo.cpp
@@ -31,7 +31,7 @@
 static void Dalvik_dalvik_bytecode_OpcodeInfo_isInvoke(const u4* args,
     JValue* pResult)
 {
-    jint opcode = (jint) args[0];
+    Opcode opcode = static_cast<Opcode>(args[0]);
     int flags = dexGetFlagsFromOpcode(opcode);
     bool result = (flags & kInstrInvoke) != 0;
     RETURN_BOOLEAN(result);
diff --git a/vm/native/dalvik_system_DexFile.c b/vm/native/dalvik_system_DexFile.cpp
similarity index 100%
rename from vm/native/dalvik_system_DexFile.c
rename to vm/native/dalvik_system_DexFile.cpp
diff --git a/vm/native/dalvik_system_VMDebug.c b/vm/native/dalvik_system_VMDebug.cpp
similarity index 96%
rename from vm/native/dalvik_system_VMDebug.c
rename to vm/native/dalvik_system_VMDebug.cpp
index 146f562..cfd8ebc 100644
--- a/vm/native/dalvik_system_VMDebug.c
+++ b/vm/native/dalvik_system_VMDebug.cpp
@@ -21,10 +21,8 @@
 #include "native/InternalNativePriv.h"
 #include "hprof/Hprof.h"
 
-#include <cutils/array.h>
 #include <string.h>
 #include <unistd.h>
-#include <errno.h>
 
 
 /*
@@ -51,30 +49,20 @@
  * static String[] getVmFeatureList()
  *
  * Return a set of strings describing available VM features (this is chiefly
- * of interest to DDMS).  Some features may be controlled by compile-time
- * or command-line flags.
+ * of interest to DDMS).
  */
 static void Dalvik_dalvik_system_VMDebug_getVmFeatureList(const u4* args,
     JValue* pResult)
 {
-    Array* features = arrayCreate();
-
-    /* VM responds to DDMS method profiling requests */
-    arrayAdd(features, "method-trace-profiling");
-    arrayAdd(features, "method-trace-profiling-streaming");
-    /* VM responds to DDMS heap dump requests */
-    arrayAdd(features, "hprof-heap-dump");
-    arrayAdd(features, "hprof-heap-dump-streaming");
-
-    char** strings = (char**) arrayUnwrap(features);
-    int count = arraySize(features);
-    ArrayObject* result = dvmCreateStringArray(strings, count);
+    const char* strings[] = { "method-trace-profiling",
+                              "method-trace-profiling-streaming",
+                              "hprof-heap-dump",
+                              "hprof-heap-dump-streaming" };
+    ArrayObject* result = dvmCreateStringArray(strings, NELEM(strings));
     dvmReleaseTrackedAlloc((Object*) result, dvmThreadSelf());
-    arrayFree(features);
     RETURN_PTR(result);
 }
 
-
 /* These must match the values in dalvik.system.VMDebug.
  */
 enum {
@@ -416,7 +404,7 @@
     ArrayObject* countArray = (ArrayObject*) args[0];
 
     if (countArray != NULL) {
-        int* storage = (int*) countArray->contents;
+        int* storage = (int*)(void*)countArray->contents;
         u4 length = countArray->length;
 
         /*
diff --git a/vm/native/dalvik_system_VMRuntime.c b/vm/native/dalvik_system_VMRuntime.cpp
similarity index 98%
rename from vm/native/dalvik_system_VMRuntime.c
rename to vm/native/dalvik_system_VMRuntime.cpp
index 3642f90..c133e64 100644
--- a/vm/native/dalvik_system_VMRuntime.c
+++ b/vm/native/dalvik_system_VMRuntime.cpp
@@ -145,7 +145,7 @@
 static void Dalvik_dalvik_system_VMRuntime_properties(const u4* args,
     JValue* pResult)
 {
-    char** strings = (char**) arrayUnwrap(gDvm.properties);
+    const char** strings = (const char**) arrayUnwrap(gDvm.properties);
     int count = arraySize(gDvm.properties);
     ArrayObject* result = dvmCreateStringArray(strings, count);
     dvmReleaseTrackedAlloc((Object*) result, dvmThreadSelf());
diff --git a/vm/native/dalvik_system_VMStack.c b/vm/native/dalvik_system_VMStack.cpp
similarity index 81%
rename from vm/native/dalvik_system_VMStack.c
rename to vm/native/dalvik_system_VMStack.cpp
index 045a100..05b439f 100644
--- a/vm/native/dalvik_system_VMStack.c
+++ b/vm/native/dalvik_system_VMStack.cpp
@@ -18,9 +18,9 @@
  * dalvik.system.VMStack
  */
 #include "Dalvik.h"
+#include "UniquePtr.h"
 #include "native/InternalNativePriv.h"
 
-
 /*
  * public static ClassLoader getCallingClassLoader()
  *
@@ -81,36 +81,23 @@
     JValue* pResult)
 {
     /* note "maxSize" is unsigned, so -1 turns into a very large value */
-    unsigned int maxSize = args[0];
-    unsigned int size = 0;
-    const unsigned int kSkip = 2;
-    const Method** methods = NULL;
-    int methodCount;
+    size_t maxSize = args[0];
+    size_t size = 0;
+    const size_t kSkip = 2;
 
     /*
      * Get an array with the stack trace in it.
      */
-    if (!dvmCreateStackTraceArray(dvmThreadSelf()->curFrame, &methods,
-            &methodCount))
-    {
-        LOGE("Failed to create stack trace array\n");
-        dvmThrowInternalError(NULL);
-        RETURN_VOID();
-    }
-
-    //int i;
-    //LOGI("dvmCreateStackTraceArray results:\n");
-    //for (i = 0; i < methodCount; i++) {
-    //    LOGI(" %2d: %s.%s\n",
-    //        i, methods[i]->clazz->descriptor, methods[i]->name);
-    //}
+    void *fp = dvmThreadSelf()->curFrame;
+    size_t depth = dvmComputeExactFrameDepth(fp);
+    UniquePtr<const Method*[]> methods(new const Method*[depth]);
+    dvmFillStackTraceArray(fp, methods.get(), depth);
 
     /*
      * Run through the array and count up how many elements there are.
      */
-    unsigned int idx;
-    for (idx = kSkip; (int) idx < methodCount && size < maxSize; idx++) {
-        const Method* meth = methods[idx];
+    for (size_t i = kSkip; i < depth && size < maxSize; ++i) {
+        const Method* meth = methods[i];
 
         if (dvmIsReflectionMethod(meth))
             continue;
@@ -122,37 +109,36 @@
      * Create an array object to hold the classes.
      * TODO: can use gDvm.classJavaLangClassArray here?
      */
-    ClassObject* classArrayClass = NULL;
-    ArrayObject* classes = NULL;
-    classArrayClass = dvmFindArrayClass("[Ljava/lang/Class;", NULL);
+    ClassObject* classArrayClass = dvmFindArrayClass("[Ljava/lang/Class;",
+                                                     NULL);
     if (classArrayClass == NULL) {
-        LOGW("Unable to find java.lang.Class array class\n");
-        goto bail;
+        LOGW("Unable to find java.lang.Class array class");
+        return;
     }
-    classes = dvmAllocArray(classArrayClass, size, kObjectArrayRefWidth,
-                ALLOC_DEFAULT);
+    ArrayObject* classes = dvmAllocArray(classArrayClass,
+                                         size,
+                                         kObjectArrayRefWidth,
+                                         ALLOC_DEFAULT);
     if (classes == NULL) {
-        LOGW("Unable to allocate class array (%d elems)\n", size);
-        goto bail;
+        LOGW("Unable to allocate class array of %zd elements", size);
+        return;
     }
 
     /*
      * Fill in the array.
      */
-    unsigned int objCount = 0;
-    for (idx = kSkip; (int) idx < methodCount; idx++) {
-        if (dvmIsReflectionMethod(methods[idx])) {
+    size_t objCount = 0;
+    for (size_t i = kSkip; i < depth; ++i) {
+        if (dvmIsReflectionMethod(methods[i])) {
             continue;
         }
-        dvmSetObjectArrayElement(classes, objCount,
-                                 (Object *)methods[idx]->clazz);
+        Object* klass = (Object *)methods[i]->clazz;
+        dvmSetObjectArrayElement(classes, objCount, klass);
         objCount++;
     }
     assert(objCount == classes->length);
 
-bail:
-    free(methods);
-    dvmReleaseTrackedAlloc((Object*) classes, NULL);
+    dvmReleaseTrackedAlloc((Object*)classes, NULL);
     RETURN_PTR(classes);
 }
 
diff --git a/vm/native/dalvik_system_Zygote.c b/vm/native/dalvik_system_Zygote.cpp
similarity index 98%
rename from vm/native/dalvik_system_Zygote.c
rename to vm/native/dalvik_system_Zygote.cpp
index 6f7e6fa..62cd0c2 100644
--- a/vm/native/dalvik_system_Zygote.c
+++ b/vm/native/dalvik_system_Zygote.cpp
@@ -168,7 +168,7 @@
 
     /* just in case gid_t and u4 are different... */
     gids = (gid_t *)alloca(sizeof(gid_t) * gidArray->length);
-    contents = (s4 *)gidArray->contents;
+    contents = (s4 *)(void *)gidArray->contents;
 
     for (i = 0 ; i < gidArray->length ; i++) {
         gids[i] = (gid_t) contents[i];
@@ -196,11 +196,11 @@
 
     memset (&rlim, 0, sizeof(rlim));
 
-    ArrayObject** tuples = (ArrayObject **)(rlimits->contents);
+    ArrayObject** tuples = (ArrayObject **)(void *)rlimits->contents;
 
     for (i = 0; i < rlimits->length; i++) {
         ArrayObject * rlimit_tuple = tuples[i];
-        s4* contents = (s4 *)rlimit_tuple->contents;
+        s4* contents = (s4 *)(void *)rlimit_tuple->contents;
         int err;
 
         if (rlimit_tuple->length != 3) {
diff --git a/vm/native/java_lang_Class.c b/vm/native/java_lang_Class.cpp
similarity index 99%
rename from vm/native/java_lang_Class.c
rename to vm/native/java_lang_Class.cpp
index a3839a3..f05faef 100644
--- a/vm/native/java_lang_Class.c
+++ b/vm/native/java_lang_Class.cpp
@@ -192,7 +192,7 @@
         }
     } else if (publicOnly) {
         u4 count, newIdx, publicCount = 0;
-        ClassObject** pSource = (ClassObject**) classes->contents;
+        ClassObject** pSource = (ClassObject**)(void*)classes->contents;
         u4 length = classes->length;
 
         /* count up public classes */
diff --git a/vm/native/java_lang_Double.c b/vm/native/java_lang_Double.cpp
similarity index 100%
rename from vm/native/java_lang_Double.c
rename to vm/native/java_lang_Double.cpp
diff --git a/vm/native/java_lang_Float.c b/vm/native/java_lang_Float.cpp
similarity index 100%
rename from vm/native/java_lang_Float.c
rename to vm/native/java_lang_Float.cpp
diff --git a/vm/native/java_lang_Math.c b/vm/native/java_lang_Math.cpp
similarity index 100%
rename from vm/native/java_lang_Math.c
rename to vm/native/java_lang_Math.cpp
diff --git a/vm/native/java_lang_Object.c b/vm/native/java_lang_Object.cpp
similarity index 100%
rename from vm/native/java_lang_Object.c
rename to vm/native/java_lang_Object.cpp
diff --git a/vm/native/java_lang_Runtime.c b/vm/native/java_lang_Runtime.cpp
similarity index 100%
rename from vm/native/java_lang_Runtime.c
rename to vm/native/java_lang_Runtime.cpp
diff --git a/vm/native/java_lang_String.c b/vm/native/java_lang_String.cpp
similarity index 100%
rename from vm/native/java_lang_String.c
rename to vm/native/java_lang_String.cpp
diff --git a/vm/native/java_lang_System.c b/vm/native/java_lang_System.cpp
similarity index 97%
rename from vm/native/java_lang_System.c
rename to vm/native/java_lang_System.cpp
index ad0b397..e6b2f1a 100644
--- a/vm/native/java_lang_System.c
+++ b/vm/native/java_lang_System.cpp
@@ -51,7 +51,7 @@
 #endif
 
 #ifdef HAVE_MEMMOVE_WORDS
-extern void _memmove_words(void* dest, const void* src, size_t n);
+extern "C" void _memmove_words(void* dest, const void* src, size_t n);
 #define move16 _memmove_words
 #define move32 _memmove_words
 #else
@@ -252,8 +252,8 @@
             int copyCount;
             ClassObject*   clazz = NULL;
 
-            srcObj = ((Object**) srcArray->contents) + srcPos;
-            dstObj = ((Object**) dstArray->contents) + dstPos;
+            srcObj = ((Object**)(void*)srcArray->contents) + srcPos;
+            dstObj = ((Object**)(void*)dstArray->contents) + dstPos;
 
             if (length > 0 && srcObj[0] != NULL)
             {
diff --git a/vm/native/java_lang_Throwable.c b/vm/native/java_lang_Throwable.cpp
similarity index 100%
rename from vm/native/java_lang_Throwable.c
rename to vm/native/java_lang_Throwable.cpp
diff --git a/vm/native/java_lang_VMClassLoader.c b/vm/native/java_lang_VMClassLoader.cpp
similarity index 100%
rename from vm/native/java_lang_VMClassLoader.c
rename to vm/native/java_lang_VMClassLoader.cpp
diff --git a/vm/native/java_lang_VMThread.c b/vm/native/java_lang_VMThread.cpp
similarity index 100%
rename from vm/native/java_lang_VMThread.c
rename to vm/native/java_lang_VMThread.cpp
diff --git a/vm/native/java_lang_reflect_AccessibleObject.c b/vm/native/java_lang_reflect_AccessibleObject.cpp
similarity index 100%
rename from vm/native/java_lang_reflect_AccessibleObject.c
rename to vm/native/java_lang_reflect_AccessibleObject.cpp
diff --git a/vm/native/java_lang_reflect_Array.c b/vm/native/java_lang_reflect_Array.cpp
similarity index 98%
rename from vm/native/java_lang_reflect_Array.c
rename to vm/native/java_lang_reflect_Array.cpp
index fe6647cd..1de0ea1 100644
--- a/vm/native/java_lang_reflect_Array.c
+++ b/vm/native/java_lang_reflect_Array.cpp
@@ -87,7 +87,7 @@
     numDim = dimArray->length;
     assert(numDim > 0 && numDim <= 255);
 
-    dimensions = (int*) dimArray->contents;
+    dimensions = (int*)(void*)dimArray->contents;
     for (i = 0; i < numDim; i++) {
         if (dimensions[i] < 0) {
             dvmThrowNegativeArraySizeException(dimensions[i]);
diff --git a/vm/native/java_lang_reflect_Constructor.c b/vm/native/java_lang_reflect_Constructor.cpp
similarity index 100%
rename from vm/native/java_lang_reflect_Constructor.c
rename to vm/native/java_lang_reflect_Constructor.cpp
diff --git a/vm/native/java_lang_reflect_Field.c b/vm/native/java_lang_reflect_Field.cpp
similarity index 100%
rename from vm/native/java_lang_reflect_Field.c
rename to vm/native/java_lang_reflect_Field.cpp
diff --git a/vm/native/java_lang_reflect_Method.c b/vm/native/java_lang_reflect_Method.cpp
similarity index 100%
rename from vm/native/java_lang_reflect_Method.c
rename to vm/native/java_lang_reflect_Method.cpp
diff --git a/vm/native/java_lang_reflect_Proxy.c b/vm/native/java_lang_reflect_Proxy.cpp
similarity index 100%
rename from vm/native/java_lang_reflect_Proxy.c
rename to vm/native/java_lang_reflect_Proxy.cpp
diff --git a/vm/native/java_util_concurrent_atomic_AtomicLong.c b/vm/native/java_util_concurrent_atomic_AtomicLong.cpp
similarity index 100%
rename from vm/native/java_util_concurrent_atomic_AtomicLong.c
rename to vm/native/java_util_concurrent_atomic_AtomicLong.cpp
diff --git a/vm/native/org_apache_harmony_dalvik_NativeTestTarget.c b/vm/native/org_apache_harmony_dalvik_NativeTestTarget.cpp
similarity index 100%
rename from vm/native/org_apache_harmony_dalvik_NativeTestTarget.c
rename to vm/native/org_apache_harmony_dalvik_NativeTestTarget.cpp
diff --git a/vm/native/org_apache_harmony_dalvik_ddmc_DdmServer.c b/vm/native/org_apache_harmony_dalvik_ddmc_DdmServer.cpp
similarity index 100%
rename from vm/native/org_apache_harmony_dalvik_ddmc_DdmServer.c
rename to vm/native/org_apache_harmony_dalvik_ddmc_DdmServer.cpp
diff --git a/vm/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.c b/vm/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cpp
similarity index 100%
rename from vm/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.c
rename to vm/native/org_apache_harmony_dalvik_ddmc_DdmVmInternal.cpp
diff --git a/vm/native/sun_misc_Unsafe.c b/vm/native/sun_misc_Unsafe.cpp
similarity index 100%
rename from vm/native/sun_misc_Unsafe.c
rename to vm/native/sun_misc_Unsafe.cpp
diff --git a/vm/reflect/Reflect.h b/vm/reflect/Reflect.h
index 80e96fa..21523eb 100644
--- a/vm/reflect/Reflect.h
+++ b/vm/reflect/Reflect.h
@@ -19,6 +19,10 @@
 #ifndef _DALVIK_REFLECT_REFLECT
 #define _DALVIK_REFLECT_REFLECT
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
 /*
  * During startup, validate the "box" classes, e.g. java/lang/Integer.
  */
@@ -260,4 +264,8 @@
 bool dvmEncodedArrayIteratorGetNext(EncodedArrayIterator* iterator,
         AnnotationValue* value);
 
+#ifdef __cplusplus
+}
+#endif
+
 #endif /*_DALVIK_REFLECT_REFLECT*/