Move definition of MessageQueue flavor to libfmq-base

Move the defintion of MQFlavor to libfmq to share it.

Test: atest fmq_unit_tests && m -j128
Bug: 142326204

Change-Id: Ib8427ded3f1d70d7aed96070311248ac6cb571c9
diff --git a/base/include/hidl/MQDescriptor.h b/base/include/hidl/MQDescriptor.h
index eb244e5..6a0bc25 100644
--- a/base/include/hidl/MQDescriptor.h
+++ b/base/include/hidl/MQDescriptor.h
@@ -20,14 +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)));
@@ -42,21 +41,6 @@
 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,6 +71,8 @@
         return mGrantors;
     }
 
+    // This should be removed if no one is using it. We shouldn't be returning
+    // a mutable reference if it's not necessary. TODO(b/162465295).
     inline ::android::hardware::hidl_vec<GrantorDescriptor> &grantors() {
         return mGrantors;
     }
@@ -101,42 +87,7 @@
 
     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;
@@ -170,9 +121,6 @@
     : 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];
     }
 }
@@ -185,13 +133,16 @@
      * 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 */
     };
 
     /*
@@ -202,12 +153,9 @@
     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]};
     }
 }
 
@@ -253,7 +201,7 @@
 
 template<typename T, MQFlavor flavor>
 size_t MQDescriptor<T, flavor>::getSize() const {
-    return static_cast<size_t>(mGrantors[DATAPTRPOS].extent);
+    return static_cast<size_t>(mGrantors[details::DATAPTRPOS].extent);
 }
 
 template<typename T, MQFlavor flavor>