1/* 2 * Copyright (C) 2016 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#include <sys/stat.h> 18#include <string> 19 20#include <jni.h> 21#include "JNIHelp.h" 22#include "ScopedUtfChars.h" 23 24uint64_t gNumNativeBytesAllocated = 0; 25 26static void finalize(uint64_t* ptr) { 27 gNumNativeBytesAllocated -= *ptr; 28 delete ptr; 29} 30 31extern "C" 32jlong Java_libcore_util_NativeAllocationRegistryTest_getNativeFinalizer(JNIEnv*, jclass) { 33 return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalize)); 34} 35 36extern "C" 37jlong Java_libcore_util_NativeAllocationRegistryTest_doNativeAllocation(JNIEnv*, 38 jclass, 39 jlong size) { 40 gNumNativeBytesAllocated += size; 41 42 // The actual allocation is a pointer to the pretend size of the allocation. 43 uint64_t* ptr = new uint64_t; 44 *ptr = static_cast<uint64_t>(size); 45 return static_cast<jlong>(reinterpret_cast<uintptr_t>(ptr)); 46} 47 48extern "C" 49jlong Java_libcore_util_NativeAllocationRegistryTest_getNumNativeBytesAllocated(JNIEnv*, jclass) { 50 return static_cast<jlong>(gNumNativeBytesAllocated); 51} 52