annotate osx/include/kj/io.h @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 0994c39f1e94
children
rev   line source
cannam@62 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
cannam@62 2 // Licensed under the MIT License:
cannam@62 3 //
cannam@62 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
cannam@62 5 // of this software and associated documentation files (the "Software"), to deal
cannam@62 6 // in the Software without restriction, including without limitation the rights
cannam@62 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
cannam@62 8 // copies of the Software, and to permit persons to whom the Software is
cannam@62 9 // furnished to do so, subject to the following conditions:
cannam@62 10 //
cannam@62 11 // The above copyright notice and this permission notice shall be included in
cannam@62 12 // all copies or substantial portions of the Software.
cannam@62 13 //
cannam@62 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
cannam@62 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
cannam@62 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
cannam@62 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
cannam@62 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
cannam@62 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
cannam@62 20 // THE SOFTWARE.
cannam@62 21
cannam@62 22 #ifndef KJ_IO_H_
cannam@62 23 #define KJ_IO_H_
cannam@62 24
cannam@62 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
cannam@62 26 #pragma GCC system_header
cannam@62 27 #endif
cannam@62 28
cannam@62 29 #include <stddef.h>
cannam@62 30 #include "common.h"
cannam@62 31 #include "array.h"
cannam@62 32 #include "exception.h"
cannam@62 33
cannam@62 34 namespace kj {
cannam@62 35
cannam@62 36 // =======================================================================================
cannam@62 37 // Abstract interfaces
cannam@62 38
cannam@62 39 class InputStream {
cannam@62 40 public:
cannam@62 41 virtual ~InputStream() noexcept(false);
cannam@62 42
cannam@62 43 size_t read(void* buffer, size_t minBytes, size_t maxBytes);
cannam@62 44 // Reads at least minBytes and at most maxBytes, copying them into the given buffer. Returns
cannam@62 45 // the size read. Throws an exception on errors. Implemented in terms of tryRead().
cannam@62 46 //
cannam@62 47 // maxBytes is the number of bytes the caller really wants, but minBytes is the minimum amount
cannam@62 48 // needed by the caller before it can start doing useful processing. If the stream returns less
cannam@62 49 // than maxBytes, the caller will usually call read() again later to get the rest. Returning
cannam@62 50 // less than maxBytes is useful when it makes sense for the caller to parallelize processing
cannam@62 51 // with I/O.
cannam@62 52 //
cannam@62 53 // Never blocks if minBytes is zero. If minBytes is zero and maxBytes is non-zero, this may
cannam@62 54 // attempt a non-blocking read or may just return zero. To force a read, use a non-zero minBytes.
cannam@62 55 // To detect EOF without throwing an exception, use tryRead().
cannam@62 56 //
cannam@62 57 // If the InputStream can't produce minBytes, it MUST throw an exception, as the caller is not
cannam@62 58 // expected to understand how to deal with partial reads.
cannam@62 59
cannam@62 60 virtual size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) = 0;
cannam@62 61 // Like read(), but may return fewer than minBytes on EOF.
cannam@62 62
cannam@62 63 inline void read(void* buffer, size_t bytes) { read(buffer, bytes, bytes); }
cannam@62 64 // Convenience method for reading an exact number of bytes.
cannam@62 65
cannam@62 66 virtual void skip(size_t bytes);
cannam@62 67 // Skips past the given number of bytes, discarding them. The default implementation read()s
cannam@62 68 // into a scratch buffer.
cannam@62 69 };
cannam@62 70
cannam@62 71 class OutputStream {
cannam@62 72 public:
cannam@62 73 virtual ~OutputStream() noexcept(false);
cannam@62 74
cannam@62 75 virtual void write(const void* buffer, size_t size) = 0;
cannam@62 76 // Always writes the full size. Throws exception on error.
cannam@62 77
cannam@62 78 virtual void write(ArrayPtr<const ArrayPtr<const byte>> pieces);
cannam@62 79 // Equivalent to write()ing each byte array in sequence, which is what the default implementation
cannam@62 80 // does. Override if you can do something better, e.g. use writev() to do the write in a single
cannam@62 81 // syscall.
cannam@62 82 };
cannam@62 83
cannam@62 84 class BufferedInputStream: public InputStream {
cannam@62 85 // An input stream which buffers some bytes in memory to reduce system call overhead.
cannam@62 86 // - OR -
cannam@62 87 // An input stream that actually reads from some in-memory data structure and wants to give its
cannam@62 88 // caller a direct pointer to that memory to potentially avoid a copy.
cannam@62 89
cannam@62 90 public:
cannam@62 91 virtual ~BufferedInputStream() noexcept(false);
cannam@62 92
cannam@62 93 ArrayPtr<const byte> getReadBuffer();
cannam@62 94 // Get a direct pointer into the read buffer, which contains the next bytes in the input. If the
cannam@62 95 // caller consumes any bytes, it should then call skip() to indicate this. This always returns a
cannam@62 96 // non-empty buffer or throws an exception. Implemented in terms of tryGetReadBuffer().
cannam@62 97
cannam@62 98 virtual ArrayPtr<const byte> tryGetReadBuffer() = 0;
cannam@62 99 // Like getReadBuffer() but may return an empty buffer on EOF.
cannam@62 100 };
cannam@62 101
cannam@62 102 class BufferedOutputStream: public OutputStream {
cannam@62 103 // An output stream which buffers some bytes in memory to reduce system call overhead.
cannam@62 104 // - OR -
cannam@62 105 // An output stream that actually writes into some in-memory data structure and wants to give its
cannam@62 106 // caller a direct pointer to that memory to potentially avoid a copy.
cannam@62 107
cannam@62 108 public:
cannam@62 109 virtual ~BufferedOutputStream() noexcept(false);
cannam@62 110
cannam@62 111 virtual ArrayPtr<byte> getWriteBuffer() = 0;
cannam@62 112 // Get a direct pointer into the write buffer. The caller may choose to fill in some prefix of
cannam@62 113 // this buffer and then pass it to write(), in which case write() may avoid a copy. It is
cannam@62 114 // incorrect to pass to write any slice of this buffer which is not a prefix.
cannam@62 115 };
cannam@62 116
cannam@62 117 // =======================================================================================
cannam@62 118 // Buffered streams implemented as wrappers around regular streams
cannam@62 119
cannam@62 120 class BufferedInputStreamWrapper: public BufferedInputStream {
cannam@62 121 // Implements BufferedInputStream in terms of an InputStream.
cannam@62 122 //
cannam@62 123 // Note that the underlying stream's position is unpredictable once the wrapper is destroyed,
cannam@62 124 // unless the entire stream was consumed. To read a predictable number of bytes in a buffered
cannam@62 125 // way without going over, you'd need this wrapper to wrap some other wrapper which itself
cannam@62 126 // implements an artificial EOF at the desired point. Such a stream should be trivial to write
cannam@62 127 // but is not provided by the library at this time.
cannam@62 128
cannam@62 129 public:
cannam@62 130 explicit BufferedInputStreamWrapper(InputStream& inner, ArrayPtr<byte> buffer = nullptr);
cannam@62 131 // Creates a buffered stream wrapping the given non-buffered stream. No guarantee is made about
cannam@62 132 // the position of the inner stream after a buffered wrapper has been created unless the entire
cannam@62 133 // input is read.
cannam@62 134 //
cannam@62 135 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
cannam@62 136 // its own. This may improve performance if the buffer can be reused.
cannam@62 137
cannam@62 138 KJ_DISALLOW_COPY(BufferedInputStreamWrapper);
cannam@62 139 ~BufferedInputStreamWrapper() noexcept(false);
cannam@62 140
cannam@62 141 // implements BufferedInputStream ----------------------------------
cannam@62 142 ArrayPtr<const byte> tryGetReadBuffer() override;
cannam@62 143 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
cannam@62 144 void skip(size_t bytes) override;
cannam@62 145
cannam@62 146 private:
cannam@62 147 InputStream& inner;
cannam@62 148 Array<byte> ownedBuffer;
cannam@62 149 ArrayPtr<byte> buffer;
cannam@62 150 ArrayPtr<byte> bufferAvailable;
cannam@62 151 };
cannam@62 152
cannam@62 153 class BufferedOutputStreamWrapper: public BufferedOutputStream {
cannam@62 154 // Implements BufferedOutputStream in terms of an OutputStream. Note that writes to the
cannam@62 155 // underlying stream may be delayed until flush() is called or the wrapper is destroyed.
cannam@62 156
cannam@62 157 public:
cannam@62 158 explicit BufferedOutputStreamWrapper(OutputStream& inner, ArrayPtr<byte> buffer = nullptr);
cannam@62 159 // Creates a buffered stream wrapping the given non-buffered stream.
cannam@62 160 //
cannam@62 161 // If the second parameter is non-null, the stream uses the given buffer instead of allocating
cannam@62 162 // its own. This may improve performance if the buffer can be reused.
cannam@62 163
cannam@62 164 KJ_DISALLOW_COPY(BufferedOutputStreamWrapper);
cannam@62 165 ~BufferedOutputStreamWrapper() noexcept(false);
cannam@62 166
cannam@62 167 void flush();
cannam@62 168 // Force the wrapper to write any remaining bytes in its buffer to the inner stream. Note that
cannam@62 169 // this only flushes this object's buffer; this object has no idea how to flush any other buffers
cannam@62 170 // that may be present in the underlying stream.
cannam@62 171
cannam@62 172 // implements BufferedOutputStream ---------------------------------
cannam@62 173 ArrayPtr<byte> getWriteBuffer() override;
cannam@62 174 void write(const void* buffer, size_t size) override;
cannam@62 175
cannam@62 176 private:
cannam@62 177 OutputStream& inner;
cannam@62 178 Array<byte> ownedBuffer;
cannam@62 179 ArrayPtr<byte> buffer;
cannam@62 180 byte* bufferPos;
cannam@62 181 UnwindDetector unwindDetector;
cannam@62 182 };
cannam@62 183
cannam@62 184 // =======================================================================================
cannam@62 185 // Array I/O
cannam@62 186
cannam@62 187 class ArrayInputStream: public BufferedInputStream {
cannam@62 188 public:
cannam@62 189 explicit ArrayInputStream(ArrayPtr<const byte> array);
cannam@62 190 KJ_DISALLOW_COPY(ArrayInputStream);
cannam@62 191 ~ArrayInputStream() noexcept(false);
cannam@62 192
cannam@62 193 // implements BufferedInputStream ----------------------------------
cannam@62 194 ArrayPtr<const byte> tryGetReadBuffer() override;
cannam@62 195 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
cannam@62 196 void skip(size_t bytes) override;
cannam@62 197
cannam@62 198 private:
cannam@62 199 ArrayPtr<const byte> array;
cannam@62 200 };
cannam@62 201
cannam@62 202 class ArrayOutputStream: public BufferedOutputStream {
cannam@62 203 public:
cannam@62 204 explicit ArrayOutputStream(ArrayPtr<byte> array);
cannam@62 205 KJ_DISALLOW_COPY(ArrayOutputStream);
cannam@62 206 ~ArrayOutputStream() noexcept(false);
cannam@62 207
cannam@62 208 ArrayPtr<byte> getArray() {
cannam@62 209 // Get the portion of the array which has been filled in.
cannam@62 210 return arrayPtr(array.begin(), fillPos);
cannam@62 211 }
cannam@62 212
cannam@62 213 // implements BufferedInputStream ----------------------------------
cannam@62 214 ArrayPtr<byte> getWriteBuffer() override;
cannam@62 215 void write(const void* buffer, size_t size) override;
cannam@62 216
cannam@62 217 private:
cannam@62 218 ArrayPtr<byte> array;
cannam@62 219 byte* fillPos;
cannam@62 220 };
cannam@62 221
cannam@62 222 class VectorOutputStream: public BufferedOutputStream {
cannam@62 223 public:
cannam@62 224 explicit VectorOutputStream(size_t initialCapacity = 4096);
cannam@62 225 KJ_DISALLOW_COPY(VectorOutputStream);
cannam@62 226 ~VectorOutputStream() noexcept(false);
cannam@62 227
cannam@62 228 ArrayPtr<byte> getArray() {
cannam@62 229 // Get the portion of the array which has been filled in.
cannam@62 230 return arrayPtr(vector.begin(), fillPos);
cannam@62 231 }
cannam@62 232
cannam@62 233 // implements BufferedInputStream ----------------------------------
cannam@62 234 ArrayPtr<byte> getWriteBuffer() override;
cannam@62 235 void write(const void* buffer, size_t size) override;
cannam@62 236
cannam@62 237 private:
cannam@62 238 Array<byte> vector;
cannam@62 239 byte* fillPos;
cannam@62 240
cannam@62 241 void grow(size_t minSize);
cannam@62 242 };
cannam@62 243
cannam@62 244 // =======================================================================================
cannam@62 245 // File descriptor I/O
cannam@62 246
cannam@62 247 class AutoCloseFd {
cannam@62 248 // A wrapper around a file descriptor which automatically closes the descriptor when destroyed.
cannam@62 249 // The wrapper supports move construction for transferring ownership of the descriptor. If
cannam@62 250 // close() returns an error, the destructor throws an exception, UNLESS the destructor is being
cannam@62 251 // called during unwind from another exception, in which case the close error is ignored.
cannam@62 252 //
cannam@62 253 // If your code is not exception-safe, you should not use AutoCloseFd. In this case you will
cannam@62 254 // have to call close() yourself and handle errors appropriately.
cannam@62 255
cannam@62 256 public:
cannam@62 257 inline AutoCloseFd(): fd(-1) {}
cannam@62 258 inline AutoCloseFd(decltype(nullptr)): fd(-1) {}
cannam@62 259 inline explicit AutoCloseFd(int fd): fd(fd) {}
cannam@62 260 inline AutoCloseFd(AutoCloseFd&& other) noexcept: fd(other.fd) { other.fd = -1; }
cannam@62 261 KJ_DISALLOW_COPY(AutoCloseFd);
cannam@62 262 ~AutoCloseFd() noexcept(false);
cannam@62 263
cannam@62 264 inline AutoCloseFd& operator=(AutoCloseFd&& other) {
cannam@62 265 AutoCloseFd old(kj::mv(*this));
cannam@62 266 fd = other.fd;
cannam@62 267 other.fd = -1;
cannam@62 268 return *this;
cannam@62 269 }
cannam@62 270
cannam@62 271 inline AutoCloseFd& operator=(decltype(nullptr)) {
cannam@62 272 AutoCloseFd old(kj::mv(*this));
cannam@62 273 return *this;
cannam@62 274 }
cannam@62 275
cannam@62 276 inline operator int() const { return fd; }
cannam@62 277 inline int get() const { return fd; }
cannam@62 278
cannam@62 279 operator bool() const = delete;
cannam@62 280 // Deleting this operator prevents accidental use in boolean contexts, which
cannam@62 281 // the int conversion operator above would otherwise allow.
cannam@62 282
cannam@62 283 inline bool operator==(decltype(nullptr)) { return fd < 0; }
cannam@62 284 inline bool operator!=(decltype(nullptr)) { return fd >= 0; }
cannam@62 285
cannam@62 286 private:
cannam@62 287 int fd;
cannam@62 288 UnwindDetector unwindDetector;
cannam@62 289 };
cannam@62 290
cannam@62 291 inline auto KJ_STRINGIFY(const AutoCloseFd& fd)
cannam@62 292 -> decltype(kj::toCharSequence(implicitCast<int>(fd))) {
cannam@62 293 return kj::toCharSequence(implicitCast<int>(fd));
cannam@62 294 }
cannam@62 295
cannam@62 296 class FdInputStream: public InputStream {
cannam@62 297 // An InputStream wrapping a file descriptor.
cannam@62 298
cannam@62 299 public:
cannam@62 300 explicit FdInputStream(int fd): fd(fd) {}
cannam@62 301 explicit FdInputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
cannam@62 302 KJ_DISALLOW_COPY(FdInputStream);
cannam@62 303 ~FdInputStream() noexcept(false);
cannam@62 304
cannam@62 305 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
cannam@62 306
cannam@62 307 inline int getFd() const { return fd; }
cannam@62 308
cannam@62 309 private:
cannam@62 310 int fd;
cannam@62 311 AutoCloseFd autoclose;
cannam@62 312 };
cannam@62 313
cannam@62 314 class FdOutputStream: public OutputStream {
cannam@62 315 // An OutputStream wrapping a file descriptor.
cannam@62 316
cannam@62 317 public:
cannam@62 318 explicit FdOutputStream(int fd): fd(fd) {}
cannam@62 319 explicit FdOutputStream(AutoCloseFd fd): fd(fd), autoclose(mv(fd)) {}
cannam@62 320 KJ_DISALLOW_COPY(FdOutputStream);
cannam@62 321 ~FdOutputStream() noexcept(false);
cannam@62 322
cannam@62 323 void write(const void* buffer, size_t size) override;
cannam@62 324 void write(ArrayPtr<const ArrayPtr<const byte>> pieces) override;
cannam@62 325
cannam@62 326 inline int getFd() const { return fd; }
cannam@62 327
cannam@62 328 private:
cannam@62 329 int fd;
cannam@62 330 AutoCloseFd autoclose;
cannam@62 331 };
cannam@62 332
cannam@62 333 // =======================================================================================
cannam@62 334 // Win32 Handle I/O
cannam@62 335
cannam@62 336 #ifdef _WIN32
cannam@62 337
cannam@62 338 class AutoCloseHandle {
cannam@62 339 // A wrapper around a Win32 HANDLE which automatically closes the handle when destroyed.
cannam@62 340 // The wrapper supports move construction for transferring ownership of the handle. If
cannam@62 341 // CloseHandle() returns an error, the destructor throws an exception, UNLESS the destructor is
cannam@62 342 // being called during unwind from another exception, in which case the close error is ignored.
cannam@62 343 //
cannam@62 344 // If your code is not exception-safe, you should not use AutoCloseHandle. In this case you will
cannam@62 345 // have to call close() yourself and handle errors appropriately.
cannam@62 346
cannam@62 347 public:
cannam@62 348 inline AutoCloseHandle(): handle((void*)-1) {}
cannam@62 349 inline AutoCloseHandle(decltype(nullptr)): handle((void*)-1) {}
cannam@62 350 inline explicit AutoCloseHandle(void* handle): handle(handle) {}
cannam@62 351 inline AutoCloseHandle(AutoCloseHandle&& other) noexcept: handle(other.handle) {
cannam@62 352 other.handle = (void*)-1;
cannam@62 353 }
cannam@62 354 KJ_DISALLOW_COPY(AutoCloseHandle);
cannam@62 355 ~AutoCloseHandle() noexcept(false);
cannam@62 356
cannam@62 357 inline AutoCloseHandle& operator=(AutoCloseHandle&& other) {
cannam@62 358 AutoCloseHandle old(kj::mv(*this));
cannam@62 359 handle = other.handle;
cannam@62 360 other.handle = (void*)-1;
cannam@62 361 return *this;
cannam@62 362 }
cannam@62 363
cannam@62 364 inline AutoCloseHandle& operator=(decltype(nullptr)) {
cannam@62 365 AutoCloseHandle old(kj::mv(*this));
cannam@62 366 return *this;
cannam@62 367 }
cannam@62 368
cannam@62 369 inline operator void*() const { return handle; }
cannam@62 370 inline void* get() const { return handle; }
cannam@62 371
cannam@62 372 operator bool() const = delete;
cannam@62 373 // Deleting this operator prevents accidental use in boolean contexts, which
cannam@62 374 // the void* conversion operator above would otherwise allow.
cannam@62 375
cannam@62 376 inline bool operator==(decltype(nullptr)) { return handle != (void*)-1; }
cannam@62 377 inline bool operator!=(decltype(nullptr)) { return handle == (void*)-1; }
cannam@62 378
cannam@62 379 private:
cannam@62 380 void* handle; // -1 (aka INVALID_HANDLE_VALUE) if not valid.
cannam@62 381 };
cannam@62 382
cannam@62 383 class HandleInputStream: public InputStream {
cannam@62 384 // An InputStream wrapping a Win32 HANDLE.
cannam@62 385
cannam@62 386 public:
cannam@62 387 explicit HandleInputStream(void* handle): handle(handle) {}
cannam@62 388 explicit HandleInputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
cannam@62 389 KJ_DISALLOW_COPY(HandleInputStream);
cannam@62 390 ~HandleInputStream() noexcept(false);
cannam@62 391
cannam@62 392 size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
cannam@62 393
cannam@62 394 private:
cannam@62 395 void* handle;
cannam@62 396 AutoCloseHandle autoclose;
cannam@62 397 };
cannam@62 398
cannam@62 399 class HandleOutputStream: public OutputStream {
cannam@62 400 // An OutputStream wrapping a Win32 HANDLE.
cannam@62 401
cannam@62 402 public:
cannam@62 403 explicit HandleOutputStream(void* handle): handle(handle) {}
cannam@62 404 explicit HandleOutputStream(AutoCloseHandle handle): handle(handle), autoclose(mv(handle)) {}
cannam@62 405 KJ_DISALLOW_COPY(HandleOutputStream);
cannam@62 406 ~HandleOutputStream() noexcept(false);
cannam@62 407
cannam@62 408 void write(const void* buffer, size_t size) override;
cannam@62 409
cannam@62 410 private:
cannam@62 411 void* handle;
cannam@62 412 AutoCloseHandle autoclose;
cannam@62 413 };
cannam@62 414
cannam@62 415 #endif // _WIN32
cannam@62 416
cannam@62 417 } // namespace kj
cannam@62 418
cannam@62 419 #endif // KJ_IO_H_