blob: 4aaa0a78c276724f0f2a517dde62481598f0b55c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Gampeed6b9df2014-11-20 22:02:20 -080017#ifndef _ANDROID_NIO_UTILS_H_
18#define _ANDROID_NIO_UTILS_H_
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019
Derek Sollenberger8b994192019-07-16 13:23:29 -040020#include <nativehelper/JNIHelp.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22namespace android {
Orion Hodsone522c992019-04-15 16:14:51 +010023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024/**
Orion Hodsonb0461eb2019-04-15 22:13:59 +010025 * Class providing scoped access to the memory backing a java.nio.Buffer instance.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026 *
Orion Hodsonb0461eb2019-04-15 22:13:59 +010027 * Instances of this class should only be allocated on the stack as heap allocation is not
28 * supported.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 *
Orion Hodsonb0461eb2019-04-15 22:13:59 +010030 * Instances of this class do not create any global references for performance reasons.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031 */
Orion Hodsonb0461eb2019-04-15 22:13:59 +010032class AutoBufferPointer final {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033public:
Orion Hodsonb0461eb2019-04-15 22:13:59 +010034 /** 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 Project9066cfe2009-03-03 19:31:44 -080045 AutoBufferPointer(JNIEnv* env, jobject nioBuffer, jboolean commit);
Orion Hodsonb0461eb2019-04-15 22:13:59 +010046
47 /** Destructor for an AutoBufferPointer instance.
48 *
49 * Releases critical managed heap array pointer if acquired.
50 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 ~AutoBufferPointer();
Bernhard Rosenkränzer4048a4b2014-11-23 22:24:32 +010052
Orion Hodsonb0461eb2019-04-15 22:13:59 +010053 /**
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 Project9066cfe2009-03-03 19:31:44 -080057 void* pointer() const { return fPointer; }
Bernhard Rosenkränzer4048a4b2014-11-23 22:24:32 +010058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059private:
Orion Hodsonb0461eb2019-04-15 22:13:59 +010060 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;
Derek Sollenberger8b994192019-07-16 13:23:29 -040071 static void* operator new(size_t);
72 static void* operator new[](size_t);
73 static void* operator new(size_t, void*);
74 static void* operator new[](size_t, void*);
75 static void operator delete(void*, size_t);
76 static void operator delete[](void*, size_t);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077};
78
79} /* namespace android */
80
Andreas Gampeed6b9df2014-11-20 22:02:20 -080081#endif // _ANDROID_NIO_UTILS_H_