Go to the documentation of this file.00001 #if !defined(HFILE_libutils) && ANDROID_VER>=400
00002 #define HFILE_libutils
00003
00004 #include "libinline.h"
00005 #include "libcutils.h"
00006
00007 typedef uint16_t char16_t;
00008
00009 extern "C" {
00010 int strzcmp16(const char16_t *s1, size_t n1, const char16_t *s2, size_t n2);
00011 enum {
00012 SYSTEM_TIME_REALTIME = 0,
00013 SYSTEM_TIME_MONOTONIC = 1,
00014 SYSTEM_TIME_PROCESS = 2,
00015 SYSTEM_TIME_THREAD = 3,
00016 SYSTEM_TIME_BOOTTIME = 4
00017 };
00018 #ifdef __cplusplus
00019 int64_t systemTime(int clock = SYSTEM_TIME_MONOTONIC);
00020 #else
00021 int64_t systemTime(int clock);
00022 #endif // def __cplusplus
00023 }
00024
00025 namespace android {
00026
00027 class RefBase {
00028 public:
00029 void incStrong(const void* id) const;
00030 void decStrong(const void* id) const;
00031 class weakref_type {
00032 public:
00033 void decWeak(const void* id);
00034 bool attemptIncStrong(const void* id);
00035 };
00036 weakref_type* createWeak(const void* id) const;
00037 protected:
00038 RefBase();
00039 virtual ~RefBase();
00040 virtual void onFirstRef();
00041 virtual void onLastStrongRef(const void* id);
00042 virtual bool onIncStrongAttempted(uint32_t flags, const void* id);
00043 virtual void onLastWeakRef(const void* id);
00044 private:
00045 void* const mRefs;
00046 };
00047
00048 template <class T> class LightRefBase {
00049 public:
00050 inline LightRefBase() : mCount(0) {}
00051 inline void incStrong(const void* id) const {
00052 android_atomic_inc(&mCount);
00053 }
00054 inline void decStrong(const void* id) const {
00055 if (android_atomic_dec(&mCount) == 1) {
00056 delete static_cast<const T*>(this);
00057 }
00058 }
00059 protected:
00060 inline ~LightRefBase() {}
00061 private:
00062 mutable volatile int32_t mCount;
00063 };
00064
00065 template <typename T> class wp {
00066 public:
00067 inline wp() { m_ptr = 0; m_refs = 0; }
00068 inline wp(T* other) { m_ptr = other; if (other) m_refs = other->createWeak(this); }
00069 inline ~wp() { if (m_ptr) m_refs->decWeak(this); }
00070 inline wp<T>& operator=(T* other) { RefBase::weakref_type* newRefs = other ? other->createWeak(this) : 0; if (m_ptr) m_refs->decWeak(this); m_ptr = other; m_refs = newRefs; return *this; }
00071 inline sp<T> promote() const { sp<T> result; if (m_ptr && m_refs->attemptIncStrong(&result)) result.m_ptr = m_ptr; return result; }
00072 private:
00073 T* m_ptr;
00074 RefBase::weakref_type* m_refs;
00075 };
00076
00077 class SharedBuffer {
00078 public:
00079 static inline size_t sizeFromData(const void* data) { return data ? (static_cast<const SharedBuffer *>(data)-1)->mSize : 0; }
00080 private:
00081 int32_t mRefs;
00082 size_t mSize;
00083 uint32_t mReserved[2];
00084 };
00085
00086 class String8 {
00087 public:
00088 String8();
00089 String8(const char *s);
00090 ~String8();
00091 inline const char* string() const { return mString; }
00092 inline operator const char*() const { return mString; }
00093 inline size_t size() const { return SharedBuffer::sizeFromData(mString) - 1; }
00094 private:
00095 const char* mString;
00096 };
00097
00098 class String16 {
00099 public:
00100 String16();
00101 String16(const char16_t *s);
00102 String16(const char *s);
00103 ~String16();
00104 inline const char16_t* string() const { return mString; }
00105 inline operator const char16_t*() const { return mString; }
00106 inline size_t size() const { return SharedBuffer::sizeFromData(mString)/sizeof(char16_t) - 1; }
00107 inline bool operator==(const String16& other) const { return strzcmp16(mString, size(), other.mString, other.size()) == 0; }
00108 private:
00109 const char16_t* mString;
00110 };
00111
00112 #if (ANDROID_VER<440)
00113 class Flattenable {
00114 public:
00115 virtual size_t getFlattenedSize() const = 0;
00116 virtual size_t getFdCount() const = 0;
00117 virtual status_t flatten(void* buffer, size_t size, int fds[], size_t count) const = 0;
00118 virtual status_t unflatten(void const* buffer, size_t size, int fds[], size_t count) = 0;
00119 protected:
00120 virtual ~Flattenable() = 0;
00121 };
00122 #endif
00123
00124 }
00125
00126 #endif //end of lib