blob: 187beeea4de1e8195589d31b74ed5e81add2175f [file] [log] [blame]
Andreas Huberdab5fc62016-08-15 09:25:02 -07001/*
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#define LOG_TAG "EphemeralStorage"
18//#define LOG_NDEBUG 0
19
20#include <android-base/logging.h>
21
22#include "EphemeralStorage.h"
23
24using ::android::hardware::hidl_string;
25using ::android::hardware::hidl_vec;
26
27namespace android {
28
29EphemeralStorage::EphemeralStorage() {
30}
31
32EphemeralStorage::~EphemeralStorage() {
33 CHECK(mItems.empty())
34 << "All item storage should have been released by now.";
35}
36
37hidl_string *EphemeralStorage::allocStringArray(size_t size) {
38 Item item;
39 item.mType = TYPE_STRING_ARRAY;
40 item.mObj = NULL;
41 item.mPtr = new hidl_string[size];
42 mItems.push_back(item);
43
44 return static_cast<hidl_string *>(item.mPtr);
45}
46
47void *EphemeralStorage::allocTemporaryStorage(size_t size) {
48 Item item;
49 item.mType = TYPE_STORAGE;
50 item.mObj = NULL;
51 item.mPtr = malloc(size);
52 mItems.push_back(item);
53
54 return item.mPtr;
55}
56
57const hidl_string *EphemeralStorage::allocTemporaryString(
58 JNIEnv *env, jstring stringObj) {
59 jstring obj = (jstring)env->NewGlobalRef(stringObj);
60 const char *val = env->GetStringUTFChars(obj, NULL);
61
62 Item item;
63 item.mType = TYPE_STRING;
64 item.mObj = obj;
65 item.mPtr = (void *)val;
66 mItems.push_back(item);
67
68 hidl_string *s = allocStringArray(1 /* size */);
69 s->setToExternal((char *)val, strlen(val));
70
71 return s;
72}
73
74#define DEFINE_ALLOC_ARRAY_METHODS(Suffix,Type,NewType) \
75const Type *EphemeralStorage::allocTemporary ## Suffix ## Array( \
76 JNIEnv *env, Type ## Array arrayObj) { \
77 Type ## Array obj = (Type ## Array)env->NewGlobalRef(arrayObj); \
78 const Type *val = env->Get ## NewType ## ArrayElements(obj, NULL); \
79 \
80 Item item; \
81 item.mType = TYPE_ ## Suffix ## _ARRAY; \
82 item.mObj = obj; \
83 item.mPtr = (void *)val; \
84 mItems.push_back(item); \
85 \
86 return val; \
87}
88
89#define DEFINE_ALLOC_VECTOR_METHODS(Suffix,Type,NewType) \
90const hidl_vec<Type> *EphemeralStorage::allocTemporary ## Suffix ## Vector( \
91 JNIEnv *env, Type ## Array arrayObj) { \
92 Type ## Array obj = (Type ## Array)env->NewGlobalRef(arrayObj); \
93 jsize len = env->GetArrayLength(obj); \
94 const Type *val = env->Get ## NewType ## ArrayElements(obj, NULL); \
95 \
96 Item item; \
97 item.mType = TYPE_ ## Suffix ## _ARRAY; \
98 item.mObj = obj; \
99 item.mPtr = (void *)val; \
100 mItems.push_back(item); \
101 \
Andreas Huber9266f992016-08-25 11:21:21 -0700102 void *vecPtr = allocTemporaryStorage(sizeof(hidl_vec<Type>)); \
Andreas Huberdab5fc62016-08-15 09:25:02 -0700103 \
Andreas Huber9266f992016-08-25 11:21:21 -0700104 hidl_vec<Type> *vec = new (vecPtr) hidl_vec<Type>; \
Andreas Huberdab5fc62016-08-15 09:25:02 -0700105 vec->setToExternal(const_cast<Type *>(val), len); \
106 \
107 return vec; \
108}
109
110DEFINE_ALLOC_ARRAY_METHODS(Int8,jbyte,Byte)
111DEFINE_ALLOC_ARRAY_METHODS(Int16,jshort,Short)
112DEFINE_ALLOC_ARRAY_METHODS(Int32,jint,Int)
113DEFINE_ALLOC_ARRAY_METHODS(Int64,jlong,Long)
114DEFINE_ALLOC_ARRAY_METHODS(Float,jfloat,Float)
115DEFINE_ALLOC_ARRAY_METHODS(Double,jdouble,Double)
116
117DEFINE_ALLOC_VECTOR_METHODS(Int8,jbyte,Byte)
118DEFINE_ALLOC_VECTOR_METHODS(Int16,jshort,Short)
119DEFINE_ALLOC_VECTOR_METHODS(Int32,jint,Int)
120DEFINE_ALLOC_VECTOR_METHODS(Int64,jlong,Long)
121DEFINE_ALLOC_VECTOR_METHODS(Float,jfloat,Float)
122DEFINE_ALLOC_VECTOR_METHODS(Double,jdouble,Double)
123
124#define DEFINE_RELEASE_ARRAY_CASE(Suffix,Type,NewType) \
125 case TYPE_ ## Suffix ## _ARRAY: \
126 { \
127 env->Release ## NewType ## ArrayElements( \
128 (Type ## Array)item.mObj, \
129 (Type *)item.mPtr, \
130 0 /* mode */); \
131 \
132 env->DeleteGlobalRef(item.mObj); \
133 break; \
134 }
135
136void EphemeralStorage::release(JNIEnv *env) {
137 for (size_t i = mItems.size(); i--;) {
138 const Item &item = mItems[i];
139
140 switch (item.mType) {
141 case TYPE_STRING_ARRAY:
142 {
143 delete[] static_cast<hidl_string *>(item.mPtr);
144 break;
145 }
146
147 case TYPE_STORAGE:
148 {
149 free(item.mPtr);
150 break;
151 }
152
153 case TYPE_STRING:
154 {
155 env->ReleaseStringUTFChars(
156 (jstring)item.mObj, (const char *)item.mPtr);
157
158 env->DeleteGlobalRef(item.mObj);
159 break;
160 }
161
162 DEFINE_RELEASE_ARRAY_CASE(Int8,jbyte,Byte)
163 DEFINE_RELEASE_ARRAY_CASE(Int16,jshort,Short)
164 DEFINE_RELEASE_ARRAY_CASE(Int32,jint,Int)
165 DEFINE_RELEASE_ARRAY_CASE(Int64,jlong,Long)
166 DEFINE_RELEASE_ARRAY_CASE(Float,jfloat,Float)
167 DEFINE_RELEASE_ARRAY_CASE(Double,jdouble,Double)
168
169 default:
170 CHECK(!"Should not be here");
171 }
172 }
173
174 mItems.clear();
175}
176
177} // namespace android
178