capnproto

FORK: Cap'n Proto serialization/RPC system - core tools and C++ library
git clone https://git.neptards.moe/neptards/capnproto.git
Log | Files | Refs | README | LICENSE

miniposix.h (3918B)


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