1 /*************************************************************************************************
2 * The compat WIN32 API of Tokyo Cabinet
3 * Copyright (C) 2006-2008 Mikio Hirabayashi
4 * Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
5 * the GNU Lesser General Public License as published by the Free Software Foundation; either
6 * version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
7 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
9 * License for more details.
10 * You should have received a copy of the GNU Lesser General Public License along with Tokyo
11 * Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
12 * Boston, MA 02111-1307 USA.
13 *************************************************************************************************/
22 int msync(void *start, size_t length, int flags)
32 * Keep track of HANDLE for CreateView
41 static TCLIST *mmap_list=NULL;
43 static void mmap_register(HANDLE h, HANDLE mv)
45 struct mmap_priv *priv;
47 if (!tcglobalmutexlock()) {
52 mmap_list = tclistnew();
55 priv = tcmalloc(sizeof(struct mmap_priv));
58 tclistpush(mmap_list, priv, sizeof(priv));
60 tcglobalmutexunlock();
64 static HANDLE mmap_unregister(HANDLE mv)
67 struct mmap_priv *priv;
71 if (!tcglobalmutexlock()) {
75 max = tclistnum(mmap_list);
76 for(i=0; nok && i<max; i++) {
77 priv = (struct mmap_priv *)tclistval(mmap_list, i, &size);
78 if (priv && (priv->mv == mv)) {
79 tclistremove(mmap_list, i, &size);
86 tcglobalmutexunlock();
93 * Emulation of mmap and unmmap for tokyo dbm
95 void *mmap(void *start, size_t length, int prot, int flags,
110 if (flags & PROT_WRITE) {
111 fm_access |= PAGE_READWRITE;
112 } else if (flags & PROT_READ) {
113 fm_access |= PAGE_READONLY;
116 if (flags & PROT_READ) {
117 mv_access |= FILE_MAP_READ;
119 if (flags & PROT_WRITE) {
120 mv_access |= FILE_MAP_WRITE;
123 h = CreateFileMapping((HANDLE)_get_osfhandle (fd),
126 0 /* MaximumSizeHigh */,
127 0 /* MaximumSizeLow */,
128 NULL /* name of the file mapping object */);
130 if (!h || h == INVALID_HANDLE_VALUE) {
134 mv = MapViewOfFile(h, mv_access,
138 if (!mv || mv == INVALID_HANDLE_VALUE) {
143 mmap_register(h, mv);
147 int munmap(void *start, size_t length)
153 h = mmap_unregister(start);
154 UnmapViewOfFile(start);
159 int regcomp(regex_t *preg, const char *regex, int cflags)
164 int regexec(const regex_t *preg, const char *string, size_t nmatch, regmatch_t pmatch[], int eflags)
169 size_t regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
175 int gettimeofday(struct timeval *tv, struct timezone *tz)
180 void regfree(regex_t *preg)
184 int glob(const char *pattern, int flags,
185 int (*errfunc) (const char *epath, int eerrno),
190 void globfree(glob_t *pglob)
194 char *realpath(const char *path, char *resolved_path)
196 strcpy(resolved_path, path);
197 return resolved_path;
201 int rand_r(unsigned int *seedp)
203 *seedp = *seedp * 1103515245 + 12345;
204 return((unsigned)(*seedp/65536) % 32768);
208 /* read from a file descriptor at a given offset */
209 ssize_t pread(int fd, void *buf, size_t count, off_t offset)
214 if ((cur_pos = _lseeki64(fd, 0, SEEK_CUR)) == (off_t)-1)
217 if (_lseeki64(fd, offset, SEEK_SET) == (off_t)-1)
220 num_read = read(fd, buf, count);
222 if (_lseeki64(fd, cur_pos, SEEK_SET) == (off_t)-1)
228 /* write to a file descriptor at a given offset */
229 ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset)
234 if ((cur_pos = _lseeki64(fd, 0, SEEK_CUR)) == (off_t)-1)
237 if (_lseeki64(fd, offset, SEEK_SET) == (off_t)-1)
240 num_write = write(fd, buf, count);
242 if (_lseeki64(fd, cur_pos, SEEK_SET) == (off_t)-1)