diff options
Diffstat (limited to 'include/utils')
-rw-r--r-- | include/utils/Compat.h | 23 | ||||
-rw-r--r-- | include/utils/Flattenable.h | 72 | ||||
-rw-r--r-- | include/utils/KeyedVector.h | 26 | ||||
-rw-r--r-- | include/utils/LinearAllocator.h | 42 | ||||
-rw-r--r-- | include/utils/Log.h | 38 | ||||
-rw-r--r-- | include/utils/SharedBuffer.h | 27 | ||||
-rw-r--r-- | include/utils/SortedVector.h | 22 | ||||
-rw-r--r-- | include/utils/SystemClock.h | 1 | ||||
-rw-r--r-- | include/utils/Timers.h | 5 | ||||
-rw-r--r-- | include/utils/Trace.h | 10 | ||||
-rw-r--r-- | include/utils/Vector.h | 29 | ||||
-rw-r--r-- | include/utils/misc.h | 40 |
12 files changed, 239 insertions, 96 deletions
diff --git a/include/utils/Compat.h b/include/utils/Compat.h index 1819266..fb7748e 100644 --- a/include/utils/Compat.h +++ b/include/utils/Compat.h @@ -39,4 +39,27 @@ static inline ssize_t pread64(int fd, void* buf, size_t nbytes, off64_t offset) #endif /* !HAVE_OFF64_T */ +#if HAVE_PRINTF_ZD +# define ZD "%zd" +# define ZD_TYPE ssize_t +#else +# define ZD "%ld" +# define ZD_TYPE long +#endif + +/* + * TEMP_FAILURE_RETRY is defined by some, but not all, versions of + * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's + * not already defined, then define it here. + */ +#ifndef TEMP_FAILURE_RETRY +/* Used to retry syscalls that can return EINTR. */ +#define TEMP_FAILURE_RETRY(exp) ({ \ + typeof (exp) _rc; \ + do { \ + _rc = (exp); \ + } while (_rc == -1 && errno == EINTR); \ + _rc; }) +#endif + #endif /* __LIB_UTILS_COMPAT_H */ diff --git a/include/utils/Flattenable.h b/include/utils/Flattenable.h index 852be3b..e40d289 100644 --- a/include/utils/Flattenable.h +++ b/include/utils/Flattenable.h @@ -24,6 +24,11 @@ namespace android { +/* + * The Flattenable interface allows an object to serialize itself out + * to a byte-buffer and an array of file descriptors. + */ + class Flattenable { public: @@ -56,6 +61,73 @@ protected: }; +/* + * LightFlattenable is a protocol allowing object to serialize themselves out + * to a byte-buffer. + * + * LightFlattenable objects must implement this protocol. + * + * LightFlattenable doesn't require the object to be virtual. + */ +template <typename T> +class LightFlattenable { +public: + // returns whether this object always flatten into the same size. + // for efficiency, this should always be inline. + inline bool isFixedSize() const; + + // returns size in bytes of the flattened object. must be a constant. + inline size_t getSize() const; + + // flattens the object into buffer. + inline status_t flatten(void* buffer) const; + + // unflattens the object from buffer of given size. + inline status_t unflatten(void const* buffer, size_t size); +}; + +template <typename T> +inline bool LightFlattenable<T>::isFixedSize() const { + return static_cast<T const*>(this)->T::isFixedSize(); +} +template <typename T> +inline size_t LightFlattenable<T>::getSize() const { + return static_cast<T const*>(this)->T::getSize(); +} +template <typename T> +inline status_t LightFlattenable<T>::flatten(void* buffer) const { + return static_cast<T const*>(this)->T::flatten(buffer); +} +template <typename T> +inline status_t LightFlattenable<T>::unflatten(void const* buffer, size_t size) { + return static_cast<T*>(this)->T::unflatten(buffer, size); +} + +/* + * LightFlattenablePod is an implementation of the LightFlattenable protocol + * for POD (plain-old-data) objects. + */ +template <typename T> +class LightFlattenablePod : public LightFlattenable<T> { +public: + inline bool isFixedSize() const { + return true; + } + + inline size_t getSize() const { + return sizeof(T); + } + inline status_t flatten(void* buffer) const { + *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this); + return NO_ERROR; + } + inline status_t unflatten(void const* buffer, size_t) { + *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer); + return NO_ERROR; + } +}; + + }; // namespace android diff --git a/include/utils/KeyedVector.h b/include/utils/KeyedVector.h index 20575ee..c4faae0 100644 --- a/include/utils/KeyedVector.h +++ b/include/utils/KeyedVector.h @@ -21,6 +21,8 @@ #include <stdint.h> #include <sys/types.h> +#include <cutils/log.h> + #include <utils/SortedVector.h> #include <utils/TypeHelpers.h> #include <utils/Errors.h> @@ -50,13 +52,16 @@ public: //! returns number of items in the vector inline size_t size() const { return mVector.size(); } - //! returns wether or not the vector is empty + //! returns whether or not the vector is empty inline bool isEmpty() const { return mVector.isEmpty(); } //! returns how many items can be stored without reallocating the backing store inline size_t capacity() const { return mVector.capacity(); } - //! setst the capacity. capacity can never be reduced less than size() + //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return mVector.setCapacity(size); } - + + // returns true if the arguments is known to be identical to this vector + inline bool isIdenticalTo(const KeyedVector& rhs) const; + /*! * accessors */ @@ -64,6 +69,7 @@ public: const VALUE& valueAt(size_t index) const; const KEY& keyAt(size_t index) const; ssize_t indexOfKey(const KEY& key) const; + const VALUE& operator[] (size_t index) const; /*! * modifying the array @@ -123,6 +129,11 @@ KeyedVector<KEY,VALUE>::KeyedVector() } template<typename KEY, typename VALUE> inline +bool KeyedVector<KEY,VALUE>::isIdenticalTo(const KeyedVector<KEY,VALUE>& rhs) const { + return mVector.array() == rhs.mVector.array(); +} + +template<typename KEY, typename VALUE> inline ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const { return mVector.indexOf( key_value_pair_t<KEY,VALUE>(key) ); } @@ -130,7 +141,7 @@ ssize_t KeyedVector<KEY,VALUE>::indexOfKey(const KEY& key) const { template<typename KEY, typename VALUE> inline const VALUE& KeyedVector<KEY,VALUE>::valueFor(const KEY& key) const { ssize_t i = this->indexOfKey(key); - assert(i>=0); + LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__); return mVector.itemAt(i).value; } @@ -140,6 +151,11 @@ const VALUE& KeyedVector<KEY,VALUE>::valueAt(size_t index) const { } template<typename KEY, typename VALUE> inline +const VALUE& KeyedVector<KEY,VALUE>::operator[] (size_t index) const { + return valueAt(index); +} + +template<typename KEY, typename VALUE> inline const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const { return mVector.itemAt(index).key; } @@ -147,7 +163,7 @@ const KEY& KeyedVector<KEY,VALUE>::keyAt(size_t index) const { template<typename KEY, typename VALUE> inline VALUE& KeyedVector<KEY,VALUE>::editValueFor(const KEY& key) { ssize_t i = this->indexOfKey(key); - assert(i>=0); + LOG_ALWAYS_FATAL_IF(i<0, "%s: key not found", __PRETTY_FUNCTION__); return mVector.editItemAt(i).value; } diff --git a/include/utils/LinearAllocator.h b/include/utils/LinearAllocator.h new file mode 100644 index 0000000..cd2521d --- /dev/null +++ b/include/utils/LinearAllocator.h @@ -0,0 +1,42 @@ +/* + * Copyright 2012, The Android Open Source Project + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef ANDROID_LINEARALLOCATOR_H +#define ANDROID_LINEARALLOCATOR_H + +#include <stddef.h> + +namespace android { + +class LinearAllocator { +public: + void* alloc(size_t size) { return 0; } + void rewindIfLastAlloc(void* ptr, size_t allocSize) {} + void dumpMemoryStats(const char* prefix = "") {} +}; + +}; // namespace android + +#endif // ANDROID_LINEARALLOCATOR_H diff --git a/include/utils/Log.h b/include/utils/Log.h index 3c6cc8b..4259c86 100644 --- a/include/utils/Log.h +++ b/include/utils/Log.h @@ -29,5 +29,43 @@ #define _LIBS_UTILS_LOG_H #include <cutils/log.h> +#include <sys/types.h> + +#ifdef __cplusplus + +namespace android { + +/* + * A very simple utility that yells in the log when an operation takes too long. + */ +class LogIfSlow { +public: + LogIfSlow(const char* tag, android_LogPriority priority, + int timeoutMillis, const char* message); + ~LogIfSlow(); + +private: + const char* const mTag; + const android_LogPriority mPriority; + const int mTimeoutMillis; + const char* const mMessage; + const int64_t mStart; +}; + +/* + * Writes the specified debug log message if this block takes longer than the + * specified number of milliseconds to run. Includes the time actually taken. + * + * { + * ALOGD_IF_SLOW(50, "Excessive delay doing something."); + * doSomething(); + * } + */ +#define ALOGD_IF_SLOW(timeoutMillis, message) \ + android::LogIfSlow _logIfSlow(LOG_TAG, ANDROID_LOG_DEBUG, timeoutMillis, message); + +} // namespace android + +#endif // __cplusplus #endif // _LIBS_UTILS_LOG_H diff --git a/include/utils/SharedBuffer.h b/include/utils/SharedBuffer.h index 24508b0..b670953 100644 --- a/include/utils/SharedBuffer.h +++ b/include/utils/SharedBuffer.h @@ -44,9 +44,6 @@ public: * users. */ static ssize_t dealloc(const SharedBuffer* released); - - //! get the SharedBuffer from the data pointer - static inline const SharedBuffer* sharedBuffer(const void* data); //! access the data for read inline const void* data() const; @@ -94,9 +91,10 @@ public: private: inline SharedBuffer() { } inline ~SharedBuffer() { } - inline SharedBuffer(const SharedBuffer&); + SharedBuffer(const SharedBuffer&); + SharedBuffer& operator = (const SharedBuffer&); - // 16 bytes. must be sized to preserve correct alingment. + // 16 bytes. must be sized to preserve correct alignment. mutable int32_t mRefs; size_t mSize; uint32_t mReserved[2]; @@ -104,10 +102,6 @@ private: // --------------------------------------------------------------------------- -const SharedBuffer* SharedBuffer::sharedBuffer(const void* data) { - return data ? reinterpret_cast<const SharedBuffer *>(data)-1 : 0; -} - const void* SharedBuffer::data() const { return this + 1; } @@ -120,19 +114,16 @@ size_t SharedBuffer::size() const { return mSize; } -SharedBuffer* SharedBuffer::bufferFromData(void* data) -{ - return ((SharedBuffer*)data)-1; +SharedBuffer* SharedBuffer::bufferFromData(void* data) { + return data ? static_cast<SharedBuffer *>(data)-1 : 0; } -const SharedBuffer* SharedBuffer::bufferFromData(const void* data) -{ - return ((const SharedBuffer*)data)-1; +const SharedBuffer* SharedBuffer::bufferFromData(const void* data) { + return data ? static_cast<const SharedBuffer *>(data)-1 : 0; } -size_t SharedBuffer::sizeFromData(const void* data) -{ - return (((const SharedBuffer*)data)-1)->mSize; +size_t SharedBuffer::sizeFromData(const void* data) { + return data ? bufferFromData(data)->mSize : 0; } bool SharedBuffer::onlyOwner() const { diff --git a/include/utils/SortedVector.h b/include/utils/SortedVector.h index 2445525..2d3e82a 100644 --- a/include/utils/SortedVector.h +++ b/include/utils/SortedVector.h @@ -21,6 +21,8 @@ #include <stdint.h> #include <sys/types.h> +#include <cutils/log.h> + #include <utils/Vector.h> #include <utils/VectorImpl.h> #include <utils/TypeHelpers.h> @@ -61,11 +63,11 @@ public: //! returns number of items in the vector inline size_t size() const { return VectorImpl::size(); } - //! returns wether or not the vector is empty + //! returns whether or not the vector is empty inline bool isEmpty() const { return VectorImpl::isEmpty(); } //! returns how many items can be stored without reallocating the backing store inline size_t capacity() const { return VectorImpl::capacity(); } - //! setst the capacity. capacity can never be reduced less than size() + //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } /*! @@ -76,7 +78,7 @@ public: inline const TYPE* array() const; //! read-write C-style access. BE VERY CAREFUL when modifying the array - //! you ust keep it sorted! You usually don't use this function. + //! you must keep it sorted! You usually don't use this function. TYPE* editArray(); //! finds the index of an item @@ -96,11 +98,9 @@ public: inline const TYPE& itemAt(size_t index) const; //! stack-usage of the vector. returns the top of the stack (last element) const TYPE& top() const; - //! same as operator [], but allows to access the vector backward (from the end) with a negative index - const TYPE& mirrorItemAt(ssize_t index) const; /*! - * modifing the array + * modifying the array */ //! add an item in the right place (and replace the one that is there) @@ -186,7 +186,9 @@ TYPE* SortedVector<TYPE>::editArray() { template<class TYPE> inline const TYPE& SortedVector<TYPE>::operator[](size_t index) const { - assert( index<size() ); + LOG_FATAL_IF(index>=size(), + "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, + int(index), int(size())); return *(array() + index); } @@ -196,12 +198,6 @@ const TYPE& SortedVector<TYPE>::itemAt(size_t index) const { } template<class TYPE> inline -const TYPE& SortedVector<TYPE>::mirrorItemAt(ssize_t index) const { - assert( (index>0 ? index : -index)<size() ); - return *(array() + ((index<0) ? (size()-index) : index)); -} - -template<class TYPE> inline const TYPE& SortedVector<TYPE>::top() const { return *(array() + size() - 1); } diff --git a/include/utils/SystemClock.h b/include/utils/SystemClock.h index 7c319be..d75264c 100644 --- a/include/utils/SystemClock.h +++ b/include/utils/SystemClock.h @@ -25,6 +25,7 @@ namespace android { int setCurrentTimeMillis(int64_t millis); int64_t uptimeMillis(); int64_t elapsedRealtime(); +int64_t elapsedRealtimeNano(); }; // namespace android diff --git a/include/utils/Timers.h b/include/utils/Timers.h index 8b4d322..92f66c9 100644 --- a/include/utils/Timers.h +++ b/include/utils/Timers.h @@ -78,9 +78,10 @@ enum { SYSTEM_TIME_REALTIME = 0, // system-wide realtime clock SYSTEM_TIME_MONOTONIC = 1, // monotonic time since unspecified starting point SYSTEM_TIME_PROCESS = 2, // high-resolution per-process clock - SYSTEM_TIME_THREAD = 3 // high-resolution per-thread clock + SYSTEM_TIME_THREAD = 3, // high-resolution per-thread clock + SYSTEM_TIME_BOOTTIME = 4 // same as SYSTEM_TIME_MONOTONIC, but including CPU suspend time }; - + // return the system-time according to the specified clock #ifdef __cplusplus nsecs_t systemTime(int clock = SYSTEM_TIME_MONOTONIC); diff --git a/include/utils/Trace.h b/include/utils/Trace.h index 4219206..41bce00 100644 --- a/include/utils/Trace.h +++ b/include/utils/Trace.h @@ -51,7 +51,10 @@ #define ATRACE_TAG_SYNC_MANAGER (1<<7) #define ATRACE_TAG_AUDIO (1<<8) #define ATRACE_TAG_VIDEO (1<<9) -#define ATRACE_TAG_LAST ATRACE_TAG_VIDEO +#define ATRACE_TAG_CAMERA (1<<10) +#define ATRACE_TAG_LAST ATRACE_TAG_CAMERA + +#define ATRACE_TAG_NOT_READY (1LL<<63) // Reserved for use during init #define ATRACE_TAG_VALID_MASK ((ATRACE_TAG_LAST - 1) | ATRACE_TAG_LAST) @@ -66,6 +69,11 @@ // function body. #define ATRACE_CALL() android::ScopedTrace ___tracer(ATRACE_TAG, __FUNCTION__) +// ATRACE_NAME traces the beginning and end of the current function. To trace +// the correct start and end times this macro should be the first line of the +// function body. +#define ATRACE_NAME(name) android::ScopedTrace ___tracer(ATRACE_TAG, name) + // ATRACE_INT traces a named integer value. This can be used to track how the // value changes over time in a trace. #define ATRACE_INT(name, value) android::Tracer::traceCounter(ATRACE_TAG, name, value) diff --git a/include/utils/Vector.h b/include/utils/Vector.h index e39a5b7..f3020d6 100644 --- a/include/utils/Vector.h +++ b/include/utils/Vector.h @@ -21,7 +21,8 @@ #include <stdint.h> #include <sys/types.h> -#include <utils/Log.h> +#include <cutils/log.h> + #include <utils/VectorImpl.h> #include <utils/TypeHelpers.h> @@ -72,11 +73,11 @@ public: //! returns number of items in the vector inline size_t size() const { return VectorImpl::size(); } - //! returns wether or not the vector is empty + //! returns whether or not the vector is empty inline bool isEmpty() const { return VectorImpl::isEmpty(); } //! returns how many items can be stored without reallocating the backing store inline size_t capacity() const { return VectorImpl::capacity(); } - //! setst the capacity. capacity can never be reduced less than size() + //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } /*! @@ -98,16 +99,14 @@ public: inline const TYPE& itemAt(size_t index) const; //! stack-usage of the vector. returns the top of the stack (last element) const TYPE& top() const; - //! same as operator [], but allows to access the vector backward (from the end) with a negative index - const TYPE& mirrorItemAt(ssize_t index) const; /*! - * modifing the array + * modifying the array */ //! copy-on write support, grants write access to an item TYPE& editItemAt(size_t index); - //! grants right acces to the top of the stack (last element) + //! grants right access to the top of the stack (last element) TYPE& editTop(); /*! @@ -189,7 +188,8 @@ public: inline void push_back(const TYPE& item) { insertAt(item, size(), 1); } inline void push_front(const TYPE& item) { insertAt(item, 0, 1); } inline iterator erase(iterator pos) { - return begin() + removeItemsAt(pos-array()); + ssize_t index = removeItemsAt(pos-array()); + return begin() + index; } protected: @@ -271,8 +271,9 @@ TYPE* Vector<TYPE>::editArray() { template<class TYPE> inline const TYPE& Vector<TYPE>::operator[](size_t index) const { - LOG_FATAL_IF( index>=size(), - "itemAt: index %d is past size %d", (int)index, (int)size() ); + LOG_FATAL_IF(index>=size(), + "%s: index=%u out of range (%u)", __PRETTY_FUNCTION__, + int(index), int(size())); return *(array() + index); } @@ -282,14 +283,6 @@ const TYPE& Vector<TYPE>::itemAt(size_t index) const { } template<class TYPE> inline -const TYPE& Vector<TYPE>::mirrorItemAt(ssize_t index) const { - LOG_FATAL_IF( (index>0 ? index : -index)>=size(), - "mirrorItemAt: index %d is past size %d", - (int)index, (int)size() ); - return *(array() + ((index<0) ? (size()-index) : index)); -} - -template<class TYPE> inline const TYPE& Vector<TYPE>::top() const { return *(array() + size() - 1); } diff --git a/include/utils/misc.h b/include/utils/misc.h index d7d5bc1..f1aa432 100644 --- a/include/utils/misc.h +++ b/include/utils/misc.h @@ -23,41 +23,12 @@ #include <sys/time.h> #include <utils/Endian.h> -namespace android { - /* get #of elements in a static array */ #ifndef NELEM # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) #endif -/* - * Make a copy of the string, using "new[]" instead of "malloc". Free the - * string with delete[]. - * - * Returns NULL if "str" is NULL. - */ -char* strdupNew(const char* str); - -/* - * Concatenate an argument vector into a single string. If argc is >= 0 - * it will be used; if it's < 0 then the last element in the arg vector - * must be NULL. - * - * This inserts a space between each argument. - * - * This does not automatically add double quotes around arguments with - * spaces in them. This practice is necessary for Win32, because Win32's - * CreateProcess call is stupid. - * - * The caller should delete[] the returned string. - */ -char* concatArgv(int argc, const char* const argv[]); - -/* - * Count up the number of arguments in "argv". The count does not include - * the final NULL entry. - */ -int countArgv(const char* const argv[]); +namespace android { /* * Some utility functions for working with files. These could be made @@ -79,15 +50,6 @@ FileType getFileType(const char* fileName); /* get the file's modification date; returns -1 w/errno set on failure */ time_t getFileModDate(const char* fileName); -/* - * Round up to the nearest power of 2. Handy for hash tables. - */ -unsigned int roundUpPower2(unsigned int val); - -void strreverse(char* begin, char* end); -void k_itoa(int value, char* str, int base); -char* itoa(int val, int base); - typedef void (*sysprop_change_callback)(void); void add_sysprop_change_callback(sysprop_change_callback cb, int priority); void report_sysprop_change(); |