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 : #ifdef NATIVE_WIN32 34 : #include <io.h> 35 : #include <direct.h> 36 : #endif 37 : 38 : /* make sure POSIX_MADV_* and posix_madvise() are defined somehow */ 39 : #ifdef HAVE_SYS_MMAN_H 40 : # ifndef __USE_BSD 41 : # define __USE_BSD 42 : # endif 43 : # include <sys/mman.h> 44 : #endif 45 : 46 : #ifdef __linux__ 47 : /* on Linux, posix_madvise does not seem to work, fall back to classic 48 : * madvise */ 49 : #undef HAVE_POSIX_MADVISE 50 : #undef HAVE_POSIX_FADVISE 51 : #undef POSIX_MADV_NORMAL 52 : #undef POSIX_MADV_RANDOM 53 : #undef POSIX_MADV_SEQUENTIAL 54 : #undef POSIX_MADV_WILLNEED 55 : #undef POSIX_MADV_DONTNEED 56 : #endif 57 : 58 : #ifndef HAVE_POSIX_MADVISE 59 : # ifdef HAVE_MADVISE 60 : # define posix_madvise madvise 61 : # define HAVE_POSIX_MADVISE 1 62 : # ifndef MADV_RANDOM 63 : # define MADV_RANDOM 0 64 : # endif 65 : # ifndef POSIX_MADV_NORMAL 66 : # define POSIX_MADV_NORMAL MADV_NORMAL 67 : # define POSIX_MADV_RANDOM MADV_RANDOM 68 : # define POSIX_MADV_SEQUENTIAL MADV_SEQUENTIAL 69 : # define POSIX_MADV_WILLNEED MADV_WILLNEED 70 : # define POSIX_MADV_DONTNEED MADV_DONTNEED 71 : # endif 72 : # else 73 : # define posix_madvise(x,y,z) 0 74 : # ifndef POSIX_MADV_NORMAL 75 : # define POSIX_MADV_NORMAL 0 76 : # define POSIX_MADV_RANDOM 0 77 : # define POSIX_MADV_SEQUENTIAL 0 78 : # define POSIX_MADV_WILLNEED 0 79 : # define POSIX_MADV_DONTNEED 0 80 : # endif 81 : # endif 82 : #endif 83 : 84 : /* in case they are still not defined, define these values as 85 : * something that doesn't do anything */ 86 : #ifndef POSIX_MADV_NORMAL 87 : #define POSIX_MADV_NORMAL 0 88 : #endif 89 : #ifndef POSIX_MADV_RANDOM 90 : #define POSIX_MADV_RANDOM 0 91 : #endif 92 : #ifndef POSIX_MADV_SEQUENTIAL 93 : #define POSIX_MADV_SEQUENTIAL 0 94 : #endif 95 : #ifndef POSIX_MADV_WILLNEED 96 : #define POSIX_MADV_WILLNEED 0 97 : #endif 98 : #ifndef POSIX_MADV_DONTNEED 99 : #define POSIX_MADV_DONTNEED 0 100 : #endif 101 : 102 : /* the new mmap modes, mimic default MADV_* madvise POSIX constants */ 103 : #define MMAP_NORMAL POSIX_MADV_NORMAL /* no further special treatment */ 104 : #define MMAP_RANDOM POSIX_MADV_RANDOM /* expect random page references */ 105 : #define MMAP_SEQUENTIAL POSIX_MADV_SEQUENTIAL /* expect sequential page references */ 106 : #define MMAP_WILLNEED POSIX_MADV_WILLNEED /* will need these pages */ 107 : #define MMAP_DONTNEED POSIX_MADV_DONTNEED /* don't need these pages */ 108 : 109 : #define MMAP_READ 1024 /* region is readable (default if omitted) */ 110 : #define MMAP_WRITE 2048 /* region may be written into */ 111 : #define MMAP_COPY 4096 /* writable, but changes never reach file */ 112 : #define MMAP_ASYNC 8192 /* asynchronous writes (default if omitted) */ 113 : #define MMAP_SYNC 16384 /* writing is done synchronously */ 114 : 115 : /* in order to be sure of madvise and msync modes, pass them to mmap() 116 : * call as well */ 117 : 118 : gdk_export size_t MT_getrss(void); 119 : 120 : gdk_export bool MT_path_absolute(const char *path); 121 : 122 : 123 : /* 124 : * @+ Posix under WIN32 125 : * WIN32 actually supports many Posix functions directly. Some it 126 : * does not, though. For some functionality we move in Monet from 127 : * Posix calls to MT_*() calls, which translate easier to WIN32. 128 : * Examples are MT_mmap() , MT_sleep_ms() and MT_path_absolute(). Why? 129 : * In the case of mmap() it is much easier for WIN32 to get a filename 130 : * parameter rather than a file-descriptor. That is the reason in the 131 : * case of mmap() to go for a MT_mmap() solution. 132 : * 133 : * For some other functionality, we do not need to abandon the Posix 134 : * interface, though. Two cases can be distinguished. Missing 135 : * functions in WIN32 are directly implemented 136 : * (e.g. dlopen()/dlsym()/dlclose()). Posix functions in WIN32 whose 137 : * functionality should be changed a bit. Examples are 138 : * stat()/rename()/mkdir()/rmdir() who under WIN32 do not work if the 139 : * path ends with a directory separator, but should work according to 140 : * Posix. We remap such functions using a define to an equivalent 141 : * win_*() function (which in its implementation calls through to the 142 : * WIN32 function). 143 : */ 144 : gdk_export void *mdlopen(const char *library, int mode); 145 : 146 : 147 : #ifdef NATIVE_WIN32 148 : 149 : #define RTLD_LAZY 1 150 : #define RTLD_NOW 2 151 : #define RTLD_GLOBAL 4 152 : 153 : gdk_export void *dlopen(const char *file, int mode); 154 : gdk_export int dlclose(void *handle); 155 : gdk_export void *dlsym(void *handle, const char *name); 156 : gdk_export char *dlerror(void); 157 : 158 : #ifndef HAVE_GETTIMEOFDAY 159 : gdk_export int gettimeofday(struct timeval *tv, int *ignore_zone); 160 : #endif 161 : 162 : #endif /* NATIVE_WIN32 */ 163 : 164 : #ifndef HAVE_LOCALTIME_R 165 : gdk_export struct tm *localtime_r(const time_t *restrict, struct tm *restrict); 166 : #endif 167 : #ifndef HAVE_GMTIME_R 168 : gdk_export struct tm *gmtime_r(const time_t *restrict, struct tm *restrict); 169 : #endif 170 : #ifndef HAVE_ASCTIME_R 171 : gdk_export char *asctime_r(const struct tm *restrict, char *restrict); 172 : #endif 173 : #ifndef HAVE_CTIME_R 174 : gdk_export char *ctime_r(const time_t *restrict, char *restrict); 175 : #endif 176 : #if !defined(HAVE_STRERROR_R) && !defined(HAVE_STRERROR_S) 177 : gdk_export int strerror_r(int errnum, char *buf, size_t buflen); 178 : #endif 179 : 180 : static inline const char * 181 1 : GDKstrerror(int errnum, char *buf, size_t buflen) 182 : { 183 : #ifdef HAVE_STRERROR_S 184 : if (strerror_s(buf, buflen, errnum) == 0) 185 : return buf; 186 : snprintf(buf, buflen, "Unknown error %d", errnum); 187 : return buf; 188 : #elif defined(STRERROR_R_CHARP) 189 1 : return strerror_r(errnum, buf, buflen); 190 : #else 191 : if (strerror_r(errnum, buf, buflen) == 0) 192 : return buf; 193 : snprintf(buf, buflen, "Unknown error %d", errnum); 194 : return buf; 195 : #endif 196 : } 197 : 198 : #endif /* GDK_POSIX_H */