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 <malloc.h> 20 #include <cutils/log.h> 21 22 #if defined(LOG_TAG) 23 #undef LOG_TAG 24 #define LOG_TAG "MOARRAM" 25 #endif 26 27 char *gPtr17; 28 char *gPtr71; 29 static int num17ByteBlocks; 30 static int num71ByteBlocks; 31 32 void 33 Java_com_android_benchmark_moarram_MainActivity_addVariableSizedBlocksNative( 34 JNIEnv* env, 35 jobject this, 36 jint id) 37 { 38 int size; 39 char **gPtr; 40 char **ptr; 41 if (id == 0) { 42 size = 17; 43 gPtr = &gPtr17; 44 } else { 45 size = 71; 46 gPtr = &gPtr71; 47 } 48 ptr = malloc(size); 49 *ptr = *gPtr; 50 *gPtr = (char *) ptr; 51 ALOGW("%d %d-byte blocks allocated so far (just allocated %p)", 52 id == 0 ? ++num17ByteBlocks : ++num71ByteBlocks, 53 size, ptr); 54 } 55 56 void 57 Java_com_android_benchmark_moarram_MainActivity_freeVariableSizedBlocksNative( 58 JNIEnv* env, 59 jobject this, 60 jint id) 61 { 62 int size; 63 char **ptr; 64 char **gPtr; 65 if (id == 0) { 66 size = 17; 67 gPtr = &gPtr17; 68 } else { 69 size = 71; 70 gPtr = &gPtr71; 71 } 72 if (*gPtr == NULL) { 73 ALOGW("All %d-byte blocks are freed", size); 74 return; 75 } 76 ptr = (char **) *gPtr; 77 *gPtr = *ptr; 78 free(ptr); 79 ALOGW("%d %d-byte blocks allocated so far (just freed %p)", 80 id == 0 ? --num17ByteBlocks : --num71ByteBlocks, 81 size, ptr); 82 } 83