annotate win64-msvc/include/kj/miniposix.h @ 148:b4bfdf10c4b3

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