annotate osx/include/kj/miniposix.h @ 134:41e769c91eca

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