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