1 /* 2 * Copyright (C) 2013, 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 "dictionary/utils/mmapped_buffer.h" 18 19 #include <cerrno> 20 #include <climits> 21 #include <cstdio> 22 #include <fcntl.h> 23 #include <sys/mman.h> 24 #include <unistd.h> 25 26 #include "dictionary/utils/file_utils.h" 27 28 namespace latinime { 29 30 /* static */ MmappedBuffer::MmappedBufferPtr MmappedBuffer::openBuffer( 31 const char *const path, const int bufferOffset, const int bufferSize, 32 const bool isUpdatable) { 33 const int mmapFd = open(path, O_RDONLY); 34 if (mmapFd < 0) { 35 AKLOGE("DICT: Can't open the source. path=%s errno=%d", path, errno); 36 return nullptr; 37 } 38 const int pagesize = sysconf(_SC_PAGESIZE); 39 const int offset = bufferOffset % pagesize; 40 int alignedOffset = bufferOffset - offset; 41 int alignedSize = bufferSize + offset; 42 const int protMode = isUpdatable ? PROT_READ | PROT_WRITE : PROT_READ; 43 void *const mmappedBuffer = mmap(0, alignedSize, protMode, MAP_PRIVATE, mmapFd, 44 alignedOffset); 45 if (mmappedBuffer == MAP_FAILED) { 46 AKLOGE("DICT: Can't mmap dictionary. errno=%d", errno); 47 close(mmapFd); 48 return nullptr; 49 } 50 uint8_t *const buffer = static_cast<uint8_t *>(mmappedBuffer) + offset; 51 if (!buffer) { 52 AKLOGE("DICT: buffer is null"); 53 close(mmapFd); 54 return nullptr; 55 } 56 return MmappedBufferPtr(new MmappedBuffer(buffer, bufferSize, mmappedBuffer, alignedSize, 57 mmapFd, isUpdatable)); 58 } 59 60 /* static */ MmappedBuffer::MmappedBufferPtr MmappedBuffer::openBuffer( 61 const char *const path, const bool isUpdatable) { 62 const int fileSize = FileUtils::getFileSize(path); 63 if (fileSize == -1) { 64 return nullptr; 65 } else if (fileSize == 0) { 66 return MmappedBufferPtr(new MmappedBuffer(isUpdatable)); 67 } else { 68 return openBuffer(path, 0 /* bufferOffset */, fileSize, isUpdatable); 69 } 70 } 71 72 /* static */ MmappedBuffer::MmappedBufferPtr MmappedBuffer::openBuffer( 73 const char *const dirPath, const char *const fileName, const bool isUpdatable) { 74 const int filePathBufferSize = PATH_MAX + 1 /* terminator */; 75 char filePath[filePathBufferSize]; 76 const int filePathLength = snprintf(filePath, filePathBufferSize, "%s%s", dirPath, 77 fileName); 78 if (filePathLength >= filePathBufferSize) { 79 return nullptr; 80 } 81 return openBuffer(filePath, isUpdatable); 82 } 83 84 MmappedBuffer::~MmappedBuffer() { 85 if (mAlignedSize == 0) { 86 return; 87 } 88 int ret = munmap(mMmappedBuffer, mAlignedSize); 89 if (ret != 0) { 90 AKLOGE("DICT: Failure in munmap. ret=%d errno=%d", ret, errno); 91 } 92 ret = close(mMmapFd); 93 if (ret != 0) { 94 AKLOGE("DICT: Failure in close. ret=%d errno=%d", ret, errno); 95 } 96 } 97 98 } // namespace latinime 99