Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 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 ANDROID_HIDL_SUPPORT_H |
| 18 | #define ANDROID_HIDL_SUPPORT_H |
| 19 | |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 21 | #include <array> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 22 | #include <iterator> |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 23 | #include <cutils/native_handle.h> |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 24 | #include <hidl/HidlInternal.h> |
Yifan Hong | a3c3184 | 2016-10-21 10:33:14 -0700 | [diff] [blame] | 25 | #include <hidl/Status.h> |
Yifan Hong | 1e265bb | 2016-11-08 12:33:44 -0800 | [diff] [blame] | 26 | #include <map> |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 27 | #include <sstream> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 28 | #include <stddef.h> |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 29 | #include <tuple> |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 30 | #include <type_traits> |
Iliyan Malchev | 692070a | 2016-09-12 16:30:44 -0700 | [diff] [blame] | 31 | #include <utils/Errors.h> |
| 32 | #include <utils/RefBase.h> |
| 33 | #include <utils/StrongPointer.h> |
Yifan Hong | 7f97f44 | 2016-11-14 18:31:05 -0800 | [diff] [blame] | 34 | #include <vector> |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 35 | |
| 36 | namespace android { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 37 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 38 | // this file is included by all hidl interface, so we must forward declare the |
| 39 | // IMemory and IBase types. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 40 | namespace hidl { |
| 41 | namespace memory { |
| 42 | namespace V1_0 { |
| 43 | struct IMemory; |
| 44 | }; // namespace V1_0 |
| 45 | }; // namespace manager |
| 46 | }; // namespace hidl |
| 47 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 48 | namespace hidl { |
| 49 | namespace base { |
| 50 | namespace V1_0 { |
| 51 | struct IBase; |
| 52 | }; // namespace V1_0 |
| 53 | }; // namespace base |
| 54 | }; // namespace hidl |
| 55 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 56 | namespace hardware { |
| 57 | |
Yifan Hong | 24332ef | 2017-03-07 16:22:19 -0800 | [diff] [blame] | 58 | namespace details { |
| 59 | // Return true on userdebug / eng builds and false on user builds. |
| 60 | bool debuggable(); |
| 61 | } // namespace details |
| 62 | |
Martijn Coenen | 9b8f9c3 | 2016-12-09 15:51:06 +0100 | [diff] [blame] | 63 | // hidl_death_recipient is a callback interfaced that can be used with |
| 64 | // linkToDeath() / unlinkToDeath() |
| 65 | struct hidl_death_recipient : public virtual RefBase { |
| 66 | virtual void serviceDied(uint64_t cookie, |
| 67 | const ::android::wp<::android::hidl::base::V1_0::IBase>& who) = 0; |
| 68 | }; |
| 69 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 70 | // hidl_handle wraps a pointer to a native_handle_t in a hidl_pointer, |
| 71 | // so that it can safely be transferred between 32-bit and 64-bit processes. |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 72 | // The ownership semantics for this are: |
| 73 | // 1) The conversion constructor and assignment operator taking a const native_handle_t* |
| 74 | // do not take ownership of the handle; this is because these operations are usually |
| 75 | // just done for IPC, and cloning by default is a waste of resources. If you want |
| 76 | // a hidl_handle to take ownership, call setTo(handle, true /*shouldOwn*/); |
| 77 | // 2) The copy constructor/assignment operator taking a hidl_handle *DO* take ownership; |
| 78 | // that is because it's not intuitive that this class encapsulates a native_handle_t |
| 79 | // which needs cloning to be valid; in particular, this allows constructs like this: |
| 80 | // hidl_handle copy; |
| 81 | // foo->someHidlCall([&](auto incoming_handle) { |
| 82 | // copy = incoming_handle; |
| 83 | // }); |
| 84 | // // copy and its enclosed file descriptors will remain valid here. |
| 85 | // 3) The move constructor does what you would expect; it only owns the handle if the |
| 86 | // original did. |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 87 | struct hidl_handle { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 88 | hidl_handle(); |
| 89 | ~hidl_handle(); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 90 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 91 | hidl_handle(const native_handle_t *handle); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 92 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 93 | // copy constructor. |
| 94 | hidl_handle(const hidl_handle &other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 95 | |
| 96 | // move constructor. |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 97 | hidl_handle(hidl_handle &&other) noexcept; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 98 | |
Steven Moreland | 6ffdc2a | 2017-01-23 12:34:33 -0800 | [diff] [blame] | 99 | // assignment operators |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 100 | hidl_handle &operator=(const hidl_handle &other); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 101 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 102 | hidl_handle &operator=(const native_handle_t *native_handle); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 103 | |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 104 | hidl_handle &operator=(hidl_handle &&other) noexcept; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 105 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 106 | void setTo(native_handle_t* handle, bool shouldOwn = false); |
| 107 | |
| 108 | const native_handle_t* operator->() const; |
| 109 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 110 | // implicit conversion to const native_handle_t* |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 111 | operator const native_handle_t *() const; |
| 112 | |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 113 | // explicit conversion |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 114 | const native_handle_t *getNativeHandle() const; |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 115 | private: |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 116 | void freeHandle(); |
| 117 | |
Andreas Huber | 6688d60 | 2017-03-30 10:58:29 -0700 | [diff] [blame] | 118 | details::hidl_pointer<const native_handle_t> mHandle __attribute__ ((aligned(8))); |
| 119 | bool mOwnsHandle __attribute ((aligned(8))); |
Martijn Coenen | 4d1e9cc | 2016-11-18 15:20:18 +0100 | [diff] [blame] | 120 | }; |
| 121 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 122 | struct hidl_string { |
| 123 | hidl_string(); |
| 124 | ~hidl_string(); |
| 125 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 126 | // copy constructor. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 127 | hidl_string(const hidl_string &); |
Steven Moreland | a21d84f | 2017-02-16 09:23:45 -0800 | [diff] [blame] | 128 | // copy from a C-style string. nullptr will create an empty string |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 129 | hidl_string(const char *); |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 130 | // copy the first length characters from a C-style string. |
| 131 | hidl_string(const char *, size_t length); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 132 | // copy from an std::string. |
| 133 | hidl_string(const std::string &); |
| 134 | |
| 135 | // move constructor. |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 136 | hidl_string(hidl_string &&) noexcept; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 137 | |
| 138 | const char *c_str() const; |
| 139 | size_t size() const; |
| 140 | bool empty() const; |
| 141 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 142 | // copy assignment operator. |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 143 | hidl_string &operator=(const hidl_string &); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 144 | // copy from a C-style string. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 145 | hidl_string &operator=(const char *s); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 146 | // copy from an std::string. |
| 147 | hidl_string &operator=(const std::string &); |
| 148 | // move assignment operator. |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 149 | hidl_string &operator=(hidl_string &&other) noexcept; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 150 | // cast to std::string. |
| 151 | operator std::string() const; |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 152 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 153 | void clear(); |
| 154 | |
| 155 | // Reference an external char array. Ownership is _not_ transferred. |
| 156 | // Caller is responsible for ensuring that underlying memory is valid |
| 157 | // for the lifetime of this hidl_string. |
| 158 | void setToExternal(const char *data, size_t size); |
| 159 | |
Andreas Huber | ebfeb36 | 2016-08-25 13:39:05 -0700 | [diff] [blame] | 160 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 161 | static const size_t kOffsetOfBuffer; |
| 162 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 163 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 164 | details::hidl_pointer<const char> mBuffer; |
| 165 | uint32_t mSize; // NOT including the terminating '\0'. |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 166 | bool mOwnsBuffer; // if true then mBuffer is a mutable char * |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 167 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 168 | // copy from data with size. Assume that my memory is freed |
| 169 | // (through clear(), for example) |
| 170 | void copyFrom(const char *data, size_t size); |
| 171 | // move from another hidl_string |
| 172 | void moveFrom(hidl_string &&); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 173 | }; |
| 174 | |
Chih-Hung Hsieh | b30feca | 2017-08-01 15:40:48 -0700 | [diff] [blame] | 175 | // Use NOLINT to suppress missing parentheses warnings around OP. |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 176 | #define HIDL_STRING_OPERATOR(OP) \ |
| 177 | inline bool operator OP(const hidl_string &hs1, const hidl_string &hs2) { \ |
Chih-Hung Hsieh | b30feca | 2017-08-01 15:40:48 -0700 | [diff] [blame] | 178 | return strcmp(hs1.c_str(), hs2.c_str()) OP 0; /* NOLINT */ \ |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 179 | } \ |
| 180 | inline bool operator OP(const hidl_string &hs, const char *s) { \ |
Chih-Hung Hsieh | b30feca | 2017-08-01 15:40:48 -0700 | [diff] [blame] | 181 | return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \ |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 182 | } \ |
| 183 | inline bool operator OP(const char *s, const hidl_string &hs) { \ |
Chih-Hung Hsieh | b30feca | 2017-08-01 15:40:48 -0700 | [diff] [blame] | 184 | return strcmp(hs.c_str(), s) OP 0; /* NOLINT */ \ |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 185 | } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 186 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 187 | HIDL_STRING_OPERATOR(==) |
| 188 | HIDL_STRING_OPERATOR(!=) |
| 189 | HIDL_STRING_OPERATOR(<) |
| 190 | HIDL_STRING_OPERATOR(<=) |
| 191 | HIDL_STRING_OPERATOR(>) |
| 192 | HIDL_STRING_OPERATOR(>=) |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 193 | |
Steven Moreland | 551396a | 2017-02-13 18:33:20 -0800 | [diff] [blame] | 194 | #undef HIDL_STRING_OPERATOR |
Yifan Hong | 5708fb4 | 2016-10-26 17:50:29 -0700 | [diff] [blame] | 195 | |
Scott Randolph | 0c84ab4 | 2017-04-03 14:07:14 -0700 | [diff] [blame] | 196 | // Send our content to the output stream |
| 197 | std::ostream& operator<<(std::ostream& os, const hidl_string& str); |
| 198 | |
| 199 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 200 | // hidl_memory is a structure that can be used to transfer |
| 201 | // pieces of shared memory between processes. The assumption |
| 202 | // of this object is that the memory remains accessible as |
| 203 | // long as the file descriptors in the enclosed mHandle |
| 204 | // - as well as all of its cross-process dups() - remain opened. |
| 205 | struct hidl_memory { |
| 206 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 207 | hidl_memory() : mHandle(nullptr), mSize(0), mName("") { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 211 | * Creates a hidl_memory object, but doesn't take ownership of |
| 212 | * the passed in native_handle_t; callers are responsible for |
| 213 | * making sure the handle remains valid while this object is |
| 214 | * used. |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 215 | */ |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 216 | hidl_memory(const hidl_string &name, const native_handle_t *handle, size_t size) |
| 217 | : mHandle(handle), |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 218 | mSize(size), |
| 219 | mName(name) |
| 220 | {} |
| 221 | |
| 222 | // copy constructor |
| 223 | hidl_memory(const hidl_memory& other) { |
| 224 | *this = other; |
| 225 | } |
| 226 | |
| 227 | // copy assignment |
| 228 | hidl_memory &operator=(const hidl_memory &other) { |
| 229 | if (this != &other) { |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame] | 230 | mHandle = other.mHandle; |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 231 | mSize = other.mSize; |
| 232 | mName = other.mName; |
| 233 | } |
| 234 | |
| 235 | return *this; |
| 236 | } |
| 237 | |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 238 | // move constructor |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 239 | hidl_memory(hidl_memory&& other) noexcept { |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 240 | *this = std::move(other); |
| 241 | } |
| 242 | |
| 243 | // move assignment |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 244 | hidl_memory &operator=(hidl_memory &&other) noexcept { |
Hridya Valsaraju | 0126889 | 2017-02-27 08:48:38 -0800 | [diff] [blame] | 245 | if (this != &other) { |
| 246 | mHandle = std::move(other.mHandle); |
| 247 | mSize = other.mSize; |
| 248 | mName = std::move(other.mName); |
| 249 | other.mSize = 0; |
| 250 | } |
| 251 | |
| 252 | return *this; |
| 253 | } |
| 254 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 255 | |
| 256 | ~hidl_memory() { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | const native_handle_t* handle() const { |
| 260 | return mHandle; |
| 261 | } |
| 262 | |
| 263 | const hidl_string &name() const { |
| 264 | return mName; |
| 265 | } |
| 266 | |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 267 | uint64_t size() const { |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 268 | return mSize; |
| 269 | } |
| 270 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame^] | 271 | // @return true if it's valid |
| 272 | inline bool valid() const { return handle() != nullptr; } |
| 273 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 274 | // offsetof(hidl_memory, mHandle) exposed since mHandle is private. |
| 275 | static const size_t kOffsetOfHandle; |
| 276 | // offsetof(hidl_memory, mName) exposed since mHandle is private. |
| 277 | static const size_t kOffsetOfName; |
Jeff Tinker | 0f3461d | 2017-01-03 10:40:55 -0800 | [diff] [blame] | 278 | |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 279 | private: |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 280 | hidl_handle mHandle __attribute__ ((aligned(8))); |
| 281 | uint64_t mSize __attribute__ ((aligned(8))); |
| 282 | hidl_string mName __attribute__ ((aligned(8))); |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 283 | }; |
| 284 | |
Howard Chen | 9bc35c4 | 2017-10-13 14:21:46 +0800 | [diff] [blame^] | 285 | // HidlMemory is a wrapper class to support sp<> for hidl_memory. It also |
| 286 | // provides factory methods to create an instance from hidl_memory or |
| 287 | // from a opened file descriptor. The number of factory methods can be increase |
| 288 | // to support other type of hidl_memory without break the ABI. |
| 289 | class HidlMemory : public virtual hidl_memory, public virtual ::android::RefBase { |
| 290 | public: |
| 291 | static sp<HidlMemory> getInstance(hidl_memory&& mem); |
| 292 | |
| 293 | static sp<HidlMemory> getInstance(const hidl_string& name, hidl_handle&& handle, uint64_t size); |
| 294 | // @param fd, shall be opened and points to the resource. |
| 295 | // @note this method takes the ownership of the fd and will close it in |
| 296 | // destructor |
| 297 | static sp<HidlMemory> getInstance(const hidl_string& name, int fd, uint64_t size); |
| 298 | |
| 299 | protected: |
| 300 | HidlMemory() : hidl_memory() {} |
| 301 | HidlMemory(const hidl_string& name, hidl_handle&& handle, size_t size) |
| 302 | : hidl_memory(name, std::move(handle), size) {} |
| 303 | ~HidlMemory(); |
| 304 | HidlMemory& operator=(hidl_memory&& src) { |
| 305 | hidl_memory::operator=(src); |
| 306 | return *this; |
| 307 | } |
| 308 | }; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 309 | //////////////////////////////////////////////////////////////////////////////// |
| 310 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 311 | template<typename T> |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 312 | struct hidl_vec { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 313 | hidl_vec() |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 314 | : mBuffer(nullptr), |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 315 | mSize(0), |
| 316 | mOwnsBuffer(true) { |
Hridya Valsaraju | 6d4acb1 | 2017-03-03 14:24:43 -0800 | [diff] [blame] | 317 | static_assert(hidl_vec<T>::kOffsetOfBuffer == 0, "wrong offset"); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Steven Moreland | aa661fc | 2017-10-06 09:40:18 -0700 | [diff] [blame] | 320 | // Note, does not initialize primitive types. |
Steven Moreland | 96e9de0 | 2017-09-29 14:11:20 -0700 | [diff] [blame] | 321 | hidl_vec(size_t size) : hidl_vec() { resize(size); } |
| 322 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 323 | hidl_vec(const hidl_vec<T> &other) : hidl_vec() { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 324 | *this = other; |
| 325 | } |
| 326 | |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 327 | hidl_vec(hidl_vec<T> &&other) noexcept |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 328 | : mOwnsBuffer(false) { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 329 | *this = std::move(other); |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 332 | hidl_vec(const std::initializer_list<T> list) |
Yifan Hong | ca1d1bf | 2016-12-19 14:26:19 -0800 | [diff] [blame] | 333 | : mOwnsBuffer(true) { |
| 334 | if (list.size() > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 335 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
Yifan Hong | ca1d1bf | 2016-12-19 14:26:19 -0800 | [diff] [blame] | 336 | } |
| 337 | mSize = static_cast<uint32_t>(list.size()); |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 338 | mBuffer = new T[mSize]; |
| 339 | |
Steven Moreland | b69926a | 2016-11-15 10:02:57 -0800 | [diff] [blame] | 340 | size_t idx = 0; |
Steven Moreland | 9fbfe47 | 2016-11-14 16:49:17 -0800 | [diff] [blame] | 341 | for (auto it = list.begin(); it != list.end(); ++it) { |
| 342 | mBuffer[idx++] = *it; |
| 343 | } |
| 344 | } |
| 345 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 346 | hidl_vec(const std::vector<T> &other) : hidl_vec() { |
| 347 | *this = other; |
| 348 | } |
| 349 | |
Tomasz Wasilczyk | a9f6073 | 2017-06-30 10:53:22 -0700 | [diff] [blame] | 350 | template <typename InputIterator, |
| 351 | typename = typename std::enable_if<std::is_convertible< |
| 352 | typename std::iterator_traits<InputIterator>::iterator_category, |
| 353 | std::input_iterator_tag>::value>::type> |
| 354 | hidl_vec(InputIterator first, InputIterator last) : mOwnsBuffer(true) { |
| 355 | auto size = std::distance(first, last); |
| 356 | if (size > static_cast<int64_t>(UINT32_MAX)) { |
| 357 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
| 358 | } |
| 359 | if (size < 0) { |
| 360 | details::logAlwaysFatal("size can't be negative."); |
| 361 | } |
| 362 | mSize = static_cast<uint32_t>(size); |
| 363 | mBuffer = new T[mSize]; |
| 364 | |
| 365 | size_t idx = 0; |
| 366 | for (; first != last; ++first) { |
| 367 | mBuffer[idx++] = static_cast<T>(*first); |
| 368 | } |
| 369 | } |
| 370 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 371 | ~hidl_vec() { |
| 372 | if (mOwnsBuffer) { |
| 373 | delete[] mBuffer; |
| 374 | } |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 375 | mBuffer = nullptr; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 376 | } |
| 377 | |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 378 | // Reference an existing array, optionally taking ownership. It is the |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 379 | // caller's responsibility to ensure that the underlying memory stays |
| 380 | // valid for the lifetime of this hidl_vec. |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 381 | void setToExternal(T *data, size_t size, bool shouldOwn = false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 382 | if (mOwnsBuffer) { |
| 383 | delete [] mBuffer; |
| 384 | } |
| 385 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 386 | if (size > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 387 | details::logAlwaysFatal("external vector size exceeds 2^32 elements."); |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 388 | } |
| 389 | mSize = static_cast<uint32_t>(size); |
Alexey Polyudov | e229901 | 2016-10-19 09:52:00 -0700 | [diff] [blame] | 390 | mOwnsBuffer = shouldOwn; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 391 | } |
| 392 | |
Alexey Polyudov | 6f0c9a1 | 2016-10-18 09:37:35 -0700 | [diff] [blame] | 393 | T *data() { |
| 394 | return mBuffer; |
| 395 | } |
| 396 | |
| 397 | const T *data() const { |
| 398 | return mBuffer; |
| 399 | } |
| 400 | |
Alexey Polyudov | c98a99c | 2016-10-18 09:43:56 -0700 | [diff] [blame] | 401 | T *releaseData() { |
| 402 | if (!mOwnsBuffer && mSize > 0) { |
| 403 | resize(mSize); |
| 404 | } |
| 405 | mOwnsBuffer = false; |
| 406 | return mBuffer; |
| 407 | } |
| 408 | |
Martijn Coenen | e8c36e1 | 2017-05-30 16:25:07 -0700 | [diff] [blame] | 409 | hidl_vec &operator=(hidl_vec &&other) noexcept { |
Janis Danisevskis | d3ddf62 | 2016-10-24 11:40:50 +0100 | [diff] [blame] | 410 | if (mOwnsBuffer) { |
| 411 | delete[] mBuffer; |
| 412 | } |
Alexey Polyudov | 0ebdbe8 | 2016-10-18 09:31:46 -0700 | [diff] [blame] | 413 | mBuffer = other.mBuffer; |
| 414 | mSize = other.mSize; |
| 415 | mOwnsBuffer = other.mOwnsBuffer; |
| 416 | other.mOwnsBuffer = false; |
| 417 | return *this; |
| 418 | } |
| 419 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 420 | hidl_vec &operator=(const hidl_vec &other) { |
| 421 | if (this != &other) { |
| 422 | if (mOwnsBuffer) { |
| 423 | delete[] mBuffer; |
| 424 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 425 | copyFrom(other, other.mSize); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | return *this; |
| 429 | } |
| 430 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 431 | // copy from an std::vector. |
| 432 | hidl_vec &operator=(const std::vector<T> &other) { |
| 433 | if (mOwnsBuffer) { |
| 434 | delete[] mBuffer; |
| 435 | } |
| 436 | copyFrom(other, other.size()); |
| 437 | return *this; |
| 438 | } |
| 439 | |
| 440 | // cast to an std::vector. |
| 441 | operator std::vector<T>() const { |
| 442 | std::vector<T> v(mSize); |
| 443 | for (size_t i = 0; i < mSize; ++i) { |
| 444 | v[i] = mBuffer[i]; |
| 445 | } |
| 446 | return v; |
| 447 | } |
| 448 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 449 | // equality check, assuming that T::operator== is defined. |
| 450 | bool operator==(const hidl_vec &other) const { |
| 451 | if (mSize != other.size()) { |
| 452 | return false; |
| 453 | } |
| 454 | for (size_t i = 0; i < mSize; ++i) { |
| 455 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 456 | return false; |
| 457 | } |
| 458 | } |
| 459 | return true; |
| 460 | } |
| 461 | |
| 462 | // inequality check, assuming that T::operator== is defined. |
| 463 | inline bool operator!=(const hidl_vec &other) const { |
| 464 | return !((*this) == other); |
| 465 | } |
| 466 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 467 | size_t size() const { |
| 468 | return mSize; |
| 469 | } |
| 470 | |
| 471 | T &operator[](size_t index) { |
| 472 | return mBuffer[index]; |
| 473 | } |
| 474 | |
| 475 | const T &operator[](size_t index) const { |
| 476 | return mBuffer[index]; |
| 477 | } |
| 478 | |
Steven Moreland | aa661fc | 2017-10-06 09:40:18 -0700 | [diff] [blame] | 479 | // Does not initialize primitive types if new size > old size. |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 480 | void resize(size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 481 | if (size > UINT32_MAX) { |
Hridya Valsaraju | b737030 | 2017-03-08 14:39:23 -0800 | [diff] [blame] | 482 | details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 483 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 484 | T *newBuffer = new T[size]; |
| 485 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 486 | for (size_t i = 0; i < std::min(static_cast<uint32_t>(size), mSize); ++i) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 487 | newBuffer[i] = mBuffer[i]; |
| 488 | } |
| 489 | |
| 490 | if (mOwnsBuffer) { |
| 491 | delete[] mBuffer; |
| 492 | } |
| 493 | mBuffer = newBuffer; |
| 494 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 495 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 496 | mOwnsBuffer = true; |
| 497 | } |
| 498 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 499 | // offsetof(hidl_string, mBuffer) exposed since mBuffer is private. |
| 500 | static const size_t kOffsetOfBuffer; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 501 | |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 502 | private: |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 503 | // Define std interator interface for walking the array contents |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 504 | template<bool is_const> |
| 505 | class iter : public std::iterator< |
| 506 | std::random_access_iterator_tag, /* Category */ |
| 507 | T, |
| 508 | ptrdiff_t, /* Distance */ |
| 509 | typename std::conditional<is_const, const T *, T *>::type /* Pointer */, |
| 510 | typename std::conditional<is_const, const T &, T &>::type /* Reference */> |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 511 | { |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 512 | using traits = std::iterator_traits<iter>; |
| 513 | using ptr_type = typename traits::pointer; |
| 514 | using ref_type = typename traits::reference; |
| 515 | using diff_type = typename traits::difference_type; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 516 | public: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 517 | iter(ptr_type ptr) : mPtr(ptr) { } |
| 518 | inline iter &operator++() { mPtr++; return *this; } |
| 519 | inline iter operator++(int) { iter i = *this; mPtr++; return i; } |
| 520 | inline iter &operator--() { mPtr--; return *this; } |
| 521 | inline iter operator--(int) { iter i = *this; mPtr--; return i; } |
| 522 | inline friend iter operator+(diff_type n, const iter &it) { return it.mPtr + n; } |
| 523 | inline iter operator+(diff_type n) const { return mPtr + n; } |
| 524 | inline iter operator-(diff_type n) const { return mPtr - n; } |
| 525 | inline diff_type operator-(const iter &other) const { return mPtr - other.mPtr; } |
| 526 | inline iter &operator+=(diff_type n) { mPtr += n; return *this; } |
| 527 | inline iter &operator-=(diff_type n) { mPtr -= n; return *this; } |
| 528 | inline ref_type operator*() const { return *mPtr; } |
| 529 | inline ptr_type operator->() const { return mPtr; } |
| 530 | inline bool operator==(const iter &rhs) const { return mPtr == rhs.mPtr; } |
| 531 | inline bool operator!=(const iter &rhs) const { return mPtr != rhs.mPtr; } |
| 532 | inline bool operator< (const iter &rhs) const { return mPtr < rhs.mPtr; } |
| 533 | inline bool operator> (const iter &rhs) const { return mPtr > rhs.mPtr; } |
| 534 | inline bool operator<=(const iter &rhs) const { return mPtr <= rhs.mPtr; } |
| 535 | inline bool operator>=(const iter &rhs) const { return mPtr >= rhs.mPtr; } |
| 536 | inline ref_type operator[](size_t n) const { return mPtr[n]; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 537 | private: |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 538 | ptr_type mPtr; |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 539 | }; |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 540 | public: |
| 541 | using iterator = iter<false /* is_const */>; |
| 542 | using const_iterator = iter<true /* is_const */>; |
| 543 | |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 544 | iterator begin() { return data(); } |
| 545 | iterator end() { return data()+mSize; } |
Yifan Hong | 64fdf4d | 2016-12-09 16:22:45 -0800 | [diff] [blame] | 546 | const_iterator begin() const { return data(); } |
| 547 | const_iterator end() const { return data()+mSize; } |
Scott Randolph | bb840f7 | 2016-11-21 14:39:26 -0800 | [diff] [blame] | 548 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 549 | private: |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 550 | details::hidl_pointer<T> mBuffer; |
| 551 | uint32_t mSize; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 552 | bool mOwnsBuffer; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 553 | |
| 554 | // copy from an array-like object, assuming my resources are freed. |
| 555 | template <typename Array> |
| 556 | void copyFrom(const Array &data, size_t size) { |
Chia-I Wu | 4b48edc | 2016-12-07 22:31:17 +0800 | [diff] [blame] | 557 | mSize = static_cast<uint32_t>(size); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 558 | mOwnsBuffer = true; |
| 559 | if (mSize > 0) { |
| 560 | mBuffer = new T[size]; |
| 561 | for (size_t i = 0; i < size; ++i) { |
| 562 | mBuffer[i] = data[i]; |
| 563 | } |
| 564 | } else { |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 565 | mBuffer = nullptr; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 568 | }; |
| 569 | |
Yifan Hong | 089ae13 | 2016-11-11 11:32:52 -0800 | [diff] [blame] | 570 | template <typename T> |
| 571 | const size_t hidl_vec<T>::kOffsetOfBuffer = offsetof(hidl_vec<T>, mBuffer); |
| 572 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 573 | //////////////////////////////////////////////////////////////////////////////// |
| 574 | |
| 575 | namespace details { |
| 576 | |
| 577 | template<size_t SIZE1, size_t... SIZES> |
| 578 | struct product { |
| 579 | static constexpr size_t value = SIZE1 * product<SIZES...>::value; |
| 580 | }; |
| 581 | |
| 582 | template<size_t SIZE1> |
| 583 | struct product<SIZE1> { |
| 584 | static constexpr size_t value = SIZE1; |
| 585 | }; |
| 586 | |
| 587 | template<typename T, size_t SIZE1, size_t... SIZES> |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 588 | struct std_array { |
| 589 | using type = std::array<typename std_array<T, SIZES...>::type, SIZE1>; |
| 590 | }; |
| 591 | |
| 592 | template<typename T, size_t SIZE1> |
| 593 | struct std_array<T, SIZE1> { |
| 594 | using type = std::array<T, SIZE1>; |
| 595 | }; |
| 596 | |
| 597 | template<typename T, size_t SIZE1, size_t... SIZES> |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 598 | struct accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 599 | |
| 600 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 601 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 602 | explicit accessor(T *base) |
| 603 | : mBase(base) { |
| 604 | } |
| 605 | |
| 606 | accessor<T, SIZES...> operator[](size_t index) { |
| 607 | return accessor<T, SIZES...>( |
| 608 | &mBase[index * product<SIZES...>::value]); |
| 609 | } |
| 610 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 611 | accessor &operator=(const std_array_type &other) { |
| 612 | for (size_t i = 0; i < SIZE1; ++i) { |
| 613 | (*this)[i] = other[i]; |
| 614 | } |
| 615 | return *this; |
| 616 | } |
| 617 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 618 | private: |
| 619 | T *mBase; |
| 620 | }; |
| 621 | |
| 622 | template<typename T, size_t SIZE1> |
| 623 | struct accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 624 | |
| 625 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 626 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 627 | explicit accessor(T *base) |
| 628 | : mBase(base) { |
| 629 | } |
| 630 | |
| 631 | T &operator[](size_t index) { |
| 632 | return mBase[index]; |
| 633 | } |
| 634 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 635 | accessor &operator=(const std_array_type &other) { |
| 636 | for (size_t i = 0; i < SIZE1; ++i) { |
| 637 | (*this)[i] = other[i]; |
| 638 | } |
| 639 | return *this; |
| 640 | } |
| 641 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 642 | private: |
| 643 | T *mBase; |
| 644 | }; |
| 645 | |
| 646 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 647 | struct const_accessor { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 648 | |
| 649 | using std_array_type = typename std_array<T, SIZE1, SIZES...>::type; |
| 650 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 651 | explicit const_accessor(const T *base) |
| 652 | : mBase(base) { |
| 653 | } |
| 654 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 655 | const_accessor<T, SIZES...> operator[](size_t index) const { |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 656 | return const_accessor<T, SIZES...>( |
| 657 | &mBase[index * product<SIZES...>::value]); |
| 658 | } |
| 659 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 660 | operator std_array_type() { |
| 661 | std_array_type array; |
| 662 | for (size_t i = 0; i < SIZE1; ++i) { |
| 663 | array[i] = (*this)[i]; |
| 664 | } |
| 665 | return array; |
| 666 | } |
| 667 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 668 | private: |
| 669 | const T *mBase; |
| 670 | }; |
| 671 | |
| 672 | template<typename T, size_t SIZE1> |
| 673 | struct const_accessor<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 674 | |
| 675 | using std_array_type = typename std_array<T, SIZE1>::type; |
| 676 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 677 | explicit const_accessor(const T *base) |
| 678 | : mBase(base) { |
| 679 | } |
| 680 | |
| 681 | const T &operator[](size_t index) const { |
| 682 | return mBase[index]; |
| 683 | } |
| 684 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 685 | operator std_array_type() { |
| 686 | std_array_type array; |
| 687 | for (size_t i = 0; i < SIZE1; ++i) { |
| 688 | array[i] = (*this)[i]; |
| 689 | } |
| 690 | return array; |
| 691 | } |
| 692 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 693 | private: |
| 694 | const T *mBase; |
| 695 | }; |
| 696 | |
| 697 | } // namespace details |
| 698 | |
| 699 | //////////////////////////////////////////////////////////////////////////////// |
| 700 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 701 | // A multidimensional array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 702 | template<typename T, size_t SIZE1, size_t... SIZES> |
| 703 | struct hidl_array { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 704 | |
| 705 | using std_array_type = typename details::std_array<T, SIZE1, SIZES...>::type; |
| 706 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 707 | hidl_array() = default; |
| 708 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 709 | // Copies the data from source, using T::operator=(const T &). |
| 710 | hidl_array(const T *source) { |
| 711 | for (size_t i = 0; i < elementCount(); ++i) { |
| 712 | mBuffer[i] = source[i]; |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 717 | hidl_array(const std_array_type &array) { |
| 718 | details::accessor<T, SIZE1, SIZES...> modifier(mBuffer); |
| 719 | modifier = array; |
| 720 | } |
| 721 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 722 | T *data() { return mBuffer; } |
| 723 | const T *data() const { return mBuffer; } |
| 724 | |
| 725 | details::accessor<T, SIZES...> operator[](size_t index) { |
| 726 | return details::accessor<T, SIZES...>( |
| 727 | &mBuffer[index * details::product<SIZES...>::value]); |
| 728 | } |
| 729 | |
| 730 | details::const_accessor<T, SIZES...> operator[](size_t index) const { |
| 731 | return details::const_accessor<T, SIZES...>( |
| 732 | &mBuffer[index * details::product<SIZES...>::value]); |
| 733 | } |
| 734 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 735 | // equality check, assuming that T::operator== is defined. |
| 736 | bool operator==(const hidl_array &other) const { |
| 737 | for (size_t i = 0; i < elementCount(); ++i) { |
| 738 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 739 | return false; |
| 740 | } |
| 741 | } |
| 742 | return true; |
| 743 | } |
| 744 | |
| 745 | inline bool operator!=(const hidl_array &other) const { |
| 746 | return !((*this) == other); |
| 747 | } |
| 748 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 749 | using size_tuple_type = std::tuple<decltype(SIZE1), decltype(SIZES)...>; |
| 750 | |
| 751 | static constexpr size_tuple_type size() { |
| 752 | return std::make_tuple(SIZE1, SIZES...); |
| 753 | } |
| 754 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 755 | static constexpr size_t elementCount() { |
| 756 | return details::product<SIZE1, SIZES...>::value; |
| 757 | } |
| 758 | |
| 759 | operator std_array_type() const { |
| 760 | return details::const_accessor<T, SIZE1, SIZES...>(mBuffer); |
| 761 | } |
| 762 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 763 | private: |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 764 | T mBuffer[elementCount()]; |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 765 | }; |
| 766 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 767 | // An array of T's. Assumes that T::operator=(const T &) is defined. |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 768 | template<typename T, size_t SIZE1> |
| 769 | struct hidl_array<T, SIZE1> { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 770 | |
| 771 | using std_array_type = typename details::std_array<T, SIZE1>::type; |
| 772 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 773 | hidl_array() = default; |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 774 | |
| 775 | // Copies the data from source, using T::operator=(const T &). |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 776 | hidl_array(const T *source) { |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 777 | for (size_t i = 0; i < elementCount(); ++i) { |
| 778 | mBuffer[i] = source[i]; |
| 779 | } |
Sasha Levitskiy | 3da6848 | 2016-11-17 16:48:43 -0800 | [diff] [blame] | 780 | } |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 781 | |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 782 | // Copies the data from the given std::array, using T::operator=(const T &). |
| 783 | hidl_array(const std_array_type &array) : hidl_array(array.data()) {} |
| 784 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 785 | T *data() { return mBuffer; } |
| 786 | const T *data() const { return mBuffer; } |
| 787 | |
| 788 | T &operator[](size_t index) { |
| 789 | return mBuffer[index]; |
| 790 | } |
| 791 | |
| 792 | const T &operator[](size_t index) const { |
| 793 | return mBuffer[index]; |
| 794 | } |
| 795 | |
Yifan Hong | 9fcbb36 | 2016-12-20 16:46:41 -0800 | [diff] [blame] | 796 | // equality check, assuming that T::operator== is defined. |
| 797 | bool operator==(const hidl_array &other) const { |
| 798 | for (size_t i = 0; i < elementCount(); ++i) { |
| 799 | if (!(mBuffer[i] == other.mBuffer[i])) { |
| 800 | return false; |
| 801 | } |
| 802 | } |
| 803 | return true; |
| 804 | } |
| 805 | |
| 806 | inline bool operator!=(const hidl_array &other) const { |
| 807 | return !((*this) == other); |
| 808 | } |
| 809 | |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 810 | static constexpr size_t size() { return SIZE1; } |
Yifan Hong | 44ab623 | 2016-11-22 16:28:24 -0800 | [diff] [blame] | 811 | static constexpr size_t elementCount() { return SIZE1; } |
| 812 | |
| 813 | // Copies the data to an std::array, using T::operator=(T). |
| 814 | operator std_array_type() const { |
| 815 | std_array_type array; |
| 816 | for (size_t i = 0; i < SIZE1; ++i) { |
| 817 | array[i] = mBuffer[i]; |
| 818 | } |
| 819 | return array; |
| 820 | } |
Andreas Huber | 00a985c | 2016-09-28 14:24:53 -0700 | [diff] [blame] | 821 | |
Andreas Huber | 20dce08 | 2016-09-22 19:39:13 -0700 | [diff] [blame] | 822 | private: |
| 823 | T mBuffer[SIZE1]; |
| 824 | }; |
| 825 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 826 | // ---------------------------------------------------------------------- |
| 827 | // Version functions |
| 828 | struct hidl_version { |
| 829 | public: |
Chia-I Wu | 666b76b | 2016-10-06 14:15:59 +0800 | [diff] [blame] | 830 | constexpr hidl_version(uint16_t major, uint16_t minor) : mMajor(major), mMinor(minor) {} |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 831 | |
Steven Moreland | 5d5ef7f | 2016-10-20 19:19:55 -0700 | [diff] [blame] | 832 | bool operator==(const hidl_version& other) const { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 833 | return (mMajor == other.get_major() && mMinor == other.get_minor()); |
| 834 | } |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 835 | |
Eino-Ville Talvala | 19f4db5 | 2016-12-14 15:19:28 -0800 | [diff] [blame] | 836 | bool operator<(const hidl_version& other) const { |
| 837 | return (mMajor < other.get_major() || |
| 838 | (mMajor == other.get_major() && mMinor < other.get_minor())); |
| 839 | } |
| 840 | |
| 841 | bool operator>(const hidl_version& other) const { |
| 842 | return other < *this; |
| 843 | } |
| 844 | |
| 845 | bool operator<=(const hidl_version& other) const { |
| 846 | return !(*this > other); |
| 847 | } |
| 848 | |
| 849 | bool operator>=(const hidl_version& other) const { |
| 850 | return !(*this < other); |
| 851 | } |
| 852 | |
Martijn Coenen | 097a767 | 2016-09-08 16:56:41 +0200 | [diff] [blame] | 853 | constexpr uint16_t get_major() const { return mMajor; } |
| 854 | constexpr uint16_t get_minor() const { return mMinor; } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 855 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 856 | private: |
| 857 | uint16_t mMajor; |
| 858 | uint16_t mMinor; |
| 859 | }; |
| 860 | |
| 861 | inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_t minor) { |
| 862 | return hidl_version(major,minor); |
| 863 | } |
| 864 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 865 | ///////////////////// toString functions |
| 866 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 867 | std::string toString(const void *t); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 868 | |
| 869 | // toString alias for numeric types |
| 870 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 871 | inline std::string toString(T t) { |
| 872 | return std::to_string(t); |
| 873 | } |
| 874 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 875 | namespace details { |
| 876 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 877 | template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> |
| 878 | inline std::string toHexString(T t, bool prefix = true) { |
| 879 | std::ostringstream os; |
| 880 | if (prefix) { os << std::showbase; } |
| 881 | os << std::hex << t; |
| 882 | return os.str(); |
| 883 | } |
| 884 | |
| 885 | template<> |
| 886 | inline std::string toHexString(uint8_t t, bool prefix) { |
| 887 | return toHexString(static_cast<int32_t>(t), prefix); |
| 888 | } |
| 889 | |
| 890 | template<> |
| 891 | inline std::string toHexString(int8_t t, bool prefix) { |
| 892 | return toHexString(static_cast<int32_t>(t), prefix); |
| 893 | } |
| 894 | |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 895 | template<typename Array> |
| 896 | std::string arrayToString(const Array &a, size_t size); |
| 897 | |
| 898 | template<size_t SIZE1> |
| 899 | std::string arraySizeToString() { |
| 900 | return std::string{"["} + toString(SIZE1) + "]"; |
| 901 | } |
| 902 | |
| 903 | template<size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 904 | std::string arraySizeToString() { |
| 905 | return std::string{"["} + toString(SIZE1) + "]" + arraySizeToString<SIZE2, SIZES...>(); |
| 906 | } |
| 907 | |
| 908 | template<typename T, size_t SIZE1> |
| 909 | std::string toString(details::const_accessor<T, SIZE1> a) { |
| 910 | return arrayToString(a, SIZE1); |
| 911 | } |
| 912 | |
| 913 | template<typename Array> |
| 914 | std::string arrayToString(const Array &a, size_t size) { |
| 915 | using android::hardware::toString; |
| 916 | std::string os; |
| 917 | os += "{"; |
| 918 | for (size_t i = 0; i < size; ++i) { |
| 919 | if (i > 0) { |
| 920 | os += ", "; |
| 921 | } |
| 922 | os += toString(a[i]); |
| 923 | } |
| 924 | os += "}"; |
| 925 | return os; |
| 926 | } |
| 927 | |
| 928 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 929 | std::string toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...> a) { |
| 930 | return arrayToString(a, SIZE1); |
| 931 | } |
| 932 | |
| 933 | } //namespace details |
| 934 | |
| 935 | inline std::string toString(const void *t) { |
| 936 | return details::toHexString(reinterpret_cast<uintptr_t>(t)); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | // debug string dump. There will be quotes around the string! |
| 940 | inline std::string toString(const hidl_string &hs) { |
| 941 | return std::string{"\""} + hs.c_str() + "\""; |
| 942 | } |
| 943 | |
| 944 | // debug string dump |
| 945 | inline std::string toString(const hidl_handle &hs) { |
| 946 | return toString(hs.getNativeHandle()); |
| 947 | } |
| 948 | |
| 949 | inline std::string toString(const hidl_memory &mem) { |
| 950 | return std::string{"memory {.name = "} + toString(mem.name()) + ", .size = " |
| 951 | + toString(mem.size()) |
| 952 | + ", .handle = " + toString(mem.handle()) + "}"; |
| 953 | } |
| 954 | |
| 955 | inline std::string toString(const sp<hidl_death_recipient> &dr) { |
| 956 | return std::string{"death_recipient@"} + toString(dr.get()); |
| 957 | } |
| 958 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 959 | // debug string dump, assuming that toString(T) is defined. |
| 960 | template<typename T> |
| 961 | std::string toString(const hidl_vec<T> &a) { |
| 962 | std::string os; |
| 963 | os += "[" + toString(a.size()) + "]"; |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 964 | os += details::arrayToString(a, a.size()); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 965 | return os; |
| 966 | } |
| 967 | |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 968 | template<typename T, size_t SIZE1> |
| 969 | std::string toString(const hidl_array<T, SIZE1> &a) { |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 970 | return details::arraySizeToString<SIZE1>() |
| 971 | + details::toString(details::const_accessor<T, SIZE1>(a.data())); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | template<typename T, size_t SIZE1, size_t SIZE2, size_t... SIZES> |
| 975 | std::string toString(const hidl_array<T, SIZE1, SIZE2, SIZES...> &a) { |
Hridya Valsaraju | 4ff88a3 | 2017-03-09 16:01:02 -0800 | [diff] [blame] | 976 | return details::arraySizeToString<SIZE1, SIZE2, SIZES...>() |
| 977 | + details::toString(details::const_accessor<T, SIZE1, SIZE2, SIZES...>(a.data())); |
Yifan Hong | be7a688 | 2017-01-05 17:30:17 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 980 | } // namespace hardware |
| 981 | } // namespace android |
| 982 | |
Martijn Coenen | c28f115 | 2016-08-22 14:06:56 +0200 | [diff] [blame] | 983 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 984 | #endif // ANDROID_HIDL_SUPPORT_H |