Line data Source code
1 : /* 2 : * SPDX-License-Identifier: MPL-2.0 3 : * 4 : * This Source Code Form is subject to the terms of the Mozilla Public 5 : * License, v. 2.0. If a copy of the MPL was not distributed with this 6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. 7 : * 8 : * Copyright 2024 MonetDB Foundation; 9 : * Copyright August 2008 - 2023 MonetDB B.V.; 10 : * Copyright 1997 - July 2008 CWI. 11 : */ 12 : 13 : #ifndef GDK_POSIX_H 14 : #define GDK_POSIX_H 15 : 16 : #include <sys/types.h> 17 : 18 : #include <time.h> 19 : 20 : #ifdef HAVE_FTIME 21 : #include <sys/timeb.h> /* ftime */ 22 : #endif 23 : #ifdef HAVE_SYS_TIME_H 24 : #include <sys/time.h> /* gettimeofday */ 25 : #endif 26 : 27 : #ifndef HAVE_SYS_SOCKET_H 28 : #ifdef HAVE_WINSOCK_H 29 : #include <winsock.h> /* for timeval */ 30 : #endif 31 : #endif 32 : 33 : #include "gdk_system.h" /* gdk_export */ 34 : 35 : #ifdef NATIVE_WIN32 36 : #include <io.h> 37 : #include <direct.h> 38 : #endif 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 : /* in order to be sure of madvise and msync modes, pass them to mmap() 118 : * call as well */ 119 : 120 : gdk_export size_t MT_getrss(void); 121 : 122 : gdk_export bool MT_path_absolute(const char *path); 123 : 124 : 125 : /* 126 : * @+ Posix under WIN32 127 : * WIN32 actually supports many Posix functions directly. Some it 128 : * does not, though. For some functionality we move in Monet from 129 : * Posix calls to MT_*() calls, which translate easier to WIN32. 130 : * Examples are MT_mmap() , MT_sleep_ms() and MT_path_absolute(). Why? 131 : * In the case of mmap() it is much easier for WIN32 to get a filename 132 : * parameter rather than a file-descriptor. That is the reason in the 133 : * case of mmap() to go for a MT_mmap() solution. 134 : * 135 : * For some other functionality, we do not need to abandon the Posix 136 : * interface, though. Two cases can be distinguished. Missing 137 : * functions in WIN32 are directly implemented 138 : * (e.g. dlopen()/dlsym()/dlclose()). Posix functions in WIN32 whose 139 : * functionality should be changed a bit. Examples are 140 : * stat()/rename()/mkdir()/rmdir() who under WIN32 do not work if the 141 : * path ends with a directory separator, but should work according to 142 : * Posix. We remap such functions using a define to an equivalent 143 : * win_*() function (which in its implementation calls through to the 144 : * WIN32 function). 145 : */ 146 : gdk_export void *mdlopen(const char *library, int mode); 147 : 148 : 149 : #ifdef NATIVE_WIN32 150 : 151 : #define RTLD_LAZY 1 152 : #define RTLD_NOW 2 153 : #define RTLD_GLOBAL 4 154 : 155 : gdk_export void *dlopen(const char *file, int mode); 156 : gdk_export int dlclose(void *handle); 157 : gdk_export void *dlsym(void *handle, const char *name); 158 : gdk_export char *dlerror(void); 159 : 160 : #ifndef HAVE_GETTIMEOFDAY 161 : gdk_export int gettimeofday(struct timeval *tv, int *ignore_zone); 162 : #endif 163 : 164 : #endif /* NATIVE_WIN32 */ 165 : 166 : #ifndef HAVE_LOCALTIME_R 167 : gdk_export struct tm *localtime_r(const time_t *restrict, struct tm *restrict); 168 : #endif 169 : #ifndef HAVE_GMTIME_R 170 : gdk_export struct tm *gmtime_r(const time_t *restrict, struct tm *restrict); 171 : #endif 172 : #ifndef HAVE_ASCTIME_R 173 : gdk_export char *asctime_r(const struct tm *restrict, char *restrict); 174 : #endif 175 : #ifndef HAVE_CTIME_R 176 : gdk_export char *ctime_r(const time_t *restrict, char *restrict); 177 : #endif 178 : #ifndef HAVE_STRERROR_R 179 : gdk_export int strerror_r(int errnum, char *buf, size_t buflen); 180 : #endif 181 : 182 : static inline const char * 183 1 : GDKstrerror(int errnum, char *buf, size_t buflen) 184 : { 185 : #if !defined(_GNU_SOURCE) || ((_POSIX_C_SOURCE >= 200112L) && !_GNU_SOURCE) 186 : if (strerror_r(errnum, buf, buflen) == 0) 187 : return buf; 188 : snprintf(buf, buflen, "Unknown error %d", errnum); 189 : return buf; 190 : #else 191 1 : return strerror_r(errnum, buf, buflen); 192 : #endif 193 : } 194 : 195 : #endif /* GDK_POSIX_H */