2009-03-04 03:31:44 +00:00
|
|
|
/*
|
|
|
|
* 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_REF_BASE_H
|
|
|
|
#define ANDROID_REF_BASE_H
|
|
|
|
|
|
|
|
#include <cutils/atomic.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdlib.h>
|
2011-02-17 04:23:43 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <utils/StrongPointer.h>
|
2012-03-16 21:45:49 +00:00
|
|
|
#include <utils/TypeHelpers.h>
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
namespace android {
|
|
|
|
|
2011-02-16 23:23:08 +00:00
|
|
|
class TextOutput;
|
|
|
|
TextOutput& printWeakPointer(TextOutput& to, const void* val);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2011-02-10 02:38:55 +00:00
|
|
|
#define COMPARE_WEAK(_op_) \
|
2009-03-04 03:31:44 +00:00
|
|
|
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> \
|
2011-02-10 02:38:55 +00:00
|
|
|
inline bool operator _op_ (const U* o) const { \
|
|
|
|
return m_ptr _op_ o; \
|
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
2013-03-19 05:27:41 +00:00
|
|
|
|
|
|
|
class ReferenceRenamer {
|
|
|
|
protected:
|
|
|
|
// destructor is purposedly not virtual so we avoid code overhead from
|
|
|
|
// subclasses; we have to make it protected to guarantee that it
|
|
|
|
// cannot be called from this base class (and to make strict compilers
|
|
|
|
// happy).
|
|
|
|
~ReferenceRenamer() { }
|
2011-02-17 04:23:43 +00:00
|
|
|
public:
|
2013-03-19 05:27:41 +00:00
|
|
|
virtual void operator()(size_t i) const = 0;
|
2011-02-17 04:23:43 +00:00
|
|
|
};
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
class RefBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void incStrong(const void* id) const;
|
|
|
|
void decStrong(const void* id) const;
|
|
|
|
|
|
|
|
void forceIncStrong(const void* id) const;
|
|
|
|
|
|
|
|
//! DEBUGGING ONLY: Get current strong ref count.
|
|
|
|
int32_t getStrongCount() const;
|
|
|
|
|
|
|
|
class weakref_type
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefBase* refBase() const;
|
|
|
|
|
|
|
|
void incWeak(const void* id);
|
|
|
|
void decWeak(const void* id);
|
|
|
|
|
2011-08-11 04:07:02 +00:00
|
|
|
// acquires a strong reference if there is already one.
|
2009-03-04 03:31:44 +00:00
|
|
|
bool attemptIncStrong(const void* id);
|
|
|
|
|
2011-08-11 04:07:02 +00:00
|
|
|
// acquires a weak reference if there is already one.
|
|
|
|
// This is not always safe. see ProcessState.cpp and BpBinder.cpp
|
|
|
|
// for proper use.
|
2009-03-04 03:31:44 +00:00
|
|
|
bool attemptIncWeak(const void* id);
|
|
|
|
|
|
|
|
//! DEBUGGING ONLY: Get current weak ref count.
|
|
|
|
int32_t getWeakCount() const;
|
|
|
|
|
|
|
|
//! DEBUGGING ONLY: Print references held on object.
|
|
|
|
void printRefs() const;
|
|
|
|
|
|
|
|
//! DEBUGGING ONLY: Enable tracking for this object.
|
|
|
|
// enable -- enable/disable tracking
|
|
|
|
// retain -- when tracking is enable, if true, then we save a stack trace
|
|
|
|
// for each reference and dereference; when retain == false, we
|
|
|
|
// match up references and dereferences and keep only the
|
|
|
|
// outstanding ones.
|
|
|
|
|
|
|
|
void trackMe(bool enable, bool retain);
|
|
|
|
};
|
|
|
|
|
|
|
|
weakref_type* createWeak(const void* id) const;
|
|
|
|
|
|
|
|
weakref_type* getWeakRefs() const;
|
|
|
|
|
|
|
|
//! DEBUGGING ONLY: Print references held on object.
|
|
|
|
inline void printRefs() const { getWeakRefs()->printRefs(); }
|
|
|
|
|
|
|
|
//! DEBUGGING ONLY: Enable tracking of object.
|
|
|
|
inline void trackMe(bool enable, bool retain)
|
|
|
|
{
|
|
|
|
getWeakRefs()->trackMe(enable, retain);
|
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
typedef RefBase basetype;
|
|
|
|
|
2011-06-13 01:05:53 +00:00
|
|
|
protected:
|
|
|
|
RefBase();
|
|
|
|
virtual ~RefBase();
|
2011-05-20 01:03:31 +00:00
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
//! Flags for extendObjectLifetime()
|
|
|
|
enum {
|
2011-08-11 04:07:02 +00:00
|
|
|
OBJECT_LIFETIME_STRONG = 0x0000,
|
2009-03-04 03:31:44 +00:00
|
|
|
OBJECT_LIFETIME_WEAK = 0x0001,
|
2011-08-11 04:07:02 +00:00
|
|
|
OBJECT_LIFETIME_MASK = 0x0001
|
2009-03-04 03:31:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void extendObjectLifetime(int32_t mode);
|
|
|
|
|
|
|
|
//! Flags for onIncStrongAttempted()
|
|
|
|
enum {
|
|
|
|
FIRST_INC_STRONG = 0x0001
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual void onFirstRef();
|
|
|
|
virtual void onLastStrongRef(const void* id);
|
|
|
|
virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
|
|
|
|
virtual void onLastWeakRef(const void* id);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class weakref_type;
|
|
|
|
class weakref_impl;
|
|
|
|
|
|
|
|
RefBase(const RefBase& o);
|
|
|
|
RefBase& operator=(const RefBase& o);
|
2011-08-11 04:07:02 +00:00
|
|
|
|
2013-03-19 05:27:41 +00:00
|
|
|
private:
|
|
|
|
friend class ReferenceMover;
|
|
|
|
|
|
|
|
static void renameRefs(size_t n, const ReferenceRenamer& renamer);
|
|
|
|
|
|
|
|
static void renameRefId(weakref_type* ref,
|
|
|
|
const void* old_id, const void* new_id);
|
|
|
|
|
|
|
|
static void renameRefId(RefBase* ref,
|
|
|
|
const void* old_id, const void* new_id);
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
weakref_impl* const mRefs;
|
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class LightRefBase
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline LightRefBase() : mCount(0) { }
|
2012-12-06 00:10:26 +00:00
|
|
|
inline void incStrong(__attribute__((unused)) const void* id) const {
|
2009-03-04 03:31:44 +00:00
|
|
|
android_atomic_inc(&mCount);
|
|
|
|
}
|
2012-12-06 00:10:26 +00:00
|
|
|
inline void decStrong(__attribute__((unused)) const void* id) const {
|
2009-03-04 03:31:44 +00:00
|
|
|
if (android_atomic_dec(&mCount) == 1) {
|
|
|
|
delete static_cast<const T*>(this);
|
|
|
|
}
|
|
|
|
}
|
2009-05-04 21:17:04 +00:00
|
|
|
//! DEBUGGING ONLY: Get current strong ref count.
|
|
|
|
inline int32_t getStrongCount() const {
|
|
|
|
return mCount;
|
|
|
|
}
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
typedef LightRefBase<T> basetype;
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
protected:
|
|
|
|
inline ~LightRefBase() { }
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
private:
|
|
|
|
friend class ReferenceMover;
|
2013-03-19 05:27:41 +00:00
|
|
|
inline static void renameRefs(size_t n, const ReferenceRenamer& renamer) { }
|
|
|
|
inline static void renameRefId(T* ref,
|
|
|
|
const void* old_id, const void* new_id) { }
|
2009-03-04 03:31:44 +00:00
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
private:
|
|
|
|
mutable volatile int32_t mCount;
|
2009-03-04 03:31:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class wp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename RefBase::weakref_type weakref_type;
|
|
|
|
|
|
|
|
inline wp() : m_ptr(0) { }
|
|
|
|
|
|
|
|
wp(T* other);
|
|
|
|
wp(const wp<T>& other);
|
|
|
|
wp(const sp<T>& other);
|
|
|
|
template<typename U> wp(U* other);
|
|
|
|
template<typename U> wp(const sp<U>& other);
|
|
|
|
template<typename U> wp(const wp<U>& other);
|
|
|
|
|
|
|
|
~wp();
|
|
|
|
|
|
|
|
// Assignment
|
|
|
|
|
|
|
|
wp& operator = (T* other);
|
|
|
|
wp& operator = (const wp<T>& other);
|
|
|
|
wp& operator = (const sp<T>& other);
|
|
|
|
|
|
|
|
template<typename U> wp& operator = (U* other);
|
|
|
|
template<typename U> wp& operator = (const wp<U>& other);
|
|
|
|
template<typename U> wp& operator = (const sp<U>& other);
|
|
|
|
|
|
|
|
void set_object_and_refs(T* other, weakref_type* refs);
|
|
|
|
|
|
|
|
// promotion to sp
|
|
|
|
|
|
|
|
sp<T> promote() const;
|
|
|
|
|
|
|
|
// Reset
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
|
|
|
|
// Accessors
|
|
|
|
|
|
|
|
inline weakref_type* get_refs() const { return m_refs; }
|
|
|
|
|
|
|
|
inline T* unsafe_get() const { return m_ptr; }
|
|
|
|
|
|
|
|
// Operators
|
2011-02-10 02:38:55 +00:00
|
|
|
|
|
|
|
COMPARE_WEAK(==)
|
|
|
|
COMPARE_WEAK(!=)
|
|
|
|
COMPARE_WEAK(>)
|
|
|
|
COMPARE_WEAK(<)
|
|
|
|
COMPARE_WEAK(<=)
|
|
|
|
COMPARE_WEAK(>=)
|
|
|
|
|
|
|
|
inline bool operator == (const wp<T>& o) const {
|
|
|
|
return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
|
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
inline bool operator == (const wp<U>& o) const {
|
|
|
|
return m_ptr == o.m_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator > (const wp<T>& o) const {
|
|
|
|
return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
|
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
inline bool operator > (const wp<U>& o) const {
|
|
|
|
return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool operator < (const wp<T>& o) const {
|
|
|
|
return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
|
|
|
|
}
|
|
|
|
template<typename U>
|
|
|
|
inline bool operator < (const wp<U>& o) const {
|
|
|
|
return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
|
|
|
|
}
|
|
|
|
inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
|
|
|
|
template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
|
|
|
|
inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
|
|
|
|
template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
|
|
|
|
inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
|
|
|
|
template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
template<typename Y> friend class sp;
|
|
|
|
template<typename Y> friend class wp;
|
|
|
|
|
|
|
|
T* m_ptr;
|
|
|
|
weakref_type* m_refs;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
TextOutput& operator<<(TextOutput& to, const wp<T>& val);
|
|
|
|
|
2011-02-10 02:38:55 +00:00
|
|
|
#undef COMPARE_WEAK
|
2009-03-04 03:31:44 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// No user serviceable parts below here.
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>::wp(T* other)
|
|
|
|
: m_ptr(other)
|
|
|
|
{
|
|
|
|
if (other) m_refs = other->createWeak(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>::wp(const wp<T>& other)
|
|
|
|
: m_ptr(other.m_ptr), m_refs(other.m_refs)
|
|
|
|
{
|
|
|
|
if (m_ptr) m_refs->incWeak(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>::wp(const sp<T>& other)
|
|
|
|
: m_ptr(other.m_ptr)
|
|
|
|
{
|
|
|
|
if (m_ptr) {
|
|
|
|
m_refs = m_ptr->createWeak(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
wp<T>::wp(U* other)
|
|
|
|
: m_ptr(other)
|
|
|
|
{
|
|
|
|
if (other) m_refs = other->createWeak(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
wp<T>::wp(const wp<U>& other)
|
|
|
|
: m_ptr(other.m_ptr)
|
|
|
|
{
|
|
|
|
if (m_ptr) {
|
|
|
|
m_refs = other.m_refs;
|
|
|
|
m_refs->incWeak(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
wp<T>::wp(const sp<U>& other)
|
|
|
|
: m_ptr(other.m_ptr)
|
|
|
|
{
|
|
|
|
if (m_ptr) {
|
|
|
|
m_refs = m_ptr->createWeak(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>::~wp()
|
|
|
|
{
|
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>& wp<T>::operator = (T* other)
|
|
|
|
{
|
|
|
|
weakref_type* newRefs =
|
|
|
|
other ? other->createWeak(this) : 0;
|
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
|
|
|
m_ptr = other;
|
|
|
|
m_refs = newRefs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>& wp<T>::operator = (const wp<T>& other)
|
|
|
|
{
|
2010-06-25 04:49:02 +00:00
|
|
|
weakref_type* otherRefs(other.m_refs);
|
|
|
|
T* otherPtr(other.m_ptr);
|
|
|
|
if (otherPtr) otherRefs->incWeak(this);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
2010-06-25 04:49:02 +00:00
|
|
|
m_ptr = otherPtr;
|
|
|
|
m_refs = otherRefs;
|
2009-03-04 03:31:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
wp<T>& wp<T>::operator = (const sp<T>& other)
|
|
|
|
{
|
|
|
|
weakref_type* newRefs =
|
|
|
|
other != NULL ? other->createWeak(this) : 0;
|
2010-06-25 04:49:02 +00:00
|
|
|
T* otherPtr(other.m_ptr);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
2010-06-25 04:49:02 +00:00
|
|
|
m_ptr = otherPtr;
|
2009-03-04 03:31:44 +00:00
|
|
|
m_refs = newRefs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
wp<T>& wp<T>::operator = (U* other)
|
|
|
|
{
|
|
|
|
weakref_type* newRefs =
|
|
|
|
other ? other->createWeak(this) : 0;
|
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
|
|
|
m_ptr = other;
|
|
|
|
m_refs = newRefs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
wp<T>& wp<T>::operator = (const wp<U>& other)
|
|
|
|
{
|
2010-06-25 04:49:02 +00:00
|
|
|
weakref_type* otherRefs(other.m_refs);
|
|
|
|
U* otherPtr(other.m_ptr);
|
|
|
|
if (otherPtr) otherRefs->incWeak(this);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
2010-06-25 04:49:02 +00:00
|
|
|
m_ptr = otherPtr;
|
|
|
|
m_refs = otherRefs;
|
2009-03-04 03:31:44 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> template<typename U>
|
|
|
|
wp<T>& wp<T>::operator = (const sp<U>& other)
|
|
|
|
{
|
|
|
|
weakref_type* newRefs =
|
|
|
|
other != NULL ? other->createWeak(this) : 0;
|
2010-06-25 04:49:02 +00:00
|
|
|
U* otherPtr(other.m_ptr);
|
2009-03-04 03:31:44 +00:00
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
2010-06-25 04:49:02 +00:00
|
|
|
m_ptr = otherPtr;
|
2009-03-04 03:31:44 +00:00
|
|
|
m_refs = newRefs;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
|
|
|
|
{
|
|
|
|
if (other) refs->incWeak(this);
|
|
|
|
if (m_ptr) m_refs->decWeak(this);
|
|
|
|
m_ptr = other;
|
|
|
|
m_refs = refs;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
sp<T> wp<T>::promote() const
|
|
|
|
{
|
2011-02-25 02:12:34 +00:00
|
|
|
sp<T> result;
|
|
|
|
if (m_ptr && m_refs->attemptIncStrong(&result)) {
|
|
|
|
result.set_pointer(m_ptr);
|
|
|
|
}
|
|
|
|
return result;
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void wp<T>::clear()
|
|
|
|
{
|
|
|
|
if (m_ptr) {
|
|
|
|
m_refs->decWeak(this);
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
|
|
|
|
{
|
2011-02-16 23:23:08 +00:00
|
|
|
return printWeakPointer(to, val.unsafe_get());
|
2009-03-04 03:31:44 +00:00
|
|
|
}
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this class just serves as a namespace so TYPE::moveReferences can stay
|
|
|
|
// private.
|
|
|
|
class ReferenceMover {
|
|
|
|
public:
|
2013-03-19 05:27:41 +00:00
|
|
|
// it would be nice if we could make sure no extra code is generated
|
|
|
|
// for sp<TYPE> or wp<TYPE> when TYPE is a descendant of RefBase:
|
|
|
|
// Using a sp<RefBase> override doesn't work; it's a bit like we wanted
|
|
|
|
// a template<typename TYPE inherits RefBase> template...
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
template<typename TYPE> static inline
|
|
|
|
void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
|
2013-03-19 05:27:41 +00:00
|
|
|
|
|
|
|
class Renamer : public ReferenceRenamer {
|
|
|
|
sp<TYPE>* d;
|
|
|
|
sp<TYPE> const* s;
|
|
|
|
virtual void operator()(size_t i) const {
|
|
|
|
// The id are known to be the sp<>'s this pointer
|
|
|
|
TYPE::renameRefId(d[i].get(), &s[i], &d[i]);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
Renamer(sp<TYPE>* d, sp<TYPE> const* s) : s(s), d(d) { }
|
|
|
|
};
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
memmove(d, s, n*sizeof(sp<TYPE>));
|
2013-03-19 05:27:41 +00:00
|
|
|
TYPE::renameRefs(n, Renamer(d, s));
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
2013-03-19 05:27:41 +00:00
|
|
|
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
template<typename TYPE> static inline
|
|
|
|
void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
|
2013-03-19 05:27:41 +00:00
|
|
|
|
|
|
|
class Renamer : public ReferenceRenamer {
|
|
|
|
wp<TYPE>* d;
|
|
|
|
wp<TYPE> const* s;
|
|
|
|
virtual void operator()(size_t i) const {
|
|
|
|
// The id are known to be the wp<>'s this pointer
|
|
|
|
TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]);
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
Renamer(wp<TYPE>* d, wp<TYPE> const* s) : s(s), d(d) { }
|
|
|
|
};
|
|
|
|
|
2011-02-17 04:23:43 +00:00
|
|
|
memmove(d, s, n*sizeof(wp<TYPE>));
|
2013-03-19 05:27:41 +00:00
|
|
|
TYPE::renameRefs(n, Renamer(d, s));
|
2011-02-17 04:23:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// specialization for moving sp<> and wp<> types.
|
|
|
|
// these are used by the [Sorted|Keyed]Vector<> implementations
|
|
|
|
// sp<> and wp<> need to be handled specially, because they do not
|
|
|
|
// have trivial copy operation in the general case (see RefBase.cpp
|
|
|
|
// when DEBUG ops are enabled), but can be implemented very
|
|
|
|
// efficiently in most cases.
|
|
|
|
|
|
|
|
template<typename TYPE> inline
|
|
|
|
void move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
|
|
|
|
ReferenceMover::move_references(d, s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TYPE> inline
|
|
|
|
void move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
|
|
|
|
ReferenceMover::move_references(d, s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TYPE> inline
|
|
|
|
void move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
|
|
|
|
ReferenceMover::move_references(d, s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename TYPE> inline
|
|
|
|
void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
|
|
|
|
ReferenceMover::move_references(d, s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-04 03:31:44 +00:00
|
|
|
}; // namespace android
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif // ANDROID_REF_BASE_H
|