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 #ifndef EPHEMERAL_STORAGE_H_
18 
19 #define EPHEMERAL_STORAGE_H_
20 
21 #include <android-base/macros.h>
22 #include <hidl/HidlSupport.h>
23 #include <jni.h>
24 #include <utils/Vector.h>
25 
26 namespace android {
27 
28 #define DECLARE_ALLOC_METHODS(Suffix,Type)                          \
29     const ::android::hardware::hidl_vec<Type> *                     \
30     allocTemporary ## Suffix ## Vector(                             \
31             JNIEnv *env, Type ## Array arrayObj);
32 
33 struct EphemeralStorage {
34     EphemeralStorage();
35     ~EphemeralStorage();
36 
37     void release(JNIEnv *env);
38 
39     hardware::hidl_string *allocStringArray(size_t size);
40 
41     void *allocTemporaryStorage(size_t size);
42 
43     const ::android::hardware::hidl_string *allocTemporaryString(
44             JNIEnv *env, jstring stringObj);
45 
46     native_handle_t *allocTemporaryNativeHandle(int numFds, int numInts);
47 
48     DECLARE_ALLOC_METHODS(Int8,jbyte)
49     DECLARE_ALLOC_METHODS(Int16,jshort)
50     DECLARE_ALLOC_METHODS(Int32,jint)
51     DECLARE_ALLOC_METHODS(Int64,jlong)
52     DECLARE_ALLOC_METHODS(Float,jfloat)
53     DECLARE_ALLOC_METHODS(Double,jdouble)
54 
55 private:
56     enum Type {
57         TYPE_STRING_ARRAY,
58         TYPE_STORAGE,
59         TYPE_STRING,
60         TYPE_Int8_ARRAY,
61         TYPE_Int16_ARRAY,
62         TYPE_Int32_ARRAY,
63         TYPE_Int64_ARRAY,
64         TYPE_Float_ARRAY,
65         TYPE_Double_ARRAY,
66         TYPE_NATIVE_HANDLE,
67     };
68 
69     struct Item {
70         Type mType;
71         jobject mObj;
72         void *mPtr;
73     };
74 
75     Vector<Item> mItems;
76 
77     DISALLOW_COPY_AND_ASSIGN(EphemeralStorage);
78 };
79 
80 #undef DECLARE_ALLOC_METHODS
81 
82 }  // namespace android
83 
84 #endif  // EPHEMERAL_STORAGE_H_
85