annotate win64-msvc/include/kj/miniposix.h @ 47:d93140aac40b

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