1 /* 2 * Copyright (C) 2008 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 #ifndef _ANDROID_NIO_UTILS_H_ 18 #define _ANDROID_NIO_UTILS_H_ 19 20 #include <jni.h> 21 22 #include <cstddef> 23 24 namespace android { 25 26 /** 27 * Class providing scoped access to the memory backing a java.nio.Buffer instance. 28 * 29 * Instances of this class should only be allocated on the stack as heap allocation is not 30 * supported. 31 * 32 * Instances of this class do not create any global references for performance reasons. 33 */ 34 class AutoBufferPointer final { 35 public: 36 /** Constructor for an AutoBufferPointer instance. 37 * 38 * @param env The current JNI env 39 * @param nioBuffer Instance of a java.nio.Buffer whose memory will be accessed. 40 * @param commit JNI_TRUE if the underlying memory will be updated and should be 41 * copied back to the managed heap. JNI_FALSE if the data will 42 * not be modified or the modifications may be discarded. 43 * 44 * The commit parameter is only applicable if the buffer is backed by a managed heap 45 * array and the runtime had to provide a copy of the data rather than the original data. 46 */ 47 AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit); 48 49 /** Destructor for an AutoBufferPointer instance. 50 * 51 * Releases critical managed heap array pointer if acquired. 52 */ 53 ~AutoBufferPointer(); 54 55 /** 56 * Returns a pointer to the current position of the buffer provided to the constructor. This 57 * pointer is only valid whilst the AutoBufferPointer instance remains in scope. 58 */ pointer()59 void* pointer() const { return fPointer; } 60 61 private: 62 JNIEnv* const fEnv; 63 void* fPointer; // Pointer to current buffer position when constructed. 64 void* fElements; // Pointer to array element 0 (null if buffer is direct, may be 65 // within fArray or point to a copy of the array). 66 jarray fArray; // Pointer to array on managed heap. 67 const jboolean fCommit; // Flag to commit data to source (when fElements is a copy of fArray). 68 69 // Unsupported constructors and operators. 70 AutoBufferPointer() = delete; 71 AutoBufferPointer(AutoBufferPointer&) = delete; 72 AutoBufferPointer& operator=(AutoBufferPointer&) = delete; 73 static void* operator new(size_t); 74 static void* operator new[](size_t); 75 static void* operator new(size_t, void*); 76 static void* operator new[](size_t, void*); 77 static void operator delete(void*, size_t); 78 static void operator delete[](void*, size_t); 79 }; 80 81 } /* namespace android */ 82 83 #endif // _ANDROID_NIO_UTILS_H_ 84