annotate win64-msvc/include/kj/miniposix.h @ 142:75bf92aa2d1f

Fixes and updates for 32-bit Windows build
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 09 Jan 2017 11:53:06 +0000
parents 42a73082be24
children 0f2d93caa50c
rev   line source
cannam@132 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@132 2 // Licensed under the MIT License:
cannam@132 3 //
cannam@132 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@132 5 // of this software and associated documentation files (the "Software"), to deal
cannam@132 6 // in the Software without restriction, including without limitation the rights
cannam@132 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@132 8 // copies of the Software, and to permit persons to whom the Software is
cannam@132 9 // furnished to do so, subject to the following conditions:
cannam@132 10 //
cannam@132 11 // The above copyright notice and this permission notice shall be included in
cannam@132 12 // all copies or substantial portions of the Software.
cannam@132 13 //
cannam@132 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@132 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@132 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@132 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@132 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@132 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@132 20 // THE SOFTWARE.
cannam@132 21
cannam@132 22 #ifndef KJ_MINIPOSIX_H_
cannam@132 23 #define KJ_MINIPOSIX_H_
cannam@132 24
cannam@132 25 // This header provides a small subset of the POSIX API which also happens to be available on
cannam@132 26 // Windows under slightly-different names.
cannam@132 27
cannam@132 28 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@132 29 #pragma GCC system_header
cannam@132 30 #endif
cannam@132 31
cannam@132 32 #if _WIN32
cannam@132 33 #include <io.h>
cannam@132 34 #include <direct.h>
cannam@132 35 #include <fcntl.h> // _O_BINARY
cannam@132 36 #else
cannam@132 37 #include <limits.h>
cannam@132 38 #include <errno.h>
cannam@132 39 #endif
cannam@132 40
cannam@132 41 #if !_WIN32 || __MINGW32__
cannam@132 42 #include <unistd.h>
cannam@132 43 #include <sys/stat.h>
cannam@132 44 #include <sys/types.h>
cannam@132 45 #endif
cannam@132 46
cannam@132 47 #if !_WIN32
cannam@132 48 #include <sys/uio.h>
cannam@132 49 #endif
cannam@132 50
cannam@132 51 namespace kj {
cannam@132 52 namespace miniposix {
cannam@132 53
cannam@132 54 #if _WIN32 && !__MINGW32__
cannam@132 55 // We're on Windows and not MinGW. So, we need to define wrappers for the POSIX API.
cannam@132 56
cannam@132 57 typedef int ssize_t;
cannam@132 58
cannam@132 59 inline ssize_t read(int fd, void* buffer, size_t size) {
cannam@132 60 return ::_read(fd, buffer, size);
cannam@132 61 }
cannam@132 62 inline ssize_t write(int fd, const void* buffer, size_t size) {
cannam@132 63 return ::_write(fd, buffer, size);
cannam@132 64 }
cannam@132 65 inline int close(int fd) {
cannam@132 66 return ::_close(fd);
cannam@132 67 }
cannam@132 68
cannam@132 69 #ifndef F_OK
cannam@132 70 #define F_OK 0 // access() existence test
cannam@132 71 #endif
cannam@132 72
cannam@132 73 #ifndef S_ISREG
cannam@132 74 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) // stat() regular file test
cannam@132 75 #endif
cannam@132 76 #ifndef S_ISDIR
cannam@132 77 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) // stat() directory test
cannam@132 78 #endif
cannam@132 79
cannam@132 80 #ifndef STDIN_FILENO
cannam@132 81 #define STDIN_FILENO 0
cannam@132 82 #endif
cannam@132 83 #ifndef STDOUT_FILENO
cannam@132 84 #define STDOUT_FILENO 1
cannam@132 85 #endif
cannam@132 86 #ifndef STDERR_FILENO
cannam@132 87 #define STDERR_FILENO 2
cannam@132 88 #endif
cannam@132 89
cannam@132 90 #else
cannam@132 91 // We're on a POSIX system or MinGW which already defines the wrappers for us.
cannam@132 92
cannam@132 93 using ::ssize_t;
cannam@132 94 using ::read;
cannam@132 95 using ::write;
cannam@132 96 using ::close;
cannam@132 97
cannam@132 98 #endif
cannam@132 99
cannam@132 100 #if _WIN32
cannam@132 101 // We're on Windows, including MinGW. pipe() and mkdir() are non-standard even on MinGW.
cannam@132 102
cannam@132 103 inline int pipe(int fds[2]) {
cannam@132 104 return ::_pipe(fds, 8192, _O_BINARY);
cannam@132 105 }
cannam@132 106 inline int mkdir(const char* path, int mode) {
cannam@132 107 return ::_mkdir(path);
cannam@132 108 }
cannam@132 109
cannam@132 110 #else
cannam@132 111 // We're on real POSIX.
cannam@132 112
cannam@132 113 using ::pipe;
cannam@132 114 using ::mkdir;
cannam@132 115
cannam@132 116 inline size_t iovMax(size_t count) {
cannam@132 117 // Apparently, there is a maximum number of iovecs allowed per call. I don't understand why.
cannam@132 118 // Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd,
cannam@132 119 // define neither.
cannam@132 120 //
cannam@132 121 // On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX),
cannam@132 122 // then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails.
cannam@132 123 //
cannam@132 124 // http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01
cannam@132 125
cannam@132 126 #if defined(IOV_MAX)
cannam@132 127 // Solaris (and others?)
cannam@132 128 return IOV_MAX;
cannam@132 129 #elif defined(UIO_MAXIOV)
cannam@132 130 // Linux
cannam@132 131 return UIO_MAXIOV;
cannam@132 132 #else
cannam@132 133 // POSIX mystery meat
cannam@132 134
cannam@132 135 long iovmax;
cannam@132 136
cannam@132 137 errno = 0;
cannam@132 138 if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) {
cannam@132 139 // assume iovmax == -1 && errno == 0 means "unbounded"
cannam@132 140 return errno ? _XOPEN_IOV_MAX : count;
cannam@132 141 } else {
cannam@132 142 return (size_t) iovmax;
cannam@132 143 }
cannam@132 144 #endif
cannam@132 145 }
cannam@132 146
cannam@132 147 #endif
cannam@132 148
cannam@132 149 } // namespace miniposix
cannam@132 150 } // namespace kj
cannam@132 151
cannam@132 152 #endif // KJ_MINIPOSIX_H_