SharedMemoryParcelable.cpp revision 942bdc0aebc88dc8b12c0e7742ec0003bbb8b80f
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#define LOG_TAG "AAudio" 18//#define LOG_NDEBUG 0 19#include <utils/Log.h> 20 21#include <stdint.h> 22#include <stdio.h> 23 24#include <sys/mman.h> 25#include <aaudio/AAudio.h> 26 27#include <binder/Parcelable.h> 28#include <utility/AAudioUtilities.h> 29 30#include "binding/SharedMemoryParcelable.h" 31 32using android::NO_ERROR; 33using android::status_t; 34using android::Parcel; 35using android::Parcelable; 36 37using namespace aaudio; 38 39SharedMemoryParcelable::SharedMemoryParcelable() {} 40SharedMemoryParcelable::~SharedMemoryParcelable() {}; 41 42void SharedMemoryParcelable::setup(int fd, int32_t sizeInBytes) { 43 mFd = fd; 44 mSizeInBytes = sizeInBytes; 45 46} 47 48status_t SharedMemoryParcelable::writeToParcel(Parcel* parcel) const { 49 status_t status = parcel->writeInt32(mSizeInBytes); 50 if (status != NO_ERROR) return status; 51 if (mSizeInBytes > 0) { 52 status = parcel->writeDupFileDescriptor(mFd); 53 ALOGE_IF(status != NO_ERROR, "SharedMemoryParcelable writeDupFileDescriptor failed : %d", 54 status); 55 } 56 return status; 57} 58 59status_t SharedMemoryParcelable::readFromParcel(const Parcel* parcel) { 60 status_t status = parcel->readInt32(&mSizeInBytes); 61 if (status != NO_ERROR) { 62 return status; 63 } 64 if (mSizeInBytes > 0) { 65 mOriginalFd = parcel->readFileDescriptor(); 66 ALOGV("SharedMemoryParcelable::readFromParcel() LEAK? mOriginalFd = %d\n", mOriginalFd); 67 mFd = fcntl(mOriginalFd, F_DUPFD_CLOEXEC, 0); 68 ALOGV("SharedMemoryParcelable::readFromParcel() LEAK? mFd = %d\n", mFd); 69 if (mFd == -1) { 70 status = -errno; 71 ALOGE("SharedMemoryParcelable readFromParcel fcntl() failed : %d", status); 72 } 73 } 74 return status; 75} 76 77aaudio_result_t SharedMemoryParcelable::close() { 78 if (mResolvedAddress != MMAP_UNRESOLVED_ADDRESS) { 79 int err = munmap(mResolvedAddress, mSizeInBytes); 80 if (err < 0) { 81 ALOGE("SharedMemoryParcelable::close() munmap() failed %d", err); 82 return AAudioConvert_androidToAAudioResult(err); 83 } 84 mResolvedAddress = MMAP_UNRESOLVED_ADDRESS; 85 } 86 if (mFd != -1) { 87 ALOGV("SharedMemoryParcelable::close() LEAK? mFd = %d\n", mFd); 88 ::close(mFd); 89 mFd = -1; 90 } 91 if (mOriginalFd != -1) { 92 ALOGV("SharedMemoryParcelable::close() LEAK? mOriginalFd = %d\n", mOriginalFd); 93 ::close(mOriginalFd); 94 mOriginalFd = -1; 95 } 96 return AAUDIO_OK; 97} 98 99aaudio_result_t SharedMemoryParcelable::resolve(int32_t offsetInBytes, int32_t sizeInBytes, 100 void **regionAddressPtr) { 101 102 if (offsetInBytes < 0) { 103 ALOGE("SharedMemoryParcelable illegal offsetInBytes = %d", offsetInBytes); 104 return AAUDIO_ERROR_OUT_OF_RANGE; 105 } else if ((offsetInBytes + sizeInBytes) > mSizeInBytes) { 106 ALOGE("SharedMemoryParcelable out of range, offsetInBytes = %d, " 107 "sizeInBytes = %d, mSizeInBytes = %d", 108 offsetInBytes, sizeInBytes, mSizeInBytes); 109 return AAUDIO_ERROR_OUT_OF_RANGE; 110 } 111 if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) { 112 mResolvedAddress = (uint8_t *) mmap(0, mSizeInBytes, PROT_READ|PROT_WRITE, 113 MAP_SHARED, mFd, 0); 114 if (mResolvedAddress == MMAP_UNRESOLVED_ADDRESS) { 115 ALOGE("SharedMemoryParcelable mmap failed for fd = %d, errno = %s", 116 mFd, strerror(errno)); 117 return AAUDIO_ERROR_INTERNAL; 118 } 119 } 120 *regionAddressPtr = mResolvedAddress + offsetInBytes; 121 ALOGV("SharedMemoryParcelable mResolvedAddress = %p", mResolvedAddress); 122 ALOGV("SharedMemoryParcelable offset by %d, *regionAddressPtr = %p", 123 offsetInBytes, *regionAddressPtr); 124 return AAUDIO_OK; 125} 126 127int32_t SharedMemoryParcelable::getSizeInBytes() { 128 return mSizeInBytes; 129} 130 131aaudio_result_t SharedMemoryParcelable::validate() { 132 if (mSizeInBytes < 0 || mSizeInBytes >= MAX_MMAP_SIZE_BYTES) { 133 ALOGE("SharedMemoryParcelable invalid mSizeInBytes = %d", mSizeInBytes); 134 return AAUDIO_ERROR_OUT_OF_RANGE; 135 } 136 if (mSizeInBytes > 0) { 137 if (mFd == -1) { 138 ALOGE("SharedMemoryParcelable uninitialized mFd = %d", mFd); 139 return AAUDIO_ERROR_INTERNAL; 140 } 141 } 142 return AAUDIO_OK; 143} 144 145void SharedMemoryParcelable::dump() { 146 ALOGD("SharedMemoryParcelable mFd = %d", mFd); 147 ALOGD("SharedMemoryParcelable mSizeInBytes = %d", mSizeInBytes); 148 ALOGD("SharedMemoryParcelable mResolvedAddress = %p", mResolvedAddress); 149} 150