• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <assert.h>
20 #include <string>
21 
22 namespace android {
23 namespace hardware {
24 
25 enum MQFlavor : uint32_t {
26     /*
27      * kSynchronizedReadWrite represents the wait-free synchronized flavor of the
28      * FMQ. It is intended to be have a single reader and single writer.
29      * Attempts to overflow/underflow returns a failure.
30      */
31     kSynchronizedReadWrite = 0x01,
32     /*
33      * kUnsynchronizedWrite represents the flavor of FMQ where writes always
34      * succeed. This flavor allows one writer and many readers. A read operation
35      * can detect an overwrite and reset the read counter.
36      */
37     kUnsynchronizedWrite = 0x02
38 };
39 
40 struct GrantorDescriptor {
41     uint32_t flags __attribute__((aligned(4)));
42     uint32_t fdIndex __attribute__((aligned(4)));
43     uint32_t offset __attribute__((aligned(4)));
44     uint64_t extent __attribute__((aligned(8)));
45 };
46 
47 static_assert(offsetof(GrantorDescriptor, flags) == 0, "wrong offset");
48 static_assert(offsetof(GrantorDescriptor, fdIndex) == 4, "wrong offset");
49 static_assert(offsetof(GrantorDescriptor, offset) == 8, "wrong offset");
50 static_assert(offsetof(GrantorDescriptor, extent) == 16, "wrong offset");
51 static_assert(sizeof(GrantorDescriptor) == 24, "wrong size");
52 static_assert(__alignof(GrantorDescriptor) == 8, "wrong alignment");
53 
54 namespace details {
55 
56 void logError(const std::string& message);
57 void errorWriteLog(int tag, const char* message);
58 void check(bool exp, const char* message);
59 
60 typedef uint64_t RingBufferPosition;
61 enum GrantorType : int { READPTRPOS = 0, WRITEPTRPOS, DATAPTRPOS, EVFLAGWORDPOS };
62 /*
63  * There should at least be GrantorDescriptors for the read counter, write
64  * counter and data buffer. A GrantorDescriptor for an EventFlag word is
65  * not required if there is no need for blocking FMQ operations.
66  */
67 static constexpr int32_t kMinGrantorCount = DATAPTRPOS + 1;
68 
69 /*
70  * Minimum number of GrantorDescriptors required if EventFlag support is
71  * needed for blocking FMQ operations.
72  */
73 static constexpr int32_t kMinGrantorCountForEvFlagSupport = EVFLAGWORDPOS + 1;
74 
alignToWordBoundary(size_t length)75 static inline size_t alignToWordBoundary(size_t length) {
76     constexpr size_t kAlignmentSize = 64;
77     static_assert(kAlignmentSize % sizeof(long) == 0, "Incompatible word size");
78 
79     /*
80      * Check if alignment to word boundary would cause an overflow.
81      */
82     check(length <= SIZE_MAX - kAlignmentSize / 8 + 1, "Queue size too large");
83 
84     return (length + kAlignmentSize / 8 - 1) & ~(kAlignmentSize / 8 - 1U);
85 }
86 
isAlignedToWordBoundary(size_t offset)87 static inline size_t isAlignedToWordBoundary(size_t offset) {
88     constexpr size_t kAlignmentSize = 64;
89     return (offset & (kAlignmentSize / 8 - 1)) == 0;
90 }
91 
92 }  // namespace details
93 }  // namespace hardware
94 }  // namespace android
95