NIIBE Yutaka wrote:
> 過去に実装が試みられて捨てられた texttrie と呼ばれていたものの削除です。
> janitor/remove-texttrie ブランチとしました。

これで mmap しているのが file_dic.c だけになったので、ここから変更する
ブランチとして janitor/mmap-readonly を作り下記の変更をコミットしました。

2010-08-03  NIIBE Yutaka  <gni...@fsij.org>

        * anthy/Makefile.am (noinst_HEADERS): Removed filemap.h.
        * anthy/filemap.h: Removed.

        * src-diclib/Makefile.am (libdiclib_la_SOURCES): Removed
          filemap.c.
        * src-diclib/filemap.c: Removed.
        * src-diclib/file_dic.c (struct file_dic): Directly have PTR
          ans SIZE.
        (anthy_mmap): Moved here (was: filemap.c).
        (anthy_quit_file_dic): Call munmap directly.

        * src-splitter/depgraph.c: Delete #include <anthy/filemap.h>.

diff --git a/AUTHORS b/AUTHORS
index 6c1a043..a78a5c8 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -7,7 +7,6 @@ TABATA Yusuke: wrote
                mkworddic/writewords.c
                src-diclib/alloc.c
                src-diclib/conf.c
-               src-diclib/filemap.c
                src-diclib/ruleparser.c
                src-diclib/xchar.c
                src-diclib/xstr.c
diff --git a/anthy/Makefile.am b/anthy/Makefile.am
index ac28fc5..fc4c6ff 100644
--- a/anthy/Makefile.am
+++ b/anthy/Makefile.am
@@ -3,7 +3,6 @@ noinst_HEADERS = xstr.h xchar.h dic.h wtype.h\
  conf.h record.h alloc.h\
  ruleparser.h splitter.h \
  segment.h ordering.h \
- filemap.h \
  logger.h segclass.h \
  depgraph.h \
  textdic.h matrix.h \
diff --git a/anthy/filemap.h b/anthy/filemap.h
deleted file mode 100644
index 867687c..0000000
--- a/anthy/filemap.h
+++ /dev/null
@@ -1,14 +0,0 @@
-/* mmapを抽象化する */
-#ifndef _filemap_h_included_
-#define _filemap_h_included_
-
-/* メモリ上にmapされたファイルのハンドル */
-struct filemapping;
-
-struct filemapping *anthy_mmap(const char *fn, int wr);
-void *anthy_mmap_address(struct filemapping *m);
-int anthy_mmap_size(struct filemapping *m);
-int anthy_mmap_is_writable(struct filemapping *m);
-void anthy_munmap(struct filemapping *m);
-
-#endif
diff --git a/src-diclib/Makefile.am b/src-diclib/Makefile.am
index 619669a..2f816b1 100644
--- a/src-diclib/Makefile.am
+++ b/src-diclib/Makefile.am
@@ -2,7 +2,7 @@ INCLUDES = -I$(top_srcdir)/ -DCONF_DIR=\"$(sysconfdir)\"

 libdiclib_la_SOURCES = \
        diclib.c \
-       file_dic.c filemap.c \
+       file_dic.c \
        xstr.c xchar.c \
        alloc.c conf.c \
        logger.c \
diff --git a/src-diclib/file_dic.c b/src-diclib/file_dic.c
index 0df30e8..abae248 100644
--- a/src-diclib/file_dic.c
+++ b/src-diclib/file_dic.c
@@ -1,8 +1,12 @@
 #include <stdio.h>
 #include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>

 #include <anthy/diclib.h>
-#include <anthy/filemap.h>
 #include <anthy/alloc.h>
 #include <anthy/conf.h>
 #include <anthy/logger.h>
@@ -13,7 +17,8 @@
  */
 struct file_dic
 {
-  struct filemapping *mapping;
+  void *ptr;
+  size_t size;
 };

 static struct file_dic fdic;
@@ -22,7 +27,7 @@ void*
 anthy_file_dic_get_section(const char* section_name)
 {
   int i;
-  char* head = anthy_mmap_address(fdic.mapping);
+  char* head = (char *)fdic.ptr;
   int* p = (int*)head;
   int entry_num = anthy_dic_ntohl(*p++);

@@ -37,6 +42,42 @@ anthy_file_dic_get_section(const char* section_name)
   return NULL;
 }

