diff options
Diffstat (limited to 'libutils/include')
| -rw-r--r-- | libutils/include/utils/BlobCache.h | 249 | ||||
| -rw-r--r-- | libutils/include/utils/Flattenable.h | 4 | ||||
| -rw-r--r-- | libutils/include/utils/LightRefBase.h | 72 | ||||
| -rw-r--r-- | libutils/include/utils/LinearTransform.h | 64 | ||||
| -rw-r--r-- | libutils/include/utils/NativeHandle.h | 4 | ||||
| -rw-r--r-- | libutils/include/utils/RefBase.h | 77 | ||||
| -rw-r--r-- | libutils/include/utils/Singleton.h | 5 | ||||
| -rw-r--r-- | libutils/include/utils/SortedVector.h | 63 | ||||
| -rw-r--r-- | libutils/include/utils/StrongPointer.h | 6 | ||||
| -rw-r--r-- | libutils/include/utils/Trace.h | 26 | ||||
| -rw-r--r-- | libutils/include/utils/TypeHelpers.h | 10 | ||||
| -rw-r--r-- | libutils/include/utils/Vector.h | 60 |
12 files changed, 189 insertions, 451 deletions
diff --git a/libutils/include/utils/BlobCache.h b/libutils/include/utils/BlobCache.h deleted file mode 100644 index 65dca9fb4..000000000 --- a/libutils/include/utils/BlobCache.h +++ /dev/null @@ -1,249 +0,0 @@ -/* - ** Copyright 2011, 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_BLOB_CACHE_H -#define ANDROID_BLOB_CACHE_H - -#include <stddef.h> - -#include <utils/Flattenable.h> -#include <utils/RefBase.h> -#include <utils/SortedVector.h> -#include <utils/threads.h> - -namespace android { - -// A BlobCache is an in-memory cache for binary key/value pairs. A BlobCache -// does NOT provide any thread-safety guarantees. -// -// The cache contents can be serialized to an in-memory buffer or mmap'd file -// and then reloaded in a subsequent execution of the program. This -// serialization is non-portable and the data should only be used by the device -// that generated it. -class BlobCache : public RefBase { - -public: - - // Create an empty blob cache. The blob cache will cache key/value pairs - // with key and value sizes less than or equal to maxKeySize and - // maxValueSize, respectively. The total combined size of ALL cache entries - // (key sizes plus value sizes) will not exceed maxTotalSize. - BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize); - - // set inserts a new binary value into the cache and associates it with the - // given binary key. If the key or value are too large for the cache then - // the cache remains unchanged. This includes the case where a different - // value was previously associated with the given key - the old value will - // remain in the cache. If the given key and value are small enough to be - // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize - // values specified to the BlobCache constructor), then the key/value pair - // will be in the cache after set returns. Note, however, that a subsequent - // call to set may evict old key/value pairs from the cache. - // - // Preconditions: - // key != NULL - // 0 < keySize - // value != NULL - // 0 < valueSize - void set(const void* key, size_t keySize, const void* value, - size_t valueSize); - - // get retrieves from the cache the binary value associated with a given - // binary key. If the key is present in the cache then the length of the - // binary value associated with that key is returned. If the value argument - // is non-NULL and the size of the cached value is less than valueSize bytes - // then the cached value is copied into the buffer pointed to by the value - // argument. If the key is not present in the cache then 0 is returned and - // the buffer pointed to by the value argument is not modified. - // - // Note that when calling get multiple times with the same key, the later - // calls may fail, returning 0, even if earlier calls succeeded. The return - // value must be checked for each call. - // - // Preconditions: - // key != NULL - // 0 < keySize - // 0 <= valueSize - size_t get(const void* key, size_t keySize, void* value, size_t valueSize); - - - // getFlattenedSize returns the number of bytes needed to store the entire - // serialized cache. - size_t getFlattenedSize() const; - - // flatten serializes the current contents of the cache into the memory - // pointed to by 'buffer'. The serialized cache contents can later be - // loaded into a BlobCache object using the unflatten method. The contents - // of the BlobCache object will not be modified. - // - // Preconditions: - // size >= this.getFlattenedSize() - status_t flatten(void* buffer, size_t size) const; - - // unflatten replaces the contents of the cache with the serialized cache - // contents in the memory pointed to by 'buffer'. The previous contents of - // the BlobCache will be evicted from the cache. If an error occurs while - // unflattening the serialized cache contents then the BlobCache will be - // left in an empty state. - // - status_t unflatten(void const* buffer, size_t size); - -private: - // Copying is disallowed. - BlobCache(const BlobCache&); - void operator=(const BlobCache&); - - // A random function helper to get around MinGW not having nrand48() - long int blob_random(); - - // clean evicts a randomly chosen set of entries from the cache such that - // the total size of all remaining entries is less than mMaxTotalSize/2. - void clean(); - - // isCleanable returns true if the cache is full enough for the clean method - // to have some effect, and false otherwise. - bool isCleanable() const; - - // A Blob is an immutable sized unstructured data blob. - class Blob : public RefBase { - public: - Blob(const void* data, size_t size, bool copyData); - ~Blob(); - - bool operator<(const Blob& rhs) const; - - const void* getData() const; - size_t getSize() const; - - private: - // Copying is not allowed. - Blob(const Blob&); - void operator=(const Blob&); - - // mData points to the buffer containing the blob data. - const void* mData; - - // mSize is the size of the blob data in bytes. - size_t mSize; - - // mOwnsData indicates whether or not this Blob object should free the - // memory pointed to by mData when the Blob gets destructed. - bool mOwnsData; - }; - - // A CacheEntry is a single key/value pair in the cache. - class CacheEntry { - public: - CacheEntry(); - CacheEntry(const sp<Blob>& key, const sp<Blob>& value); - CacheEntry(const CacheEntry& ce); - - bool operator<(const CacheEntry& rhs) const; - const CacheEntry& operator=(const CacheEntry&); - - sp<Blob> getKey() const; - sp<Blob> getValue() const; - - void setValue(const sp<Blob>& value); - - private: - - // mKey is the key that identifies the cache entry. - sp<Blob> mKey; - - // mValue is the cached data associated with the key. - sp<Blob> mValue; - }; - - // A Header is the header for the entire BlobCache serialization format. No - // need to make this portable, so we simply write the struct out. - struct Header { - // mMagicNumber is the magic number that identifies the data as - // serialized BlobCache contents. It must always contain 'Blb$'. - uint32_t mMagicNumber; - - // mBlobCacheVersion is the serialization format version. - uint32_t mBlobCacheVersion; - - // mDeviceVersion is the device-specific version of the cache. This can - // be used to invalidate the cache. - uint32_t mDeviceVersion; - - // mNumEntries is number of cache entries following the header in the - // data. - size_t mNumEntries; - - // mBuildId is the build id of the device when the cache was created. - // When an update to the build happens (via an OTA or other update) this - // is used to invalidate the cache. - int mBuildIdLength; - char mBuildId[]; - }; - - // An EntryHeader is the header for a serialized cache entry. No need to - // make this portable, so we simply write the struct out. Each EntryHeader - // is followed imediately by the key data and then the value data. - // - // The beginning of each serialized EntryHeader is 4-byte aligned, so the - // number of bytes that a serialized cache entry will occupy is: - // - // ((sizeof(EntryHeader) + keySize + valueSize) + 3) & ~3 - // - struct EntryHeader { - // mKeySize is the size of the entry key in bytes. - size_t mKeySize; - - // mValueSize is the size of the entry value in bytes. - size_t mValueSize; - - // mData contains both the key and value data for the cache entry. The - // key comes first followed immediately by the value. - uint8_t mData[]; - }; - - // mMaxKeySize is the maximum key size that will be cached. Calls to - // BlobCache::set with a keySize parameter larger than mMaxKeySize will - // simply not add the key/value pair to the cache. - const size_t mMaxKeySize; - - // mMaxValueSize is the maximum value size that will be cached. Calls to - // BlobCache::set with a valueSize parameter larger than mMaxValueSize will - // simply not add the key/value pair to the cache. - const size_t mMaxValueSize; - - // mMaxTotalSize is the maximum size that all cache entries can occupy. This - // includes space for both keys and values. When a call to BlobCache::set - // would otherwise cause this limit to be exceeded, either the key/value - // pair passed to BlobCache::set will not be cached or other cache entries - // will be evicted from the cache to make room for the new entry. - const size_t mMaxTotalSize; - - // mTotalSize is the total combined size of all keys and values currently in - // the cache. - size_t mTotalSize; - - // mRandState is the pseudo-random number generator state. It is passed to - // nrand48 to generate random numbers when needed. - unsigned short mRandState[3]; - - // mCacheEntries stores all the cache entries that are resident in memory. - // Cache entries are added to it by the 'set' method. - SortedVector<CacheEntry> mCacheEntries; -}; - -} - -#endif // ANDROID_BLOB_CACHE_H diff --git a/libutils/include/utils/Flattenable.h b/libutils/include/utils/Flattenable.h index 22b811a14..070c71026 100644 --- a/libutils/include/utils/Flattenable.h +++ b/libutils/include/utils/Flattenable.h @@ -189,11 +189,11 @@ public: } inline status_t flatten(void* buffer, size_t size) const { if (size < sizeof(T)) return NO_MEMORY; - *reinterpret_cast<T*>(buffer) = *static_cast<T const*>(this); + memcpy(buffer, static_cast<T const*>(this), sizeof(T)); return NO_ERROR; } inline status_t unflatten(void const* buffer, size_t) { - *static_cast<T*>(this) = *reinterpret_cast<T const*>(buffer); + memcpy(static_cast<T*>(this), buffer, sizeof(T)); return NO_ERROR; } }; diff --git a/libutils/include/utils/LightRefBase.h b/libutils/include/utils/LightRefBase.h new file mode 100644 index 000000000..65257edb9 --- /dev/null +++ b/libutils/include/utils/LightRefBase.h @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017 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. + */ + +#pragma once + +/* + * See documentation in RefBase.h + */ + +#include <atomic> + +#include <sys/types.h> + +namespace android { + +class ReferenceRenamer; + +template <class T> +class LightRefBase +{ +public: + inline LightRefBase() : mCount(0) { } + inline void incStrong(__attribute__((unused)) const void* id) const { + mCount.fetch_add(1, std::memory_order_relaxed); + } + inline void decStrong(__attribute__((unused)) const void* id) const { + if (mCount.fetch_sub(1, std::memory_order_release) == 1) { + std::atomic_thread_fence(std::memory_order_acquire); + delete static_cast<const T*>(this); + } + } + //! DEBUGGING ONLY: Get current strong ref count. + inline int32_t getStrongCount() const { + return mCount.load(std::memory_order_relaxed); + } + + typedef LightRefBase<T> basetype; + +protected: + inline ~LightRefBase() { } + +private: + friend class ReferenceMover; + inline static void renameRefs(size_t /*n*/, const ReferenceRenamer& /*renamer*/) { } + inline static void renameRefId(T* /*ref*/, const void* /*old_id*/ , const void* /*new_id*/) { } + +private: + mutable std::atomic<int32_t> mCount; +}; + + +// This is a wrapper around LightRefBase that simply enforces a virtual +// destructor to eliminate the template requirement of LightRefBase +class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> { +public: + virtual ~VirtualLightRefBase() = default; +}; + +}; // namespace android diff --git a/libutils/include/utils/LinearTransform.h b/libutils/include/utils/LinearTransform.h deleted file mode 100644 index 04cb355c7..000000000 --- a/libutils/include/utils/LinearTransform.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (C) 2011 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 _LIBS_UTILS_LINEAR_TRANSFORM_H -#define _LIBS_UTILS_LINEAR_TRANSFORM_H - -#include <stdint.h> - -namespace android { - -// LinearTransform defines a structure which hold the definition of a -// transformation from single dimensional coordinate system A into coordinate -// system B (and back again). Values in A and in B are 64 bit, the linear -// scale factor is expressed as a rational number using two 32 bit values. -// -// Specifically, let -// f(a) = b -// F(b) = f^-1(b) = a -// then -// -// f(a) = (((a - a_zero) * a_to_b_numer) / a_to_b_denom) + b_zero; -// -// and -// -// F(b) = (((b - b_zero) * a_to_b_denom) / a_to_b_numer) + a_zero; -// -struct LinearTransform { - int64_t a_zero; - int64_t b_zero; - int32_t a_to_b_numer; - uint32_t a_to_b_denom; - - // Transform from A->B - // Returns true on success, or false in the case of a singularity or an - // overflow. - bool doForwardTransform(int64_t a_in, int64_t* b_out) const; - - // Transform from B->A - // Returns true on success, or false in the case of a singularity or an - // overflow. - bool doReverseTransform(int64_t b_in, int64_t* a_out) const; - - // Helpers which will reduce the fraction N/D using Euclid's method. - template <class T> static void reduce(T* N, T* D); - static void reduce(int32_t* N, uint32_t* D); -}; - - -} - -#endif // _LIBS_UTILS_LINEAR_TRANSFORM_H diff --git a/libutils/include/utils/NativeHandle.h b/libutils/include/utils/NativeHandle.h index b82516879..73fe804cc 100644 --- a/libutils/include/utils/NativeHandle.h +++ b/libutils/include/utils/NativeHandle.h @@ -24,7 +24,7 @@ typedef struct native_handle native_handle_t; namespace android { -class NativeHandle: public LightRefBase<NativeHandle> { +class NativeHandle : public LightRefBase<NativeHandle> { public: // Create a refcounted wrapper around a native_handle_t, and declare // whether the wrapper owns the handle (so that it should clean up the @@ -41,7 +41,7 @@ private: friend class LightRefBase<NativeHandle>; NativeHandle(native_handle_t* handle, bool ownsHandle); - virtual ~NativeHandle(); + ~NativeHandle(); native_handle_t* mHandle; bool mOwnsHandle; diff --git a/libutils/include/utils/RefBase.h b/libutils/include/utils/RefBase.h index 36016cde6..223b6669d 100644 --- a/libutils/include/utils/RefBase.h +++ b/libutils/include/utils/RefBase.h @@ -177,6 +177,9 @@ #include <stdlib.h> #include <string.h> +// LightRefBase used to be declared in this header, so we have to include it +#include <utils/LightRefBase.h> + #include <utils/StrongPointer.h> #include <utils/TypeHelpers.h> @@ -216,7 +219,7 @@ inline bool operator _op_ (const U* o) const { \ class ReferenceRenamer { protected: - // destructor is purposedly not virtual so we avoid code overhead from + // destructor is purposely 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). @@ -246,13 +249,13 @@ public: { public: RefBase* refBase() const; - + void incWeak(const void* id); void decWeak(const void* id); - + // acquires a strong reference if there is already one. bool attemptIncStrong(const void* id); - + // acquires a weak reference if there is already one. // This is not always safe. see ProcessState.cpp and BpBinder.cpp // for proper use. @@ -268,12 +271,12 @@ public: // 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 + // 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; @@ -345,56 +348,12 @@ private: // --------------------------------------------------------------------------- -template <class T> -class LightRefBase -{ -public: - inline LightRefBase() : mCount(0) { } - inline void incStrong(__attribute__((unused)) const void* id) const { - mCount.fetch_add(1, std::memory_order_relaxed); - } - inline void decStrong(__attribute__((unused)) const void* id) const { - if (mCount.fetch_sub(1, std::memory_order_release) == 1) { - std::atomic_thread_fence(std::memory_order_acquire); - delete static_cast<const T*>(this); - } - } - //! DEBUGGING ONLY: Get current strong ref count. - inline int32_t getStrongCount() const { - return mCount.load(std::memory_order_relaxed); - } - - typedef LightRefBase<T> basetype; - -protected: - inline ~LightRefBase() { } - -private: - friend class ReferenceMover; - inline static void renameRefs(size_t /*n*/, - const ReferenceRenamer& /*renamer*/) { } - inline static void renameRefId(T* /*ref*/, - const void* /*old_id*/ , const void* /*new_id*/) { } - -private: - mutable std::atomic<int32_t> mCount; -}; - -// This is a wrapper around LightRefBase that simply enforces a virtual -// destructor to eliminate the template requirement of LightRefBase -class VirtualLightRefBase : public LightRefBase<VirtualLightRefBase> { -public: - virtual ~VirtualLightRefBase(); -}; - -// --------------------------------------------------------------------------- - template <typename T> class wp { public: typedef typename RefBase::weakref_type weakref_type; - + inline wp() : m_ptr(0) { } wp(T* other); // NOLINT(implicit) @@ -405,31 +364,31 @@ public: template<typename U> wp(const wp<U>& other); // NOLINT(implicit) ~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 diff --git a/libutils/include/utils/Singleton.h b/libutils/include/utils/Singleton.h index abb72f510..9afedd4a0 100644 --- a/libutils/include/utils/Singleton.h +++ b/libutils/include/utils/Singleton.h @@ -18,9 +18,12 @@ #define ANDROID_UTILS_SINGLETON_H #include <stdint.h> + +// some vendor code assumes they have atoi() after including this file. +#include <stdlib.h> + #include <sys/types.h> #include <utils/Mutex.h> -#include <utils/threads.h> #include <cutils/compiler.h> namespace android { diff --git a/libutils/include/utils/SortedVector.h b/libutils/include/utils/SortedVector.h index 86f349645..5b2a23200 100644 --- a/libutils/include/utils/SortedVector.h +++ b/libutils/include/utils/SortedVector.h @@ -37,18 +37,18 @@ class SortedVector : private SortedVectorImpl public: typedef TYPE value_type; - - /*! + + /*! * Constructors and destructors */ - + SortedVector(); SortedVector(const SortedVector<TYPE>& rhs); virtual ~SortedVector(); /*! copy operator */ - const SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; - SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs); + const SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; + SortedVector<TYPE>& operator = (const SortedVector<TYPE>& rhs); /* * empty the vector @@ -56,7 +56,7 @@ public: inline void clear() { VectorImpl::clear(); } - /*! + /*! * vector stats */ @@ -69,11 +69,11 @@ public: //! sets the capacity. capacity can never be reduced less than size() inline ssize_t setCapacity(size_t size) { return VectorImpl::setCapacity(size); } - /*! + /*! * C-style array access */ - - //! read-only C-style access + + //! read-only C-style access inline const TYPE* array() const; //! read-write C-style access. BE VERY CAREFUL when modifying the array @@ -82,12 +82,12 @@ public: //! finds the index of an item ssize_t indexOf(const TYPE& item) const; - + //! finds where this item should be inserted size_t orderOf(const TYPE& item) const; - - - /*! + + + /*! * accessors */ @@ -104,7 +104,7 @@ public: //! add an item in the right place (and replace the one that is there) ssize_t add(const TYPE& item); - + //! editItemAt() MUST NOT change the order of this item TYPE& editItemAt(size_t index) { return *( static_cast<TYPE *>(VectorImpl::editItemLocation(index)) ); @@ -113,7 +113,7 @@ public: //! merges a vector into this one ssize_t merge(const Vector<TYPE>& vector); ssize_t merge(const SortedVector<TYPE>& vector); - + //! removes an item ssize_t remove(const TYPE&); @@ -121,7 +121,24 @@ public: inline ssize_t removeItemsAt(size_t index, size_t count = 1); //! remove one item inline ssize_t removeAt(size_t index) { return removeItemsAt(index); } - + + /* + * these inlines add some level of compatibility with STL. + */ + typedef TYPE* iterator; + typedef TYPE const* const_iterator; + + inline iterator begin() { return editArray(); } + inline iterator end() { return editArray() + size(); } + inline const_iterator begin() const { return array(); } + inline const_iterator end() const { return array() + size(); } + inline void reserve(size_t n) { setCapacity(n); } + inline bool empty() const{ return isEmpty(); } + inline iterator erase(iterator pos) { + ssize_t index = removeItemsAt(pos-array()); + return begin() + index; + } + protected: virtual void do_construct(void* storage, size_t num) const; virtual void do_destroy(void* storage, size_t num) const; @@ -159,13 +176,13 @@ SortedVector<TYPE>::~SortedVector() { template<class TYPE> inline SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) { SortedVectorImpl::operator = (rhs); - return *this; + return *this; } template<class TYPE> inline const SortedVector<TYPE>& SortedVector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const { SortedVectorImpl::operator = (rhs); - return *this; + return *this; } template<class TYPE> inline @@ -235,7 +252,7 @@ ssize_t SortedVector<TYPE>::removeItemsAt(size_t index, size_t count) { // --------------------------------------------------------------------------- template<class TYPE> -void SortedVector<TYPE>::do_construct(void* storage, size_t num) const { +UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_construct(void* storage, size_t num) const { construct_type( reinterpret_cast<TYPE*>(storage), num ); } @@ -245,22 +262,22 @@ void SortedVector<TYPE>::do_destroy(void* storage, size_t num) const { } template<class TYPE> -void SortedVector<TYPE>::do_copy(void* dest, const void* from, size_t num) const { +UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_copy(void* dest, const void* from, size_t num) const { copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); } template<class TYPE> -void SortedVector<TYPE>::do_splat(void* dest, const void* item, size_t num) const { +UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_splat(void* dest, const void* item, size_t num) const { splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num ); } template<class TYPE> -void SortedVector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const { +UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const { move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); } template<class TYPE> -void SortedVector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const { +UTILS_VECTOR_NO_CFI void SortedVector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const { move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); } diff --git a/libutils/include/utils/StrongPointer.h b/libutils/include/utils/StrongPointer.h index c2f772222..0c2060791 100644 --- a/libutils/include/utils/StrongPointer.h +++ b/libutils/include/utils/StrongPointer.h @@ -17,12 +17,6 @@ #ifndef ANDROID_STRONG_POINTER_H #define ANDROID_STRONG_POINTER_H -#include <cutils/atomic.h> - -#include <stdint.h> -#include <sys/types.h> -#include <stdlib.h> - // --------------------------------------------------------------------------- namespace android { diff --git a/libutils/include/utils/Trace.h b/libutils/include/utils/Trace.h index eeba40d65..5e9229c1e 100644 --- a/libutils/include/utils/Trace.h +++ b/libutils/include/utils/Trace.h @@ -19,16 +19,8 @@ #if defined(__ANDROID__) -#include <fcntl.h> #include <stdint.h> -#include <stdio.h> -#include <string.h> -#include <sys/stat.h> -#include <sys/types.h> -#include <unistd.h> - -#include <cutils/compiler.h> -#include <utils/threads.h> + #include <cutils/trace.h> // See <cutils/trace.h> for more ATRACE_* macros. @@ -37,6 +29,7 @@ #define _PASTE(x, y) x ## y #define PASTE(x, y) _PASTE(x,y) #define ATRACE_NAME(name) android::ScopedTrace PASTE(___tracer, __LINE__) (ATRACE_TAG, name) + // ATRACE_CALL is an ATRACE_NAME that uses the current function name. #define ATRACE_CALL() ATRACE_NAME(__FUNCTION__) @@ -44,14 +37,13 @@ namespace android { class ScopedTrace { public: -inline ScopedTrace(uint64_t tag, const char* name) - : mTag(tag) { - atrace_begin(mTag,name); -} - -inline ~ScopedTrace() { - atrace_end(mTag); -} + inline ScopedTrace(uint64_t tag, const char* name) : mTag(tag) { + atrace_begin(mTag, name); + } + + inline ~ScopedTrace() { + atrace_end(mTag); + } private: uint64_t mTag; diff --git a/libutils/include/utils/TypeHelpers.h b/libutils/include/utils/TypeHelpers.h index 2a2522722..28fbca508 100644 --- a/libutils/include/utils/TypeHelpers.h +++ b/libutils/include/utils/TypeHelpers.h @@ -36,7 +36,7 @@ template <typename T> struct trait_trivial_ctor { enum { value = false }; }; template <typename T> struct trait_trivial_dtor { enum { value = false }; }; template <typename T> struct trait_trivial_copy { enum { value = false }; }; template <typename T> struct trait_trivial_move { enum { value = false }; }; -template <typename T> struct trait_pointer { enum { value = false }; }; +template <typename T> struct trait_pointer { enum { value = false }; }; template <typename T> struct trait_pointer<T*> { enum { value = true }; }; template <typename TYPE> @@ -59,13 +59,13 @@ template <typename T, typename U> struct aggregate_traits { enum { is_pointer = false, - has_trivial_ctor = + has_trivial_ctor = traits<T>::has_trivial_ctor && traits<U>::has_trivial_ctor, - has_trivial_dtor = + has_trivial_dtor = traits<T>::has_trivial_dtor && traits<U>::has_trivial_dtor, - has_trivial_copy = + has_trivial_copy = traits<T>::has_trivial_copy && traits<U>::has_trivial_copy, - has_trivial_move = + has_trivial_move = traits<T>::has_trivial_move && traits<U>::has_trivial_move }; }; diff --git a/libutils/include/utils/Vector.h b/libutils/include/utils/Vector.h index 9a643f9b0..7e00123f7 100644 --- a/libutils/include/utils/Vector.h +++ b/libutils/include/utils/Vector.h @@ -24,6 +24,20 @@ #include <utils/TypeHelpers.h> #include <utils/VectorImpl.h> +/* + * Used to blacklist some functions from CFI. + * + */ +#ifndef __has_attribute +#define __has_attribute(x) 0 +#endif + +#if __has_attribute(no_sanitize) +#define UTILS_VECTOR_NO_CFI __attribute__((no_sanitize("cfi"))) +#else +#define UTILS_VECTOR_NO_CFI +#endif + // --------------------------------------------------------------------------- namespace android { @@ -42,11 +56,11 @@ class Vector : private VectorImpl { public: typedef TYPE value_type; - - /*! + + /*! * Constructors and destructors */ - + Vector(); Vector(const Vector<TYPE>& rhs); explicit Vector(const SortedVector<TYPE>& rhs); @@ -54,7 +68,7 @@ public: /*! copy operator */ const Vector<TYPE>& operator = (const Vector<TYPE>& rhs) const; - Vector<TYPE>& operator = (const Vector<TYPE>& rhs); + Vector<TYPE>& operator = (const Vector<TYPE>& rhs); const Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs) const; Vector<TYPE>& operator = (const SortedVector<TYPE>& rhs); @@ -65,7 +79,7 @@ public: inline void clear() { VectorImpl::clear(); } - /*! + /*! * vector stats */ @@ -87,13 +101,13 @@ public: /*! * C-style array access */ - - //! read-only C-style access + + //! read-only C-style access inline const TYPE* array() const; //! read-write C-style access TYPE* editArray(); - - /*! + + /*! * accessors */ @@ -113,10 +127,10 @@ public: //! grants right access to the top of the stack (last element) TYPE& editTop(); - /*! + /*! * append/insert another vector */ - + //! insert another vector at a given index ssize_t insertVectorAt(const Vector<TYPE>& vector, size_t index); @@ -130,10 +144,10 @@ public: //! append an array at the end of this vector ssize_t appendArray(const TYPE* array, size_t length); - /*! + /*! * add/insert/replace items */ - + //! insert one or several items initialized with their default constructor inline ssize_t insertAt(size_t index, size_t numItems = 1); //! insert one or several items initialized from a prototype item @@ -147,7 +161,7 @@ public: //! same as push() but returns the index the item was added at (or an error) inline ssize_t add(); //! same as push() but returns the index the item was added at (or an error) - ssize_t add(const TYPE& item); + ssize_t add(const TYPE& item); //! replace an item with a new one initialized with its default constructor inline ssize_t replaceAt(size_t index); //! replace an item with a new one @@ -165,10 +179,10 @@ public: /*! * sort (stable) the array */ - + typedef int (*compar_t)(const TYPE* lhs, const TYPE* rhs); typedef int (*compar_r_t)(const TYPE* lhs, const TYPE* rhs, void* state); - + inline status_t sort(compar_t cmp); inline status_t sort(compar_r_t cmp, void* state); @@ -237,7 +251,7 @@ Vector<TYPE>::~Vector() { template<class TYPE> inline Vector<TYPE>& Vector<TYPE>::operator = (const Vector<TYPE>& rhs) { VectorImpl::operator = (rhs); - return *this; + return *this; } template<class TYPE> inline @@ -255,7 +269,7 @@ Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) { template<class TYPE> inline const Vector<TYPE>& Vector<TYPE>::operator = (const SortedVector<TYPE>& rhs) const { VectorImpl::operator = (rhs); - return *this; + return *this; } template<class TYPE> inline @@ -380,7 +394,7 @@ status_t Vector<TYPE>::sort(Vector<TYPE>::compar_r_t cmp, void* state) { // --------------------------------------------------------------------------- template<class TYPE> -void Vector<TYPE>::do_construct(void* storage, size_t num) const { +UTILS_VECTOR_NO_CFI void Vector<TYPE>::do_construct(void* storage, size_t num) const { construct_type( reinterpret_cast<TYPE*>(storage), num ); } @@ -390,22 +404,22 @@ void Vector<TYPE>::do_destroy(void* storage, size_t num) const { } template<class TYPE> -void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const { +UTILS_VECTOR_NO_CFI void Vector<TYPE>::do_copy(void* dest, const void* from, size_t num) const { copy_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); } template<class TYPE> -void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const { +UTILS_VECTOR_NO_CFI void Vector<TYPE>::do_splat(void* dest, const void* item, size_t num) const { splat_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(item), num ); } template<class TYPE> -void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const { +UTILS_VECTOR_NO_CFI void Vector<TYPE>::do_move_forward(void* dest, const void* from, size_t num) const { move_forward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); } template<class TYPE> -void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const { +UTILS_VECTOR_NO_CFI void Vector<TYPE>::do_move_backward(void* dest, const void* from, size_t num) const { move_backward_type( reinterpret_cast<TYPE*>(dest), reinterpret_cast<const TYPE*>(from), num ); } |
