Skip to content

Commit

Permalink
Resolve mutual dependency of base/mmap.
Browse files Browse the repository at this point in the history
Move SystemUtil::MaybeMLock and SystemUtil::MaybeMUnlock to Mmap
to resolve the mutual dependency of base/mmap on base/file_util
and base/system_util.

By this CL, base/mmap becomes a single build target.

BUG=none
TEST=unittest
  • Loading branch information
Noriyuki Takahashi authored and yukawa committed May 30, 2015
1 parent 2ceaee9 commit 5afb412
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 84 deletions.
1 change: 0 additions & 1 deletion src/base/file_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@

#include "base/file_stream.h"
#include "base/logging.h"
#include "base/mmap.h"
#include "base/number_util.h"
#include "base/util.h"
#include "testing/base/public/gunit.h"
Expand Down
26 changes: 23 additions & 3 deletions src/base/mmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

#include "base/port.h"
#include "base/logging.h"
#include "base/system_util.h"
#include "base/util.h"

#ifdef OS_WIN
Expand Down Expand Up @@ -181,15 +180,15 @@ bool Mmap::Open(const char *filename, const char *mode) {
return false;
}

SystemUtil::MaybeMLock(ptr, size_);
MaybeMLock(ptr, size_);
text_ = reinterpret_cast<char *>(ptr);
size_ = st.st_size;
return true;
}

void Mmap::Close() {
if (text_ != NULL) {
SystemUtil::MaybeMUnlock(text_, size_);
MaybeMUnlock(text_, size_);
munmap(reinterpret_cast<char *>(text_), size_);
}

Expand Down Expand Up @@ -256,4 +255,25 @@ bool Mmap::SyncToFile() {
}
#endif // MOZC_USE_PEPPER_FILE_IO

int Mmap::MaybeMLock(const void *addr, size_t len) {
// TODO(yukawa): Integrate mozc_cache service.
#if defined(OS_WIN) || defined(OS_ANDROID) || defined(__native_client__)
return -1;
#else // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
return mlock(addr, len);
#endif // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
}

int Mmap::MaybeMUnlock(const void *addr, size_t len) {
#if defined(OS_WIN) || defined(OS_ANDROID) || defined(__native_client__)
return -1;
#else // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
return munlock(addr, len);
#endif // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
}

} // namespace mozc
22 changes: 19 additions & 3 deletions src/base/mmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,30 @@
#include "base/port.h"

namespace mozc {

class Mmap {
public:
Mmap();
~Mmap() {
Close();
}
~Mmap() { Close(); }

bool Open(const char *filename, const char *mode = "r");
void Close();

// Following mlock/munlock related functions work based on target environment.
// In the case of Android, Native Client, Windows, we don't want to call
// actual functions, so these functions do nothing and return -1. In other
// cases, these functions call actual mlock/munlock functions and return it's
// result.
// On Android, page-out is probably acceptable because
// - Smaller RAM on the device.
// - The storage is (usually) solid state thus page-in/out is expected to
// be faster.
// On Linux, in the kernel version >= 2.6.9, user process can mlock. In older
// kernel, it fails if the process is running in user priviledge.
// TODO(team): Check in mac that mlock is really necessary.
static int MaybeMLock(const void *addr, size_t len);
static int MaybeMUnlock(const void *addr, size_t len);

#ifndef MOZC_USE_PEPPER_FILE_IO
char &operator[](size_t n) { return *(text_ + n); }
char operator[](size_t n) const { return *(text_ + n); }
Expand Down Expand Up @@ -82,6 +97,7 @@ class Mmap {

DISALLOW_COPY_AND_ASSIGN(Mmap);
};

} // namespace mozc

#endif // MOZC_BASE_MMAP_H_
32 changes: 32 additions & 0 deletions src/base/mmap_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,37 @@ TEST(MmapTest, MmapTest) {
FileUtil::Unlink(filename);
}
}

#if defined(OS_WIN)
TEST(MmapTest, WindowsMaybeMLockTest) {
size_t data_len = 32;
void *addr = malloc(data_len);
EXPECT_EQ(-1, Mmap::MaybeMLock(addr, data_len));
EXPECT_EQ(-1, Mmap::MaybeMUnlock(addr, data_len));
free(addr);
}
#elif defined(OS_MACOSX)
TEST(MmapTest, MacMaybeMLockTest) {
size_t data_len = 32;
void *addr = malloc(data_len);
EXPECT_EQ(0, Mmap::MaybeMLock(addr, data_len));
EXPECT_EQ(0, Mmap::MaybeMUnlock(addr, data_len));
free(addr);
}
#else
TEST(MmapTest, LinuxMaybeMLockTest) {
size_t data_len = 32;
void *addr = malloc(data_len);
#if defined(OS_ANDROID) || defined(__native_client__)
EXPECT_EQ(-1, Mmap::MaybeMLock(addr, data_len));
EXPECT_EQ(-1, Mmap::MaybeMUnlock(addr, data_len));
#else
EXPECT_EQ(0, Mmap::MaybeMLock(addr, data_len));
EXPECT_EQ(0, Mmap::MaybeMUnlock(addr, data_len));
#endif // defined(OS_ANDROID) || defined(__native_client__)
free(addr);
}
#endif // OS_WIN, OS_MACOSX, else.

} // namespace
} // namespace mozc
24 changes: 3 additions & 21 deletions src/base/system_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@
#include "base/win_util.h"

#ifdef OS_ANDROID
// HACK to avoid a bug in sysconf in android.
#include "android/jni/sysconf.h"
#define sysconf mysysconf
#include "base/android_util.h"
#endif // OS_ANDROID

Expand Down Expand Up @@ -1072,25 +1075,4 @@ bool SystemUtil::IsLittleEndian() {
#endif // OS_WIN
}

