Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: third_party/android_prediction/suggest/policyimpl/dictionary/utils/mmapped_buffer.cpp

Issue 1247903003: Add spellcheck and word suggestion to the prediction service (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: changed third_party/prediction to third_party/android_prediction; added CHROMIUM.diff Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 "third_party/android_prediction/suggest/policyimpl/dictionary/utils/mma pped_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 "third_party/android_prediction/suggest/policyimpl/dictionary/utils/fil e_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, mmap Fd,
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 isUpda table) {
74 const int filePathBufferSize = PATH_MAX + 1 /* terminator */;
75 char filePath[filePathBufferSize];
76 const int filePathLength = snprintf(filePath, filePathBufferSize, "%s%s", di rPath,
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698