diff options
author | Chunseok Lee <chunseok.lee@samsung.com> | 2018-05-04 17:57:16 +0900 |
---|---|---|
committer | Chunseok Lee <chunseok.lee@samsung.com> | 2018-05-04 17:57:16 +0900 |
commit | 07659ccd9fe7b1cf1547cc6cad78bcf489f0a361 (patch) | |
tree | cf3a123812b7f1ad8b50d7d0ace891e0c03c6110 /runtimes/nn/depend/libutils/include/utils/StrongPointer.h | |
parent | da6f7a3e8360a49fd073a6e0031a4da134d9d984 (diff) | |
download | nnfw-44439e0eae2c6abf8dcee1ba95f371724f65155c.tar.gz nnfw-44439e0eae2c6abf8dcee1ba95f371724f65155c.tar.bz2 nnfw-44439e0eae2c6abf8dcee1ba95f371724f65155c.zip |
Imported Upstream version 0.1upstream/0.1submit/tizen/20180504.091146
Diffstat (limited to 'runtimes/nn/depend/libutils/include/utils/StrongPointer.h')
-rw-r--r-- | runtimes/nn/depend/libutils/include/utils/StrongPointer.h | 245 |
1 files changed, 245 insertions, 0 deletions
diff --git a/runtimes/nn/depend/libutils/include/utils/StrongPointer.h b/runtimes/nn/depend/libutils/include/utils/StrongPointer.h new file mode 100644 index 000000000..0c2060791 --- /dev/null +++ b/runtimes/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<typename T> class wp; + +// --------------------------------------------------------------------------- + +#define COMPARE(_op_) \ +inline bool operator _op_ (const sp<T>& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} \ +inline bool operator _op_ (const T* o) const { \ + return m_ptr _op_ o; \ +} \ +template<typename U> \ +inline bool operator _op_ (const sp<U>& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} \ +template<typename U> \ +inline bool operator _op_ (const U* o) const { \ + return m_ptr _op_ o; \ +} \ +inline bool operator _op_ (const wp<T>& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} \ +template<typename U> \ +inline bool operator _op_ (const wp<U>& o) const { \ + return m_ptr _op_ o.m_ptr; \ +} + +// --------------------------------------------------------------------------- + +template<typename T> +class sp { +public: + inline sp() : m_ptr(0) { } + + sp(T* other); // NOLINT(implicit) + sp(const sp<T>& other); + sp(sp<T>&& other); + template<typename U> sp(U* other); // NOLINT(implicit) + template<typename U> sp(const sp<U>& other); // NOLINT(implicit) + template<typename U> sp(sp<U>&& other); // NOLINT(implicit) + + ~sp(); + + // Assignment + + sp& operator = (T* other); + sp& operator = (const sp<T>& other); + sp& operator = (sp<T>&& other); + + template<typename U> sp& operator = (const sp<U>& other); + template<typename U> sp& operator = (sp<U>&& other); + template<typename U> 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<typename Y> friend class sp; + template<typename Y> 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<typename T> +sp<T>::sp(T* other) + : m_ptr(other) { + if (other) + other->incStrong(this); +} + +template<typename T> +sp<T>::sp(const sp<T>& other) + : m_ptr(other.m_ptr) { + if (m_ptr) + m_ptr->incStrong(this); +} + +template<typename T> +sp<T>::sp(sp<T>&& other) + : m_ptr(other.m_ptr) { + other.m_ptr = nullptr; +} + +template<typename T> template<typename U> +sp<T>::sp(U* other) + : m_ptr(other) { + if (other) + (static_cast<T*>(other))->incStrong(this); +} + +template<typename T> template<typename U> +sp<T>::sp(const sp<U>& other) + : m_ptr(other.m_ptr) { + if (m_ptr) + m_ptr->incStrong(this); +} + +template<typename T> template<typename U> +sp<T>::sp(sp<U>&& other) + : m_ptr(other.m_ptr) { + other.m_ptr = nullptr; +} + +template<typename T> +sp<T>::~sp() { + if (m_ptr) + m_ptr->decStrong(this); +} + +template<typename T> +sp<T>& sp<T>::operator =(const sp<T>& other) { + // Force m_ptr to be read twice, to heuristically check for data races. + T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); + T* otherPtr(other.m_ptr); + if (otherPtr) otherPtr->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); + m_ptr = otherPtr; + return *this; +} + +template<typename T> +sp<T>& sp<T>::operator =(sp<T>&& other) { + T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); + m_ptr = other.m_ptr; + other.m_ptr = nullptr; + return *this; +} + +template<typename T> +sp<T>& sp<T>::operator =(T* other) { + T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); + if (other) other->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); + m_ptr = other; + return *this; +} + +template<typename T> template<typename U> +sp<T>& sp<T>::operator =(const sp<U>& other) { + T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); + T* otherPtr(other.m_ptr); + if (otherPtr) otherPtr->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); + m_ptr = otherPtr; + return *this; +} + +template<typename T> template<typename U> +sp<T>& sp<T>::operator =(sp<U>&& other) { + T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); + if (m_ptr) m_ptr->decStrong(this); + if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); + m_ptr = other.m_ptr; + other.m_ptr = nullptr; + return *this; +} + +template<typename T> template<typename U> +sp<T>& sp<T>::operator =(U* other) { + T* oldPtr(*const_cast<T* volatile*>(&m_ptr)); + if (other) (static_cast<T*>(other))->incStrong(this); + if (oldPtr) oldPtr->decStrong(this); + if (oldPtr != *const_cast<T* volatile*>(&m_ptr)) sp_report_race(); + m_ptr = other; + return *this; +} + +template<typename T> +void sp<T>::force_set(T* other) { + other->forceIncStrong(this); + m_ptr = other; +} + +template<typename T> +void sp<T>::clear() { + if (m_ptr) { + m_ptr->decStrong(this); + m_ptr = 0; + } +} + +template<typename T> +void sp<T>::set_pointer(T* ptr) { + m_ptr = ptr; +} + +}; // namespace android + +// --------------------------------------------------------------------------- + +#endif // ANDROID_STRONG_POINTER_H |