int SystemUtil::MaybeMLock(const void *addr, size_t len) {
// TODO(yukawa): Integrate mozc_cache service.
#if defined(OS_WIN) || defined(OS_ANDROID) || defined(__native_client__)
return -1;
#else // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
return mlock(addr, len);
#endif // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
}

int SystemUtil::MaybeMUnlock(const void *addr, size_t len) {
#if defined(OS_WIN) || defined(OS_ANDROID) || defined(__native_client__)
return -1;
#else // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
return munlock(addr, len);
#endif // defined(OS_WIN) || defined(OS_ANDROID) ||
// defined(__native_client__)
}

} // namespace mozc
16 changes: 0 additions & 16 deletions src/base/system_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,22 +209,6 @@ class SystemUtil {
// check endian-ness at runtime.
static bool IsLittleEndian();

// Following mlock/munlock related functions work based on target environment.
// In the case of Android, Native Client, Windows, we don't want to call
// actual functions, so these functions do nothing and return -1. In other
// cases, these functions call actual mlock/munlock functions and return it's
// result.
// On Android, page-out is probably acceptable because
// - Smaller RAM on the device.
// - The storage is (usually) solid state thus page-in/out is expected to
// be faster.
// On Linux, in the kernel version >= 2.6.9, user process can mlock. In older
// kernel, it fails if the process is running in user priviledge.
// TODO(horo): Check in mac that mlock is really necessary.
static int MaybeMLock(const void *addr, size_t len);

static int MaybeMUnlock(const void *addr, size_t len);

private:
DISALLOW_IMPLICIT_CONSTRUCTORS(SystemUtil);
};
Expand Down
35 changes: 0 additions & 35 deletions src/base/system_util_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,39 +239,4 @@ TEST_F(SystemUtilTest, GetOSVersionStringTestForAndroid) {
}
#endif // OS_ANDROID

#ifdef OS_WIN
TEST_F(SystemUtilTest, WindowsMaybeMLockTest) {
size_t data_len = 32;
void *addr = malloc(data_len);
EXPECT_EQ(-1, SystemUtil::MaybeMLock(addr, data_len));
EXPECT_EQ(-1, SystemUtil::MaybeMUnlock(addr, data_len));
free(addr);
}
#endif // OS_WIN

#ifdef OS_MACOSX
TEST_F(SystemUtilTest, MacMaybeMLockTest) {
size_t data_len = 32;
void *addr = malloc(data_len);
EXPECT_EQ(0, SystemUtil::MaybeMLock(addr, data_len));
EXPECT_EQ(0, SystemUtil::MaybeMUnlock(addr, data_len));
free(addr);
}
#endif // OS_MACOSX

TEST_F(SystemUtilTest, LinuxMaybeMLockTest) {
size_t data_len = 32;
void *addr = malloc(data_len);
#ifdef OS_LINUX
#if defined(OS_ANDROID) || defined(__native_client__)
EXPECT_EQ(-1, SystemUtil::MaybeMLock(addr, data_len));
EXPECT_EQ(-1, SystemUtil::MaybeMUnlock(addr, data_len));
#else
EXPECT_EQ(0, SystemUtil::MaybeMLock(addr, data_len));
EXPECT_EQ(0, SystemUtil::MaybeMUnlock(addr, data_len));
#endif // defined(OS_ANDROID) || defined(__native_client__)
#endif // OS_LINUX
free(addr);
}

} // namespace mozc
4 changes: 2 additions & 2 deletions src/dictionary/system/system_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
#include <vector>

#include "base/logging.h"
#include "base/mmap.h"
#include "base/port.h"
#include "base/string_piece.h"
#include "base/system_util.h"
#include "base/util.h"
#include "dictionary/dictionary_token.h"
#include "dictionary/file/dictionary_file.h"
Expand Down Expand Up @@ -598,7 +598,7 @@ SystemDictionary *SystemDictionary::Builder::Build() {
// has the priviledge to mlock.
// Note that we don't munlock the space because it's always better to keep
// the singleton system dictionary paged in as long as the process runs.
SystemUtil::MaybeMLock(spec_->ptr, spec_->len);
Mmap::MaybeMLock(spec_->ptr, spec_->len);
if (!instance->dictionary_file_->OpenFromImage(spec_->ptr, spec_->len)) {
LOG(ERROR) << "Failed to open system dictionary image";
return NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/dictionary/system/value_dictionary.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
#include <string>

#include "base/logging.h"
#include "base/mmap.h"
#include "base/port.h"
#include "base/string_piece.h"
#include "base/system_util.h"
#include "dictionary/dictionary_token.h"
#include "dictionary/file/dictionary_file.h"
#include "dictionary/pos_matcher.h"
Expand Down Expand Up @@ -80,7 +80,7 @@ ValueDictionary *ValueDictionary::CreateValueDictionaryFromImage(
// has the priviledge to mlock.
// Note that we don't munlock the space because it's always better to keep
// the singleton system dictionary paged in as long as the process runs.
SystemUtil::MaybeMLock(ptr, len);
Mmap::MaybeMLock(ptr, len);
scoped_ptr<ValueDictionary> instance(new ValueDictionary(pos_matcher));
DCHECK(instance.get());
if (!instance->dictionary_file_->OpenFromImage(ptr, len)) {
Expand Down
2 changes: 1 addition & 1 deletion src/mozc_version_template.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MAJOR=2
MINOR=17
BUILD=2097
BUILD=2098
REVISION=102
# NACL_DICTIONARY_VERSION is the target version of the system dictionary to be
# downloaded by NaCl Mozc.
Expand Down

0 comments on commit 5afb412

Please sign in to comment.