Merge "Fix warnings in libutils headers"

This commit is contained in:
Treehugger Robot 2016-09-22 21:24:46 +00:00 committed by Gerrit Code Review
commit 65a1633ec3
14 changed files with 115 additions and 85 deletions

View file

@ -45,13 +45,8 @@ static inline ssize_t pwrite64(int fd, const void* buf, size_t nbytes, off64_t o
#define DEFFILEMODE 0666 #define DEFFILEMODE 0666
#endif /* _WIN32 */ #endif /* _WIN32 */
#if defined(_WIN32)
#define ZD "%ld"
#define ZD_TYPE long
#else
#define ZD "%zd" #define ZD "%zd"
#define ZD_TYPE ssize_t #define ZD_TYPE ssize_t
#endif
/* /*
* Needed for cases where something should be constexpr if possible, but not * Needed for cases where something should be constexpr if possible, but not

View file

@ -17,6 +17,7 @@
#ifndef _LIBS_UTILS_CONDITION_H #ifndef _LIBS_UTILS_CONDITION_H
#define _LIBS_UTILS_CONDITION_H #define _LIBS_UTILS_CONDITION_H
#include <limits.h>
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h> #include <time.h>
@ -120,7 +121,7 @@ inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {
// On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit. // On 32-bit devices, tv_sec is 32-bit, but `reltime` is 64-bit.
int64_t reltime_sec = reltime/1000000000; int64_t reltime_sec = reltime/1000000000;
ts.tv_nsec += reltime%1000000000; ts.tv_nsec += static_cast<long>(reltime%1000000000);
if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) { if (reltime_sec < INT64_MAX && ts.tv_nsec >= 1000000000) {
ts.tv_nsec -= 1000000000; ts.tv_nsec -= 1000000000;
++reltime_sec; ++reltime_sec;
@ -133,11 +134,7 @@ inline status_t Condition::waitRelative(Mutex& mutex, nsecs_t reltime) {
time_sec += reltime_sec; time_sec += reltime_sec;
} }
#if defined(__LP64__) ts.tv_sec = (time_sec > LONG_MAX) ? LONG_MAX : static_cast<long>(time_sec);
ts.tv_sec = time_sec;
#else
ts.tv_sec = (time_sec > INT32_MAX) ? INT32_MAX : time_sec;
#endif
return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts); return -pthread_cond_timedwait(&mCond, &mutex.mMutex, &ts);
} }

View file

@ -181,7 +181,7 @@ template<typename KEY, typename VALUE> inline
ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) { ssize_t KeyedVector<KEY,VALUE>::replaceValueAt(size_t index, const VALUE& item) {
if (index<size()) { if (index<size()) {
mVector.editItemAt(index).value = item; mVector.editItemAt(index).value = item;
return index; return static_cast<ssize_t>(index);
} }
return BAD_INDEX; return BAD_INDEX;
} }

View file

@ -49,7 +49,7 @@ typedef int (*Looper_callbackFunc)(int fd, int events, void* data);
*/ */
struct Message { struct Message {
Message() : what(0) { } Message() : what(0) { }
Message(int what) : what(what) { } Message(int w) : what(w) { }
/* The message type. (interpretation is left up to the handler) */ /* The message type. (interpretation is left up to the handler) */
int what; int what;
@ -66,7 +66,7 @@ struct Message {
*/ */
class MessageHandler : public virtual RefBase { class MessageHandler : public virtual RefBase {
protected: protected:
virtual ~MessageHandler() { } virtual ~MessageHandler();
public: public:
/** /**
@ -97,7 +97,7 @@ private:
*/ */
class LooperCallback : public virtual RefBase { class LooperCallback : public virtual RefBase {
protected: protected:
virtual ~LooperCallback() { } virtual ~LooperCallback();
public: public:
/** /**
@ -436,8 +436,8 @@ private:
struct MessageEnvelope { struct MessageEnvelope {
MessageEnvelope() : uptime(0) { } MessageEnvelope() : uptime(0) { }
MessageEnvelope(nsecs_t uptime, const sp<MessageHandler> handler, MessageEnvelope(nsecs_t u, const sp<MessageHandler> h,
const Message& message) : uptime(uptime), handler(handler), message(message) { const Message& m) : uptime(u), handler(h), message(m) {
} }
nsecs_t uptime; nsecs_t uptime;

View file

@ -166,7 +166,7 @@ LruCache<TKey, TValue>::LruCache(uint32_t maxCapacity)
, mOldest(NULL) , mOldest(NULL)
, mYoungest(NULL) , mYoungest(NULL)
, mMaxCapacity(maxCapacity) , mMaxCapacity(maxCapacity)
, mNullValue(NULL) { , mNullValue(0) {
mSet->max_load_factor(1.0); mSet->max_load_factor(1.0);
}; };

View file

@ -212,7 +212,7 @@ protected:
// subclasses; we have to make it protected to guarantee that it // subclasses; we have to make it protected to guarantee that it
// cannot be called from this base class (and to make strict compilers // cannot be called from this base class (and to make strict compilers
// happy). // happy).
~ReferenceRenamer() { } ~ReferenceRenamer();
public: public:
virtual void operator()(size_t i) const = 0; virtual void operator()(size_t i) const = 0;
}; };
@ -372,7 +372,7 @@ private:
// destructor to eliminate the template requirement of LightRefBase // destructor to eliminate the template requirement of LightRefBase
class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> { class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> {
public: public:
virtual ~VirtualLightRefBase() {} virtual ~VirtualLightRefBase();
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -646,42 +646,42 @@ public:
// a template<typename TYPE inherits RefBase> template... // a template<typename TYPE inherits RefBase> template...
template<typename TYPE> static inline template<typename TYPE> static inline
void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) { void move_references(sp<TYPE>* dest, sp<TYPE> const* src, size_t n) {
class Renamer : public ReferenceRenamer { class Renamer : public ReferenceRenamer {
sp<TYPE>* d; sp<TYPE>* d_;
sp<TYPE> const* s; sp<TYPE> const* s_;
virtual void operator()(size_t i) const { virtual void operator()(size_t i) const {
// The id are known to be the sp<>'s this pointer // The id are known to be the sp<>'s this pointer
TYPE::renameRefId(d[i].get(), &s[i], &d[i]); TYPE::renameRefId(d_[i].get(), &s_[i], &d_[i]);
} }
public: public:
Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d(d), s(s) { } Renamer(sp<TYPE>* d, sp<TYPE> const* s) : d_(d), s_(s) { }
virtual ~Renamer() { } virtual ~Renamer() { }
}; };
memmove(d, s, n*sizeof(sp<TYPE>)); memmove(dest, src, n*sizeof(sp<TYPE>));
TYPE::renameRefs(n, Renamer(d, s)); TYPE::renameRefs(n, Renamer(dest, src));
} }
template<typename TYPE> static inline template<typename TYPE> static inline
void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) { void move_references(wp<TYPE>* dest, wp<TYPE> const* src, size_t n) {
class Renamer : public ReferenceRenamer { class Renamer : public ReferenceRenamer {
wp<TYPE>* d; wp<TYPE>* d_;
wp<TYPE> const* s; wp<TYPE> const* s_;
virtual void operator()(size_t i) const { virtual void operator()(size_t i) const {
// The id are known to be the wp<>'s this pointer // The id are known to be the wp<>'s this pointer
TYPE::renameRefId(d[i].get_refs(), &s[i], &d[i]); TYPE::renameRefId(d_[i].get_refs(), &s_[i], &d_[i]);
} }
public: public:
Renamer(wp<TYPE>* d, wp<TYPE> const* s) : d(d), s(s) { } Renamer(wp<TYPE>* rd, wp<TYPE> const* rs) : d_(rd), s_(rs) { }
virtual ~Renamer() { } virtual ~Renamer() { }
}; };
memmove(d, s, n*sizeof(wp<TYPE>)); memmove(dest, src, n*sizeof(wp<TYPE>));
TYPE::renameRefs(n, Renamer(d, s)); TYPE::renameRefs(n, Renamer(dest, src));
} }
}; };
@ -712,7 +712,6 @@ void move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
ReferenceMover::move_references(d, s, n); ReferenceMover::move_references(d, s, n);
} }
}; // namespace android }; // namespace android
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View file

@ -19,6 +19,7 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <utils/Mutex.h>
#include <utils/threads.h> #include <utils/threads.h>
#include <cutils/compiler.h> #include <cutils/compiler.h>
@ -45,8 +46,8 @@ public:
} }
protected: protected:
~Singleton() { }; ~Singleton() { }
Singleton() { }; Singleton() { }
private: private:
Singleton(const Singleton&); Singleton(const Singleton&);
@ -55,6 +56,12 @@ private:
static TYPE* sInstance; static TYPE* sInstance;
}; };
template <typename TYPE>
Mutex Singleton<TYPE>::sLock;
template <typename TYPE>
TYPE* Singleton<TYPE>::sInstance;
/* /*
* use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file * use ANDROID_SINGLETON_STATIC_INSTANCE(TYPE) in your implementation file
* (eg: <TYPE>.cpp) to create the static instance of Singleton<>'s attributes, * (eg: <TYPE>.cpp) to create the static instance of Singleton<>'s attributes,

View file

@ -137,7 +137,7 @@ template<typename T> template<typename U>
sp<T>::sp(U* other) sp<T>::sp(U* other)
: m_ptr(other) { : m_ptr(other) {
if (other) if (other)
((T*) other)->incStrong(this); (static_cast<T*>(other))->incStrong(this);
} }
template<typename T> template<typename U> template<typename T> template<typename U>
@ -212,7 +212,7 @@ sp<T>& sp<T>::operator =(sp<U>&& other) {
template<typename T> template<typename U> template<typename T> template<typename U>
sp<T>& sp<T>::operator =(U* other) { sp<T>& sp<T>::operator =(U* other) {
if (other) if (other)
((T*) other)->incStrong(this); (static_cast<T*>(other))->incStrong(this);
if (m_ptr) if (m_ptr)
m_ptr->decStrong(this); m_ptr->decStrong(this);
m_ptr = other; m_ptr = other;

View file

@ -18,6 +18,8 @@
#define ANDROID_TYPE_HELPERS_H #define ANDROID_TYPE_HELPERS_H
#include <new> #include <new>
#include <type_traits>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
@ -178,49 +180,61 @@ void splat_type(TYPE* where, const TYPE* what, size_t n) {
} }
} }
template<typename TYPE> inline template<typename TYPE>
void move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) { struct use_trivial_move : public std::integral_constant<bool,
if ((traits<TYPE>::has_trivial_dtor && traits<TYPE>::has_trivial_copy) (traits<TYPE>::has_trivial_dtor && traits<TYPE>::has_trivial_copy)
|| traits<TYPE>::has_trivial_move) || traits<TYPE>::has_trivial_move
{ > {};
memmove(d,s,n*sizeof(TYPE));
} else { template<typename TYPE>
d += n; typename std::enable_if<use_trivial_move<TYPE>::value>::type
s += n; inline
while (n > 0) { move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
n--; memmove(d, s, n*sizeof(TYPE));
--d, --s; }
if (!traits<TYPE>::has_trivial_copy) {
new(d) TYPE(*s); template<typename TYPE>
} else { typename std::enable_if<!use_trivial_move<TYPE>::value>::type
*d = *s; inline
} move_forward_type(TYPE* d, const TYPE* s, size_t n = 1) {
if (!traits<TYPE>::has_trivial_dtor) { d += n;
s->~TYPE(); s += n;
} while (n > 0) {
n--;
--d, --s;
if (!traits<TYPE>::has_trivial_copy) {
new(d) TYPE(*s);
} else {
*d = *s;
}
if (!traits<TYPE>::has_trivial_dtor) {
s->~TYPE();
} }
} }
} }
template<typename TYPE> inline template<typename TYPE>
void move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) { typename std::enable_if<use_trivial_move<TYPE>::value>::type
if ((traits<TYPE>::has_trivial_dtor && traits<TYPE>::has_trivial_copy) inline
|| traits<TYPE>::has_trivial_move) move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
{ memmove(d, s, n*sizeof(TYPE));
memmove(d,s,n*sizeof(TYPE)); }
} else {
while (n > 0) { template<typename TYPE>
n--; typename std::enable_if<!use_trivial_move<TYPE>::value>::type
if (!traits<TYPE>::has_trivial_copy) { inline
new(d) TYPE(*s); move_backward_type(TYPE* d, const TYPE* s, size_t n = 1) {
} else { while (n > 0) {
*d = *s; n--;
} if (!traits<TYPE>::has_trivial_copy) {
if (!traits<TYPE>::has_trivial_dtor) { new(d) TYPE(*s);
s->~TYPE(); } else {
} *d = *s;
d++, s++;
} }
if (!traits<TYPE>::has_trivial_dtor) {
s->~TYPE();
}
d++, s++;
} }
} }
@ -239,6 +253,11 @@ struct key_value_pair_t {
VALUE value; VALUE value;
key_value_pair_t() { } key_value_pair_t() { }
key_value_pair_t(const key_value_pair_t& o) : key(o.key), value(o.value) { } key_value_pair_t(const key_value_pair_t& o) : key(o.key), value(o.value) { }
key_value_pair_t& operator=(const key_value_pair_t& o) {
key = o.key;
value = o.value;
return *this;
}
key_value_pair_t(const KEY& k, const VALUE& v) : key(k), value(v) { } key_value_pair_t(const KEY& k, const VALUE& v) : key(k), value(v) { }
explicit key_value_pair_t(const KEY& k) : key(k) { } explicit key_value_pair_t(const KEY& k) : key(k) { }
inline bool operator < (const key_value_pair_t& o) const { inline bool operator < (const key_value_pair_t& o) const {
@ -275,8 +294,7 @@ typedef uint32_t hash_t;
template <typename TKey> template <typename TKey>
hash_t hash_type(const TKey& key); hash_t hash_type(const TKey& key);
/* Built-in hash code specializations. /* Built-in hash code specializations */
* Assumes pointers are 32bit. */
#define ANDROID_INT32_HASH(T) \ #define ANDROID_INT32_HASH(T) \
template <> inline hash_t hash_type(const T& value) { return hash_t(value); } template <> inline hash_t hash_type(const T& value) { return hash_t(value); }
#define ANDROID_INT64_HASH(T) \ #define ANDROID_INT64_HASH(T) \
@ -284,7 +302,11 @@ hash_t hash_type(const TKey& key);
return hash_t((value >> 32) ^ value); } return hash_t((value >> 32) ^ value); }
#define ANDROID_REINTERPRET_HASH(T, R) \ #define ANDROID_REINTERPRET_HASH(T, R) \
template <> inline hash_t hash_type(const T& value) { \ template <> inline hash_t hash_type(const T& value) { \
return hash_type(*reinterpret_cast<const R*>(&value)); } R newValue; \
static_assert(sizeof(newValue) == sizeof(value), "size mismatch"); \
memcpy(&newValue, &value, sizeof(newValue)); \
return hash_type(newValue); \
}
ANDROID_INT32_HASH(bool) ANDROID_INT32_HASH(bool)
ANDROID_INT32_HASH(int8_t) ANDROID_INT32_HASH(int8_t)

View file

@ -60,6 +60,7 @@ ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len);
* Returns the size actually used for storing the string. * Returns the size actually used for storing the string.
* dst" is not nul-terminated when dst_len is fully used (like strncpy). * dst" is not nul-terminated when dst_len is fully used (like strncpy).
* *
* \code
* Example 1 * Example 1
* "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84) * "src" == \u3042\u3044 (\xE3\x81\x82\xE3\x81\x84)
* "src_len" == 2 * "src_len" == 2
@ -87,6 +88,7 @@ ssize_t utf32_to_utf8_length(const char32_t *src, size_t src_len);
* Returned value == 6 * Returned value == 6
* "dst" becomes \xE3\x81\x82\xE3\x81\x84 * "dst" becomes \xE3\x81\x82\xE3\x81\x84
* (note that "dst" is NOT nul-terminated, like strncpy) * (note that "dst" is NOT nul-terminated, like strncpy)
* \endcode
*/ */
void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len); void utf32_to_utf8(const char32_t* src, size_t src_len, char* dst, size_t dst_len);

View file

@ -371,12 +371,12 @@ ssize_t Vector<TYPE>::removeItemsAt(size_t index, size_t count) {
template<class TYPE> inline template<class TYPE> inline
status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) { status_t Vector<TYPE>::sort(Vector<TYPE>::compar_t cmp) {
return VectorImpl::sort((VectorImpl::compar_t)cmp); return VectorImpl::sort(reinterpret_cast<VectorImpl::compar_t>(cmp));
} }
template<class TYPE> inline template<class TYPE> inline
status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) { status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) {
return VectorImpl::sort((VectorImpl::compar_r_t)cmp, state); return VectorImpl::sort(reinterpret_cast<VectorImpl::compar_r_t>(cmp), state);
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------

View file

@ -98,7 +98,7 @@ FileMap::~FileMap(void)
} }
#if defined(__MINGW32__) #if defined(__MINGW32__)
if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) { if (mBasePtr && UnmapViewOfFile(mBasePtr) == 0) {
ALOGD("UnmapViewOfFile(%p) failed, error = %" PRId32 "\n", mBasePtr, ALOGD("UnmapViewOfFile(%p) failed, error = %lu\n", mBasePtr,
GetLastError() ); GetLastError() );
} }
if (mFileMapping != INVALID_HANDLE_VALUE) { if (mFileMapping != INVALID_HANDLE_VALUE) {
@ -138,7 +138,7 @@ bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t le
mFileHandle = (HANDLE) _get_osfhandle(fd); mFileHandle = (HANDLE) _get_osfhandle(fd);
mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL); mFileMapping = CreateFileMapping( mFileHandle, NULL, protect, 0, 0, NULL);
if (mFileMapping == NULL) { if (mFileMapping == NULL) {
ALOGE("CreateFileMapping(%p, %" PRIx32 ") failed with error %" PRId32 "\n", ALOGE("CreateFileMapping(%p, %lx) failed with error %lu\n",
mFileHandle, protect, GetLastError() ); mFileHandle, protect, GetLastError() );
return false; return false;
} }
@ -153,7 +153,7 @@ bool FileMap::create(const char* origFileName, int fd, off64_t offset, size_t le
(DWORD)(adjOffset), (DWORD)(adjOffset),
adjLength ); adjLength );
if (mBasePtr == NULL) { if (mBasePtr == NULL) {
ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %" PRId32 "\n", ALOGE("MapViewOfFile(%" PRId64 ", %zu) failed with error %lu\n",
adjOffset, adjLength, GetLastError() ); adjOffset, adjLength, GetLastError() );
CloseHandle(mFileMapping); CloseHandle(mFileMapping);
mFileMapping = INVALID_HANDLE_VALUE; mFileMapping = INVALID_HANDLE_VALUE;

View file

@ -677,4 +677,8 @@ void Looper::Request::initEventItem(struct epoll_event* eventItem) const {
eventItem->data.fd = fd; eventItem->data.fd = fd;
} }
MessageHandler::~MessageHandler() { }
LooperCallback::~LooperCallback() { }
} // namespace android } // namespace android

View file

@ -770,4 +770,8 @@ void RefBase::renameRefId(RefBase* ref,
ref->mRefs->renameWeakRefId(old_id, new_id); ref->mRefs->renameWeakRefId(old_id, new_id);
} }
ReferenceRenamer::~ReferenceRenamer() {}
VirtualLightRefBase::~VirtualLightRefBase() {}
}; // namespace android }; // namespace android