1/* 2 * Copyright (C) 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#include "AshmemMapper.h" 18 19#include <sys/mman.h> 20 21#include "AshmemMemory.h" 22 23namespace android { 24namespace hidl { 25namespace memory { 26namespace V1_0 { 27namespace implementation { 28 29// Methods from ::android::hidl::memory::V1_0::IMapper follow. 30Return<sp<IMemory>> AshmemMapper::mapMemory(const hidl_memory& mem) { 31 if (mem.handle()->numFds == 0) { 32 return nullptr; 33 } 34 35 int fd = mem.handle()->data[0]; 36 void* data = mmap(0, mem.size(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); 37 if (data == MAP_FAILED) { 38 // mmap never maps at address zero without MAP_FIXED, so we can avoid 39 // exposing clients to MAP_FAILED. 40 return nullptr; 41 } 42 43 return new AshmemMemory(mem, data); 44} 45 46} // namespace implementation 47} // namespace V1_0 48} // namespace memory 49} // namespace hidl 50} // namespace android 51