| 1 | /* |
| 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | * |
| 6 | * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V. |
| 7 | */ |
| 8 | |
| 9 | #ifndef GDK_POSIX_H |
| 10 | #define GDK_POSIX_H |
| 11 | |
| 12 | #include <sys/types.h> |
| 13 | |
| 14 | #include <time.h> |
| 15 | |
| 16 | #ifdef HAVE_FTIME |
| 17 | #include <sys/timeb.h> /* ftime */ |
| 18 | #endif |
| 19 | #ifdef HAVE_SYS_TIME_H |
| 20 | #include <sys/time.h> /* gettimeofday */ |
| 21 | #endif |
| 22 | |
| 23 | #ifdef HAVE_WINSOCK_H |
| 24 | #include <winsock.h> /* for timeval */ |
| 25 | #endif |
| 26 | |
| 27 | #include "gdk_system.h" /* gdk_export */ |
| 28 | |
| 29 | #ifdef NATIVE_WIN32 |
| 30 | #include <io.h> |
| 31 | #include <direct.h> |
| 32 | #endif |
| 33 | |
| 34 | /* |
| 35 | * @- virtual memory |
| 36 | */ |
| 37 | #define MT_VMUNITLOG 16 |
| 38 | #define MT_VMUNITSIZE (1 << MT_VMUNITLOG) |
| 39 | |
| 40 | /* make sure POSIX_MADV_* and posix_madvise() are defined somehow */ |
| 41 | #ifdef HAVE_SYS_MMAN_H |
| 42 | # ifndef __USE_BSD |
| 43 | # define __USE_BSD |
| 44 | # endif |
| 45 | # include <sys/mman.h> |
| 46 | #endif |
| 47 | |
| 48 | #ifdef __linux__ |
| 49 | /* on Linux, posix_madvise does not seem to work, fall back to classic |
| 50 | * madvise */ |
| 51 | #undef HAVE_POSIX_MADVISE |
| 52 | #undef HAVE_POSIX_FADVISE |
| 53 | #undef POSIX_MADV_NORMAL |
| 54 | #undef POSIX_MADV_RANDOM |
| 55 | #undef POSIX_MADV_SEQUENTIAL |
| 56 | #undef POSIX_MADV_WILLNEED |
| 57 | #undef POSIX_MADV_DONTNEED |
| 58 | #endif |
| 59 | |
| 60 | #ifndef HAVE_POSIX_MADVISE |
| 61 | # ifdef HAVE_MADVISE |
| 62 | # define posix_madvise madvise |
| 63 | # define HAVE_POSIX_MADVISE 1 |
| 64 | # ifndef MADV_RANDOM |
| 65 | # define MADV_RANDOM 0 |
| 66 | # endif |
| 67 | # ifndef POSIX_MADV_NORMAL |
| 68 | # define POSIX_MADV_NORMAL MADV_NORMAL |
| 69 | # define POSIX_MADV_RANDOM MADV_RANDOM |
| 70 | # define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL |
| 71 | # define POSIX_MADV_WILLNEED MADV_WILLNEED |
| 72 | # define POSIX_MADV_DONTNEED MADV_DONTNEED |
| 73 | # endif |
| 74 | # else |
| 75 | # define posix_madvise(x,y,z) 0 |
| 76 | # ifndef POSIX_MADV_NORMAL |
| 77 | # define POSIX_MADV_NORMAL 0 |
| 78 | # define POSIX_MADV_RANDOM 0 |
| 79 | # define POSIX_MADV_SEQUENTIAL 0 |
| 80 | # define POSIX_MADV_WILLNEED 0 |
| 81 | # define POSIX_MADV_DONTNEED 0 |
| 82 | # endif |
| 83 | # endif |
| 84 | #endif |
| 85 | |
| 86 | /* in case they are still not defined, define these values as |
| 87 | * something that doesn't do anything */ |
| 88 | #ifndef POSIX_MADV_NORMAL |
| 89 | #define POSIX_MADV_NORMAL 0 |
| 90 | #endif |
| 91 | #ifndef POSIX_MADV_RANDOM |
| 92 | #define POSIX_MADV_RANDOM 0 |
| 93 | #endif |
| 94 | #ifndef POSIX_MADV_SEQUENTIAL |
| 95 | #define POSIX_MADV_SEQUENTIAL 0 |
| 96 | #endif |
| 97 | #ifndef POSIX_MADV_WILLNEED |
| 98 | #define POSIX_MADV_WILLNEED 0 |
| 99 | #endif |
| 100 | #ifndef POSIX_MADV_DONTNEED |
| 101 | #define POSIX_MADV_DONTNEED 0 |
| 102 | #endif |
| 103 | |
| 104 | /* the new mmap modes, mimic default MADV_* madvise POSIX constants */ |
| 105 | #define MMAP_NORMAL POSIX_MADV_NORMAL /* no further special treatment */ |
| 106 | #define MMAP_RANDOM POSIX_MADV_RANDOM /* expect random page references */ |
| 107 | #define MMAP_SEQUENTIAL POSIX_MADV_SEQUENTIAL /* expect sequential page references */ |
| 108 | #define MMAP_WILLNEED POSIX_MADV_WILLNEED /* will need these pages */ |
| 109 | #define MMAP_DONTNEED POSIX_MADV_DONTNEED /* don't need these pages */ |
| 110 | |
| 111 | #define MMAP_READ 1024 /* region is readable (default if ommitted) */ |
| 112 | #define MMAP_WRITE 2048 /* region may be written into */ |
| 113 | #define MMAP_COPY 4096 /* writable, but changes never reach file */ |
| 114 | #define MMAP_ASYNC 8192 /* asynchronous writes (default if ommitted) */ |
| 115 | #define MMAP_SYNC 16384 /* writing is done synchronously */ |
| 116 | |
| 117 | #define MT_MMAP_LOG 27 |
| 118 | #define MT_MMAP_TILE (1<<MT_MMAP_LOG) |
| 119 | #define MT_MMAP_BUFSIZE 4096 |
| 120 | |
| 121 | /* in order to be sure of madvise and msync modes, pass them to mmap() |
| 122 | * call as well */ |
| 123 | |
| 124 | gdk_export size_t (void); |
| 125 | |
| 126 | gdk_export void *MT_mmap(const char *path, int mode, size_t len); |
| 127 | gdk_export int MT_munmap(void *p, size_t len); |
| 128 | |
| 129 | gdk_export bool MT_path_absolute(const char *path); |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * @+ Posix under WIN32 |
| 134 | * WIN32 actually supports many Posix functions directly. Some it |
| 135 | * does not, though. For some functionality we move in Monet from |
| 136 | * Posix calls to MT_*() calls, which translate easier to WIN32. |
| 137 | * Examples are MT_mmap() , MT_sleep_ms() and MT_path_absolute(). Why? |
| 138 | * In the case of mmap() it is much easier for WIN32 to get a filename |
| 139 | * parameter rather than a file-descriptor. That is the reason in the |
| 140 | * case of mmap() to go for a MT_mmap() solution. |
| 141 | * |
| 142 | * For some other functionality, we do not need to abandon the Posix |
| 143 | * interface, though. Two cases can be distinguished. Missing |
| 144 | * functions in WIN32 are directly implemented |
| 145 | * (e.g. dlopen()/dlsym()/dlclose()). Posix functions in WIN32 whose |
| 146 | * functionality should be changed a bit. Examples are |
| 147 | * stat()/rename()/mkdir()/rmdir() who under WIN32 do not work if the |
| 148 | * path ends with a directory separator, but should work according to |
| 149 | * Posix. We remap such functions using a define to an equivalent |
| 150 | * win_*() function (which in its implementation calls through to the |
| 151 | * WIN32 function). |
| 152 | */ |
| 153 | gdk_export void *mdlopen(const char *library, int mode); |
| 154 | |
| 155 | #ifdef NATIVE_WIN32 |
| 156 | |
| 157 | #define RTLD_LAZY 1 |
| 158 | #define RTLD_NOW 2 |
| 159 | #define RTLD_GLOBAL 4 |
| 160 | |
| 161 | gdk_export void *dlopen(const char *file, int mode); |
| 162 | gdk_export int dlclose(void *handle); |
| 163 | gdk_export void *dlsym(void *handle, const char *name); |
| 164 | gdk_export char *dlerror(void); |
| 165 | |
| 166 | #ifndef HAVE_GETTIMEOFDAY |
| 167 | gdk_export int gettimeofday(struct timeval *tv, int *ignore_zone); |
| 168 | #endif |
| 169 | gdk_export int win_stat(const char *, struct stat *); |
| 170 | gdk_export int win_rmdir(const char *); |
| 171 | gdk_export int win_rename(const char *, const char *); |
| 172 | gdk_export int win_unlink(const char *); |
| 173 | gdk_export int win_mkdir(const char *, const int mode); |
| 174 | |
| 175 | #define _stat64(x,y) win_stat(x,y) |
| 176 | #define mkdir win_mkdir |
| 177 | #define rmdir win_rmdir |
| 178 | #define rename win_rename |
| 179 | #define remove win_unlink |
| 180 | |
| 181 | #endif /* NATIVE_WIN32 */ |
| 182 | |
| 183 | #endif /* GDK_POSIX_H */ |
| 184 | |