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

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

Powered by Google App Engine
This is Rietveld 408576698