+static int
+anthy_mmap (const char *fn)
+{
+  int fd;
+  void *ptr;
+  struct stat st;
+
+  fd = open (fn, O_RDONLY);
+  if (fd == -1) {
+    anthy_log(0, "Failed to open (%s).\n", fn);
+    return -1;
+  }
+
+  if (fstat(fd, &st) < 0) {
+    anthy_log(0, "Failed to stat() (%s).\n", fn);
+    close(fd);
+    return -1;
+  }
+  if (st.st_size == 0) {
+    anthy_log(0, "Failed to mmap 0size file (%s).\n", fn);
+    close(fd);
+    return -1;
+  }
+
+  ptr = mmap (NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+  close(fd);
+  if (ptr == MAP_FAILED) {
+    anthy_log(0, "Failed to mmap() (%s).\n", fn);
+    return -1;
+  }
+
+  fdic.ptr = ptr;
+  fdic.size = st.st_size;
+  return 0;
+}
+
 int
 anthy_init_file_dic(void)
 {
@@ -48,8 +89,7 @@ anthy_init_file_dic(void)
   }

   /* 辞書をメモリ上にmapする */
-  fdic.mapping = anthy_mmap(fn, 0);
-  if (!fdic.mapping) {
+  if (anthy_mmap (fn) < 0) {
     anthy_log(0, "failed to init file dic.\n");
     return -1;
   }
@@ -60,6 +100,6 @@ anthy_init_file_dic(void)
 void
 anthy_quit_file_dic(void)
 {
-  anthy_munmap(fdic.mapping);
+  munmap (fdic.ptr, fdic.size);
 }

diff --git a/src-diclib/filemap.c b/src-diclib/filemap.c
deleted file mode 100644
index 7b3317d..0000000
--- a/src-diclib/filemap.c
+++ /dev/null
@@ -1,135 +0,0 @@
-/* mmapを抽象化する
- *
- * *Unix系のシステムコールをソース中に散らさないため
- * *将来的には一つのファイルを複数の目的にmapすることも考慮
- *
- * Copyright (C) 2005 TABATA Yusuke
- *
- */
-/*
-  This library is free software; you can redistribute it and/or
-  modify it under the terms of the GNU Lesser General Public
-  License as published by the Free Software Foundation; either
-  version 2 of the License, or (at your option) any later version.
-
-  This library is distributed in the hope that it will be useful,
-  but WITHOUT ANY WARRANTY; without even the implied warranty of
-  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-  Lesser General Public License for more details.
-
-  You should have received a copy of the GNU Lesser General Public
-  License along with this library; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
- */
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <stdlib.h>
-
-#include <anthy/filemap.h>
-#include <anthy/logger.h>
-
-struct filemapping {
-  /* 書き込みモード */
-  int wr;
-  /* mmapしたアドレス */
-  void *ptr;
-  /* mmapした領域の長さ */
-  size_t size;
-};
-
-struct filemapping *
-anthy_mmap(const char *fn, int wr)
-{
-  int fd;
-  void *ptr;
-  int r;
-  struct filemapping *m;
-  struct stat st;
-  int prot;
-  int flags;
-  mode_t mode;
-
-  if (wr) {
-    prot = PROT_READ | PROT_WRITE;
-    flags = O_RDWR;
-    mode = S_IRUSR | S_IWUSR;
-  } else {
-    prot = PROT_READ;
-    flags = O_RDONLY;
-    mode = S_IRUSR;
-  }
-
-  fd = open(fn, flags, mode);
-
-  if (fd == -1) {
-    anthy_log(0, "Failed to open (%s).\n", fn);
-    return NULL;
-  }
-
-  r = fstat(fd, &st);
-  if (r == -1) {
-    anthy_log(0, "Failed to stat() (%s).\n", fn);
-    close(fd);
-    return NULL;
-  }
-  if (st.st_size == 0) {
-    anthy_log(0, "Failed to mmap 0size file (%s).\n", fn);
-    close(fd);
-    return NULL;
-  }
-
-  ptr = mmap(NULL, st.st_size, prot, MAP_SHARED, fd, 0);
-  close(fd);
-  if (ptr == MAP_FAILED) {
-    anthy_log(0, "Failed to mmap() (%s).\n", fn);
-    return NULL;
-  }
-
-  /* mmapに成功したので情報を返す */
-  m = malloc(sizeof(struct filemapping));
-  m->size = st.st_size;
-  m->ptr = ptr;
-  m->wr = wr;
-
-  return m;
-}
-
-void *
-anthy_mmap_address(struct filemapping *m)
-{
-  if (!m) {
-    return NULL;
-  }
-  return m->ptr;
-}
-
-int
-anthy_mmap_size(struct filemapping *m)
-{
-  if (!m) {
-    return 0;
-  }
-  return m->size;
-}
-
-int
-anthy_mmap_is_writable(struct filemapping *m)
-{
-  if (!m) {
-    return 0;
-  }
-  return m->wr;
-}
-
-void
-anthy_munmap(struct filemapping *m)
-{
-  if (!m) {
-    return ;
-  }
-  munmap(m->ptr, m->size);
-  free(m);
-}
diff --git a/src-splitter/depgraph.c b/src-splitter/depgraph.c
index d01bcd4..700c849 100644
--- a/src-splitter/depgraph.c
+++ b/src-splitter/depgraph.c
@@ -29,7 +29,6 @@
 #include <anthy/conf.h>
 #include <anthy/ruleparser.h>
 #include <anthy/xstr.h>
-#include <anthy/filemap.h>
 #include <anthy/logger.h>
 #include <anthy/segclass.h>
 #include <anthy/splitter.h>

_______________________________________________
Anthy-dev mailing list
Anthy-dev@lists.sourceforge.jp
http://lists.sourceforge.jp/mailman/listinfo/anthy-dev

メールによる返信