• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define LOG_TAG "LargeParcelable"
18 
19 #include "SharedMemory.h"
20 
21 #include "MappedFile.h"
22 
23 #include <android-base/unique_fd.h>
24 #include <cutils/ashmem.h>
25 #include <utils/Errors.h>
26 #include <utils/Log.h>
27 
28 #include <assert.h>
29 #include <errno.h>
30 #include <sys/mman.h>
31 
32 #include <memory>
33 
34 namespace android {
35 namespace automotive {
36 namespace car_binder_lib {
37 
38 using ::android::OK;
39 using ::android::status_t;
40 using ::android::base::borrowed_fd;
41 using ::android::base::unique_fd;
42 
SharedMemory(unique_fd fd)43 SharedMemory::SharedMemory(unique_fd fd) : mBorrowedFd(-1), mOwned(true) {
44     int intFd = fd.get();
45     if (!ashmem_valid(intFd)) {
46         ALOGE("%s", "the FD is not valid");
47         mErrno = errno;
48         return;
49     }
50     int size = ashmem_get_size_region(intFd);
51     if (size < 0) {
52         ALOGE("ashmem_get_size_region failed, error: %s", std::strerror(errno));
53         mErrno = errno;
54         return;
55     }
56     mOwnedFd = std::move(fd);
57     mSize = size;
58 }
59 
SharedMemory(borrowed_fd fd)60 SharedMemory::SharedMemory(borrowed_fd fd) : mBorrowedFd(-1), mOwned(false) {
61     int intFd = fd.get();
62     if (!ashmem_valid(intFd)) {
63         ALOGE("%s", "the FD is not valid");
64         return;
65     }
66     int size = ashmem_get_size_region(intFd);
67     if (size < 0) {
68         ALOGE("ashmem_get_size_region failed, error: %s", std::strerror(errno));
69         mErrno = errno;
70         return;
71     }
72     mBorrowedFd = std::move(fd);
73     mSize = size;
74 }
75 
SharedMemory(size_t size)76 SharedMemory::SharedMemory(size_t size) : mBorrowedFd(-1), mOwned(true) {
77     int fd = ashmem_create_region("SharedMemory", size);
78     if (fd < 0) {
79         ALOGE("ASharedMemory_create failed, error: %s", std::strerror(errno));
80         mErrno = errno;
81         return;
82     }
83     mOwnedFd.reset(fd);
84     mSize = size;
85 }
86 
lock()87 status_t SharedMemory::lock() {
88     int result = ashmem_set_prot_region(getFd(), PROT_READ);
89     if (result != 0) {
90         ALOGE("ASharedMemory_setProt failed, error: %s", std::strerror(errno));
91         mErrno = errno;
92         return -result;
93     }
94     mLocked = true;
95     return OK;
96 }
97 
98 }  // namespace car_binder_lib
99 }  // namespace automotive
100 }  // namespace android
101