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 
24 uint64_t gNumNativeBytesAllocated = 0;
25 
finalize(uint64_t * ptr)26 static void finalize(uint64_t* ptr) {
27   gNumNativeBytesAllocated -= *ptr;
28   delete ptr;
29 }
30 
31 extern "C"
Java_libcore_util_NativeAllocationRegistryTest_getNativeFinalizer(JNIEnv *,jclass)32 jlong Java_libcore_util_NativeAllocationRegistryTest_getNativeFinalizer(JNIEnv*, jclass) {
33   return static_cast<jlong>(reinterpret_cast<uintptr_t>(&finalize));
34 }
35 
36 extern "C"
Java_libcore_util_NativeAllocationRegistryTest_doNativeAllocation(JNIEnv *,jclass,jlong size)37 jlong 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 
48 extern "C"
Java_libcore_util_NativeAllocationRegistryTest_getNumNativeBytesAllocated(JNIEnv *,jclass)49 jlong Java_libcore_util_NativeAllocationRegistryTest_getNumNativeBytesAllocated(JNIEnv*, jclass) {
50   return static_cast<jlong>(gNumNativeBytesAllocated);
51 }
52