libnativehelper: refactoring for Runtime Module

This change puts a C ABI between the caller and libnativehelper which
means each could link separate libc++ library instances using
namespaces.

Bug: b/119840313
Test: Builds & boots
Test: atest JniInvocation_test
Test: atest libnativehelper_api_test

Change-Id: Idaa4ddd3118cbb4f3997abdcf355220ef80eb775
diff --git a/include/nativehelper/JNIHelp.h b/include/nativehelper/JNIHelp.h
index d70fdad..08616b7 100644
--- a/include/nativehelper/JNIHelp.h
+++ b/include/nativehelper/JNIHelp.h
@@ -19,30 +19,29 @@
  *
  * This file may be included by C or C++ code, which is trouble because jni.h
  * uses different typedefs for JNIEnv in each language.
- *
- * TODO: remove C support.
  */
 #ifndef NATIVEHELPER_JNIHELP_H_
 #define NATIVEHELPER_JNIHELP_H_
 
-#include "jni.h"
 #include <errno.h>
 #include <unistd.h>
 
+#include <jni.h>
+#include "module_api.h"
+
 #ifndef NELEM
 # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0])))
 #endif
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 /*
  * Register one or more native methods with a particular class.
  * "className" looks like "java/lang/String". Aborts on failure.
  * TODO: fix all callers and change the return type to void.
  */
-int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods);
+MODULE_API int jniRegisterNativeMethods(C_JNIEnv* env,
+                                        const char* className,
+                                        const JNINativeMethod* gMethods,
+                                        int numMethods);
 
 /*
  * Throw an exception with the specified class and an optional message.
@@ -58,22 +57,38 @@
  *
  * Currently aborts the VM if it can't throw the exception.
  */
-int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
+MODULE_API int jniThrowException(C_JNIEnv* env, const char* className, const char* msg);
+
+/*
+ * Throw an exception with the specified class and formatted error message.
+ *
+ * The "className" argument will be passed directly to FindClass, which
+ * takes strings with slashes (e.g. "java/lang/Object").
+ *
+ * If an exception is currently pending, we log a warning message and
+ * clear it.
+ *
+ * Returns 0 on success, nonzero if something failed (e.g. the exception
+ * class couldn't be found, so *an* exception will still be pending).
+ *
+ * Currently aborts the VM if it can't throw the exception.
+ */
+MODULE_API int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
 
 /*
  * Throw a java.lang.NullPointerException, with an optional message.
  */
-int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
+MODULE_API int jniThrowNullPointerException(C_JNIEnv* env, const char* msg);
 
 /*
  * Throw a java.lang.RuntimeException, with an optional message.
  */
-int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
+MODULE_API int jniThrowRuntimeException(C_JNIEnv* env, const char* msg);
 
 /*
  * Throw a java.io.IOException, generating the message from errno.
  */
-int jniThrowIOException(C_JNIEnv* env, int errnum);
+MODULE_API int jniThrowIOException(C_JNIEnv* env, int errnum);
 
 /*
  * Return a pointer to a locale-dependent error string explaining errno
@@ -81,56 +96,54 @@
  * This function is thread-safe (unlike strerror) and portable (unlike
  * strerror_r).
  */
-const char* jniStrError(int errnum, char* buf, size_t buflen);
+MODULE_API const char* jniStrError(int errnum, char* buf, size_t buflen);
 
 /*
  * Returns a new java.io.FileDescriptor for the given int fd.
  */
-jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
+MODULE_API jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd);
 
 /*
  * Returns the int fd from a java.io.FileDescriptor.
  */
-int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
+MODULE_API int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
 
 /*
  * Sets the int fd in a java.io.FileDescriptor.  Throws java.lang.NullPointerException
  * if fileDescriptor is null.
  */
-void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value);
+MODULE_API void jniSetFileDescriptorOfFD(C_JNIEnv* env,
+                                         jobject fileDescriptor,
+                                         int value);
 
 /*
  * Returns the long ownerId from a java.io.FileDescriptor.
  */
-jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
+MODULE_API jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor);
 
 /*
  * Returns the reference from a java.lang.ref.Reference.
  */
-jobject jniGetReferent(C_JNIEnv* env, jobject ref);
+MODULE_API jobject jniGetReferent(C_JNIEnv* env, jobject ref);
 
 /*
  * Returns a Java String object created from UTF-16 data either from jchar or,
  * if called from C++11, char16_t (a bitwise identical distinct type).
  */
-jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
+MODULE_API jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len);
 
 /*
  * Log a message and an exception.
  * If exception is NULL, logs the current exception in the JNI environment.
  */
-void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
-
-#ifdef __cplusplus
-}
-#endif
-
+MODULE_API void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception);
 
 /*
  * For C++ code, we provide inlines that map to the C functions.  g++ always
  * inlines these, even on non-optimized builds.
  */
 #if defined(__cplusplus)
+
 inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) {
     return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods);
 }
@@ -139,8 +152,6 @@
     return jniThrowException(&env->functions, className, msg);
 }
 
-extern "C" int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args);
-
 /*
  * Equivalent to jniThrowException but with a printf-like format string and
  * variable-length argument list. This is only available in C++.
@@ -204,7 +215,7 @@
   void operator=(const TypeName&) = delete
 #endif  // !defined(DISALLOW_COPY_AND_ASSIGN)
 
-#endif
+#endif  // defined(__cplusplus)
 
 /*
  * TEMP_FAILURE_RETRY is defined by some, but not all, versions of