From: 김수진/동작제어Lab(SR)/Engineer/삼성전자 Date: Wed, 28 Mar 2018 07:32:22 +0000 (+0900) Subject: Copy StrongPointer from ANN (#255) X-Git-Tag: 0.1~559 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a880bb432f1da5c3ba33d241f7c33d6a948f6a97;p=platform%2Fcore%2Fml%2Fnnfw.git Copy StrongPointer from ANN (#255) * Copy StrongPointer from ANN This commit copies StrongPointer from ANN. Signed-off-by: sjsujinkim * Inlucde StrongPointer.h into HidlSupport.h --- 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 c747a35..55e302a 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 @@ -27,6 +27,7 @@ #include #include #include +#include #if 0 // REF-ANN #include @@ -42,7 +43,6 @@ #include #include #include -#include #include #endif diff --git a/src/runtime/ref/nn/depend/libutils/include/utils/StrongPointer.h b/src/runtime/ref/nn/depend/libutils/include/utils/StrongPointer.h new file mode 100644 index 0000000..0c20607 --- /dev/null +++ b/src/runtime/ref/nn/depend/libutils/include/utils/StrongPointer.h @@ -0,0 +1,245 @@ +/* + * Copyright (C) 2005 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. + */ + +#ifndef ANDROID_STRONG_POINTER_H +#define ANDROID_STRONG_POINTER_H + +// --------------------------------------------------------------------------- +namespace android { + +template class wp; + +// --------------------------------------------------------------------------- + +#define COMPARE(_op_) \ +inline bool operator _op_ (const sp& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} \ +inline bool operator _op_ (const T* o) const { \ + return m_ptr _op_ o; \ +} \ +template \ +inline bool operator _op_ (const sp& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} \ +template \ +inline bool operator _op_ (const U* o) const { \ + return m_ptr _op_ o; \ +} \ +inline bool operator _op_ (const wp& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} \ +template \ +inline bool operator _op_ (const wp& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} + +// --------------------------------------------------------------------------- + +template +class sp { +public: + inline sp() : m_ptr(0) { } + + sp(T* other); // NOLINT(implicit) + sp(const sp& other); + sp(sp&& other); + template sp(U* other); // NOLINT(implicit) + template sp(const sp& other); // NOLINT(implicit) + template sp(sp&& other); // NOLINT(implicit) + + ~sp(); + + // Assignment + + sp& operator = (T* other); + sp& operator = (const sp& other); + sp& operator = (sp&& other); + + template sp& operator = (const sp& other); + template sp& operator = (sp&& other); + template sp& operator = (U* other); + + //! Special optimization for use by ProcessState (and nobody else). + void force_set(T* other); + + // Reset + + void clear(); + + // Accessors + + inline T& operator* () const { return *m_ptr; } + inline T* operator-> () const { return m_ptr; } + inline T* get() const { return m_ptr; } + + // Operators + + COMPARE(==) + COMPARE(!=) + COMPARE(>) + COMPARE(<) + COMPARE(<=) + COMPARE(>=) + +private: + template friend class sp; + template friend class wp; + void set_pointer(T* ptr); + T* m_ptr; +}; + +// For code size reasons, we do not want this inlined or templated. +void sp_report_race(); + +#undef COMPARE + +// --------------------------------------------------------------------------- +// No user serviceable parts below here. + +template +sp::sp(T* other) + : m_ptr(other) { + if (other) + other->incStrong(this); +} + +template +sp::sp(const sp& other) + : m_ptr(other.m_ptr) { + if (m_ptr) + m_ptr->incStrong(this); +} + +template +sp::sp(sp&& other) + : m_ptr(other.m_ptr) { + other.m_ptr = nullptr; +} + +template template +sp::sp(U* other) + : m_ptr(other) { + if (other) + (static_cast(other))->incStrong(this); +} + +template template +sp::sp(const sp& other) + : m_ptr(other.m_ptr) { + if (m_ptr) + m_ptr->incStrong(this); +} + +template template +sp::sp(sp&& other) + : m_ptr(other.m_ptr) { + other.m_ptr = nullptr; +} + +template +sp::~sp() { + if (m_ptr) + m_ptr->decStrong(this); +} + +template +sp& sp::operator =(const sp& other) { + // Force m_ptr to be read twice, to heuristically check for data races. + T* oldPtr(*const_cast(&m_ptr)); + T* otherPtr(other.m_ptr); + if (otherPtr) otherPtr->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = otherPtr; + return *this; +} + +template +sp& sp::operator =(sp&& other) { + T* oldPtr(*const_cast(&m_ptr)); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = other.m_ptr; + other.m_ptr = nullptr; + return *this; +} + +template +sp& sp::operator =(T* other) { + T* oldPtr(*const_cast(&m_ptr)); + if (other) other->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = other; + return *this; +} + +template template +sp& sp::operator =(const sp& other) { + T* oldPtr(*const_cast(&m_ptr)); + T* otherPtr(other.m_ptr); + if (otherPtr) otherPtr->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = otherPtr; + return *this; +} + +template template +sp& sp::operator =(sp&& other) { + T* oldPtr(*const_cast(&m_ptr)); + if (m_ptr) m_ptr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = other.m_ptr; + other.m_ptr = nullptr; + return *this; +} + +template template +sp& sp::operator =(U* other) { + T* oldPtr(*const_cast(&m_ptr)); + if (other) (static_cast(other))->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast(&m_ptr)) sp_report_race(); + m_ptr = other; + return *this; +} + +template +void sp::force_set(T* other) { + other->forceIncStrong(this); + m_ptr = other; +} + +template +void sp::clear() { + if (m_ptr) { + m_ptr->decStrong(this); + m_ptr = 0; + } +} + +template +void sp::set_pointer(T* ptr) { + m_ptr = ptr; +} + +}; // namespace android + +// --------------------------------------------------------------------------- + +#endif // ANDROID_STRONG_POINTER_H