From 7cc4960d7fe1107696a698ac2d4bb5bb95c32f54 Mon Sep 17 00:00:00 2001 From: "jiseob.jang" Date: Mon, 26 Mar 2018 20:31:31 +0900 Subject: [PATCH] Introduce hidl_string from libhidl of android. This commit introduce hidl_string from libhidl of android. Signed-off-by: jiseob.jang --- .../ref/nn/depend/libhidl/base/CMakeLists.txt | 2 +- .../ref/nn/depend/libhidl/base/HidlSupport.cpp | 284 +++++++++++++++++++++ .../depend/libhidl/base/include/hidl/HidlSupport.h | 9 +- src/runtime/ref/nn/include/Log.h | 1 + 4 files changed, 290 insertions(+), 6 deletions(-) create mode 100644 src/runtime/ref/nn/depend/libhidl/base/HidlSupport.cpp diff --git a/src/runtime/ref/nn/depend/libhidl/base/CMakeLists.txt b/src/runtime/ref/nn/depend/libhidl/base/CMakeLists.txt index 95a245e..b0a6d07 100644 --- a/src/runtime/ref/nn/depend/libhidl/base/CMakeLists.txt +++ b/src/runtime/ref/nn/depend/libhidl/base/CMakeLists.txt @@ -12,7 +12,7 @@ SET(INC_DIRS SET(CUR_SRCS # ${CMAKE_CURRENT_SOURCE_DIR}/HidlInternal.cpp -# ${CMAKE_CURRENT_SOURCE_DIR}/HidlSupport.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/HidlSupport.cpp # ${CMAKE_CURRENT_SOURCE_DIR}/Status.cpp # ${CMAKE_CURRENT_SOURCE_DIR}/TaskRunner.cpp ) diff --git a/src/runtime/ref/nn/depend/libhidl/base/HidlSupport.cpp b/src/runtime/ref/nn/depend/libhidl/base/HidlSupport.cpp new file mode 100644 index 0000000..9f6320a --- /dev/null +++ b/src/runtime/ref/nn/depend/libhidl/base/HidlSupport.cpp @@ -0,0 +1,284 @@ +/* + * 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 "HidlSupport" + +#include + +#include + +#include +#if 0 // REF-ANN +#include +#include +#endif + +namespace android { +namespace hardware { + +#if 0 // REF-ANN +namespace details { +bool debuggable() { +#ifdef LIBHIDL_TARGET_DEBUGGABLE + return true; +#else + return false; +#endif +} +} // namespace details + +hidl_handle::hidl_handle() { + mHandle = nullptr; + mOwnsHandle = false; +} + +hidl_handle::~hidl_handle() { + freeHandle(); +} + +hidl_handle::hidl_handle(const native_handle_t *handle) { + mHandle = handle; + mOwnsHandle = false; +} + +// copy constructor. +hidl_handle::hidl_handle(const hidl_handle &other) { + mOwnsHandle = false; + *this = other; +} + +// move constructor. +hidl_handle::hidl_handle(hidl_handle &&other) noexcept { + mOwnsHandle = false; + *this = std::move(other); +} + +// assignment operators +hidl_handle &hidl_handle::operator=(const hidl_handle &other) { + if (this == &other) { + return *this; + } + freeHandle(); + if (other.mHandle != nullptr) { + mHandle = native_handle_clone(other.mHandle); + if (mHandle == nullptr) { + PLOG(FATAL) << "Failed to clone native_handle in hidl_handle"; + } + mOwnsHandle = true; + } else { + mHandle = nullptr; + mOwnsHandle = false; + } + return *this; +} + +hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) { + freeHandle(); + mHandle = native_handle; + mOwnsHandle = false; + return *this; +} + +hidl_handle &hidl_handle::operator=(hidl_handle &&other) noexcept { + if (this != &other) { + freeHandle(); + mHandle = other.mHandle; + mOwnsHandle = other.mOwnsHandle; + other.mHandle = nullptr; + other.mOwnsHandle = false; + } + return *this; +} + +void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) { + freeHandle(); + mHandle = handle; + mOwnsHandle = shouldOwn; +} + +const native_handle_t* hidl_handle::operator->() const { + return mHandle; +} + +// implicit conversion to const native_handle_t* +hidl_handle::operator const native_handle_t *() const { + return mHandle; +} + +// explicit conversion +const native_handle_t *hidl_handle::getNativeHandle() const { + return mHandle; +} + +void hidl_handle::freeHandle() { + if (mOwnsHandle && mHandle != nullptr) { + // This can only be true if: + // 1. Somebody called setTo() with shouldOwn=true, so we know the handle + // wasn't const to begin with. + // 2. Copy/assignment from another hidl_handle, in which case we have + // cloned the handle. + // 3. Move constructor from another hidl_handle, in which case the original + // hidl_handle must have been non-const as well. + native_handle_t *handle = const_cast( + static_cast(mHandle)); + native_handle_close(handle); + native_handle_delete(handle); + mHandle = nullptr; + } +} +#endif + +static const char *const kEmptyString = ""; + +hidl_string::hidl_string() + : mBuffer(kEmptyString), + mSize(0), + mOwnsBuffer(false) { +} + +hidl_string::~hidl_string() { + clear(); +} + +hidl_string::hidl_string(const char *s) : hidl_string() { + if (s == nullptr) { + return; + } + + copyFrom(s, strlen(s)); +} + +hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { + copyFrom(s, length); +} + +hidl_string::hidl_string(const hidl_string &other): hidl_string() { + copyFrom(other.c_str(), other.size()); +} + +hidl_string::hidl_string(const std::string &s) : hidl_string() { + copyFrom(s.c_str(), s.size()); +} + +hidl_string::hidl_string(hidl_string &&other) noexcept : hidl_string() { + moveFrom(std::forward(other)); +} + +hidl_string &hidl_string::operator=(hidl_string &&other) noexcept { + if (this != &other) { + clear(); + moveFrom(std::forward(other)); + } + return *this; +} + +hidl_string &hidl_string::operator=(const hidl_string &other) { + if (this != &other) { + clear(); + copyFrom(other.c_str(), other.size()); + } + + return *this; +} + +hidl_string &hidl_string::operator=(const char *s) { + clear(); + + if (s == nullptr) { + return *this; + } + + copyFrom(s, strlen(s)); + return *this; +} + +hidl_string &hidl_string::operator=(const std::string &s) { + clear(); + copyFrom(s.c_str(), s.size()); + return *this; +} + +hidl_string::operator std::string() const { + return std::string(mBuffer, mSize); +} + +std::ostream& operator<<(std::ostream& os, const hidl_string& str) { + os << str.c_str(); + return os; +} + +void hidl_string::copyFrom(const char *data, size_t size) { + // assume my resources are freed. + + if (size > UINT32_MAX) { + LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size; + } + char *buf = (char *)malloc(size + 1); + memcpy(buf, data, size); + buf[size] = '\0'; + mBuffer = buf; + + mSize = static_cast(size); + mOwnsBuffer = true; +} + +void hidl_string::moveFrom(hidl_string &&other) { + // assume my resources are freed. + + mBuffer = std::move(other.mBuffer); + mSize = other.mSize; + mOwnsBuffer = other.mOwnsBuffer; + + other.mOwnsBuffer = false; + other.clear(); +} + +void hidl_string::clear() { + if (mOwnsBuffer && (mBuffer != kEmptyString)) { + free(const_cast(static_cast(mBuffer))); + } + + mBuffer = kEmptyString; + mSize = 0; + mOwnsBuffer = false; +} + +void hidl_string::setToExternal(const char *data, size_t size) { + if (size > UINT32_MAX) { + LOG(FATAL) << "string size can't exceed 2^32 bytes: " << size; + } + clear(); + + mBuffer = data; + mSize = static_cast(size); + mOwnsBuffer = false; +} + +const char *hidl_string::c_str() const { + return mBuffer; +} + +size_t hidl_string::size() const { + return mSize; +} + +bool hidl_string::empty() const { + return mSize == 0; +} + +} // namespace hardware +} // namespace android + + diff --git a/src/runtime/ref/nn/depend/libhidl/base/include/hidl/HidlSupport.h b/src/runtime/ref/nn/depend/libhidl/base/include/hidl/HidlSupport.h index f51f6b3..c747a35 100644 --- a/src/runtime/ref/nn/depend/libhidl/base/include/hidl/HidlSupport.h +++ b/src/runtime/ref/nn/depend/libhidl/base/include/hidl/HidlSupport.h @@ -17,6 +17,8 @@ #ifndef ANDROID_HIDL_SUPPORT_H #define ANDROID_HIDL_SUPPORT_H +#include + #include #include #include @@ -130,6 +132,7 @@ private: details::hidl_pointer mHandle __attribute__ ((aligned(8))); bool mOwnsHandle __attribute ((aligned(8))); }; +#endif struct hidl_string { hidl_string(); @@ -206,7 +209,6 @@ HIDL_STRING_OPERATOR(>=) // Send our content to the output stream std::ostream& operator<<(std::ostream& os, const hidl_string& str); -#endif // hidl_memory is a structure that can be used to transfer @@ -275,10 +277,7 @@ struct hidl_memory { return mHandle; } - const std::string &name() const { -#if 0 // REF-ANN const hidl_string &name() const { -#endif return mName; } @@ -294,7 +293,7 @@ struct hidl_memory { private: const native_handle_t *mHandle __attribute__ ((aligned(8))); // TODO-NNRT: This was hidl_handle. uint64_t mSize __attribute__ ((aligned(8))); - std::string mName __attribute__ ((aligned(8))); // TODO-NNRT: This was hidl_string. + hidl_string mName __attribute__ ((aligned(8))); #if 0 // REF-ANN hidl_handle mHandle __attribute__ ((aligned(8))); uint64_t mSize __attribute__ ((aligned(8))); diff --git a/src/runtime/ref/nn/include/Log.h b/src/runtime/ref/nn/include/Log.h index ac39355..412f4ec 100644 --- a/src/runtime/ref/nn/include/Log.h +++ b/src/runtime/ref/nn/include/Log.h @@ -2,3 +2,4 @@ #define LOG(...) std::cout #define VLOG(...) LOG(...) +#define PLOG(...) LOG(...) -- 2.7.4