src/main/android/SDL_android_main.c
author Ryan C. Gordon
Mon, 05 Jan 2015 01:41:42 -0500
changeset 9306 817656bd36ec
parent 9238 3e53bd4510a2
child 9828 4e0e4f544027
permissions -rw-r--r--
Clang static analysis builds should use C runtime directly.

This is a little macro magic to use malloc() directly instead of SDL_malloc(),
etc, so static analysis tests that know about the C runtime can function
properly, and understand that we are dealing with heap allocations, etc.

This changed our static analysis report from 5 outstanding bugs to 30.

5x as many bugs were hidden by SDL_malloc() not being recognized as malloc()
by the static analyzer!
     1 /*
     2     SDL_android_main.c, placed in the public domain by Sam Lantinga  3/13/14
     3 */
     4 #include "../../SDL_internal.h"
     5 
     6 #ifdef __ANDROID__
     7 
     8 /* Include the SDL main definition header */
     9 #include "SDL_main.h"
    10 
    11 /*******************************************************************************
    12                  Functions called by JNI
    13 *******************************************************************************/
    14 #include <jni.h>
    15 
    16 /* Called before SDL_main() to initialize JNI bindings in SDL library */
    17 extern void SDL_Android_Init(JNIEnv* env, jclass cls);
    18 
    19 /* Start up the SDL app */
    20 int Java_org_libsdl_app_SDLActivity_nativeInit(JNIEnv* env, jclass cls, jobject array)
    21 {
    22     int i;
    23     int argc;
    24     int status;
    25 
    26     /* This interface could expand with ABI negotiation, callbacks, etc. */
    27     SDL_Android_Init(env, cls);
    28 
    29     SDL_SetMainReady();
    30 
    31     /* Prepare the arguments. */
    32 
    33     int len = (*env)->GetArrayLength(env, array);
    34     char* argv[1 + len + 1];
    35     argc = 0;
    36     /* Use the name "app_process" so PHYSFS_platformCalcBaseDir() works.
    37        https://bitbucket.org/MartinFelis/love-android-sdl2/issue/23/release-build-crash-on-start
    38      */
    39     argv[argc++] = SDL_strdup("app_process");
    40     for (i = 0; i < len; ++i) {
    41         const char* utf;
    42         char* arg = NULL;
    43         jstring string = (*env)->GetObjectArrayElement(env, array, i);
    44         if (string) {
    45             utf = (*env)->GetStringUTFChars(env, string, 0);
    46             if (utf) {
    47                 arg = SDL_strdup(utf);
    48                 (*env)->ReleaseStringUTFChars(env, string, utf);
    49             }
    50             (*env)->DeleteLocalRef(env, string);
    51         }
    52         if (!arg) {
    53             arg = SDL_strdup("");
    54         }
    55         argv[argc++] = arg;
    56     }
    57     argv[argc] = NULL;
    58 
    59 
    60     /* Run the application. */
    61 
    62     status = SDL_main(argc, argv);
    63 
    64     /* Release the arguments. */
    65 
    66     for (i = 0; i < argc; ++i) {
    67         SDL_free(argv[i]);
    68     }
    69 
    70     /* Do not issue an exit or the whole application will terminate instead of just the SDL thread */
    71     /* exit(status); */
    72 
    73     return status;
    74 }
    75 
    76 #endif /* __ANDROID__ */
    77 
    78 /* vi: set ts=4 sw=4 expandtab: */