1 /*
2  * Copyright 2016 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 #ifndef ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
18 #define ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
19 
20 #include <stdint.h>
21 #include <sys/mman.h>
22 
23 #include <android-base/unique_fd.h>
24 #include <android/media/SharedFileRegion.h>
25 
26 namespace aaudio {
27 
28 // Arbitrary limits for range checks.
29 #define MAX_SHARED_MEMORIES (32)
30 #define MAX_MMAP_OFFSET_BYTES (32 * 1024 * 8)
31 #define MAX_MMAP_SIZE_BYTES (32 * 1024 * 8)
32 
33 /**
34  * This is a parcelable description of a shared memory referenced by a file descriptor.
35  * It may be divided into several regions.
36  * The memory can be shared using Binder or simply shared between threads.
37  */
38 class SharedMemoryParcelable {
39 public:
40     SharedMemoryParcelable() = default;
41 
42     // Ctor from a parcelable representation.
43     // Since the parcelable object owns a unique FD, move semantics are provided to avoid the need
44     // to dupe.
45     explicit SharedMemoryParcelable(android::media::SharedFileRegion&& parcelable);
46 
47     /**
48      * Make a dup() of the fd and store it for later use.
49      *
50      * @param fd
51      * @param sizeInBytes
52      */
53     void setup(const android::base::unique_fd& fd, int32_t sizeInBytes);
54 
55     void setup(const SharedMemoryParcelable& sharedMemoryParcelable);
56 
57     // mmap() shared memory
58     aaudio_result_t resolve(int32_t offsetInBytes, int32_t sizeInBytes, void **regionAddressPtr);
59 
60     // munmap() any mapped memory
61     aaudio_result_t close();
62 
63     aaudio_result_t closeAndReleaseFd();
64 
65     int32_t getSizeInBytes();
66 
isInUse()67     bool isInUse() const { return mFd.get() != -1; }
68 
69     void dump() const;
70 
71     // Extract a parcelable representation of this object.
72     // Since we own a unique FD, move semantics are provided to avoid the need to dupe.
73     android::media::SharedFileRegion parcelable() &&;
74 
75     // Copy this instance. Duplicates the underlying FD.
76     SharedMemoryParcelable dup() const;
77 
78 private:
79 #define MMAP_UNRESOLVED_ADDRESS    reinterpret_cast<uint8_t*>(MAP_FAILED)
80 
81     android::base::unique_fd   mFd;
82     int64_t                    mSizeInBytes = 0;
83     int64_t                    mOffsetInBytes = 0;
84     uint8_t                   *mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
85 
86     aaudio_result_t resolveSharedMemory(const android::base::unique_fd& fd);
87     aaudio_result_t validate() const;
88 };
89 
90 } /* namespace aaudio */
91 
92 #endif //ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
93