diff options
Diffstat (limited to 'base/include/hidl')
-rw-r--r-- | base/include/hidl/HidlInternal.h | 11 | ||||
-rw-r--r-- | base/include/hidl/HidlSupport.h | 8 | ||||
-rw-r--r-- | base/include/hidl/MQDescriptor.h | 123 |
3 files changed, 38 insertions, 104 deletions
diff --git a/base/include/hidl/HidlInternal.h b/base/include/hidl/HidlInternal.h index 3cd246a..3d1a444 100644 --- a/base/include/hidl/HidlInternal.h +++ b/base/include/hidl/HidlInternal.h @@ -201,7 +201,10 @@ struct HidlInstrumentor { // A list of registered instrumentation callbacks. std::vector<InstrumentationCallback> mInstrumentationCallbacks; // Flag whether to enable instrumentation. - bool mEnableInstrumentation; + union { + bool mEnableInstrumentation; + void* mReserved0; + }; // Prefix to lookup the instrumentation libraries. std::string mInstrumentationLibPackage; // Used for dlsym to load the profiling method for given interface. @@ -209,6 +212,12 @@ struct HidlInstrumentor { }; +#ifdef __LP64__ +static_assert(sizeof(HidlInstrumentor) == 88, "HidlInstrumentor size frozen by prebuilts"); +#else +static_assert(sizeof(HidlInstrumentor) == 44, "HidlInstrumentor size frozen by prebuilts"); +#endif + } // namespace details } // namespace hardware } // namespace android diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h index 1b91c26..1bb38e8 100644 --- a/base/include/hidl/HidlSupport.h +++ b/base/include/hidl/HidlSupport.h @@ -342,7 +342,6 @@ struct hidl_vec { memset(mPad, 0, sizeof(mPad)); } - // Note, does not initialize primitive types. hidl_vec(size_t size) : hidl_vec() { resize(size); } hidl_vec(const hidl_vec<T> &other) : hidl_vec() { @@ -412,7 +411,7 @@ struct hidl_vec { } T *releaseData() { - if (!mOwnsBuffer && mSize > 0) { + if (!mOwnsBuffer && mBuffer != nullptr) { resize(mSize); } mOwnsBuffer = false; @@ -507,7 +506,7 @@ struct hidl_vec { return mBuffer[index]; } - // Does not initialize primitive types if new size > old size. + // Copies over old elements fitting in new size. Value initializes the rest. void resize(size_t size) { if (size > UINT32_MAX) { details::logAlwaysFatal("hidl_vec can't hold more than 2^32 elements."); @@ -1044,6 +1043,9 @@ constexpr hidl_invalid_type<T> hidl_enum_values; */ template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>> struct hidl_enum_range { + // Container-like associated type. + using value_type = T; + constexpr auto begin() const { return std::begin(details::hidl_enum_values<T>); } constexpr auto cbegin() const { return begin(); } constexpr auto rbegin() const { return std::rbegin(details::hidl_enum_values<T>); } diff --git a/base/include/hidl/MQDescriptor.h b/base/include/hidl/MQDescriptor.h index 0f61cb5..0429444 100644 --- a/base/include/hidl/MQDescriptor.h +++ b/base/include/hidl/MQDescriptor.h @@ -20,43 +20,13 @@ #include <unistd.h> #include <cutils/native_handle.h> +#include <fmq/MQDescriptorBase.h> #include <hidl/HidlInternal.h> #include <hidl/HidlSupport.h> namespace android { namespace hardware { -typedef uint64_t RingBufferPosition; - -struct GrantorDescriptor { - uint32_t flags __attribute__ ((aligned(4))); - uint32_t fdIndex __attribute__ ((aligned(4))); - uint32_t offset __attribute__ ((aligned(4))); - uint64_t extent __attribute__ ((aligned(8))); -}; - -static_assert(offsetof(GrantorDescriptor, flags) == 0, "wrong offset"); -static_assert(offsetof(GrantorDescriptor, fdIndex) == 4, "wrong offset"); -static_assert(offsetof(GrantorDescriptor, offset) == 8, "wrong offset"); -static_assert(offsetof(GrantorDescriptor, extent) == 16, "wrong offset"); -static_assert(sizeof(GrantorDescriptor) == 24, "wrong size"); -static_assert(__alignof(GrantorDescriptor) == 8, "wrong alignment"); - -enum MQFlavor : uint32_t { - /* - * kSynchronizedReadWrite represents the wait-free synchronized flavor of the - * FMQ. It is intended to be have a single reader and single writer. - * Attempts to overflow/underflow returns a failure. - */ - kSynchronizedReadWrite = 0x01, - /* - * kUnsynchronizedWrite represents the flavor of FMQ where writes always - * succeed. This flavor allows one writer and many readers. A read operation - * can detect an overwrite and reset the read counter. - */ - kUnsynchronizedWrite = 0x02 -}; - template <typename T, MQFlavor flavor> struct MQDescriptor { // Takes ownership of handle @@ -87,10 +57,6 @@ struct MQDescriptor { return mGrantors; } - inline ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() { - return mGrantors; - } - inline const ::native_handle_t *handle() const { return mHandle; } @@ -101,42 +67,7 @@ struct MQDescriptor { static const size_t kOffsetOfGrantors; static const size_t kOffsetOfHandle; - enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS, EVFLAGWORDPOS }; - - /* - * There should at least be GrantorDescriptors for the read counter, write - * counter and data buffer. A GrantorDescriptor for an EventFlag word is - * not required if there is no need for blocking FMQ operations. - */ - static constexpr int32_t kMinGrantorCount = DATAPTRPOS + 1; - - /* - * Minimum number of GrantorDescriptors required if EventFlag support is - * needed for blocking FMQ operations. - */ - static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1; - - //TODO(b/34160777) Identify a better solution that supports remoting. - static inline size_t alignToWordBoundary(size_t length) { - constexpr size_t kAlignmentSize = 64; - if (kAlignmentSize % __WORDSIZE != 0) { - details::logAlwaysFatal("Incompatible word size"); - } - - /* - * Check if alignment to word boundary would cause an overflow. - */ - if (length > SIZE_MAX - kAlignmentSize/8 + 1) { - details::logAlwaysFatal("Queue size too large"); - } - - return (length + kAlignmentSize/8 - 1) & ~(kAlignmentSize/8 - 1U); - } - static inline size_t isAlignedToWordBoundary(size_t offset) { - constexpr size_t kAlignmentSize = 64; - return (offset & (kAlignmentSize/8 - 1)) == 0; - } private: ::android::hardware::hidl_vec<GrantorDescriptor> mGrantors; ::android::hardware::details::hidl_pointer<native_handle_t> mHandle; @@ -164,38 +95,34 @@ using MQDescriptorSync = MQDescriptor<T, kSynchronizedReadWrite>; template<typename T> using MQDescriptorUnsync = MQDescriptor<T, kUnsynchronizedWrite>; -template<typename T, MQFlavor flavor> -MQDescriptor<T, flavor>::MQDescriptor( - const std::vector<GrantorDescriptor>& grantors, - native_handle_t* nhandle, - size_t size) - : mHandle(nhandle), - mQuantum(size), - mFlags(flavor) { +template <typename T, MQFlavor flavor> +MQDescriptor<T, flavor>::MQDescriptor(const std::vector<GrantorDescriptor>& grantors, + native_handle_t* nhandle, size_t size) + : mHandle(nhandle), mQuantum(static_cast<uint32_t>(size)), mFlags(flavor) { mGrantors.resize(grantors.size()); for (size_t i = 0; i < grantors.size(); ++i) { - if (isAlignedToWordBoundary(grantors[i].offset) == false) { - details::logAlwaysFatal("Grantor offsets need to be aligned"); - } mGrantors[i] = grantors[i]; } } -template<typename T, MQFlavor flavor> -MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandle, - size_t messageSize, bool configureEventFlag) - : mHandle(nHandle), mQuantum(messageSize), mFlags(flavor) { +template <typename T, MQFlavor flavor> +MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t* nHandle, + size_t messageSize, bool configureEventFlag) + : mHandle(nHandle), mQuantum(static_cast<uint32_t>(messageSize)), mFlags(flavor) { /* * If configureEventFlag is true, allocate an additional spot in mGrantor * for containing the fd and offset for mmapping the EventFlag word. */ - mGrantors.resize(configureEventFlag? kMinGrantorCountForEvFlagSupport : kMinGrantorCount); + mGrantors.resize(configureEventFlag ? details::kMinGrantorCountForEvFlagSupport + : details::kMinGrantorCount); size_t memSize[] = { - sizeof(RingBufferPosition), /* memory to be allocated for read pointer counter */ - sizeof(RingBufferPosition), /* memory to be allocated for write pointer counter */ - bufferSize, /* memory to be allocated for data buffer */ - sizeof(std::atomic<uint32_t>)/* memory to be allocated for EventFlag word */ + sizeof(details::RingBufferPosition), /* memory to be allocated for read pointer counter + */ + sizeof(details::RingBufferPosition), /* memory to be allocated for write pointer counter + */ + bufferSize, /* memory to be allocated for data buffer */ + sizeof(std::atomic<uint32_t>) /* memory to be allocated for EventFlag word */ }; /* @@ -206,12 +133,9 @@ MQDescriptor<T, flavor>::MQDescriptor(size_t bufferSize, native_handle_t *nHandl for (size_t grantorPos = 0, offset = 0; grantorPos < mGrantors.size(); offset += memSize[grantorPos++]) { - mGrantors[grantorPos] = { - 0 /* grantor flags */, - 0 /* fdIndex */, - static_cast<uint32_t>(alignToWordBoundary(offset)), - memSize[grantorPos] - }; + mGrantors[grantorPos] = {0 /* grantor flags */, 0 /* fdIndex */, + static_cast<uint32_t>(details::alignToWordBoundary(offset)), + memSize[grantorPos]}; } } @@ -234,9 +158,8 @@ MQDescriptor<T, flavor>& MQDescriptor<T, flavor>::operator=(const MQDescriptor& mHandle->data[i] = dup(other.mHandle->data[i]); } - memcpy(&mHandle->data[other.mHandle->numFds], - &other.mHandle->data[other.mHandle->numFds], - other.mHandle->numInts * sizeof(int)); + memcpy(&mHandle->data[other.mHandle->numFds], &other.mHandle->data[other.mHandle->numFds], + static_cast<size_t>(other.mHandle->numInts) * sizeof(int)); } return *this; @@ -258,7 +181,7 @@ MQDescriptor<T, flavor>::~MQDescriptor() { template<typename T, MQFlavor flavor> size_t MQDescriptor<T, flavor>::getSize() const { - return mGrantors[DATAPTRPOS].extent; + return static_cast<size_t>(mGrantors[details::DATAPTRPOS].extent); } template<typename T, MQFlavor flavor> |