summaryrefslogtreecommitdiff
path: root/src-diclib/filemap.c
blob: 7b3317da25c53f455146d967090cf44d65619f66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* 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);
}