Commit 690c5ad9 authored by Tom Lee's avatar Tom Lee

PR feedback #1

parent 562f5417
......@@ -109,7 +109,7 @@ pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = capnp.pc capnp-rpc.pc
noinst_HEADERS = \
src/kj/iovmax.h
src/kj/miniposix.h
includekj_HEADERS = \
src/kj/common.h \
......@@ -123,7 +123,6 @@ includekj_HEADERS = \
src/kj/exception.h \
src/kj/debug.h \
src/kj/arena.h \
src/kj/miniposix.h \
src/kj/io.h \
src/kj/tuple.h \
src/kj/one-of.h \
......
......@@ -24,7 +24,7 @@
#include "debug.h"
#include "thread.h"
#include "io.h"
#include "iovmax.h"
#include "miniposix.h"
#include <unistd.h>
#include <sys/uio.h>
#include <errno.h>
......@@ -299,7 +299,7 @@ private:
Promise<void> writeInternal(ArrayPtr<const byte> firstPiece,
ArrayPtr<const ArrayPtr<const byte>> morePieces) {
const size_t iovmax = iovMax(1 + morePieces.size());
const size_t iovmax = kj::miniposix::iovMax(1 + morePieces.size());
// If there are more than IOV_MAX pieces, we'll only write the first IOV_MAX for now, and
// then we'll loop later.
KJ_STACK_ARRAY(struct iovec, iov, kj::min(1 + morePieces.size(), iovmax), 16, 128);
......@@ -1110,7 +1110,7 @@ Promise<size_t> DatagramPortImpl::send(
msg.msg_name = const_cast<void*>(implicitCast<const void*>(addr.getRaw()));
msg.msg_namelen = addr.getRawSize();
const size_t iovmax = iovMax(pieces.size());
const size_t iovmax = kj::miniposix::iovMax(pieces.size());
KJ_STACK_ARRAY(struct iovec, iov, kj::min(pieces.size(), iovmax), 16, 64);
for (size_t i: kj::indices(pieces)) {
......
......@@ -22,7 +22,6 @@
#include "io.h"
#include "debug.h"
#include "miniposix.h"
#include "iovmax.h"
#include <algorithm>
#include <errno.h>
......@@ -290,7 +289,7 @@ void FdOutputStream::write(ArrayPtr<const ArrayPtr<const byte>> pieces) {
}
#else
const size_t iovmax = iovMax(pieces.size());
const size_t iovmax = miniposix::iovMax(pieces.size());
while (pieces.size() > iovmax) {
write(pieces.slice(0, iovmax));
pieces = pieces.slice(iovmax, pieces.size());
......
// Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
// Licensed under the MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef KJ_IOVMAX_H_
#define KJ_IOVMAX_H_
#if !_WIN32
#include <limits.h>
#include <errno.h>
// Apparently, there is a maximum number of iovecs allowed per call. I don't understand why.
// Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd,
// define neither.
//
// On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX),
// try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails.
//
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01
inline size_t iovMax(size_t count)
{
#if defined(IOV_MAX)
// Solaris (and others?)
return IOV_MAX;
#elif defined(UIO_MAXIOV)
// Linux
return UIO_MAXIOV;
#else
// POSIX mystery meat
long iovmax;
errno = 0;
if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) {
// assume iovmax == -1 && errno == 0 means "unbounded"
return errno ? _XOPEN_IOV_MAX : count;
}
else {
return (size_t) iovmax;
}
#endif
}
#endif
#endif // KJ_IOVMAX_H_
......@@ -32,6 +32,9 @@
#if _WIN32
#include <io.h>
#include <direct.h>
#else
#include <limits.h>
#include <errno.h>
#endif
#if !_WIN32 || __MINGW32__
......@@ -94,6 +97,37 @@ inline int mkdir(const char* path, int mode) {
using ::pipe;
using ::mkdir;
inline size_t iovMax(size_t count) {
// Apparently, there is a maximum number of iovecs allowed per call. I don't understand why.
// Most platforms define IOV_MAX but Linux defines only UIO_MAXIOV and others, like Hurd,
// define neither.
//
// On platforms where both IOV_MAX and UIO_MAXIOV are undefined, we poke sysconf(_SC_IOV_MAX),
// then try to fall back to the POSIX-mandated minimum of _XOPEN_IOV_MAX if that fails.
//
// http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html#tag_13_23_03_01
#if defined(IOV_MAX)
// Solaris (and others?)
return IOV_MAX;
#elif defined(UIO_MAXIOV)
// Linux
return UIO_MAXIOV;
#else
// POSIX mystery meat
long iovmax;
errno = 0;
if ((iovmax = sysconf(_SC_IOV_MAX)) == -1) {
// assume iovmax == -1 && errno == 0 means "unbounded"
return errno ? _XOPEN_IOV_MAX : count;
} else {
return (size_t) iovmax;
}
#endif
}
#endif
} // namespace miniposix
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment