File: EphemeralStorage.cpp

package info (click to toggle)
android-platform-frameworks-base 1%3A10.0.0%2Br36-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 321,788 kB
  • sloc: java: 962,234; cpp: 274,314; xml: 242,770; python: 5,060; sh: 1,432; ansic: 494; makefile: 47; sed: 19
file content (175 lines) | stat: -rw-r--r-- 6,333 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "EphemeralStorage"
//#define LOG_NDEBUG 0

#include <android-base/logging.h>

#include "EphemeralStorage.h"

using ::android::hardware::hidl_string;
using ::android::hardware::hidl_vec;

namespace android {

EphemeralStorage::EphemeralStorage() {
}

EphemeralStorage::~EphemeralStorage() {
    CHECK(mItems.empty())
        << "All item storage should have been released by now.";
}

hidl_string *EphemeralStorage::allocStringArray(size_t size) {
    Item item;
    item.mType = TYPE_STRING_ARRAY;
    item.mObj = NULL;
    item.mPtr = new hidl_string[size];
    mItems.push_back(item);

    return static_cast<hidl_string *>(item.mPtr);
}

void *EphemeralStorage::allocTemporaryStorage(size_t size) {
    Item item;
    item.mType = TYPE_STORAGE;
    item.mObj = NULL;
    item.mPtr = malloc(size);
    mItems.push_back(item);

    return item.mPtr;
}

const hidl_string *EphemeralStorage::allocTemporaryString(
        JNIEnv *env, jstring stringObj) {
    jstring obj = (jstring)env->NewGlobalRef(stringObj);
    const char *val = env->GetStringUTFChars(obj, NULL);

    Item item;
    item.mType = TYPE_STRING;
    item.mObj = obj;
    item.mPtr = (void *)val;
    mItems.push_back(item);

    hidl_string *s = allocStringArray(1 /* size */);
    s->setToExternal((char *)val, strlen(val));

    return s;
}

native_handle_t *EphemeralStorage::allocTemporaryNativeHandle(
        int numFds, int numInts) {
    Item item;
    item.mType = TYPE_NATIVE_HANDLE;
    item.mObj = nullptr;
    item.mPtr = native_handle_create(numFds, numInts);
    mItems.push_back(item);

    return static_cast<native_handle_t*>(item.mPtr);
}

#define DEFINE_ALLOC_VECTOR_METHODS(Suffix,Type,NewType)                       \
const hidl_vec<Type> *EphemeralStorage::allocTemporary ## Suffix ## Vector(    \
        JNIEnv *env, Type ## Array arrayObj) {                                 \
    Type ## Array obj = (Type ## Array)env->NewGlobalRef(arrayObj);            \
    jsize len = env->GetArrayLength(obj);                                      \
    const Type *val = env->Get ## NewType ## ArrayElements(obj, NULL);         \
                                                                               \
    Item item;                                                                 \
    item.mType = TYPE_ ## Suffix ## _ARRAY;                                    \
    item.mObj = obj;                                                           \
    item.mPtr = (void *)val;                                                   \
    mItems.push_back(item);                                                    \
                                                                               \
    void *vecPtr = allocTemporaryStorage(sizeof(hidl_vec<Type>));              \
                                                                               \
    hidl_vec<Type> *vec = new (vecPtr) hidl_vec<Type>;                         \
    vec->setToExternal(const_cast<Type *>(val), len);                          \
                                                                               \
    return vec;                                                                \
}

DEFINE_ALLOC_VECTOR_METHODS(Int8,jbyte,Byte)
DEFINE_ALLOC_VECTOR_METHODS(Int16,jshort,Short)
DEFINE_ALLOC_VECTOR_METHODS(Int32,jint,Int)
DEFINE_ALLOC_VECTOR_METHODS(Int64,jlong,Long)
DEFINE_ALLOC_VECTOR_METHODS(Float,jfloat,Float)
DEFINE_ALLOC_VECTOR_METHODS(Double,jdouble,Double)

#define DEFINE_RELEASE_ARRAY_CASE(Suffix,Type,NewType)                         \
            case TYPE_ ## Suffix ## _ARRAY:                                    \
            {                                                                  \
                env->Release ## NewType ## ArrayElements(                      \
                        (Type ## Array)item.mObj,                              \
                        (Type *)item.mPtr,                                     \
                        0 /* mode */);                                         \
                                                                               \
                env->DeleteGlobalRef(item.mObj);                               \
                break;                                                         \
            }

__attribute__((no_sanitize("unsigned-integer-overflow")))
void EphemeralStorage::release(JNIEnv *env) {
    for (size_t i = mItems.size(); i--;) {
        const Item &item = mItems[i];

        switch (item.mType) {
            case TYPE_STRING_ARRAY:
            {
                delete[] static_cast<hidl_string *>(item.mPtr);
                break;
            }

            case TYPE_STORAGE:
            {
                free(item.mPtr);
                break;
            }

            case TYPE_STRING:
            {
                env->ReleaseStringUTFChars(
                        (jstring)item.mObj, (const char *)item.mPtr);

                env->DeleteGlobalRef(item.mObj);
                break;
            }

            DEFINE_RELEASE_ARRAY_CASE(Int8,jbyte,Byte)
            DEFINE_RELEASE_ARRAY_CASE(Int16,jshort,Short)
            DEFINE_RELEASE_ARRAY_CASE(Int32,jint,Int)
            DEFINE_RELEASE_ARRAY_CASE(Int64,jlong,Long)
            DEFINE_RELEASE_ARRAY_CASE(Float,jfloat,Float)
            DEFINE_RELEASE_ARRAY_CASE(Double,jdouble,Double)

            case TYPE_NATIVE_HANDLE:
            {
                int err = native_handle_delete(static_cast<native_handle_t *>(item.mPtr));
                CHECK(err == 0);
                break;
            }

            default:
                CHECK(!"Should not be here");
        }
    }

    mItems.clear();
}

}  // namespace android