annotate win64-msvc/include/kj/miniposix.h @ 64:eccd51b72864

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