Home | History | Annotate | Download | only in binding
      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 <binder/Parcel.h>
     25 #include <binder/Parcelable.h>
     26 
     27 namespace aaudio {
     28 
     29 // Arbitrary limits for sanity checks. TODO remove after debugging.
     30 #define MAX_SHARED_MEMORIES (32)
     31 #define MAX_MMAP_OFFSET_BYTES (32 * 1024 * 8)
     32 #define MAX_MMAP_SIZE_BYTES (32 * 1024 * 8)
     33 
     34 /**
     35  * This is a parcelable description of a shared memory referenced by a file descriptor.
     36  * It may be divided into several regions.
     37  * The memory can be shared using Binder or simply shared between threads.
     38  */
     39 class SharedMemoryParcelable : public android::Parcelable {
     40 public:
     41     SharedMemoryParcelable();
     42     virtual ~SharedMemoryParcelable();
     43 
     44     /**
     45      * Make a dup() of the fd and store it for later use.
     46      *
     47      * @param fd
     48      * @param sizeInBytes
     49      */
     50     void setup(const android::base::unique_fd& fd, int32_t sizeInBytes);
     51 
     52     virtual android::status_t writeToParcel(android::Parcel* parcel) const override;
     53 
     54     virtual android::status_t readFromParcel(const android::Parcel* parcel) override;
     55 
     56     // mmap() shared memory
     57     aaudio_result_t resolve(int32_t offsetInBytes, int32_t sizeInBytes, void **regionAddressPtr);
     58 
     59     // munmap() any mapped memory
     60     aaudio_result_t close();
     61 
     62     int32_t getSizeInBytes();
     63 
     64     void dump();
     65 
     66 protected:
     67 
     68 #define MMAP_UNRESOLVED_ADDRESS    reinterpret_cast<uint8_t*>(MAP_FAILED)
     69 
     70     aaudio_result_t resolveSharedMemory(const android::base::unique_fd& fd);
     71 
     72     android::base::unique_fd   mFd;
     73     int32_t     mSizeInBytes = 0;
     74     uint8_t    *mResolvedAddress = MMAP_UNRESOLVED_ADDRESS;
     75 
     76 private:
     77 
     78     aaudio_result_t validate() const;
     79 
     80 };
     81 
     82 } /* namespace aaudio */
     83 
     84 #endif //ANDROID_AAUDIO_SHARED_MEMORY_PARCELABLE_H
     85