1 /* 2 * Copyright (C) 2021 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 CPP_CAR_BINDER_LIB_LARGEPARCELABLE_INCLUDE_SHAREDMEMORY_H_ 18 #define CPP_CAR_BINDER_LIB_LARGEPARCELABLE_INCLUDE_SHAREDMEMORY_H_ 19 20 #include "MappedFile.h" 21 22 #include <android-base/unique_fd.h> 23 #include <cutils/ashmem.h> 24 #include <utils/Errors.h> 25 26 #include <unistd.h> 27 28 #include <memory> 29 30 namespace android { 31 namespace automotive { 32 namespace car_binder_lib { 33 34 using ::android::status_t; 35 using ::android::base::borrowed_fd; 36 using ::android::base::unique_fd; 37 38 // SharedMemory represents a shared memory file object. 39 class SharedMemory { 40 public: 41 // Initialize the shared memory object with the file descriptor to a shared memory file. The fd 42 // is owned by this class. Caller should use isValid() to check whether the initialization 43 // succeed and use getErr() to get error if isValid() is not true. 44 explicit SharedMemory(unique_fd fd); 45 46 // Initialize the shared memory object with an unowned file descriptor to a shared memory file. 47 explicit SharedMemory(borrowed_fd fd); 48 49 // Create a shared memory object with 'size'. Caller should use isValid() to check whether the 50 // initialization succeed and use getErr() to get error if isValid() is not true. 51 explicit SharedMemory(size_t size); 52 isValid()53 inline bool isValid() const { 54 if (mOwned) { 55 return mOwnedFd.ok() && ashmem_valid(mOwnedFd.get()); 56 } else { 57 return mBorrowedFd.get() >= 0 && ashmem_valid(mBorrowedFd.get()); 58 } 59 } 60 getSize()61 inline size_t getSize() const { return mSize; } 62 getErr()63 inline status_t getErr() const { return -mErrno; } 64 getFd()65 inline int getFd() const { 66 if (mOwned) { 67 return mOwnedFd.get(); 68 } 69 return mBorrowedFd.get(); 70 } 71 getDupFd()72 inline unique_fd getDupFd() const { 73 unique_fd fd(dup(getFd())); 74 return std::move(fd); 75 } 76 mapReadWrite()77 inline std::unique_ptr<MappedFile> mapReadWrite() const { 78 assert(!mLocked); 79 bool writable = true; 80 return std::unique_ptr<MappedFile>(new MappedFile(getFd(), mSize, writable)); 81 } 82 mapReadOnly()83 inline std::unique_ptr<MappedFile> mapReadOnly() const { 84 bool writable = false; 85 return std::unique_ptr<MappedFile>(new MappedFile(getFd(), mSize, writable)); 86 } 87 88 status_t lock(); 89 90 private: 91 unique_fd mOwnedFd; 92 borrowed_fd mBorrowedFd; 93 int mErrno = 0; 94 bool mLocked = false; 95 size_t mSize = 0; 96 bool mOwned; 97 }; 98 99 } // namespace car_binder_lib 100 } // namespace automotive 101 } // namespace android 102 103 #endif // CPP_CAR_BINDER_LIB_LARGEPARCELABLE_INCLUDE_SHAREDMEMORY_H_ 104