Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  *
     16  */
     17 
     18 #include <jni.h>
     19 #include <cutils/log.h>
     20 
     21 #if defined(LOG_TAG)
     22 #undef LOG_TAG
     23 #define LOG_TAG "MOARRAM"
     24 #endif
     25 
     26 char *gPtr17;
     27 char *gPtr71;
     28 static int num17ByteBlocks;
     29 static int num71ByteBlocks;
     30 
     31 void
     32 Java_com_android_benchmark_moarram_MainActivity_addVariableSizedBlocksNative(
     33     JNIEnv*  env,
     34     jobject  this,
     35     jint id)
     36 {
     37     int size;
     38     char **gPtr;
     39     char **ptr;
     40     if (id == 0) {
     41         size = 17;
     42         gPtr = &gPtr17;
     43     } else {
     44         size = 71;
     45         gPtr = &gPtr71;
     46     }
     47     ptr = malloc(size);
     48     *ptr = *gPtr;
     49     *gPtr = (char *) ptr;
     50     ALOGW("%d %d-byte blocks allocated so far (just allocated %p)",
     51           id == 0 ? ++num17ByteBlocks : ++num71ByteBlocks,
     52           size, ptr);
     53 }
     54 
     55 void
     56 Java_com_android_benchmark_moarram_MainActivity_freeVariableSizedBlocksNative(
     57     JNIEnv*  env,
     58     jobject  this,
     59     jint id)
     60 {
     61     int size;
     62     char **ptr;
     63     char **gPtr;
     64     if (id == 0) {
     65         size = 17;
     66         gPtr = &gPtr17;
     67     } else {
     68         size = 71;
     69         gPtr = &gPtr71;
     70     }
     71     if (*gPtr == NULL) {
     72         ALOGW("All %d-byte blocks are freed", size);
     73         return;
     74     }
     75     ptr = (char **) *gPtr;
     76     *gPtr = *ptr;
     77     free(ptr);
     78     ALOGW("%d %d-byte blocks allocated so far (just freed %p)",
     79           id == 0 ? --num17ByteBlocks : --num71ByteBlocks,
     80           size, ptr);
     81 }
     82